class BitAnalytics

Constants

VERSION

Attributes

redis[RW]

Public Class Methods

new(options=nil) click to toggle source
# File lib/bit_analytics.rb, line 7
def initialize(options=nil)
  @redis = if options
    Redis.new(options)
  else
    Redis.new
  end
end

Public Instance Methods

_prefix_key(event_name, date) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_events() click to toggle source

Delete all events from the database.

# 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_keys() click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source

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)

# 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) click to toggle source
# 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) click to toggle source
# 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