Skip to content

Commit

Permalink
Add .aggregate() function (#11556)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashid Khan authored May 12, 2017
1 parent 99266fc commit f593eb3
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const filename = require('path').basename(__filename);
const fn = require(`../aggregate/index.js`);

import _ from 'lodash';
const expect = require('chai').expect;
import invoke from './helpers/invoke_series_fn.js';

describe(filename, () => {

let seriesList;
beforeEach(() => {
seriesList = require('./fixtures/seriesList.js')();
});

it('first', () => {
return invoke(fn, [seriesList, 'first']).then((r) => {
expect(_.map(r.output.list[1].data, 1)).to.eql([100, 100, 100, 100]);
});
});

it('last', () => {
return invoke(fn, [seriesList, 'last']).then((r) => {
expect(_.map(r.output.list[1].data, 1)).to.eql([20, 20, 20, 20]);
});
});

it('min', () => {
return invoke(fn, [seriesList, 'min']).then((r) => {
expect(_.map(r.output.list[1].data, 1)).to.eql([20, 20, 20, 20]);
});
});

it('max', () => {
return invoke(fn, [seriesList, 'max']).then((r) => {
expect(_.map(r.output.list[1].data, 1)).to.eql([100, 100, 100, 100]);
});
});

it('sum', () => {
return invoke(fn, [seriesList, 'sum']).then((r) => {
expect(_.map(r.output.list[1].data, 1)).to.eql([220, 220, 220, 220]);
});
});

it('cardinality', () => {
return invoke(fn, [seriesList, 'cardinality']).then((r) => {
expect(_.map(r.output.list[1].data, 1)).to.eql([3, 3, 3, 3]);
});
});

it('avg', () => {
return invoke(fn, [seriesList, 'avg']).then((r) => {
expect(_.map(r.output.list[1].data, 1)).to.eql([55, 55, 55, 55]);
});
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ from 'lodash';

module.exports = function (points) {
return _.sum(points) / points.length;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ from 'lodash';

module.exports = function (points) {
return _.uniq(points).length;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ from 'lodash';

module.exports = function (points) {
return _.first(points);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import alter from '../../lib/alter.js';
import Chainable from '../../lib/classes/chainable';
import _ from 'lodash';

const functions = {
avg: require('./avg'),
cardinality: require('./cardinality'),
min: require('./min'),
max: require('./max'),
last: require('./last'),
first: require('./first'),
sum: require('./sum')
};

module.exports = new Chainable('aggregate', {
args: [
{
name: 'inputSeries',
types: ['seriesList']
},
{
name: 'function',
types: ['string'],
help: 'One of ' + _.keys(functions).join(', ')
}
],
help: 'Creates a static line based on result of processing all points in the series.' +
' Available functions: ' + _.keys(functions).join(', '),
fn: function aggregateFn(args) {
const fn = functions[args.byName.function];
if (!fn) throw new Error('.aggregate() function must be one of: ' + _.keys(functions).join(', '));

return alter(args, function (eachSeries) {
const times = _.map(eachSeries.data, 0);
const values = _.map(eachSeries.data, 1);

eachSeries.data = _.zip(times, _.fill(values, fn(values)));
return eachSeries;
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ from 'lodash';

module.exports = function (points) {
return _.last(points);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ from 'lodash';

module.exports = function (points) {
return _.max(points);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ from 'lodash';

module.exports = function (points) {
return _.min(points);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ from 'lodash';

module.exports = function (points) {
return _.sum(points);
};

0 comments on commit f593eb3

Please sign in to comment.