From fe2c1a923571820d5daee54324deaf966a3d49ed Mon Sep 17 00:00:00 2001 From: manson Date: Fri, 8 Aug 2014 17:33:18 +0800 Subject: [PATCH] update dashboard for better performance --- app/helpers/application_helper.rb | 25 +++++++++++++++++-------- app/models/site.rb | 2 ++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5a29539..a1bf835 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -248,15 +248,24 @@ module ApplicationHelper end def get_month_traffic - result = [] - (0..30).each do |i| - visits = Impression.where( created_at: { - '$gte' => Time.now.beginning_of_day-i.days, - '$lte' => Time.now.end_of_day-i.days} - ).count - result.push([ Time.now.beginning_of_day-i.days, visits]) + site = Site.first + 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'] = [] + (1..30).each do |i| + visits = Impression.where( created_at: { + '$gte' => Time.now.beginning_of_day-i.days, + '$lte' => Time.now.end_of_day-i.days} + ).distinct(:request_hash).count + site.month_traffic_cache['result'].push([ Time.now.beginning_of_day-i.days, visits]) + end + + site.month_traffic_cache['updated_at'] = Time.now + site.save end - [:name=> t(:visitors_count),:data=>result] + + [:name=> t(:visitors_count),:data=>site.month_traffic_cache['result']] end # def get_today_traffic diff --git a/app/models/site.rb b/app/models/site.rb index 293ad47..11a302d 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -51,6 +51,8 @@ class Site field :store_confirmation, :type => Boolean, :default => false field :meta_viewport, :type => Boolean, :default => false + field :month_traffic_cache + belongs_to :design has_many :site_metas, :autosave => true, :dependent => :destroy validate :in_use_locales, :minimum_enabled_locales