diff --git a/src/core_plugins/timelion/server/series_functions/__tests__/aggregate.js b/src/core_plugins/timelion/server/series_functions/__tests__/aggregate.js new file mode 100644 index 0000000000000..ec5d849d7a733 --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/__tests__/aggregate.js @@ -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]); + }); + }); + +}); diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/avg.js b/src/core_plugins/timelion/server/series_functions/aggregate/avg.js new file mode 100644 index 0000000000000..90a4ff5eda964 --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/avg.js @@ -0,0 +1,5 @@ +import _ from 'lodash'; + +module.exports = function (points) { + return _.sum(points) / points.length; +}; diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/cardinality.js b/src/core_plugins/timelion/server/series_functions/aggregate/cardinality.js new file mode 100644 index 0000000000000..e1f0d08834d62 --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/cardinality.js @@ -0,0 +1,5 @@ +import _ from 'lodash'; + +module.exports = function (points) { + return _.uniq(points).length; +}; diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/first.js b/src/core_plugins/timelion/server/series_functions/aggregate/first.js new file mode 100644 index 0000000000000..3036c5b541d45 --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/first.js @@ -0,0 +1,5 @@ +import _ from 'lodash'; + +module.exports = function (points) { + return _.first(points); +}; diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/index.js b/src/core_plugins/timelion/server/series_functions/aggregate/index.js new file mode 100644 index 0000000000000..11a6befb5a93d --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/index.js @@ -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; + }); + } +}); diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/last.js b/src/core_plugins/timelion/server/series_functions/aggregate/last.js new file mode 100644 index 0000000000000..ef214c896a4dd --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/last.js @@ -0,0 +1,5 @@ +import _ from 'lodash'; + +module.exports = function (points) { + return _.last(points); +}; diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/max.js b/src/core_plugins/timelion/server/series_functions/aggregate/max.js new file mode 100644 index 0000000000000..3edf18002cec0 --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/max.js @@ -0,0 +1,5 @@ +import _ from 'lodash'; + +module.exports = function (points) { + return _.max(points); +}; diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/min.js b/src/core_plugins/timelion/server/series_functions/aggregate/min.js new file mode 100644 index 0000000000000..8c5ce385272ab --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/min.js @@ -0,0 +1,5 @@ +import _ from 'lodash'; + +module.exports = function (points) { + return _.min(points); +}; diff --git a/src/core_plugins/timelion/server/series_functions/aggregate/sum.js b/src/core_plugins/timelion/server/series_functions/aggregate/sum.js new file mode 100644 index 0000000000000..e32d6e7f73228 --- /dev/null +++ b/src/core_plugins/timelion/server/series_functions/aggregate/sum.js @@ -0,0 +1,5 @@ +import _ from 'lodash'; + +module.exports = function (points) { + return _.sum(points); +};