lib/tabs/metrics/value.rb in tabs-0.9.1 vs lib/tabs/metrics/value.rb in tabs-1.0.0

- old
+ new

@@ -11,27 +11,25 @@ end def record(value, timestamp=Time.now) timestamp.utc Tabs::Resolution.all.each do |resolution| - formatted_time = Tabs::Resolution.serialize(resolution, timestamp) - stat_key = "stat:value:#{key}:data:#{formatted_time}" - update_values(stat_key, value) + store_key = storage_key(resolution, timestamp) + update_values(store_key, value) + Tabs::Resolution.expire(resolution, store_key, timestamp) end true end def stats(period, resolution) timestamps = timestamp_range period, resolution - keys = timestamps.map do |ts| - formatted_time = Tabs::Resolution.serialize(resolution, ts) - "stat:value:#{key}:data:#{formatted_time}" + keys = timestamps.map do |timestamp| + storage_key(resolution, timestamp) end values = mget(*keys).map do |v| - value = v.nil? ? default_value(0) : v - value = Hash[value.map { |k, i| [k, to_numeric(i)] }] + value = v.nil? ? default_value(0) : JSON.parse(v) value["timestamp"] = timestamps.shift value.with_indifferent_access end Stats.new(period, resolution, values) @@ -39,44 +37,54 @@ def drop! del_by_prefix("stat:value:#{key}") end + def drop_by_resolution!(resolution) + del_by_prefix("stat:value:#{key}:data:#{resolution}") + end + + def storage_key(resolution, timestamp) + formatted_time = Tabs::Resolution.serialize(resolution, timestamp) + "stat:value:#{key}:data:#{resolution}:#{formatted_time}" + end + private def update_values(stat_key, value) - count = update_count(stat_key) - sum = update_sum(stat_key, value) - update_min(stat_key, value) - update_max(stat_key, value) - update_avg(stat_key, sum, count) + 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 - def update_count(stat_key) - hincrby(stat_key, "count", 1) + def get_current_hash(stat_key) + hash = get(stat_key) + return JSON.parse(hash) if hash + default_value end - def update_sum(stat_key, value) - hincrby(stat_key, "sum", value) + def increment(hash, value) + hash["count"] += 1 + hash["sum"] += value.to_f end - def update_min(stat_key, value) - min = (hget(stat_key, "min") || 0).to_i - hset(stat_key, "min", value) if value < min || min == 0 + def update_min(hash, value) + hash["min"] = value.to_f if hash["min"].nil? || value.to_f < hash["min"] end - def update_max(stat_key, value) - max = (hget(stat_key, "max") || 0).to_i - hset(stat_key, "max", value) if value > max || max == 0 + def update_max(hash, value) + hash["max"] = value.to_f if hash["max"].nil? || value.to_f > hash["max"] end - def update_avg(stat_key, sum, count) - avg = sum.to_f / count.to_f - hset(stat_key, "avg", avg) + def update_avg(hash) + hash["avg"] = hash["sum"].to_f / hash["count"] end def default_value(nil_value=nil) - { "count" => 0, "min" => nil_value, "max" => nil_value, "sum" => 0, "avg" => 0 } + { "count" => 0, "min" => nil_value, "max" => nil_value, "sum" => 0.0, "avg" => 0.0 } end end end end