update dashboard for better performance

This commit is contained in:
manson 2014-08-08 17:33:18 +08:00
parent e0b22e11bb
commit fe2c1a9235
2 changed files with 19 additions and 8 deletions

View File

@ -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

View File

@ -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