lib/fnordmetric/gauge_modifiers.rb in fnordmetric-0.5.1 vs lib/fnordmetric/gauge_modifiers.rb in fnordmetric-0.5.2
- old
+ new
@@ -3,10 +3,12 @@
def incr(gauge_name, value=1)
gauge = fetch_gauge(gauge_name)
assure_two_dimensional!(gauge)
if gauge.unique?
incr_uniq(gauge, value)
+ elsif gauge.average?
+ incr_avg(gauge, value)
else
incr_tick(gauge, value)
end
end
@@ -16,28 +18,59 @@
@redis.hsetnx(gauge.key, gauge.tick_at(time), head).callback do |_new|
@redis.hincrby(gauge.key, gauge.tick_at(time), value) unless _new
end
end
else
- @redis.hincrby(gauge.key, gauge.tick_at(time), value)
+ @redis.hsetnx(gauge.key, gauge.tick_at(time), 0).callback do
+ @redis.hincrby(gauge.key, gauge.tick_at(time), value)
+ end
end
end
- def incr_uniq(gauge, value)
- return false unless session_key
+ def incr_uniq(gauge, value, field_name=nil)
+ return false if session_key.blank?
@redis.sadd(gauge.tick_key(time, :sessions), session_key).callback do |_new|
@redis.expire(gauge.tick_key(time, :sessions), gauge.tick)
- if _new
+ if (_new == 1) || (_new == true) #redis vs. em-redis
@redis.incr(gauge.tick_key(time, :"sessions-count")).callback do |sc|
- incr_tick(gauge, value)
+ field_name ? incr_field_by(gauge, field_name, value) : incr_tick(gauge, value)
end
end
end
end
+ def incr_avg(gauge, value)
+ @redis.incr(gauge.tick_key(time, :"value-count")).callback do
+ incr_tick(gauge, value)
+ end
+ end
+
def incr_field(gauge_name, field_name, value=1)
gauge = fetch_gauge(gauge_name)
assure_three_dimensional!(gauge)
- # here be dragons
+ if gauge.unique?
+ incr_uniq(gauge, value, field_name)
+ else
+ incr_field_by(gauge, field_name, value)
+ end
end
+
+ def incr_field_by(gauge, field_name, value)
+ @redis.zincrby(gauge.tick_key(time), value, field_name).callback do
+ @redis.incrby(gauge.tick_key(time, :count), 1)
+ end
+ end
+
+ def set_value(gauge_name, value)
+ gauge = fetch_gauge(gauge_name)
+ assure_two_dimensional!(gauge)
+ @redis.hset(gauge.key, gauge.tick_at(time), value)
+ end
+
+ def set_field(gauge_name, field_name, value)
+ gauge = fetch_gauge(gauge_name)
+ assure_three_dimensional!(gauge)
+ @redis.zadd(gauge.tick_key(time), value, field_name)
+ end
+
end
\ No newline at end of file