Class: Minuteman

Inherits:
Object
  • Object
show all
Defined in:
lib/minuteman.rb,
lib/minuteman/time_span.rb,
lib/minuteman/time_events.rb,
lib/minuteman/keys_methods.rb,
lib/minuteman/bit_operations.rb,
lib/minuteman/time_spans/day.rb,
lib/minuteman/time_spans/hour.rb,
lib/minuteman/time_spans/week.rb,
lib/minuteman/time_spans/year.rb,
lib/minuteman/time_spans/month.rb,
lib/minuteman/time_spans/minute.rb,
lib/minuteman/bit_operations/data.rb,
lib/minuteman/bit_operations/plain.rb,
lib/minuteman/bit_operations/result.rb,
lib/minuteman/bit_operations/operation.rb,
lib/minuteman/bit_operations/with_data.rb

Defined Under Namespace

Modules: BitOperations, KeysMethods, TimeEvents Classes: Day, Hour, Minute, Month, TimeSpan, Week, Year

Constant Summary collapse

PREFIX =
"minuteman"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Minuteman

Public: Initializes Minuteman

options - The hash to be sent to Redis.new



29
30
31
# File 'lib/minuteman.rb', line 29

def initialize(options = {})
  @redis = Redis.new(options)
end

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



21
22
23
# File 'lib/minuteman.rb', line 21

def redis
  @redis
end

Instance Method Details

#eventsObject

Public: List all the events given the minuteman namespace



69
70
71
72
# File 'lib/minuteman.rb', line 69

def events
  keys = @redis.keys([PREFIX, "*", "????"].join("_"))
  keys.map { |key| key.split("_")[1] }
end

#mark(event_name, ids, time = Time.now.utc) ⇒ Object

Public: Marks an id to a given event on a given time

event_name - The event name to be searched for
ids        - The ids to be tracked

Examples

analytics = Minuteman.new
analytics.mark("login", 1)
analytics.mark("login", [2, 3, 4])


56
57
58
59
60
61
62
63
64
65
# File 'lib/minuteman.rb', line 56

def mark(event_name, ids, time = Time.now.utc)
  event_time = time.kind_of?(Time) ? time : Time.parse(time.to_s)
  time_events = TimeEvents.start(redis, event_name, event_time)

  @redis.multi do
    time_events.each do |event|
      Array(ids).each { |id| redis.setbit(event.key, id, 1) }
    end
  end
end

#operationsObject

Public: List all the operations executed in a given the minuteman namespace



76
77
78
# File 'lib/minuteman.rb', line 76

def operations
  @redis.keys([operations_cache_key_prefix, "*"].join("_"))
end

#reset_allObject

Public: Resets all the used keys



89
90
91
92
# File 'lib/minuteman.rb', line 89

def reset_all
  keys = @redis.keys([PREFIX, "*"].join("_"))
  @redis.del(keys) if keys.any?
end

#reset_operations_cacheObject

Public: Resets the bit operation cache keys



82
83
84
85
# File 'lib/minuteman.rb', line 82

def reset_operations_cache
  keys = @redis.keys([operations_cache_key_prefix, "*"].join("_"))
  @redis.del(keys) if keys.any?
end