lib/ganymed/sampler/derive.rb in ganymed-0.2.3 vs lib/ganymed/sampler/derive.rb in ganymed-0.3.0

- old
+ new

@@ -8,29 +8,20 @@ # going from the last to the current value of the data source. Derive is # similar to a {Counter} {DataSource data source} in that it produces a # rate/s gauge, but only accepts absolute values and produces the # derivative instead of summation of all samples. # - # In order to get (meaningful) realtime events from a Derive {DataSource} - # samples must be sent multiple times per second. Derive does not store the - # previously flushed value for performance reasons and therefore cannot - # infer a rate/s gauge with only one event per flush. - # class Derive < DataSource - def flush(tick, &block) - each(tick) do |ns, origin, events| - values = events.sort_by do |event| # sort by timestamp - event[0] - end.each_cons(2).map do |a, b| # derive each consective sample - (b[1] - a[1])/(b[0] - a[0]) # ∆value / ∆ts + def flush + each do |ns, origin, ts| + values = ts.each_cons(2).map do |a, b| + (b[1].last - a[1].last)/(b[0] - a[0]) # ∆value / ∆ts + end.reject do |value| + value < 0 # ignore overflow/counter wrap end yield ns, origin, values end - end - - def feed(ns, origin, ts, value) - add(ns, origin, [ts, value]) end end end end