diff --git a/temp_file/app/models/site.rb b/temp_file/app/models/site.rb index 87b3b6a..b7b5ec0 100644 --- a/temp_file/app/models/site.rb +++ b/temp_file/app/models/site.rb @@ -72,6 +72,9 @@ class Site mount_uploader :favicon, ImageUploader mount_uploader :mobile_icon, ImageUploader Impression.class_eval{ + def self.create_date_for_group(date) + {'_id'=>{'year'=>date.year,'month'=>date.month,'day'=>date.day},'count'=>0} + end def self.group_by(field,day_limit,start_day=Date.today,format = 'day') limit_ele = self.desc(:id).where({ created_at: { "$lt" => start_day-(day_limit-2).days }}).first || self.first key_op = [['year','$year'],['month', '$month'], ['day', '$dayOfMonth']] @@ -84,7 +87,11 @@ class Site {"$group" => {"_id" => group_id_fields,"count" => {"$sum" => 1}}}, {"$sort" => {"_id"=>-1}} ] - collection.aggregate(pipeline) + tmp = collection.aggregate(pipeline) + if tmp.count < day_limit + tmp.concat(Array.new(day_limit-tmp.count).map.with_index.map{|v,i| create_date_for_group(start_day+i.days)}) + end + tmp end } after_initialize do