From bf07637fc144777ba94d547bb2a640f01b389cd8 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 14 Aug 2015 21:22:19 +0300 Subject: [PATCH] Resolved #69 - Able to select min, max or avg value for downsampling. --- zabbix/datasource.js | 17 ++++++++--------- zabbix/helperFunctions.js | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 3c7d03e..cd5c0a1 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -171,21 +171,20 @@ 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; - var handleFunction; if ((from < useTrendsFrom) && self.trends) { - history = self.zabbixAPI.getTrends(items, from, to); - handleFunction = zabbixHelperSrv.handleTrendResponse; + 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 { - history = self.zabbixAPI.getHistory(items, from, to); - handleFunction = zabbixHelperSrv.handleHistoryResponse; + history = self.zabbixAPI.getHistory(items, from, to) + .then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, target.scale)); } - return history - .then(_.bind(handleFunction, zabbixHelperSrv, items, alias, target.scale)) - .then(function (timeseries) { + return history.then(function (timeseries) { var timeseries_data = _.flatten(timeseries); return _.map(timeseries_data, function (timeseries) { diff --git a/zabbix/helperFunctions.js b/zabbix/helperFunctions.js index a1d064a..f59a470 100644 --- a/zabbix/helperFunctions.js +++ b/zabbix/helperFunctions.js @@ -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: [[, ], ...] * } */ - 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) { @@ -219,6 +229,7 @@ function (angular, _) { * @param {Object[]} datapoints [[, ], ...] * @param {integer} time_to Panel time to * @param {integer} ms_interval Interval in milliseconds for grouping datapoints + * @param {string} func Value to return: min, max or avg * @return {Object[]} [[, ], ...] */ this.downsampleSeries = function(datapoints, time_to, ms_interval, func) {