diff --git a/temp_file/app/helpers/orbit_backend_helper.rb b/temp_file/app/helpers/orbit_backend_helper.rb index 2632c41..fd938e6 100644 --- a/temp_file/app/helpers/orbit_backend_helper.rb +++ b/temp_file/app/helpers/orbit_backend_helper.rb @@ -1,11 +1,40 @@ require "uri" require "net/http" module OrbitBackendHelper - def self.included(base) - ActionView::Helpers::FormBuilder.send(:include, Orbit::FormBuilder) + ActionView::Helpers::FormBuilder.send(:include,OrbitFormHelper) end + def group_impression_by_day(field,day_limit,start_day=Date.today,format = 'day') + limit_ele = Impression.desc(:id).where({ created_at: { "$lt" => start_day-(day_limit-2).days }}).first || Impression.first + key_op = [['year','$year'],['month', '$month'], ['day', '$dayOfMonth']] + key_op = key_op.take(1 + key_op.find_index { |key, op| format == key }) + project_date_fields = Hash[*key_op.collect { |key, op| [key, {op => "$#{field}"}] }.flatten] + group_id_fields = Hash[*key_op.collect { |key, op| [key, "$#{key}"] }.flatten] + pipeline = [ + {"$match"=> {"_id" => {"$gte" => (limit_ele['_id'] rescue '')}}}, + {"$project" => {field => 1}.merge(project_date_fields)}, + {"$group" => {"_id" => group_id_fields,"count" => {"$sum" => 1}}}, + {"$sort" => {"_id"=>-1}} + ] + tmp = Impression.collection.aggregate(pipeline) + if tmp.count < day_limit + tmp1 = tmp + tmp = (0...day_limit).collect do |i| + d = start_day - i.days + d_year = d.year + d_month = d.month + d_day = d.day + count = tmp1.select do |v| + n_date = v['_id'] + n_date['year']==d_year && n_date['month']==d_month && n_date['day']==d_day + end[0]['count'] rescue 0 + {'_id'=>{'year'=>d_year,'month'=>d_month,'day'=>d_day},'count'=>count} + end + puts ['tmp',tmp] + end + tmp + end def thead(field,center=false,enable_sort=true) sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s active = params[:sort].eql? sort @@ -18,67 +47,6 @@ module OrbitBackendHelper "#{th_data}".html_safe end - def datetime_picker(object_name, method, options = {}) - options[:icon_time] ||= 'icons-clock' - options[:icon_date] ||= 'icons-calendar' - options[:icon_clear] ||= 'icons-cross-3' - options[:input_class] ||= 'input-large' - options[:new_record] = true if options[:new_record].nil? - options[:value] ||= options[:object].send(method) if options[:object] && options[:object][method] - case options[:picker_type] - when 'date' - content_tag :div, :id => options[:id], :class => options[:class] do - date_picker(object_name, method, options) - end - when 'time' - content_tag :div, :id => options[:id], :class => options[:class] do - time_picker(object_name, method, options) - end - when 'separated' - options[:label] ||= I18n.t('datetime_picker.separated.label') - content_tag :div, :id => options[:id], :class => "separated_picker #{options[:class]}" do - concat label_tag options[:label] unless options[:no_label] - concat hidden_field(object_name, method, :value => options[:value]) - concat separated_picker(object_name, method, options) - end - when 'simple' - content_tag :div, :id => options[:id], :class => options[:class] do - simple_picker(object_name, method, options) - end - else - content_tag :div, :id => options[:id], :class => options[:class] do - default_picker(object_name, method, options) - end - end - end - - def default_picker(object_name, method, options) - custom = {} - custom[:format] = options[:format] || 'yyyy/MM/dd hh:mm' - custom[:value] = format_value(options[:value], custom[:format]) if options[:value] && !options[:new_record] - custom[:value] = "" if options[:new_record] - custom[:picker_class] = 'default_picker' - custom[:label] = options[:label] || I18n.t('datetime_picker.default.label') - custom[:placeholder] = options[:placeholder] || I18n.t('datetime_picker.default.placeholder') - picker(object_name, method, options.merge(custom)) - end - - def picker(object_name, method, options) - content_tag :div, :class => "#{options[:picker_class]} input-append", :style => "#{(options[:picker_class].eql?('time_picker') && options[:value].blank? && options[:separated]) ? 'pointer-events:none' : nil}" do - concat label_tag options[:label] unless options[:no_label] - concat text_field object_name, method, :placeholder => options[:placeholder], :class => options[:input_class], 'data-format' => options[:format], :value => options[:value], :data => options[:data] - concat (content_tag :span, :class => 'add-on clearDate' do - content_tag :i, nil, :class => options[:icon_clear] - end) - concat (content_tag :span, :class => 'add-on iconbtn' do - content_tag :i, nil, 'data-time-icon' => options[:icon_time], 'data-date-icon' => options[:icon_date] - end) - end - end - - def format_value(value, format = 'yyyy-MM-dd hh:mm') - value.strftime(format.gsub('yyyy', '%Y').gsub('MM', '%m').gsub('dd', '%d').gsub('hh', '%H').gsub('mm', '%M')) rescue "" - end def add_attribute(partial, f, attribute) new_object = f.object.send(attribute).build rescue nil @@ -152,7 +120,7 @@ module OrbitBackendHelper #if site.month_traffic_cache.blank? or (site.month_traffic_cache['updated_at'] < (Time.now-1.day) rescue true) #site.month_traffic_cache = {} #site.month_traffic_cache['result'] = [] - @result_of_thirty_day = Impression.group_by(:created_at,30) + @result_of_thirty_day = group_impression_by_day(:created_at,30) trafic_result = @result_of_thirty_day.map do |v| date = v['_id'].values [Date.new(date[0],date[1],date[2]).to_time,v['count']] @@ -267,8 +235,3 @@ module OrbitBackendHelper end -module Orbit::FormBuilder - def datetime_picker(method, options = {}) - @template.datetime_picker(@object_name, method, objectify_options(options)) - end -end