Refactoring: separate api request for each target. handleMultipleRequest() no more needed.
This commit is contained in:
@@ -33,22 +33,20 @@ function (angular, _, kbn) {
|
|||||||
|
|
||||||
ZabbixAPIDatasource.prototype.query = function(options) {
|
ZabbixAPIDatasource.prototype.query = function(options) {
|
||||||
// get from & to in seconds
|
// get from & to in seconds
|
||||||
var from = kbn.parseDate(options.range.from).getTime();
|
var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000);
|
||||||
var to = kbn.parseDate(options.range.to).getTime();
|
var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000);
|
||||||
|
|
||||||
from = Math.ceil(from/1000);
|
|
||||||
to = Math.ceil(to/1000);
|
|
||||||
|
|
||||||
|
// Create request for each target
|
||||||
var promises = _.map(options.targets, function(target) {
|
var promises = _.map(options.targets, function(target) {
|
||||||
|
|
||||||
// Remove undefined and hidden targets
|
// Remove undefined and hidden targets
|
||||||
if (target.hide || !target.item) {
|
if (target.hide || !target.item) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
// Perform request and then handle result
|
||||||
return this.performTimeSeriesQuery(target.item, from, to).then(function (response) {
|
return this.performTimeSeriesQuery(target.item, from, to).then(_.partial(
|
||||||
return self.handleZabbixAPIResponse(response, target)
|
this.handleZabbixAPIResponse, target));
|
||||||
});
|
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
return $q.all(promises).then(function(results) {
|
return $q.all(promises).then(function(results) {
|
||||||
@@ -58,7 +56,7 @@ function (angular, _, kbn) {
|
|||||||
|
|
||||||
|
|
||||||
// Request data from Zabbix API
|
// Request data from Zabbix API
|
||||||
ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(response, target) {
|
ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(target, response) {
|
||||||
/**
|
/**
|
||||||
* Response should be in the format:
|
* Response should be in the format:
|
||||||
* data: [
|
* data: [
|
||||||
@@ -74,16 +72,10 @@ function (angular, _, kbn) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var series = {
|
var series = {
|
||||||
// Lookup itemid:alias map
|
|
||||||
target: target.alias,
|
target: target.alias,
|
||||||
|
|
||||||
datapoints: _.map(response, function (p) {
|
datapoints: _.map(response, function (p) {
|
||||||
|
|
||||||
// Value must be a number for properly work
|
// Value must be a number for properly work
|
||||||
var value = Number(p.value);
|
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];
|
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
|
// Get authentication token
|
||||||
ZabbixAPIDatasource.prototype.performZabbixAPILogin = function() {
|
ZabbixAPIDatasource.prototype.performZabbixAPILogin = function() {
|
||||||
var options = {
|
var options = {
|
||||||
|
|||||||
Reference in New Issue
Block a user