Fixed history request for multiple value type items.

This commit is contained in:
Alexander Zobnin
2015-05-09 16:05:30 +03:00
parent f3edf17431
commit 8ee4dd6a5c

View File

@@ -39,6 +39,7 @@ function (angular, _, kbn) {
// Need for find target alias // Need for find target alias
var targets = options.targets; var targets = options.targets;
// TODO: remove undefined targets from request
// Check that all targets defined // Check that all targets defined
var targetsDefined = options.targets.every(function (target, index, array) { var targetsDefined = options.targets.every(function (target, index, array) {
return target.item; return target.item;
@@ -72,9 +73,7 @@ function (angular, _, kbn) {
*/ */
// Index returned datapoints by item/metric id // Index returned datapoints by item/metric id
var indexed_result = _.groupBy(response, function (history_item) { var indexed_result = _.groupBy(response, 'itemid');
return history_item.itemid;
});
// Reduce timeseries to the same size for stacking and tooltip work properly // Reduce timeseries to the same size for stacking and tooltip work properly
var min_length = _.min(_.map(indexed_result, function (history) { var min_length = _.min(_.map(indexed_result, function (history) {
@@ -124,6 +123,9 @@ function (angular, _, kbn) {
ZabbixAPIDatasource.prototype.doZabbixAPIRequest = function(request_data) { ZabbixAPIDatasource.prototype.doZabbixAPIRequest = function(request_data) {
var options = { var options = {
method: 'POST', method: 'POST',
headers: {
'Content-Type': 'application/json'
},
url: this.url, url: this.url,
data: request_data data: request_data
}; };
@@ -158,14 +160,17 @@ function (angular, _, kbn) {
* @param items: array of zabbix api item objects * @param items: array of zabbix api item objects
*/ */
ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) { ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) {
var item_ids = items.map(function (item, index, array) {
return item.itemid;
});
// TODO: if different value types passed? // Group items by value type for separate requests
// Perform multiple api request. var items_by_value_type = _.groupBy(items, 'value_type');
var history_type = items[0].value_type;
var self = this;
var apiRequests = [];
// Prepare requests for each value type
_.each(items_by_value_type, function (value, key, list) {
var item_ids = _.map(value, 'itemid');
var history_type = key;
var data = { var data = {
jsonrpc: '2.0', jsonrpc: '2.0',
method: 'history.get', method: 'history.get',
@@ -175,10 +180,10 @@ function (angular, _, kbn) {
itemids: item_ids, itemids: item_ids,
sortfield: 'clock', sortfield: 'clock',
sortorder: 'ASC', sortorder: 'ASC',
limit: this.limitmetrics, limit: self.limitmetrics,
time_from: start, time_from: start,
}, },
auth: this.auth, auth: self.auth,
id: 1 id: 1
}; };
@@ -187,7 +192,36 @@ function (angular, _, kbn) {
data.params.time_till = end; data.params.time_till = end;
} }
return this.doZabbixAPIRequest(data); apiRequests.push(self.doZabbixAPIRequest(data));
});
return this.handleMultipleRequest(apiRequests);
};
// 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;
}; };