diff --git a/zabbix/datasource.js b/zabbix/datasource.js index ec0798a..31b0252 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -173,13 +173,31 @@ function (angular, _, kbn) { // Use alias only for single metric, otherwise use item names var alias = target.item.name === 'All' || itemnames.length > 1 ? undefined : templateSrv.replace(target.alias); + var history; + var handleFunction; if ((from < useTrendsFrom) && self.trends) { - return self.zabbixAPI.getTrends(items, from, to) - .then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, target.scale)); + history = self.zabbixAPI.getTrends(items, from, to); + handleFunction = zabbixHelperSrv.handleTrendResponse; } else { - return self.zabbixAPI.getHistory(items, from, to) - .then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale)); + history = self.zabbixAPI.getHistory(items, from, to); + handleFunction = zabbixHelperSrv.handleHistoryResponse; } + + return history + .then(_.bind(handleFunction, zabbixHelperSrv, items, alias, target.scale)) + .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 +213,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 }; }); }; diff --git a/zabbix/helperFunctions.js b/zabbix/helperFunctions.js index 4f076b3..a1d064a 100644 --- a/zabbix/helperFunctions.js +++ b/zabbix/helperFunctions.js @@ -216,12 +216,12 @@ function (angular, _) { /** * Downsample datapoints series * - * @param {array} datapoints [[, ], ...] + * @param {Object[]} datapoints [[, ], ...] * @param {integer} time_to Panel time to * @param {integer} ms_interval Interval in milliseconds for grouping datapoints - * @return {array} [[, ], ...] + * @return {Object[]} [[, ], ...] */ - 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 +231,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; - downsampledSeries.push([value_avg, timeWindow.to]); + + 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 +260,7 @@ function (angular, _) { points_sum = 0; points_num = 0; + frame = []; // Process point again i++; diff --git a/zabbix/partials/query.editor.html b/zabbix/partials/query.editor.html index 1ac3ccc..d5a7170 100644 --- a/zabbix/partials/query.editor.html +++ b/zabbix/partials/query.editor.html @@ -131,6 +131,21 @@ placeholder="Host filter (regex)" ng-blur="targetBlur()"> + +
  • Downsampling
  • +
  • + + + + +
  • diff --git a/zabbix/queryCtrl.js b/zabbix/queryCtrl.js index d565d1a..ad2a2bb 100644 --- a/zabbix/queryCtrl.js +++ b/zabbix/queryCtrl.js @@ -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'}],