Class: Statlysis::LastestVisits
- Defined in:
- lib/statlysis/cron/top/lastest_visits.rb
Overview
See tech details at mvj3.github.io/2013/01/30/recent-visitors-implement/
Instance Attribute Summary collapse
-
#clock ⇒ Object
Returns the value of attribute clock.
-
#reject_proc ⇒ Object
Returns the value of attribute reject_proc.
Attributes inherited from Top
#logs, #pattern_proc, #result_limit, #stat_model, #user_id_proc, #user_info_proc
Attributes inherited from Cron
#multiple_dataset, #source_type, #time_column, #time_unit, #time_zone
Instance Method Summary collapse
-
#initialize(source, opts = {}) ⇒ LastestVisits
constructor
pattern_proc is a proc to extract user_id or url_prefix to compute the top visitors from log user_id_proc is a proc to extract user_id from log user_info_proc is a proc to extract visitor informations(like id, name, …) reject_proc filter visitors.
- #output ⇒ Object
- #reclock(default_time = nil) ⇒ Object
- #write ⇒ Object
Methods inherited from Top
Methods inherited from Cron
#_source, #group_by_columns?, #is_activerecord?, #is_mongoid?, #is_orm?, #is_time_column_integer?, #reoutput, #run, #setup_stat_model, #source_where_array, #time_column?, #time_range
Methods included from Common
Constructor Details
#initialize(source, opts = {}) ⇒ LastestVisits
pattern_proc is a proc to extract user_id or url_prefix to compute the top visitors from log user_id_proc is a proc to extract user_id from log user_info_proc is a proc to extract visitor informations(like id, name, …) reject_proc filter visitors
14 15 16 17 18 19 20 21 |
# File 'lib/statlysis/cron/top/lastest_visits.rb', line 14 def initialize source, opts = {} # set variables cron.reclock opts[:default_time] cron.reject_proc = opts[:reject_proc] || proc {|pattern, user_id| pattern.to_i == user_id.to_i } super Utils.setup_pattern_table_and_model cron.stat_table_name cron end |
Instance Attribute Details
#clock ⇒ Object
Returns the value of attribute clock.
6 7 8 |
# File 'lib/statlysis/cron/top/lastest_visits.rb', line 6 def clock @clock end |
#reject_proc ⇒ Object
Returns the value of attribute reject_proc.
7 8 9 |
# File 'lib/statlysis/cron/top/lastest_visits.rb', line 7 def reject_proc @reject_proc end |
Instance Method Details
#output ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/statlysis/cron/top/lastest_visits.rb', line 23 def output cron.logs = cron.source.asc(cron.time_column).where(cron.time_column => {"$gte" => cron.clock.current}).limit(1000).to_a return {} if cron.logs.blank? cron.logs.inject({}) do |h, log| pattern = cron.pattern_proc.call(log) if pattern h[pattern] ||= [] user_id = cron.user_id_proc.call(log).to_i h[pattern] << user_id if not user_id.zero? end h end end |
#reclock(default_time = nil) ⇒ Object
48 49 50 |
# File 'lib/statlysis/cron/top/lastest_visits.rb', line 48 def reclock default_time = nil cron.clock = Clock.new cron.stat_table_name, (default_time || cron.clock.current) end |
#write ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/statlysis/cron/top/lastest_visits.rb', line 37 def write logger.info "#{Time.now.strftime('%H:%M:%S')} #{cron.stat_model} #{cron.output.inspect}" cron.output.each do |pattern, user_ids| s = cron.stat_model.find_or_create(:pattern => pattern) old_array = (JSON.parse(s.result) rescue []).map {|i| Array(i)[0] } new_user_ids = (old_array + user_ids).reverse.uniq.reverse # ensure the right items will overwrite the left [1,4,5,7,4,3,3,2,1,5].uniq => [1, 4, 5, 7, 3, 2] s.update :result => new_user_ids.reject {|user_id| cron.reject_proc.call(pattern, user_id) rescue false }.map {|user_id| cron.user_info_proc.call(user_id) }.compact[0..cron.result_limit].to_json end cron.clock.update cron.logs.last.try(cron.time_column) end |