From 3696bfd786a95f3dc5ed2fd16b6861e7cb66da14 Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 3 Mar 2020 20:15:17 +0800 Subject: [PATCH] fix traffic data counting slow problem --- temp_file/app/helpers/orbit_backend_helper.rb | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/temp_file/app/helpers/orbit_backend_helper.rb b/temp_file/app/helpers/orbit_backend_helper.rb index 94f2d1d..790cf6a 100644 --- a/temp_file/app/helpers/orbit_backend_helper.rb +++ b/temp_file/app/helpers/orbit_backend_helper.rb @@ -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