module Minuteman

Constants

LUA_CACHE
LUA_OPERATIONS
Result

Public Class Methods

add(action, time = Time.now.utc, users = []) click to toggle source
# File lib/minuteman.rb, line 51
def add(action, time = Time.now.utc, users = [])
  time_spans.each do |time_span|
    process do
      counter = Minuteman::Counter.create({
        type: action,
        time: patterns[time_span].call(time)
      })

      counter.incr
    end
  end

  Array(users).each do |user|
    time_spans.each do |time_span|
      counter = Minuteman::Counter::User.create({
        user_id: user.id,
        type: action,
        time: patterns[time_span].call(time)
      })

      counter.incr
    end
  end
end
analyze(action) click to toggle source
# File lib/minuteman.rb, line 76
def analyze(action)
  analyzers_cache[action]
end
config() click to toggle source
# File lib/minuteman.rb, line 8
def config
  @_configuration ||= Configuration.new
end
configure() { |config| ... } click to toggle source
# File lib/minuteman.rb, line 12
def configure
  yield(config)
end
count(action) click to toggle source
# File lib/minuteman.rb, line 80
def count(action)
  counters_cache[action]
end
events() click to toggle source
# File lib/minuteman.rb, line 28
def events
  config.redis.call("SMEMBERS", "#{Minuteman.prefix}::Events")
end
patterns() click to toggle source
# File lib/minuteman.rb, line 20
def patterns
  config.patterns
end
prefix() click to toggle source
# File lib/minuteman.rb, line 16
def prefix
  config.prefix
end
time_spans() click to toggle source
# File lib/minuteman.rb, line 24
def time_spans
  @_time_spans = patterns.keys
end
track(action, users = nil, time = Time.now.utc) click to toggle source
# File lib/minuteman.rb, line 32
def track(action, users = nil, time = Time.now.utc)
  users = Minuteman::User.create if users.nil?

  Array(users).each do |user|
    process do
      time_spans.each do |time_span|
        event = Minuteman::Event.find_or_create(
          type: action,
          time: patterns[time_span].call(time)
        )

        event.setbit(user.id)
      end
    end
  end

  users
end

Private Class Methods

analyzers_cache() click to toggle source
# File lib/minuteman.rb, line 94
def analyzers_cache
  @_analyzers_cache ||= Hash.new do |h,k|
    h[k] = Minuteman::Analyzer.new(k)
  end
end
counters_cache() click to toggle source
# File lib/minuteman.rb, line 100
def counters_cache
  @_counters_cache ||= Hash.new do |h,k|
    h[k] = Minuteman::Analyzer.new(k, Minuteman::Counter)
  end
end
process(&block) click to toggle source
# File lib/minuteman.rb, line 86
def process(&block)
  if !!config.parallel
    Thread.current(&block)
  else
    block.call
  end
end