UNPKG

2 kBJavaScriptView Raw
1import Aggregate from './Aggregate';
2import {ValidAggregateOps} from './util/AggregateOps';
3import {extend, inherits} from 'vega-util';
4
5/**
6 * Extend input tuples with aggregate values.
7 * Calcuates aggregate values and joins them with the input stream.
8 * @constructor
9 */
10export default function JoinAggregate(params) {
11 Aggregate.call(this, params);
12}
13
14JoinAggregate.Definition = {
15 'type': 'JoinAggregate',
16 'metadata': {'modifies': true},
17 'params': [
18 { 'name': 'groupby', 'type': 'field', 'array': true },
19 { 'name': 'fields', 'type': 'field', 'null': true, 'array': true },
20 { 'name': 'ops', 'type': 'enum', 'array': true, 'values': ValidAggregateOps },
21 { 'name': 'as', 'type': 'string', 'null': true, 'array': true },
22 { 'name': 'key', 'type': 'field' }
23 ]
24};
25
26var prototype = inherits(JoinAggregate, Aggregate);
27
28prototype.transform = function(_, pulse) {
29 var aggr = this,
30 mod = _.modified(),
31 cells;
32
33 // process all input tuples to calculate aggregates
34 if (aggr.value && (mod || pulse.modified(aggr._inputs, true))) {
35 cells = aggr.value = mod ? aggr.init(_) : {};
36 pulse.visit(pulse.SOURCE, function(t) { aggr.add(t); });
37 } else {
38 cells = aggr.value = aggr.value || this.init(_);
39 pulse.visit(pulse.REM, function(t) { aggr.rem(t); });
40 pulse.visit(pulse.ADD, function(t) { aggr.add(t); });
41 }
42
43 // update aggregation cells
44 aggr.changes();
45
46 // write aggregate values to input tuples
47 pulse.visit(pulse.SOURCE, function(t) {
48 extend(t, cells[aggr.cellkey(t)].tuple);
49 });
50
51 return pulse.reflow(mod).modifies(this._outputs);
52};
53
54prototype.changes = function() {
55 var adds = this._adds,
56 mods = this._mods,
57 i, n;
58
59 for (i=0, n=this._alen; i<n; ++i) {
60 this.celltuple(adds[i]);
61 adds[i] = null; // for garbage collection
62 }
63
64 for (i=0, n=this._mlen; i<n; ++i) {
65 this.celltuple(mods[i]);
66 mods[i] = null; // for garbage collection
67 }
68
69 this._alen = this._mlen = 0; // reset list of active cells
70};