require 'ganymed/sampler/datasource' module Ganymed class Sampler ## # A Derive {DataSource data source} will store the derivative of the line # 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 end yield ns, origin, values end end def feed(ns, origin, ts, value) add(ns, origin, [ts, value]) end end end end