Class: BitAnalytics

Inherits:
Object
  • Object
show all
Defined in:
lib/bit_analytics.rb,
lib/bit_analytics/version.rb

Defined Under Namespace

Modules: MixinContains, MixinCounts, MixinEventsMisc, RedisConnection

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ BitAnalytics

Returns a new instance of BitAnalytics.



7
8
9
10
11
12
13
# File 'lib/bit_analytics.rb', line 7

def initialize(options=nil)
  @redis = if options
    Redis.new(options)
  else
    Redis.new
  end
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



5
6
7
# File 'lib/bit_analytics.rb', line 5

def redis
  @redis
end

Instance Method Details

#_prefix_key(event_name, date) ⇒ Object

— Private —



114
115
116
# File 'lib/bit_analytics.rb', line 114

def _prefix_key(event_name, date)
  return 'bitanalytics_%s_%s' % [event_name, date]
end

#bit_op_and(event, *events) ⇒ Object

— BitOps —



91
92
93
94
95
96
# File 'lib/bit_analytics.rb', line 91

def bit_op_and(event, *events)
  bit_operation = BitOperation.new('AND', event, *events)
  bit_operation.redis = @redis
  bit_operation.execute
  bit_operation
end

#bit_op_or(event, *events) ⇒ Object



98
99
100
101
102
103
# File 'lib/bit_analytics.rb', line 98

def bit_op_or(event, *events)
  bit_operation = BitOperation.new('OR', event, *events)
  bit_operation.redis = @redis
  bit_operation.execute
  bit_operation
end

#bit_op_xor(event, *events) ⇒ Object



105
106
107
108
109
110
# File 'lib/bit_analytics.rb', line 105

def bit_op_xor(event, *events)
  bit_operation = BitOperation.new('XOR', event, *events)
  bit_operation.redis = @redis
  bit_operation.execute
  bit_operation
end

#day_events(event_name, year, month, day) ⇒ Object



77
78
79
80
81
# File 'lib/bit_analytics.rb', line 77

def day_events(event_name, year, month, day)
  day_events = DayEvents.new(event_name, year, month, day)
  day_events.redis = @redis
  day_events
end

#delete_all_eventsObject

Delete all events from the database.



52
53
54
55
# File 'lib/bit_analytics.rb', line 52

def delete_all_events
  keys = @redis.keys('bitanalytics_*')
  @redis.del(*keys) unless keys.empty?
end

#delete_temporary_bitop_keysObject

Delete all temporary keys that are used when using bit operations.



58
59
60
61
# File 'lib/bit_analytics.rb', line 58

def delete_temporary_bitop_keys
  keys = @redis.keys('bitanalytics_bitop_*')
  @redis.del(keys) unless keys.empty?
end

#hour_events(event_name, year, month, day, hour) ⇒ Object



83
84
85
86
87
# File 'lib/bit_analytics.rb', line 83

def hour_events(event_name, year, month, day, hour)
  hour_events = HourEvents.new(event_name, year, month, day, hour)
  hour_events.redis = @redis
  hour_events
end

#mark_event(event_name, uuid, now: nil, track_hourly: nil) ⇒ Object

Marks an event for hours, days, weeks and months.

  • event_name - The name of the event, could be “active” or “new_signups”

  • uuid - An unique id, typically user id. The id should not be huge, read Redis documentation why (bitmaps)

  • now - Which date should be used as a reference point, default is ‘Time.now.getutc`

  • track_hourly - Should hourly stats be tracked

Example

Mark id 1 as active @bit_analytics.mark_event(‘active’, 1) Mark task completed for id 252 @bit_analytics.mark_event(‘tasks:completed’, 252)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bit_analytics.rb', line 27

def mark_event(event_name, uuid, now: nil, track_hourly: nil)
  # Has memory applications
  track_hourly ||= false
  now ||= Time.now.getutc
  # E.g. ['2014', '03'] for 17th of January 2014
  iso_date = now.strftime('%Y-%V').split('-')

  events = [
    MonthEvents.new(event_name, now.year, now.month),
    WeekEvents.new(event_name, iso_date[0], iso_date[1]),
    DayEvents.new(event_name, now.year, now.month, now.day),
  ]

  if track_hourly
    events << HourEvents.new(event_name, now.year, now.month, now.day, now.hour) 
  end

  @redis.pipelined do 
    events.each do |event|
      @redis.setbit(event.redis_key, uuid, 1)
    end
  end
end

#month_events(event_name, year, month) ⇒ Object

— Events —



65
66
67
68
69
# File 'lib/bit_analytics.rb', line 65

def month_events(event_name, year, month)
  month_events = MonthEvents.new(event_name, year, month)
  month_events.redis = @redis
  month_events
end

#week_events(event_name, year, week) ⇒ Object



71
72
73
74
75
# File 'lib/bit_analytics.rb', line 71

def week_events(event_name, year, week)
  week_events = WeekEvents.new(event_name, year, week)
  week_events.redis = @redis
  week_events
end