lib/fnordmetric/gauge_calculations.rb in fnordmetric-0.5.1 vs lib/fnordmetric/gauge_calculations.rb in fnordmetric-0.5.2
- old
+ new
@@ -1,12 +1,19 @@
module FnordMetric::GaugeCalculations
@@avg_per_session_proc = proc{ |_v, _t|
- #raise redis.get(tick_key(_t, :"sessions-count")).inspect
- (_v.to_f / (redis.get(tick_key(_t, :"sessions-count"))||0).to_i)
+ (_v.to_f / (redis.get(tick_key(_t, :"sessions-count"))||1).to_i)
}
+ @@count_per_session_proc = proc{ |_v, _t|
+ (redis.get(tick_key(_t, :"sessions-count"))||0).to_i
+ }
+
+ @@avg_per_count_proc = proc{ |_v, _t|
+ (_v.to_f / (redis.get(tick_key(_t, :"value-count"))||1).to_i)
+ }
+
def value_at(time, opts={}, &block)
_t = tick_at(time)
_v = redis.hget(key, _t)
calculate_value(_v, _t, opts, block)
@@ -25,16 +32,33 @@
def values_in(range, opts={}, &block)
values_at((tick_at(range.first)..range.last).step(tick))
end
def calculate_value(_v, _t, opts, block)
- block = @@avg_per_session_proc if opts[:avg_per_session]
-
+ block = @@avg_per_count_proc if average?
+ #block = @@count_per_session_proc if unique?
+ block = @@avg_per_session_proc if unique? && average?
+
if block
instance_exec(_v, _t, &block)
else
_v
end
+ end
+
+ def field_values_at(time, opts={}, &block)
+ opts[:max_fields] ||= 50
+ redis.zrevrange(
+ tick_key(time),
+ 0, opts[:max_fields]-1,
+ :withscores => true
+ ).in_groups_of(2).map do |key, val|
+ [key, calculate_value(val, time, opts, block)]
+ end
+ end
+
+ def field_values_total(time)
+ (redis.get(tick_key(time, :count))||0).to_i
end
def redis
@opts[:redis]
end
\ No newline at end of file