From 89c065b0475a8ff6c6473ec9678b34404b276658 Mon Sep 17 00:00:00 2001 From: chiu Date: Wed, 4 Mar 2020 11:06:36 +0800 Subject: [PATCH] fix error --- temp_file/app/models/site.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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