-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rashid Khan
authored
May 12, 2017
1 parent
99266fc
commit f593eb3
Showing
9 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/core_plugins/timelion/server/series_functions/__tests__/aggregate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); | ||
|
||
}); |
5 changes: 5 additions & 0 deletions
5
src/core_plugins/timelion/server/series_functions/aggregate/avg.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/core_plugins/timelion/server/series_functions/aggregate/cardinality.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/core_plugins/timelion/server/series_functions/aggregate/first.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import _ from 'lodash'; | ||
|
||
module.exports = function (points) { | ||
return _.first(points); | ||
}; |
41 changes: 41 additions & 0 deletions
41
src/core_plugins/timelion/server/series_functions/aggregate/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
}); |
5 changes: 5 additions & 0 deletions
5
src/core_plugins/timelion/server/series_functions/aggregate/last.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import _ from 'lodash'; | ||
|
||
module.exports = function (points) { | ||
return _.last(points); | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/core_plugins/timelion/server/series_functions/aggregate/max.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import _ from 'lodash'; | ||
|
||
module.exports = function (points) { | ||
return _.max(points); | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/core_plugins/timelion/server/series_functions/aggregate/min.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import _ from 'lodash'; | ||
|
||
module.exports = function (points) { | ||
return _.min(points); | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/core_plugins/timelion/server/series_functions/aggregate/sum.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import _ from 'lodash'; | ||
|
||
module.exports = function (points) { | ||
return _.sum(points); | ||
}; |