UNPKG

1.19 kBtext/coffeescriptView Raw
1class CompoundAggregator
2 constructor: ->
3 @aggregators = {}
4 track: (name, opts) ->
5 getValue = switch typeof opts.field
6 when 'string' then ((o) -> o[opts.field])
7 when 'number' then ((o) -> opts.field)
8 when 'function' then ((o) -> opts.field(o))
9 else ((o) -> o)
10 @aggregators[name] = {getValue:getValue, aggregator:opts.aggregator(opts)}
11 console.log("Tracking '#{@aggregators[name].aggregator.name}' as '#{name}' with #{JSON.stringify(opts)}")
12 on: (name, event, callback) ->
13 if arguments.length == 3
14 @aggregators[name].aggregator.on(event, callback)
15 else
16 event = name
17 callback = event
18 agg.aggregator.on(event, callback) for name, agg of @aggregators
19 push: (time, obj) ->
20 for name, agg of @aggregators
21 val = agg.getValue(obj)
22 agg.aggregator.push(time, val)
23 value: (name) ->
24 if typeof name is 'string'
25 console.log("Aggregation '#{name}' does not exist!") unless @aggregators[name]
26 @aggregators[name].aggregator.value()
27 else
28 ret = {}
29 for name, agg of @aggregators
30 ret[name] = agg.aggregator.value()
31 ret
32
33exports.create = -> new CompoundAggregator()