new function percentil

This commit is contained in:
Pedro Henrique Ribeiro Freitas
2016-09-28 16:42:24 -03:00
parent 61d1fede11
commit 2a66cac230
2 changed files with 37 additions and 0 deletions

View File

@@ -158,6 +158,32 @@ export default class DataProcessor {
return timeseries; return timeseries;
} }
static PERCENTIL(interval, n, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _.groupBy(flattenedPoints, function(point) {
// Calculate time for group of points
return Math.floor(point[1] / ms_interval) * ms_interval;
});
// frame: { '<unixtime>': [[<value>, <unixtime>], ...] }
// return [{ '<unixtime>': <value> }, { '<unixtime>': <value> }, ...]
var grouped = _.mapValues(frames, function(frame) {
var points = _.map(frame, function(point) {
return point[0];
});
var sorted = _.sortBy(points);
return sorted[Math.floor(sorted.length*n/100)];
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function(value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
static scale(factor, datapoints) { static scale(factor, datapoints) {
return _.map(datapoints, point => { return _.map(datapoints, point => {
return [ return [
@@ -208,6 +234,7 @@ export default class DataProcessor {
groupBy: this.groupByWrapper, groupBy: this.groupByWrapper,
scale: this.scale, scale: this.scale,
delta: this.delta, delta: this.delta,
percentil: this.PERCENTIL,
aggregateBy: this.aggregateByWrapper, aggregateBy: this.aggregateByWrapper,
average: _.partial(this.aggregateWrapper, this.AVERAGE), average: _.partial(this.aggregateWrapper, this.AVERAGE),
min: _.partial(this.aggregateWrapper, this.MIN), min: _.partial(this.aggregateWrapper, this.MIN),

View File

@@ -72,6 +72,16 @@ addFuncDef({
defaultParams: ['1m'], defaultParams: ['1m'],
}); });
addFuncDef({
name: 'percentil',
category: 'Aggregate',
params: [
{ name: 'interval', type: 'string' },
{ name: 'percent', type: 'string' }
],
defaultParams: ['1m','95'],
});
addFuncDef({ addFuncDef({
name: 'min', name: 'min',
category: 'Aggregate', category: 'Aggregate',