Class: TabsTabs::Metrics::Counter

Inherits:
Object
  • Object
show all
Includes:
Helpers, Storage
Defined in:
lib/tabs_tabs/metrics/counter.rb,
lib/tabs_tabs/metrics/counter/stats.rb

Defined Under Namespace

Classes: Stats

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#normalize_period, #timestamp_range, #to_numeric

Methods included from Storage

#del, #del_by_prefix, #exists, #expireat, #get, #hdel, #hget, #hgetall, #hincrby, #hkeys, #hset, #incr, #mget, #redis, #rpush, #sadd, #set, #sismember, #smembers, #smembers_all, #tabs_key, #ttl

Constructor Details

#initialize(key) ⇒ Counter

Returns a new instance of Counter.



9
10
11
# File 'lib/tabs_tabs/metrics/counter.rb', line 9

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/tabs_tabs/metrics/counter.rb', line 7

def key
  @key
end

Instance Method Details

#drop!Object



42
43
44
# File 'lib/tabs_tabs/metrics/counter.rb', line 42

def drop!
  del_by_prefix("stat:counter:#{key}")
end

#drop_by_resolution!(resolution) ⇒ Object



46
47
48
# 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) ⇒ Object



13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



50
51
52
53
# 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

#totalObject



38
39
40
# File 'lib/tabs_tabs/metrics/counter.rb', line 38

def total
  (get("stat:counter:#{key}:total") || 0).to_i
end