From ceac2b842c6c77defd559944a8b69413d3e7fd18 Mon Sep 17 00:00:00 2001 From: "Matt K. Fu" Date: Thu, 18 Jul 2013 12:03:18 +0800 Subject: [PATCH] fix impression page counter --- .../orbit_frontend_component_controller.rb | 6 +++ app/helpers/application_helper.rb | 39 ++++++++------- app/jobs/dashboard_counter.rb | 25 ++-------- app/jobs/delayed_impressionist.rb | 50 ++++++++++++++++--- app/models/page.rb | 2 +- lib/delay_impressionist_request.rb | 18 +++++++ lib/orbit_app/module/visitor_counter.rb | 29 +++++++++++ .../announcement/app/models/bulletin.rb | 2 +- 8 files changed, 124 insertions(+), 47 deletions(-) create mode 100644 lib/delay_impressionist_request.rb create mode 100644 lib/orbit_app/module/visitor_counter.rb diff --git a/app/controllers/orbit_frontend_component_controller.rb b/app/controllers/orbit_frontend_component_controller.rb index 09621e5f4..5d004aba2 100644 --- a/app/controllers/orbit_frontend_component_controller.rb +++ b/app/controllers/orbit_frontend_component_controller.rb @@ -7,4 +7,10 @@ class OrbitFrontendComponentController< ApplicationController @app_title = request.fullpath.split('/')[2] end + def delay_impressionist(item) + user_id = current_user.nil? ? nil : current_user.id + Resque.enqueue(DelayedImpressionist,:request=>DelayImpressionistRequest.new(request),:obj=>item,:class=>item.class.to_s,:controller_name=>controller_name,:action_name=>action_name,:user=>user_id) + end + + end \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2c538cd44..7acb74515 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,5 @@ module ApplicationHelper - + include OrbitApp::Module::VisitorCounter FLASH_NOTICE_KEYS = [:error, :notice, :warning] @@ -7,8 +7,9 @@ module ApplicationHelper request.referer.include?('?') ? [request.referer,param_hash.to_param].join('&') : [request.referer,param_hash.to_param].join('?') end - def delayed_impressionist(object) - # Resque.enqueue_at(1.minute.from_now,DelayedImpressionist,object) + def delayed_impressionist(item) + user_id = current_user.nil? ? nil : current_user.id + Resque.enqueue(DelayedImpressionist,:request=>DelayImpressionistRequest.new(request),:obj=>item,:class=>item.class.to_s,:controller_name=>controller_name,:action_name=>action_name,:user=>user_id) end def check_user_role_enable(attribute_fields) @@ -242,25 +243,27 @@ module ApplicationHelper "#{t(:view_count)}: #{object.view_count}" end - def display_visitors(options={}) - Impression.where(options).and(:referrer.ne => nil).distinct(:session_hash).count - end + # def display_visitors(options={}) + # # Impression.where(options).and(:referrer.ne => nil).distinct(:session_hash).count + # impressions = Impression.where(options).and(:referrer.ne => nil) + # impressions.map{|i| i[:session_hash]}.uniq.count + # end - def display_visitors_today - display_visitors(created_at: {'$gte' => Date.today.beginning_of_day, '$lte' => Date.today.end_of_day}) - end + # def display_visitors_today + # display_visitors(created_at: {'$gte' => Date.today.beginning_of_day, '$lte' => Date.today.end_of_day}) + # end - def display_visitors_this_week - display_visitors(created_at: {'$gte' => Date.today.beginning_of_week, '$lte' => Date.today.end_of_week}) - end + # def display_visitors_this_week + # display_visitors(created_at: {'$gte' => Date.today.beginning_of_week, '$lte' => Date.today.end_of_week}) + # end - def display_visitors_this_month - display_visitors(created_at: {'$gte' => Date.today.beginning_of_month, '$lte' => Date.today.end_of_month}) - end + # def display_visitors_this_month + # display_visitors(created_at: {'$gte' => Date.today.beginning_of_month, '$lte' => Date.today.end_of_month}) + # end - def display_visitors_this_year - display_visitors(created_at: {'$gte' => Date.today.beginning_of_year, '$lte' => Date.today.end_of_year}) - end + # def display_visitors_this_year + # display_visitors(created_at: {'$gte' => Date.today.beginning_of_year, '$lte' => Date.today.end_of_year}) + # end def display_date_time(object) object.strftime("%Y-%m-%d %H:%M") diff --git a/app/jobs/dashboard_counter.rb b/app/jobs/dashboard_counter.rb index 68b2ca470..48fbab0ce 100644 --- a/app/jobs/dashboard_counter.rb +++ b/app/jobs/dashboard_counter.rb @@ -1,4 +1,6 @@ class DashboardCounter < Resque::Plugins::RestrictionJob + include OrbitApp::Module::VisitorCounter + @queue = :high def self.perform(*args) @@ -9,28 +11,9 @@ class DashboardCounter < Resque::Plugins::RestrictionJob site.dashboard_counter[:visitors_this_year] = obj.display_visitors_this_year site.save OrbitLogger.info "DashboardCounter done #{site.dashboard_counter.to_s}" - end - - def display_visitors(options={}) - impressions = Impression.where(options).and(:referrer.ne => nil) - impressions.map{|i| i[:session_hash]}.uniq.count - end - - def display_visitors_today - display_visitors(created_at: {'$gte' => Date.today.beginning_of_day, '$lte' => Date.today.end_of_day}) - end - - def display_visitors_this_week - display_visitors(created_at: {'$gte' => Date.today.beginning_of_week, '$lte' => Date.today.end_of_week}) - end - - def display_visitors_this_month - display_visitors(created_at: {'$gte' => Date.today.beginning_of_month, '$lte' => Date.today.end_of_month}) - end - - def display_visitors_this_year - display_visitors(created_at: {'$gte' => Date.today.beginning_of_year, '$lte' => Date.today.end_of_year}) + #Notice this is mainly used at user dashbroad. however the code there is currentlly using realtime evaluation. + #and the site model will need a field call dashboard_counter to complete this job. end end diff --git a/app/jobs/delayed_impressionist.rb b/app/jobs/delayed_impressionist.rb index 06072c45d..041b6e34c 100644 --- a/app/jobs/delayed_impressionist.rb +++ b/app/jobs/delayed_impressionist.rb @@ -1,7 +1,45 @@ -class DelayedImpressionist - @queue = :low - def self.perform(object) - impressionist(object) - true - end +class DelayedImpressionist + def self.before_filter(*args) + return + end + + include ImpressionistController::InstanceMethods + @queue = :low + + def initialize(request,controller_name,action_name,user,obj) + @controller_name = controller_name + @action_name = action_name + @user = User.find user unless user.nil? + @request = request + end + + def self.perform(*args) + arg = args[0] + @request= DelayImpressionistRequest.new + @request.restore(arg["request"]) + request = @request + obj = eval(arg["class"]).find arg["obj"]["_id"] + # imp = new(@request,arg["controller_name"],arg["action_name"],arg["user"],obj) + new_impression = obj.impressions.build(:user_id=>arg["user"],:controller_name=>arg["controller_name"],:action_name=>arg["action_name"],:ip_address=>@request.remote_ip,:referrer=>@request.referer) + new_impression.save + obj.update_attribute(:view_count,obj.impression_count) + end + +private + def user + @user + end + + def action_name + @action_name + end + + def controller_name + @controller_name + end + + def request + @request + end + end \ No newline at end of file diff --git a/app/models/page.rb b/app/models/page.rb index b2947ca8b..87e793c47 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -3,7 +3,7 @@ class Page < Item include Impressionist::Impressionable include ParserFrontEnd - is_impressionable :counter_cache => { :column_name => :view_count } + is_impressionable #:counter_cache => { :column_name => :view_count } field :content, localize: true field :app_frontend_url diff --git a/lib/delay_impressionist_request.rb b/lib/delay_impressionist_request.rb new file mode 100644 index 000000000..cbc8e0e29 --- /dev/null +++ b/lib/delay_impressionist_request.rb @@ -0,0 +1,18 @@ +class DelayImpressionistRequest + attr_accessor :session_options,:remote_ip,:referer,:user_agent + def initialize(request=nil) + if request + self.session_options=request.session_options + self.remote_ip=request.remote_ip + self.referer=request.referer + self.user_agent=request.user_agent + end + end + + def restore(args) + args.each do |key,value| + self.send("#{key}=", value) + end + end + +end \ No newline at end of file diff --git a/lib/orbit_app/module/visitor_counter.rb b/lib/orbit_app/module/visitor_counter.rb new file mode 100644 index 000000000..ed1ec3e40 --- /dev/null +++ b/lib/orbit_app/module/visitor_counter.rb @@ -0,0 +1,29 @@ +module OrbitApp + module Module + module VisitorCounter + Version = "0.1" + + def display_visitors(options={}) + impressions = Impression.where(options).and(:referrer.ne => nil) + impressions.map{|i| i[:session_hash]}.uniq.count + end + + def display_visitors_today + display_visitors(created_at: {'$gte' => Date.today.beginning_of_day, '$lte' => Date.today.end_of_day}) + end + + def display_visitors_this_week + display_visitors(created_at: {'$gte' => Date.today.beginning_of_week, '$lte' => Date.today.end_of_week}) + end + + def display_visitors_this_month + display_visitors(created_at: {'$gte' => Date.today.beginning_of_month, '$lte' => Date.today.end_of_month}) + end + + def display_visitors_this_year + display_visitors(created_at: {'$gte' => Date.today.beginning_of_year, '$lte' => Date.today.end_of_year}) + end + + end # of VisitorCounter + end # of Module +end # of OrbitApp diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 207b3a544..1771e2a69 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -14,7 +14,7 @@ class Bulletin include OrbitTag::Taggable taggable - is_impressionable :counter_cache => { :column_name => :view_count } + is_impressionable #:counter_cache => { :column_name => :view_count } field :title, localize: true field :subtitle, localize: true