move timeseries processing into 'timeseries' module

This commit is contained in:
Alexander Zobnin
2017-06-25 18:51:55 +03:00
parent 4b10ac1ec2
commit b0da0ffb3e
8 changed files with 852 additions and 643 deletions

View File

@@ -1,118 +1,11 @@
'use strict'; 'use strict';
System.register(['lodash', './utils'], function (_export, _context) { System.register(['lodash', './utils', './timeseries'], function (_export, _context) {
"use strict"; "use strict";
var _, utils, metricFunctions, aggregationFunctions; var _, utils, ts, downsampleSeries, groupBy, sumSeries, scale, delta, SUM, COUNT, AVERAGE, MIN, MAX, MEDIAN, metricFunctions, aggregationFunctions;
/** function limit(order, n, orderByFunc, timeseries) {
* Downsample datapoints series
*/
function downsampleSeries(datapoints, time_to, ms_interval, func) {
var downsampledSeries = [];
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0;
var points_num = 0;
var value_avg = 0;
var frame = [];
for (var i = datapoints.length - 1; i >= 0; i -= 1) {
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
frame.push(datapoints[i][0]);
} else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_.max(frame), timeWindow.to]);
} else if (func === "min") {
downsampledSeries.push([_.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _.groupBy(datapoints, 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];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function (value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _.uniq(_.map(_.flatten(timeseries, true), function (point) {
return point[1];
}));
new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function (series) {
var timestamps = _.map(series, function (point) {
return point[1];
});
var new_points = _.map(_.difference(new_timestamps, timestamps), function (timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}function limit(order, n, orderByFunc, timeseries) {
var orderByCallback = aggregationFunctions[orderByFunc]; var orderByCallback = aggregationFunctions[orderByFunc];
var sortByIteratee = function sortByIteratee(ts) { var sortByIteratee = function sortByIteratee(ts) {
var values = _.map(ts.datapoints, function (point) { var values = _.map(ts.datapoints, function (point) {
@@ -126,31 +19,14 @@ System.register(['lodash', './utils'], function (_export, _context) {
} else { } else {
return sortedTimeseries.slice(-n); return sortedTimeseries.slice(-n);
} }
}function SUM(values) { }
var sum = 0;
_.each(values, function (value) { function setAlias(alias, timeseries) {
sum += value;
});
return sum;
}function COUNT(values) {
return values.length;
}function AVERAGE(values) {
var sum = 0;
_.each(values, function (value) {
sum += value;
});
return sum / values.length;
}function MIN(values) {
return _.min(values);
}function MAX(values) {
return _.max(values);
}function MEDIAN(values) {
var sorted = _.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}function setAlias(alias, timeseries) {
timeseries.target = alias; timeseries.target = alias;
return timeseries; return timeseries;
}function replaceAlias(regexp, newAlias, timeseries) { }
function replaceAlias(regexp, newAlias, timeseries) {
var pattern = void 0; var pattern = void 0;
if (utils.isRegex(regexp)) { if (utils.isRegex(regexp)) {
pattern = utils.buildRegex(regexp); pattern = utils.buildRegex(regexp);
@@ -161,105 +37,71 @@ System.register(['lodash', './utils'], function (_export, _context) {
var alias = timeseries.target.replace(pattern, newAlias); var alias = timeseries.target.replace(pattern, newAlias);
timeseries.target = alias; timeseries.target = alias;
return timeseries; return timeseries;
}function setAliasByRegex(alias, timeseries) { }
function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias); timeseries.target = extractText(timeseries.target, alias);
return timeseries; return timeseries;
}function extractText(str, pattern) { }
function extractText(str, pattern) {
var extractPattern = new RegExp(pattern); var extractPattern = new RegExp(pattern);
var extractedValue = extractPattern.exec(str); var extractedValue = extractPattern.exec(str);
extractedValue = extractedValue[0]; extractedValue = extractedValue[0];
return extractedValue; return extractedValue;
}function scale(factor, datapoints) { }
return _.map(datapoints, function (point) {
return [point[0] * factor, point[1]]; function groupByWrapper(interval, groupFunc, datapoints) {
});
}function delta(datapoints) {
var newSeries = [];
var deltaValue = void 0;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}function groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = aggregationFunctions[groupFunc]; var groupByCallback = aggregationFunctions[groupFunc];
return groupBy(interval, groupByCallback, datapoints); return groupBy(interval, groupByCallback, datapoints);
}function aggregateByWrapper(interval, aggregateFunc, datapoints) { }
function aggregateByWrapper(interval, aggregateFunc, datapoints) {
// Flatten all points in frame and then just use groupBy() // Flatten all points in frame and then just use groupBy()
var flattenedPoints = _.flatten(datapoints, true); var flattenedPoints = _.flatten(datapoints, true);
var groupByCallback = aggregationFunctions[aggregateFunc]; var groupByCallback = aggregationFunctions[aggregateFunc];
return groupBy(interval, groupByCallback, flattenedPoints); return groupBy(interval, groupByCallback, flattenedPoints);
}function aggregateWrapper(groupByCallback, interval, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
return groupBy(interval, groupByCallback, flattenedPoints);
}function sortByTime(series) {
return _.sortBy(series, function (point) {
return point[1];
});
} }
/** function aggregateWrapper(groupByCallback, interval, datapoints) {
* Interpolate series with gaps var flattenedPoints = _.flatten(datapoints, true);
*/ return groupBy(interval, groupByCallback, flattenedPoints);
function interpolateSeries(series) { }
var left, right;
// Interpolate series function timeShift(interval, range) {
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]);
}
}function findNearestRight(series, point) {
var point_index = _.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}function findNearestLeft(series, point) {
var point_index = _.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}function timeShift(interval, range) {
var shift = utils.parseTimeShiftInterval(interval) / 1000; var shift = utils.parseTimeShiftInterval(interval) / 1000;
return _.map(range, function (time) { return _.map(range, function (time) {
return time - shift; return time - shift;
}); });
}function unShiftTimeSeries(interval, datapoints) { }
function unShiftTimeSeries(interval, datapoints) {
var unshift = utils.parseTimeShiftInterval(interval); var unshift = utils.parseTimeShiftInterval(interval);
return _.map(datapoints, function (dp) { return _.map(datapoints, function (dp) {
return [dp[0], dp[1] + unshift]; return [dp[0], dp[1] + unshift];
}); });
}return { }
return {
setters: [function (_lodash) { setters: [function (_lodash) {
_ = _lodash.default; _ = _lodash.default;
}, function (_utils) { }, function (_utils) {
utils = _utils; utils = _utils;
}, function (_timeseries) {
ts = _timeseries.default;
}], }],
execute: function () { execute: function () {
downsampleSeries = ts.downsample;
groupBy = ts.groupBy;
sumSeries = ts.sumSeries;
scale = ts.scale;
delta = ts.delta;
SUM = ts.SUM;
COUNT = ts.COUNT;
AVERAGE = ts.AVERAGE;
MIN = ts.MIN;
MAX = ts.MAX;
MEDIAN = ts.MEDIAN;
metricFunctions = { metricFunctions = {
groupBy: groupByWrapper, groupBy: groupByWrapper,
scale: scale, scale: scale,

File diff suppressed because one or more lines are too long

248
dist/datasource-zabbix/timeseries.js vendored Normal file
View File

@@ -0,0 +1,248 @@
'use strict';
System.register(['lodash', './utils'], function (_export, _context) {
"use strict";
var _, utils, exportedFunctions;
/**
* Downsample time series by using given function (avg, min, max).
*/
/**
* timeseries.js
*
* This module contains functions for working with time series.
*/
function downsample(datapoints, time_to, ms_interval, func) {
var downsampledSeries = [];
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0;
var points_num = 0;
var value_avg = 0;
var frame = [];
for (var i = datapoints.length - 1; i >= 0; i -= 1) {
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
frame.push(datapoints[i][0]);
} else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_.max(frame), timeWindow.to]);
} else if (func === "min") {
downsampledSeries.push([_.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _.groupBy(datapoints, 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];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function (value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
/**
* Summarize set of time series into one.
* @param {object[]} timeseries
*/
function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _.uniq(_.map(_.flatten(timeseries, true), function (point) {
return point[1];
}));
new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function (series) {
var timestamps = _.map(series, function (point) {
return point[1];
});
var new_points = _.map(_.difference(new_timestamps, timestamps), function (timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}function scale(factor, datapoints) {
return _.map(datapoints, function (point) {
return [point[0] * factor, point[1]];
});
}function delta(datapoints) {
var newSeries = [];
var deltaValue = void 0;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}function SUM(values) {
var sum = 0;
_.each(values, function (value) {
sum += value;
});
return sum;
}function COUNT(values) {
return values.length;
}function AVERAGE(values) {
var sum = 0;
_.each(values, function (value) {
sum += value;
});
return sum / values.length;
}function MIN(values) {
return _.min(values);
}function MAX(values) {
return _.max(values);
}function MEDIAN(values) {
var sorted = _.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
///////////////////////
// Utility functions //
///////////////////////
function sortByTime(series) {
return _.sortBy(series, function (point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]);
}
}function findNearestRight(series, point) {
var point_index = _.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}function findNearestLeft(series, point) {
var point_index = _.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}
////////////
// Export //
////////////
return {
setters: [function (_lodash) {
_ = _lodash.default;
}, function (_utils) {
utils = _utils;
}],
execute: function () {
exportedFunctions = {
downsample: downsample,
groupBy: groupBy,
sumSeries: sumSeries,
scale: scale,
delta: delta,
SUM: SUM,
COUNT: COUNT,
AVERAGE: AVERAGE,
MIN: MIN,
MAX: MAX,
MEDIAN: MEDIAN
};
_export('default', exportedFunctions);
}
};
});
//# sourceMappingURL=timeseries.js.map

File diff suppressed because one or more lines are too long

View File

@@ -12,120 +12,26 @@ var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils); var utils = _interopRequireWildcard(_utils);
var _timeseries = require('./timeseries');
var _timeseries2 = _interopRequireDefault(_timeseries);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/** var downsampleSeries = _timeseries2.default.downsample;
* Downsample datapoints series var groupBy = _timeseries2.default.groupBy;
*/ var sumSeries = _timeseries2.default.sumSeries;
function downsampleSeries(datapoints, time_to, ms_interval, func) { var scale = _timeseries2.default.scale;
var downsampledSeries = []; var delta = _timeseries2.default.delta;
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0; var SUM = _timeseries2.default.SUM;
var points_num = 0; var COUNT = _timeseries2.default.COUNT;
var value_avg = 0; var AVERAGE = _timeseries2.default.AVERAGE;
var frame = []; var MIN = _timeseries2.default.MIN;
var MAX = _timeseries2.default.MAX;
for (var i = datapoints.length - 1; i >= 0; i -= 1) { var MEDIAN = _timeseries2.default.MEDIAN;
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
frame.push(datapoints[i][0]);
} else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_lodash2.default.max(frame), timeWindow.to]);
} else if (func === "min") {
downsampledSeries.push([_lodash2.default.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _lodash2.default.groupBy(datapoints, 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 = _lodash2.default.mapValues(frames, function (frame) {
var points = _lodash2.default.map(frame, function (point) {
return point[0];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_lodash2.default.map(grouped, function (value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _lodash2.default.uniq(_lodash2.default.map(_lodash2.default.flatten(timeseries, true), function (point) {
return point[1];
}));
new_timestamps = _lodash2.default.sortBy(new_timestamps);
var interpolated_timeseries = _lodash2.default.map(timeseries, function (series) {
var timestamps = _lodash2.default.map(series, function (point) {
return point[1];
});
var new_points = _lodash2.default.map(_lodash2.default.difference(new_timestamps, timestamps), function (timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_lodash2.default.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}
function limit(order, n, orderByFunc, timeseries) { function limit(order, n, orderByFunc, timeseries) {
var orderByCallback = aggregationFunctions[orderByFunc]; var orderByCallback = aggregationFunctions[orderByFunc];
@@ -143,39 +49,6 @@ function limit(order, n, orderByFunc, timeseries) {
} }
} }
function SUM(values) {
var sum = 0;
_lodash2.default.each(values, function (value) {
sum += value;
});
return sum;
}
function COUNT(values) {
return values.length;
}
function AVERAGE(values) {
var sum = 0;
_lodash2.default.each(values, function (value) {
sum += value;
});
return sum / values.length;
}
function MIN(values) {
return _lodash2.default.min(values);
}
function MAX(values) {
return _lodash2.default.max(values);
}
function MEDIAN(values) {
var sorted = _lodash2.default.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
function setAlias(alias, timeseries) { function setAlias(alias, timeseries) {
timeseries.target = alias; timeseries.target = alias;
return timeseries; return timeseries;
@@ -206,22 +79,6 @@ function extractText(str, pattern) {
return extractedValue; return extractedValue;
} }
function scale(factor, datapoints) {
return _lodash2.default.map(datapoints, function (point) {
return [point[0] * factor, point[1]];
});
}
function delta(datapoints) {
var newSeries = [];
var deltaValue = void 0;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}
function groupByWrapper(interval, groupFunc, datapoints) { function groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = aggregationFunctions[groupFunc]; var groupByCallback = aggregationFunctions[groupFunc];
return groupBy(interval, groupByCallback, datapoints); return groupBy(interval, groupByCallback, datapoints);
@@ -239,65 +96,6 @@ function aggregateWrapper(groupByCallback, interval, datapoints) {
return groupBy(interval, groupByCallback, flattenedPoints); return groupBy(interval, groupByCallback, flattenedPoints);
} }
function sortByTime(series) {
return _lodash2.default.sortBy(series, function (point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}
function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]);
}
}
function findNearestRight(series, point) {
var point_index = _lodash2.default.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _lodash2.default.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}
function timeShift(interval, range) { function timeShift(interval, range) {
var shift = utils.parseTimeShiftInterval(interval) / 1000; var shift = utils.parseTimeShiftInterval(interval) / 1000;
return _lodash2.default.map(range, function (time) { return _lodash2.default.map(range, function (time) {

View File

@@ -0,0 +1,270 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Downsample time series by using given function (avg, min, max).
*/
/**
* timeseries.js
*
* This module contains functions for working with time series.
*/
function downsample(datapoints, time_to, ms_interval, func) {
var downsampledSeries = [];
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0;
var points_num = 0;
var value_avg = 0;
var frame = [];
for (var i = datapoints.length - 1; i >= 0; i -= 1) {
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
frame.push(datapoints[i][0]);
} else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_lodash2.default.max(frame), timeWindow.to]);
} else if (func === "min") {
downsampledSeries.push([_lodash2.default.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _lodash2.default.groupBy(datapoints, 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 = _lodash2.default.mapValues(frames, function (frame) {
var points = _lodash2.default.map(frame, function (point) {
return point[0];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_lodash2.default.map(grouped, function (value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
/**
* Summarize set of time series into one.
* @param {object[]} timeseries
*/
function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _lodash2.default.uniq(_lodash2.default.map(_lodash2.default.flatten(timeseries, true), function (point) {
return point[1];
}));
new_timestamps = _lodash2.default.sortBy(new_timestamps);
var interpolated_timeseries = _lodash2.default.map(timeseries, function (series) {
var timestamps = _lodash2.default.map(series, function (point) {
return point[1];
});
var new_points = _lodash2.default.map(_lodash2.default.difference(new_timestamps, timestamps), function (timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_lodash2.default.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}
function scale(factor, datapoints) {
return _lodash2.default.map(datapoints, function (point) {
return [point[0] * factor, point[1]];
});
}
function delta(datapoints) {
var newSeries = [];
var deltaValue = void 0;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}
function SUM(values) {
var sum = 0;
_lodash2.default.each(values, function (value) {
sum += value;
});
return sum;
}
function COUNT(values) {
return values.length;
}
function AVERAGE(values) {
var sum = 0;
_lodash2.default.each(values, function (value) {
sum += value;
});
return sum / values.length;
}
function MIN(values) {
return _lodash2.default.min(values);
}
function MAX(values) {
return _lodash2.default.max(values);
}
function MEDIAN(values) {
var sorted = _lodash2.default.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
///////////////////////
// Utility functions //
///////////////////////
function sortByTime(series) {
return _lodash2.default.sortBy(series, function (point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}
function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]);
}
}
function findNearestRight(series, point) {
var point_index = _lodash2.default.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _lodash2.default.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}
////////////
// Export //
////////////
var exportedFunctions = {
downsample: downsample,
groupBy: groupBy,
sumSeries: sumSeries,
scale: scale,
delta: delta,
SUM: SUM,
COUNT: COUNT,
AVERAGE: AVERAGE,
MIN: MIN,
MAX: MAX,
MEDIAN: MEDIAN
};
exports.default = exportedFunctions;

View File

@@ -1,118 +1,19 @@
import _ from 'lodash'; import _ from 'lodash';
import * as utils from './utils'; import * as utils from './utils';
import ts from './timeseries';
/** let downsampleSeries = ts.downsample;
* Downsample datapoints series let groupBy = ts.groupBy;
*/ let sumSeries = ts.sumSeries;
function downsampleSeries(datapoints, time_to, ms_interval, func) { let scale = ts.scale;
var downsampledSeries = []; let delta = ts.delta;
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0; let SUM = ts.SUM;
var points_num = 0; let COUNT = ts.COUNT;
var value_avg = 0; let AVERAGE = ts.AVERAGE;
var frame = []; let MIN = ts.MIN;
let MAX = ts.MAX;
for (var i = datapoints.length - 1; i >= 0; i -= 1) { let MEDIAN = ts.MEDIAN;
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
frame.push(datapoints[i][0]);
}
else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_.max(frame), timeWindow.to]);
}
else if (func === "min") {
downsampledSeries.push([_.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _.groupBy(datapoints, 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];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function(value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _.uniq(_.map(_.flatten(timeseries, true), function(point) {
return point[1];
}));
new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function(series) {
var timestamps = _.map(series, function(point) {
return point[1];
});
var new_points = _.map(_.difference(new_timestamps, timestamps), function(timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}
function limit(order, n, orderByFunc, timeseries) { function limit(order, n, orderByFunc, timeseries) {
let orderByCallback = aggregationFunctions[orderByFunc]; let orderByCallback = aggregationFunctions[orderByFunc];
@@ -130,39 +31,6 @@ function limit(order, n, orderByFunc, timeseries) {
} }
} }
function SUM(values) {
var sum = 0;
_.each(values, function(value) {
sum += value;
});
return sum;
}
function COUNT(values) {
return values.length;
}
function AVERAGE(values) {
var sum = 0;
_.each(values, function(value) {
sum += value;
});
return sum / values.length;
}
function MIN(values) {
return _.min(values);
}
function MAX(values) {
return _.max(values);
}
function MEDIAN(values) {
var sorted = _.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
function setAlias(alias, timeseries) { function setAlias(alias, timeseries) {
timeseries.target = alias; timeseries.target = alias;
return timeseries; return timeseries;
@@ -193,25 +61,6 @@ function extractText(str, pattern) {
return extractedValue; return extractedValue;
} }
function scale(factor, datapoints) {
return _.map(datapoints, point => {
return [
point[0] * factor,
point[1]
];
});
}
function delta(datapoints) {
let newSeries = [];
let deltaValue;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}
function groupByWrapper(interval, groupFunc, datapoints) { function groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = aggregationFunctions[groupFunc]; var groupByCallback = aggregationFunctions[groupFunc];
return groupBy(interval, groupByCallback, datapoints); return groupBy(interval, groupByCallback, datapoints);
@@ -229,65 +78,6 @@ function aggregateWrapper(groupByCallback, interval, datapoints) {
return groupBy(interval, groupByCallback, flattenedPoints); return groupBy(interval, groupByCallback, flattenedPoints);
} }
function sortByTime(series) {
return _.sortBy(series, function(point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}
function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return (left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]));
}
}
function findNearestRight(series, point) {
var point_index = _.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}
function timeShift(interval, range) { function timeShift(interval, range) {
let shift = utils.parseTimeShiftInterval(interval) / 1000; let shift = utils.parseTimeShiftInterval(interval) / 1000;
return _.map(range, time => { return _.map(range, time => {

View File

@@ -0,0 +1,260 @@
/**
* timeseries.js
*
* This module contains functions for working with time series.
*/
import _ from 'lodash';
import * as utils from './utils';
/**
* Downsample time series by using given function (avg, min, max).
*/
function downsample(datapoints, time_to, ms_interval, func) {
var downsampledSeries = [];
var timeWindow = {
from: time_to * 1000 - ms_interval,
to: time_to * 1000
};
var points_sum = 0;
var points_num = 0;
var value_avg = 0;
var frame = [];
for (var i = datapoints.length - 1; i >= 0; i -= 1) {
if (timeWindow.from < datapoints[i][1] && datapoints[i][1] <= timeWindow.to) {
points_sum += datapoints[i][0];
points_num++;
frame.push(datapoints[i][0]);
}
else {
value_avg = points_num ? points_sum / points_num : 0;
if (func === "max") {
downsampledSeries.push([_.max(frame), timeWindow.to]);
}
else if (func === "min") {
downsampledSeries.push([_.min(frame), timeWindow.to]);
}
// avg by default
else {
downsampledSeries.push([value_avg, timeWindow.to]);
}
// Shift time window
timeWindow.to = timeWindow.from;
timeWindow.from -= ms_interval;
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;
}
}
return downsampledSeries.reverse();
}
/**
* Group points by given time interval
* datapoints: [[<value>, <unixtime>], ...]
*/
function groupBy(interval, groupByCallback, datapoints) {
var ms_interval = utils.parseInterval(interval);
// Calculate frame timestamps
var frames = _.groupBy(datapoints, 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];
});
return groupByCallback(points);
});
// Convert points to Grafana format
return sortByTime(_.map(grouped, function (value, timestamp) {
return [Number(value), Number(timestamp)];
}));
}
/**
* Summarize set of time series into one.
* @param {object[]} timeseries
*/
function sumSeries(timeseries) {
// Calculate new points for interpolation
var new_timestamps = _.uniq(_.map(_.flatten(timeseries, true), function (point) {
return point[1];
}));
new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function (series) {
var timestamps = _.map(series, function (point) {
return point[1];
});
var new_points = _.map(_.difference(new_timestamps, timestamps), function (timestamp) {
return [null, timestamp];
});
var new_series = series.concat(new_points);
return sortByTime(new_series);
});
_.each(interpolated_timeseries, interpolateSeries);
var new_timeseries = [];
var sum;
for (var i = new_timestamps.length - 1; i >= 0; i--) {
sum = 0;
for (var j = interpolated_timeseries.length - 1; j >= 0; j--) {
sum += interpolated_timeseries[j][i][0];
}
new_timeseries.push([sum, new_timestamps[i]]);
}
return sortByTime(new_timeseries);
}
function scale(factor, datapoints) {
return _.map(datapoints, point => {
return [
point[0] * factor,
point[1]
];
});
}
function delta(datapoints) {
let newSeries = [];
let deltaValue;
for (var i = 1; i < datapoints.length; i++) {
deltaValue = datapoints[i][0] - datapoints[i - 1][0];
newSeries.push([deltaValue, datapoints[i][1]]);
}
return newSeries;
}
function SUM(values) {
var sum = 0;
_.each(values, function (value) {
sum += value;
});
return sum;
}
function COUNT(values) {
return values.length;
}
function AVERAGE(values) {
var sum = 0;
_.each(values, function (value) {
sum += value;
});
return sum / values.length;
}
function MIN(values) {
return _.min(values);
}
function MAX(values) {
return _.max(values);
}
function MEDIAN(values) {
var sorted = _.sortBy(values);
return sorted[Math.floor(sorted.length / 2)];
}
///////////////////////
// Utility functions //
///////////////////////
function sortByTime(series) {
return _.sortBy(series, function (point) {
return point[1];
});
}
/**
* Interpolate series with gaps
*/
function interpolateSeries(series) {
var left, right;
// Interpolate series
for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) {
left = findNearestLeft(series, series[i]);
right = findNearestRight(series, series[i]);
if (!left) {
left = right;
}
if (!right) {
right = left;
}
series[i][0] = linearInterpolation(series[i][1], left, right);
}
}
return series;
}
function linearInterpolation(timestamp, left, right) {
if (left[1] === right[1]) {
return (left[0] + right[0]) / 2;
} else {
return (left[0] + (right[0] - left[0]) / (right[1] - left[1]) * (timestamp - left[1]));
}
}
function findNearestRight(series, point) {
var point_index = _.indexOf(series, point);
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestRight;
}
function findNearestLeft(series, point) {
var point_index = _.indexOf(series, point);
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) {
return series[i];
}
}
return nearestLeft;
}
////////////
// Export //
////////////
const exportedFunctions = {
downsample,
groupBy,
sumSeries,
scale,
delta,
SUM,
COUNT,
AVERAGE,
MIN,
MAX,
MEDIAN
};
export default exportedFunctions;