class Tabs::Metrics::Value
Attributes
key[R]
Public Class Methods
new(key)
click to toggle source
# File lib/tabs/metrics/value.rb, line 9 def initialize(key) @key = key end
Public Instance Methods
drop!()
click to toggle source
# File lib/tabs/metrics/value.rb, line 38 def drop! del_by_prefix("stat:value:#{key}") end
drop_by_resolution!(resolution)
click to toggle source
# File lib/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)
click to toggle source
# File lib/tabs/metrics/value.rb, line 13 def record(value, timestamp=Time.now) timestamp.utc Tabs::Resolution.all.each do |resolution| store_key = storage_key(resolution, timestamp) update_values(store_key, value) Tabs::Resolution.expire(resolution, store_key, timestamp) end true end
stats(period, resolution)
click to toggle source
# File lib/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)
click to toggle source
# File lib/tabs/metrics/value.rb, line 46 def storage_key(resolution, timestamp) formatted_time = Tabs::Resolution.serialize(resolution, timestamp) "stat:value:#{key}:data:#{resolution}:#{formatted_time}" end
Private Instance Methods
default_value(nil_value=nil)
click to toggle source
# File lib/tabs/metrics/value.rb, line 85 def default_value(nil_value=nil) { "count" => 0, "min" => nil_value, "max" => nil_value, "sum" => 0.0, "avg" => 0.0 } end
get_current_hash(stat_key)
click to toggle source
# File lib/tabs/metrics/value.rb, line 62 def get_current_hash(stat_key) hash = get(stat_key) return JSON.parse(hash) if hash default_value end
increment(hash, value)
click to toggle source
# File lib/tabs/metrics/value.rb, line 68 def increment(hash, value) hash["count"] += 1 hash["sum"] += value.to_f end
update_avg(hash)
click to toggle source
# File lib/tabs/metrics/value.rb, line 81 def update_avg(hash) hash["avg"] = hash["sum"].to_f / hash["count"] end
update_max(hash, value)
click to toggle source
# File lib/tabs/metrics/value.rb, line 77 def update_max(hash, value) hash["max"] = value.to_f if hash["max"].nil? || value.to_f > hash["max"] end
update_min(hash, value)
click to toggle source
# File lib/tabs/metrics/value.rb, line 73 def update_min(hash, value) hash["min"] = value.to_f if hash["min"].nil? || value.to_f < hash["min"] end
update_values(stat_key, value)
click to toggle source
# File lib/tabs/metrics/value.rb, line 53 def update_values(stat_key, value) hash = get_current_hash(stat_key) increment(hash, value) update_min(hash, value) update_max(hash, value) update_avg(hash) set(stat_key, JSON.generate(hash)) end