From a62d0e60fc3abe5eef4c5c8448499ea6ab13c740 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 18 May 2015 11:45:10 +0300 Subject: [PATCH] Refactoring: separate api request for each target. handleMultipleRequest() no more needed. --- zabbix/datasource.js | 50 +++++++------------------------------------- 1 file changed, 8 insertions(+), 42 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index cdadd26..599718f 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -33,22 +33,20 @@ function (angular, _, kbn) { ZabbixAPIDatasource.prototype.query = function(options) { // get from & to in seconds - var from = kbn.parseDate(options.range.from).getTime(); - var to = kbn.parseDate(options.range.to).getTime(); - - from = Math.ceil(from/1000); - to = Math.ceil(to/1000); + var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000); + var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000); + // Create request for each target var promises = _.map(options.targets, function(target) { + // Remove undefined and hidden targets if (target.hide || !target.item) { return []; } - var self = this; - return this.performTimeSeriesQuery(target.item, from, to).then(function (response) { - return self.handleZabbixAPIResponse(response, target) - }); + // Perform request and then handle result + return this.performTimeSeriesQuery(target.item, from, to).then(_.partial( + this.handleZabbixAPIResponse, target)); }, this); return $q.all(promises).then(function(results) { @@ -58,7 +56,7 @@ function (angular, _, kbn) { // Request data from Zabbix API - ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(response, target) { + ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(target, response) { /** * Response should be in the format: * data: [ @@ -74,16 +72,10 @@ function (angular, _, kbn) { */ var series = { - // Lookup itemid:alias map target: target.alias, - datapoints: _.map(response, function (p) { - // Value must be a number for properly work var value = Number(p.value); - - // TODO: Correct time for proper stacking - //var clock = Math.round(Number(p.clock) / 60) * 60; return [value, p.clock * 1000]; }) }; @@ -158,32 +150,6 @@ function (angular, _, kbn) { }; - // Handle multiple request - ZabbixAPIDatasource.prototype.handleMultipleRequest = function(apiRequests) { - var history = []; - var performedQuery = null; - - // Build chain of api requests and put all history data into single array - _.each(apiRequests, function (apiRequest) { - if(!performedQuery) { - performedQuery = apiRequest.then(function (response) { - history = history.concat(response); - return history; - }); - } else { - performedQuery = performedQuery.then(function () { - return apiRequest.then(function (response) { - history = history.concat(response); - return history; - }); - }); - } - }); - - return performedQuery; - }; - - // Get authentication token ZabbixAPIDatasource.prototype.performZabbixAPILogin = function() { var options = {