fix traffic data counting slow problem
This commit is contained in:
parent
ee07f1c40d
commit
3696bfd786
|
@ -114,17 +114,29 @@ module OrbitBackendHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_visitors_today
|
def display_visitors_today
|
||||||
|
if (defined? @result_of_thirty_day).nil?
|
||||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_day})
|
display_visitors(created_at: {'$gte' => Time.now.beginning_of_day})
|
||||||
|
else
|
||||||
|
@result_of_thirty_day[0]['count']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_visitors_this_week
|
def display_visitors_this_week
|
||||||
|
if (defined? @result_of_thirty_day).nil?
|
||||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_week})
|
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
|
end
|
||||||
|
|
||||||
def display_visitors_this_month
|
def display_visitors_this_month
|
||||||
|
if (defined? @result_of_thirty_day).nil?
|
||||||
visitors_this_month = Rails.cache.fetch("visitors_this_month", expires_in: 1.day) do
|
visitors_this_month = Rails.cache.fetch("visitors_this_month", expires_in: 1.day) do
|
||||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_month})
|
display_visitors(created_at: {'$gte' => Time.now.beginning_of_month})
|
||||||
end
|
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
|
visitors_this_month
|
||||||
end
|
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)
|
#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 = {}
|
||||||
#site.month_traffic_cache['result'] = []
|
#site.month_traffic_cache['result'] = []
|
||||||
result = Impression.group_by(:created_at,30)
|
@result_of_thirty_day = Impression.group_by(:created_at,30)
|
||||||
trafic_result = result.map do |v|
|
trafic_result = @result_of_thirty_day.map do |v|
|
||||||
date = v['_id'].values
|
date = v['_id'].values
|
||||||
[Date.new(date[0],date[1],date[2]).to_time,v['count']]
|
[Date.new(date[0],date[1],date[2]).to_time,v['count']]
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue