fix impression page counter

This commit is contained in:
Matt K. Fu 2013-07-18 12:03:18 +08:00
parent c763817ebf
commit ceac2b842c
8 changed files with 124 additions and 47 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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