Class: TabsTabs::Metrics::Value

Inherits:
Object
  • Object
show all
Includes:
Helpers, Storage
Defined in:
lib/tabs_tabs/metrics/value.rb,
lib/tabs_tabs/metrics/value/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) ⇒ Value

Returns a new instance of Value.



9
10
11
# File 'lib/tabs_tabs/metrics/value.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/value.rb', line 7

def key
  @key
end

Instance Method Details

#drop!Object



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

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

#drop_by_resolution!(resolution) ⇒ Object



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

def drop_by_resolution!(resolution)
  del_by_prefix("stat:value:#{key}:data:#{resolution}")
end

#record(value, timestamp = Time.now) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/tabs_tabs/metrics/value.rb', line 13

def record(value, timestamp=Time.now)
  timestamp.utc
  TabsTabs::Resolution.all.each do |resolution|
    store_key = storage_key(resolution, timestamp)
    update_values(store_key, value)
    TabsTabs::Resolution.expire(resolution, store_key, timestamp)
  end
  true
end

#stats(period, resolution) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tabs_tabs/metrics/value.rb', line 23

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|
    value = v.nil? ? default_value(0) : JSON.parse(v)
    value["timestamp"] = timestamps.shift
    value.with_indifferent_access
  end

  Stats.new(period, resolution, values)
end

#storage_key(resolution, timestamp) ⇒ Object



46
47
48
49
# File 'lib/tabs_tabs/metrics/value.rb', line 46

def storage_key(resolution, timestamp)
  formatted_time = TabsTabs::Resolution.serialize(resolution, timestamp)
  "stat:value:#{key}:data:#{resolution}:#{formatted_time}"
end