lib/ganymed/sampler/counter.rb in ganymed-0.2.3 vs lib/ganymed/sampler/counter.rb in ganymed-0.3.0
- old
+ new
@@ -1,32 +1,19 @@
+require 'ganymed/ext/array'
require 'ganymed/sampler/datasource'
module Ganymed
class Sampler
##
# A Counter {DataSource} processes samples counting a specific event (such
# as a webserver request, user login, etc) and produces a rate/s gauge.
#
class Counter < DataSource
- def flush(tick, &block)
- each(tick) do |ns, origin, events|
- values = events.sort_by do |event| # sort by timestamp
- event[0]
- end.group_by do |event| # group by timestamp
- event[0].to_i
- end.map do |_, events| # calculate counts for each second.
- events.reduce(0) do |memo, event| # sum(event.count)
- memo + event[1]
- end
- end
-
- yield ns, origin, values
+ def flush
+ each do |ns, origin, ts|
+ yield ns, origin, ts.values.map(&:sum)
end
- end
-
- def feed(ns, origin, ts, value)
- add(ns, origin, [ts, value])
end
end
end
end