Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .aggregate() function to timelion #11556

Merged
merged 1 commit into from
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need a wrapper around lodash ? we use it directly everywhere else in our codebase ? (goes for this, min, max, sum, last)

};
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar comment as @ppisljar. Why not just inline the implementations here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're separate because I reuse them in a function I haven't submitted a pull for yet: https://github.com/rashidkpc/timelion-extras/blob/master/functions/orderby/index.js

};

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);
};