Class: BitAnalytics
- Inherits:
-
Object
- Object
- BitAnalytics
- Defined in:
- lib/bit_analytics.rb,
lib/bit_analytics/version.rb
Direct Known Subclasses
BitOperation, DayEvents, HourEvents, MonthEvents, WeekEvents
Defined Under Namespace
Modules: MixinContains, MixinCounts, MixinEventsMisc, RedisConnection
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
-
#_prefix_key(event_name, date) ⇒ Object
— Private —.
-
#bit_op_and(event, *events) ⇒ Object
— BitOps —.
- #bit_op_or(event, *events) ⇒ Object
- #bit_op_xor(event, *events) ⇒ Object
- #day_events(event_name, year, month, day) ⇒ Object
-
#delete_all_events ⇒ Object
Delete all events from the database.
-
#delete_temporary_bitop_keys ⇒ Object
Delete all temporary keys that are used when using bit operations.
- #hour_events(event_name, year, month, day, hour) ⇒ Object
-
#initialize(options = nil) ⇒ BitAnalytics
constructor
A new instance of BitAnalytics.
-
#mark_event(event_name, uuid, now: nil, track_hourly: nil) ⇒ Object
— Events marking and deleting — Marks an event for hours, days, weeks and months.
-
#month_events(event_name, year, month) ⇒ Object
— Events —.
- #week_events(event_name, year, week) ⇒ Object
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(=nil) @redis = if Redis.new() else Redis.new end end |
Instance Attribute Details
#redis ⇒ Object
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 —
113 114 115 |
# File 'lib/bit_analytics.rb', line 113 def _prefix_key(event_name, date) return 'bitanalytics_%s_%s' % [event_name, date] end |
#bit_op_and(event, *events) ⇒ Object
— BitOps —
90 91 92 93 94 95 |
# File 'lib/bit_analytics.rb', line 90 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
97 98 99 100 101 102 |
# File 'lib/bit_analytics.rb', line 97 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
104 105 106 107 108 109 |
# File 'lib/bit_analytics.rb', line 104 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
76 77 78 79 80 |
# File 'lib/bit_analytics.rb', line 76 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_events ⇒ Object
Delete all events from the database.
51 52 53 54 |
# File 'lib/bit_analytics.rb', line 51 def delete_all_events keys = @redis.keys('bitanalytics_*') @redis.del(*keys) unless keys.empty? end |
#delete_temporary_bitop_keys ⇒ Object
Delete all temporary keys that are used when using bit operations.
57 58 59 60 |
# File 'lib/bit_analytics.rb', line 57 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
82 83 84 85 86 |
# File 'lib/bit_analytics.rb', line 82 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
— Events marking and deleting — Marks an event for hours, days, weeks and months. :param :event_name The name of the event, could be “active” or “new_signups” :param :uuid An unique id, typically user id. The id should not be huge, read Redis documentation why (bitmaps) :param :now Which date should be used as a reference point, default is ‘datetime.utcnow` :param :track_hourly Should hourly stats be tracked, defaults to bitanalytics.TRACK_HOURLY, but an be changed Examples: Mark id 1 as active mark_event(’active’, 1) Mark task completed for id 252 mark_event(‘tasks:completed’, 252)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bit_analytics.rb', line 26 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 —
64 65 66 67 68 |
# File 'lib/bit_analytics.rb', line 64 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
70 71 72 73 74 |
# File 'lib/bit_analytics.rb', line 70 def week_events(event_name, year, week) week_events = WeekEvents.new(event_name, year, week) week_events.redis = @redis week_events end |