mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
Add .aggregate() function (#11556)
This commit is contained in:
parent
99266fca67
commit
f593eb3d06
9 changed files with 133 additions and 0 deletions
|
@ -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]);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
|
@ -0,0 +1,5 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
module.exports = function (points) {
|
||||
return _.sum(points) / points.length;
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
module.exports = function (points) {
|
||||
return _.uniq(points).length;
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
module.exports = function (points) {
|
||||
return _.first(points);
|
||||
};
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
});
|
|
@ -0,0 +1,5 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
module.exports = function (points) {
|
||||
return _.last(points);
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
module.exports = function (points) {
|
||||
return _.max(points);
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
module.exports = function (points) {
|
||||
return _.min(points);
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
module.exports = function (points) {
|
||||
return _.sum(points);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue