class Microstat

Public Class Methods

new(redis: "redis://localhost:6379", namespace: 'mustat') click to toggle source
# File lib/microstat.rb, line 4
def initialize(redis: "redis://localhost:6379", namespace: 'mustat')
  @redis = Redis.new(url: redis)
  @key_space = "#{namespace}:events"
end

Public Instance Methods

date(event_name, date_string) click to toggle source

Returns a count for an arbitrary date string in the yyyymmdd format

# File lib/microstat.rb, line 40
def date(event_name, date_string)
  @redis.get "#{@key_space}:#{event_name}:#{date_string}"
end
dates(event_name) click to toggle source

Given an event type, return an array of dates where we have data

# File lib/microstat.rb, line 30
def dates(event_name)
  @redis.smembers "#{@key_space}:#{event_name}:dates"
end
events() click to toggle source

Lists the known event types

# File lib/microstat.rb, line 10
def events
  @redis.smembers @key_space
end
history(event_name) click to toggle source

Returns a hash with date:count tuples for a given event

# File lib/microstat.rb, line 45
def history(event_name)
  historical_counts = {}

  for date in dates(event_name)
    historical_counts[date] = date(event_name, date)
  end

  return historical_counts
end
today(event_name) click to toggle source

Return today's count for an event

# File lib/microstat.rb, line 35
def today(event_name)
  @redis.get "#{@key_space}:#{event_name}:#{timestamp_today}"
end
total(event_name) click to toggle source

Return the total count for an event

# File lib/microstat.rb, line 56
def total(event_name)
  @redis.get "#{@key_space}:#{event_name}:total"
end
track(event_name) click to toggle source

Increments a specific event's count, returns the total

# File lib/microstat.rb, line 15
def track(event_name)
  # Add the event to the known event space
  @redis.sadd @key_space, event_name

  # Add today as one of the tracked dates for this event
  @redis.sadd "#{@key_space}:#{event_name}:dates", timestamp_today

  # Increment event count for the current date
  @redis.incr "#{@key_space}:#{event_name}:#{timestamp_today}"

  # Increvent total event count, return it
  @redis.incr "#{@key_space}:#{event_name}:total"
end

Private Instance Methods

timestamp_today() click to toggle source

Returns the current date timestamp as a string for key matching

# File lib/microstat.rb, line 63
def timestamp_today
  # Grab current timestamp and format as YYYYMMDD
  Time.new.strftime("%Y%m%d")
end