Module: SirTracksAlot::EventHelper

Included in:
Activity
Defined in:
lib/sir_tracks_alot/event_helper.rb

Constant Summary collapse

DATE_FORMATS =
{:hourly => '%Y/%m/%d %H:00', :daily => '%Y/%m/%d'}

Instance Method Summary collapse

Instance Method Details

#views(resolution = nil) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sir_tracks_alot/event_helper.rb', line 5

def views(resolution = nil)
  return events.size if resolution.nil?

  groups = {}

  date_format = resolution.kind_of?(String) ? resolution : DATE_FORMATS[resolution]

  events.each do |event|
    time = Time.at(event.to_i).utc.strftime(date_format) # lop of some detail
    groups[time.to_s] ||= 0
    groups[time.to_s] += 1 
  end

  return groups
end

#visits(sd = 1800, resolution = nil) ⇒ Object

Count one visit for all events every session_duration (in seconds) e.g. 3 visits within 15 minutes counts as one, 1 visit 2 days later counts as another


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sir_tracks_alot/event_helper.rb', line 23

def visits(sd = 1800, resolution = nil)
  session_duration = sd || 1800      
  last_event = events.first

  count = events.size > 0 ? 1 : 0 # in case last_event == event (below)
  groups = {}

  events.each do |event|
    boundary = (event.to_i - last_event.to_i > session_duration) # have we crossed a session boundary?

    if resolution.nil? # i.e. don't group
      count += 1 if boundary
    else
      date_format = resolution.kind_of?(String) ? resolution : DATE_FORMATS[resolution]
      time = Time.at(event.to_i).utc.strftime(date_format) # lop of some detail

      groups[time.to_s] ||= count
      groups[time.to_s] += 1 if boundary
    end

    last_event = event # try the next bounary
  end

  resolution.nil? ? count : groups
end