Merge branch 'feature-aggregation' into develop

This commit is contained in:
Alexander Zobnin
2015-08-14 21:24:49 +03:00
4 changed files with 79 additions and 20 deletions

View File

@@ -171,15 +171,32 @@ function (angular, _, kbn) {
items = _.flatten(items);
// Use alias only for single metric, otherwise use item names
var alias = target.item.name === 'All' || itemnames.length > 1 ? undefined : templateSrv.replace(target.alias, options.scopedVars);
var alias = target.item.name === 'All' || itemnames.length > 1 ?
undefined : templateSrv.replace(target.alias, options.scopedVars);
var history;
if ((from < useTrendsFrom) && self.trends) {
return self.zabbixAPI.getTrends(items, from, to)
.then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale));
var points = target.downsampleFunction ? target.downsampleFunction.value : "avg";
history = self.zabbixAPI.getTrends(items, from, to)
.then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale, points));
} else {
return self.zabbixAPI.getHistory(items, from, to)
history = self.zabbixAPI.getHistory(items, from, to)
.then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale));
}
return history.then(function (timeseries) {
var timeseries_data = _.flatten(timeseries);
return _.map(timeseries_data, function (timeseries) {
// Series downsampling
if (timeseries.datapoints.length > options.maxDataPoints) {
var ms_interval = Math.floor((to - from) / options.maxDataPoints) * 1000;
var downsampleFunc = target.downsampleFunction ? target.downsampleFunction.value : "avg";
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval, downsampleFunc);
}
return timeseries;
});
});
}
});
} else {
@@ -195,16 +212,7 @@ function (angular, _, kbn) {
return $q.all(_.flatten(promises)).then(function (results) {
var timeseries_data = _.flatten(results);
var data = _.map(timeseries_data, function (timeseries) {
// Series downsampling
if (timeseries.datapoints.length > options.maxDataPoints) {
var ms_interval = Math.floor((to - from) / options.maxDataPoints) * 1000;
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval);
}
return timeseries;
});
return { data: data };
return { data: timeseries_data };
});
};

View File

@@ -72,6 +72,7 @@ function (angular, _) {
* @param {Array} items Array of Zabbix Items
* @param alias
* @param scale
* @param {string} points Point value to return: min, max or avg
* @param {Array} trends Array of Zabbix Trends
*
* @return {Array} Array of timeseries in Grafana format
@@ -80,7 +81,7 @@ function (angular, _) {
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.handleTrendResponse = function (items, alias, scale, trends) {
this.handleTrendResponse = function (items, alias, scale, points, trends) {
// Group items and trends by itemid
var indexed_items = _.indexBy(items, 'itemid');
@@ -95,7 +96,16 @@ function (angular, _) {
datapoints: _.map(trends, function (p) {
// Value must be a number for properly work
var value = Number(p.value_avg);
var value;
if (points === "min") {
value = Number(p.value_min);
}
else if (points === "max") {
value = Number(p.value_max);
}
else {
value = Number(p.value_avg);
}
// Apply scale
if (scale) {
@@ -216,12 +226,13 @@ function (angular, _) {
/**
* Downsample datapoints series
*
* @param {array} datapoints [[<value>, <unixtime>], ...]
* @param {Object[]} datapoints [[<value>, <unixtime>], ...]
* @param {integer} time_to Panel time to
* @param {integer} ms_interval Interval in milliseconds for grouping datapoints
* @return {array} [[<value>, <unixtime>], ...]
* @param {string} func Value to return: min, max or avg
* @return {Object[]} [[<value>, <unixtime>], ...]
*/
this.downsampleSeries = function(datapoints, time_to, ms_interval) {
this.downsampleSeries = function(datapoints, time_to, ms_interval, func) {
var downsampledSeries = [];
var timeWindow = {
from: time_to * 1000 - ms_interval,
@@ -231,14 +242,28 @@ function (angular, _) {
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;
@@ -246,6 +271,7 @@ function (angular, _) {
points_sum = 0;
points_num = 0;
frame = [];
// Process point again
i++;

View File

@@ -131,6 +131,21 @@
placeholder="Host filter (regex)"
ng-blur="targetBlur()">
</li>
<!-- Downsampling function -->
<li class="tight-form-item input-medium">Downsampling</li>
<li>
<select class="tight-form-input input-small"
ng-change="targetBlur()"
ng-model="target.downsampleFunction"
bs-tooltip="'Downsampling function'"
ng-options="func.name for func in downsampleFunctionList track by func.name">
</select>
<a bs-tooltip="target.errors.metric"
style="color: rgb(229, 189, 28)"
ng-show="target.errors.metric">
<i class="icon-warning-sign"></i>
</a>
</li>
</ul>
<div class="clearfix"></div>

View File

@@ -24,6 +24,16 @@ define([
$scope.itserviceList = [{name: "test"}];
$scope.updateITServiceList();
} else {
$scope.downsampleFunctionList = [
{name: "avg", value: "avg"},
{name: "min", value: "min"},
{name: "max", value: "max"}
];
// Set avg by default
if (!$scope.target.downsampleFunction) {
$scope.target.downsampleFunction = $scope.downsampleFunctionList[0];
}
$scope.metric = {
hostGroupList: [],
hostList: [{name: '*', visible_name: 'All'}],