Added DataProcessingService for timeseries post-processing.
This commit is contained in:
99
plugins/datasource-zabbix/dataProcessingService.js
Normal file
99
plugins/datasource-zabbix/dataProcessingService.js
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
define([
|
||||||
|
'angular',
|
||||||
|
'lodash',
|
||||||
|
'moment'
|
||||||
|
],
|
||||||
|
function (angular, _, moment) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var module = angular.module('grafana.services');
|
||||||
|
|
||||||
|
module.service('DataProcessingService', function() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downsample datapoints series
|
||||||
|
*/
|
||||||
|
this.downsampleSeries = function(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>], ...]
|
||||||
|
*/
|
||||||
|
this.groupBy = function(datapoints, ms_interval, groupByCallback) {
|
||||||
|
var frames = _.groupBy(datapoints, function(point) {
|
||||||
|
var group_time = Number(moment.utc(point[1]).startOf('minute').valueOf());
|
||||||
|
group_time = Math.ceil(point[1] / ms_interval) * ms_interval;
|
||||||
|
return group_time;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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 _.map(grouped, function(value, timestamp) {
|
||||||
|
return [Number(value), Number(timestamp)];
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.AVERAGE = function(values) {
|
||||||
|
var sum = 0;
|
||||||
|
_.each(values, function(value) {
|
||||||
|
sum += value;
|
||||||
|
});
|
||||||
|
return sum / values.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -6,6 +6,7 @@ define([
|
|||||||
'./directives',
|
'./directives',
|
||||||
'./zabbixAPI',
|
'./zabbixAPI',
|
||||||
'./helperFunctions',
|
'./helperFunctions',
|
||||||
|
'./dataProcessingService',
|
||||||
'./zabbixCache',
|
'./zabbixCache',
|
||||||
'./queryCtrl'
|
'./queryCtrl'
|
||||||
],
|
],
|
||||||
@@ -13,8 +14,8 @@ function (angular, _, dateMath) {
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv,
|
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv,
|
||||||
ZabbixAPI, zabbixHelperSrv, ZabbixCache, QueryProcessor) {
|
ZabbixAPI, ZabbixCache, QueryProcessor, DataProcessingService) {
|
||||||
|
|
||||||
// General data source settings
|
// General data source settings
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
@@ -119,12 +120,6 @@ function (angular, _, dateMath) {
|
|||||||
|
|
||||||
var items = self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter);
|
var items = self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter);
|
||||||
|
|
||||||
// Use alias only for single metric, otherwise use item names
|
|
||||||
var alias;
|
|
||||||
if (items.length === 1) {
|
|
||||||
alias = templateSrv.replace(target.alias, options.scopedVars);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add hostname for items from multiple hosts
|
// Add hostname for items from multiple hosts
|
||||||
var addHostName = target.host.isRegex;
|
var addHostName = target.host.isRegex;
|
||||||
|
|
||||||
@@ -153,6 +148,8 @@ function (angular, _, dateMath) {
|
|||||||
var downsampleFunc = target.downsampleFunction ? target.downsampleFunction.value : "avg";
|
var downsampleFunc = target.downsampleFunction ? target.downsampleFunction.value : "avg";
|
||||||
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval, downsampleFunc);
|
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval, downsampleFunc);
|
||||||
}
|
}
|
||||||
|
var groupBuInterval = 60000;
|
||||||
|
timeseries.datapoints = DataProcessingService.groupBy(timeseries.datapoints, groupBuInterval, DataProcessingService.AVERAGE);
|
||||||
return timeseries;
|
return timeseries;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user