Class: SirTracksAlot::Count

Inherits:
Persistable show all
Extended by:
FilterHelper
Defined in:
lib/sir_tracks_alot/count.rb

Constant Summary collapse

ACTIONS =
[:create, :view, :login, :search, :update, :destroy]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FilterHelper

extract_filter_options, filter

Methods inherited from Persistable

#==, #eql?, find_or_create, #hash

Class Method Details

.count(options_for_find = {}, options_for_sort = {:limit => 500}, resolution = :hourly) ⇒ Object

Convert a default of 500 activities into counts


56
57
58
59
60
61
# File 'lib/sir_tracks_alot/count.rb', line 56

def self.count(options_for_find = {}, options_for_sort = {:limit => 500}, resolution = :hourly)
  options_for_find = options_for_find.to_hash
  session_duration = options_for_find.delete(:session_duration)
  activities = Activity.find(options_for_find.merge(:counted => 0, :action => [], :category => [])).sort(options_for_sort)
  rollup(activities, session_duration, resolution)
end

.rollup(activities, session_duration, resolution = :hourly) ⇒ Object

Rollup all activities by resolution Create one count for actor and one count for target


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sir_tracks_alot/count.rb', line 88

def self.rollup(activities, session_duration, resolution = :hourly)
  counts = []

  activities.each do |activity|        
    count = Count.find_or_create(:owner => activity.owner, :category => activity.category,  :target => activity.target, :action => activity.action)

    activity.views(resolution).each do |time, views|
      date = Time.parse(time).to_i
      summary = count.summaries.find(:date => date).first || Summary.create(:date => date)
      count.summaries << summary
      summary.update(:views => (summary.views||0).to_i + views.to_i)
    end

    activity.visits(session_duration, resolution).each do |time, visits|
      date = Time.parse(time).to_i          
      summary = count.summaries.find(:date => date).first || Summary.create(:date => date)
      count.summaries << summary
      summary.update(:visits => (summary.visits||0).to_i + visits.to_i)
    end

    activity.counted!        
    counts << count
  end
  
  counts
end

.rows(options_for_find, title = nil) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sir_tracks_alot/count.rb', line 39

def self.rows(options_for_find, title = nil)
  groups = {}
  
  filter(options_for_find).each do |count|
    t = title || count.target || count.actor # don't currently support counts with both actor and target set
    
    count.summaries.each do |summary|
      groups[t] ||= [0,0]
      groups[t][0] += summary.views.to_i
      groups[t][1] += summary.visits.to_i
    end
  end
  
  groups.collect{|g| g.flatten}
end

.summarize(resolution = :daily, options_for_find = {}) ⇒ Object

Summarize counts daily or hourly All times are in UTC


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sir_tracks_alot/count.rb', line 65

def self.summarize(resolution = :daily, options_for_find = {})
  counts = {}

  filter(options_for_find).each do |count|
    count.summaries.each do |summary|          
      key = case resolution
        when :daily
          Time.at(summary.date.to_i).utc.strftime('%Y/%m/%d')
        when :hourly
          Time.at(summary.date.to_i).utc.strftime('%Y/%m/%d %H:00 UTC')
        end
    
      counts[key]   ||= [0,0]
      counts[key][0] += summary.views.to_i
      counts[key][1] += summary.visits.to_i
    end
  end
  
  counts
end

.total(what, options_for_find) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sir_tracks_alot/count.rb', line 21

def self.total(what, options_for_find)
  what = [what] unless what.kind_of?(Array)      
  views, visits = 0, 0

  filter(options_for_find).each do |count|
    count.summaries.each do |summary|
      views +=  summary.views.to_i if what.include?(:views)
      visits += summary.visits.to_i if what.include?(:visits)
    end
  end      

  return [views, visits] if what == [:views, :visits]
  return [visits, views] if what == [:visits, :views]
  return views if what == [:views]
  return visits if what == [:visits]
  raise ArgumentError("what must be one or both of :views, :visits")      
end

Instance Method Details

#to_hashObject


115
116
117
# File 'lib/sir_tracks_alot/count.rb', line 115

def to_hash
  {:owner => owner, :actor => actor, :target => target, :category => category, :id => id}
end