Reduce number of api requests in query() method.

This commit is contained in:
Alexander Zobnin
2015-05-31 14:29:48 +03:00
parent c71ad03cf5
commit ed0138560c
2 changed files with 28 additions and 22 deletions

View File

@@ -71,13 +71,12 @@ function (angular, _, kbn) {
if (itemnames && (itemnames.length < this.limitmetrics)) { if (itemnames && (itemnames.length < this.limitmetrics)) {
// Find items by item names and perform queries // Find items by item names and perform queries
var self = this; var self = this;
return $q.all(_.map(hosts, function (hostname) { // TODO: do one query.
if (hosts.length > 1) { if (hosts.length > 1) {
// Select the host that the item belongs for multiple hosts request // Select the host that the item belongs for multiple hosts request
var selectHosts = true; var selectHosts = true;
} }
return this.findZabbixItem(hostname, itemnames, selectHosts); return this.findZabbixItem(hosts, itemnames, selectHosts).then(function (items) {
}, this)).then(function (items) {
items = _.flatten(items); items = _.flatten(items);
return self.performTimeSeriesQuery(items, from, to) return self.performTimeSeriesQuery(items, from, to)
.then(_.partial(self.handleHistoryResponse, items)); .then(_.partial(self.handleHistoryResponse, items));
@@ -150,6 +149,7 @@ function (angular, _, kbn) {
return $q.when(_.map(grouped_history, function (history, itemid) { return $q.when(_.map(grouped_history, function (history, itemid) {
var item = indexed_items[itemid]; var item = indexed_items[itemid];
var series = { var series = {
// TODO: remove []
target: (item.hosts ? '['+item.hosts[0].name+']: ' : '') + expandItemName(item), target: (item.hosts ? '['+item.hosts[0].name+']: ' : '') + expandItemName(item),
datapoints: _.map(history, function (p) { datapoints: _.map(history, function (p) {
// Value must be a number for properly work // Value must be a number for properly work
@@ -323,12 +323,12 @@ function (angular, _, kbn) {
}; };
ZabbixAPIDatasource.prototype.findZabbixHost = function (hostname) { ZabbixAPIDatasource.prototype.findZabbixHost = function (hostnames) {
var params = { var params = {
output: ['host', 'name'], output: ['host', 'name'],
search: { search: {
host: hostname, host: hostnames,
name: hostname name: hostnames
}, },
searchByAny: true, searchByAny: true,
searchWildcardsEnabled: true searchWildcardsEnabled: true
@@ -350,17 +350,21 @@ function (angular, _, kbn) {
}; };
ZabbixAPIDatasource.prototype.findZabbixItem = function (host, itemnames, /* optional */ selectHosts) { ZabbixAPIDatasource.prototype.findZabbixItem = function (hosts, itemnames, /* optional */ selectHosts) {
var params = { var self = this;
output: ['name', 'key_', 'value_type'], return this.findZabbixHost(hosts).then(function (hosts) {
host: host var hostids = _.map(hosts, 'hostid');
}; var params = {
if (selectHosts) { output: ['name', 'key_', 'value_type'],
params.selectHosts = ['name']; hostids: hostids
} };
return this.performZabbixAPIRequest('item.get', params).then(function (items) { if (selectHosts) {
return _.filter(items, function (item) { params.selectHosts = ['name'];
return _.contains(itemnames, expandItemName(item)); }
return self.performZabbixAPIRequest('item.get', params).then(function (items) {
return _.filter(items, function (item) {
return _.contains(itemnames, expandItemName(item));
});
}); });
}); });
}; };
@@ -451,6 +455,7 @@ function (angular, _, kbn) {
return _.map(result, function (item) { return _.map(result, function (item) {
var itemname = expandItemName(item) var itemname = expandItemName(item)
return { return {
// TODO: select only unique names
text: (item.hosts ? '['+item.hosts[0].name+']: ' : '') + itemname, text: (item.hosts ? '['+item.hosts[0].name+']: ' : '') + itemname,
expandable: false expandable: false
}; };

View File

@@ -21,7 +21,8 @@ function (angular, _) {
// Update host group, host, application and item lists // Update host group, host, application and item lists
$scope.updateGroupList(); $scope.updateGroupList();
$scope.updateHostList() $scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList(); $scope.updateItemList();
setItemAlias(); setItemAlias();