class TabsTabs::Metrics::Counter

Attributes

key[R]

Public Class Methods

new(key) click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 9
def initialize(key)
  @key = key
end

Public Instance Methods

drop!() click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 42
def drop!
  del_by_prefix("stat:counter:#{key}")
end
drop_by_resolution!(resolution) click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 46
def drop_by_resolution!(resolution)
  del_by_prefix("stat:counter:#{key}:count:#{resolution}")
end
increment(timestamp=Time.now) click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 13
def increment(timestamp=Time.now)
  timestamp.utc
  TabsTabs::Resolution.all.each do |resolution|
    increment_resolution(resolution, timestamp)
  end
  increment_total
  true
end
stats(period, resolution) click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 22
def stats(period, resolution)
  timestamps = timestamp_range period, resolution
  keys = timestamps.map do |timestamp|
    storage_key(resolution, timestamp)
  end

  values = mget(*keys).map do |v|
    {
      "timestamp" => timestamps.shift,
      "count" => (v || 0).to_i
    }.with_indifferent_access
  end

  Stats.new(period, resolution, values)
end
storage_key(resolution, timestamp) click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 50
def storage_key(resolution, timestamp)
  formatted_time = TabsTabs::Resolution.serialize(resolution, timestamp)
  "stat:counter:#{key}:count:#{resolution}:#{formatted_time}"
end
total() click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 38
def total
  (get("stat:counter:#{key}:total") || 0).to_i
end

Private Instance Methods

increment_resolution(resolution, timestamp) click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 57
def increment_resolution(resolution, timestamp)
  store_key = storage_key(resolution, timestamp)
  incr(store_key)
  TabsTabs::Resolution.expire(resolution, store_key, timestamp)
end
increment_total() click to toggle source
# File lib/tabs_tabs/metrics/counter.rb, line 63
def increment_total
  incr("stat:counter:#{key}:total")
end