fix traffic data counting slow problem

This commit is contained in:
chiu 2020-03-03 20:15:17 +08:00
parent ee07f1c40d
commit 3696bfd786
1 changed files with 18 additions and 6 deletions

View File

@ -114,16 +114,28 @@ module OrbitBackendHelper
end
def display_visitors_today
display_visitors(created_at: {'$gte' => Time.now.beginning_of_day})
if (defined? @result_of_thirty_day).nil?
display_visitors(created_at: {'$gte' => Time.now.beginning_of_day})
else
@result_of_thirty_day[0]['count']
end
end
def display_visitors_this_week
display_visitors(created_at: {'$gte' => Time.now.beginning_of_week})
if (defined? @result_of_thirty_day).nil?
display_visitors(created_at: {'$gte' => Time.now.beginning_of_week})
else
@result_of_thirty_day[0..((Time.now-Time.now.beginning_of_week)/86400).floor].map{|v| v['count']}.reduce{|v,x| v+x}
end
end
def display_visitors_this_month
visitors_this_month = Rails.cache.fetch("visitors_this_month", expires_in: 1.day) do
display_visitors(created_at: {'$gte' => Time.now.beginning_of_month})
if (defined? @result_of_thirty_day).nil?
visitors_this_month = Rails.cache.fetch("visitors_this_month", expires_in: 1.day) do
display_visitors(created_at: {'$gte' => Time.now.beginning_of_month})
end
else
@result_of_thirty_day[0..((Time.now-Time.now.beginning_of_month)/86400).floor].map{|v| v['count']}.reduce{|v,x| v+x}
end
visitors_this_month
end
@ -140,8 +152,8 @@ 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 = Impression.group_by(:created_at,30)
trafic_result = result.map do |v|
@result_of_thirty_day = Impression.group_by(: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']]
end