From 50c0764d0154d2bff9ff1688fd4dcb4d4d9e407c Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 18 Feb 2016 23:47:31 +0300 Subject: [PATCH] Cache and queryProcessor refactor. --- plugins/datasource-zabbix/queryProcessor.js | 28 ++++++---------- plugins/datasource-zabbix/zabbixAPI.js | 24 ++++++++++++- plugins/datasource-zabbix/zabbixCache.js | 37 +++++++++++++++++++-- 3 files changed, 69 insertions(+), 20 deletions(-) diff --git a/plugins/datasource-zabbix/queryProcessor.js b/plugins/datasource-zabbix/queryProcessor.js index 97f128a..c5a78ec 100644 --- a/plugins/datasource-zabbix/queryProcessor.js +++ b/plugins/datasource-zabbix/queryProcessor.js @@ -49,7 +49,6 @@ function (angular, _, utils) { this.filterHosts = function(groupFilter) { var groups = []; - var hosts = []; return self.cache.getGroups().then(function(groupList) { // Filter groups by regex @@ -69,15 +68,15 @@ function (angular, _, utils) { } } - if (groups) { - var groupids = _.map(groups, 'groupid'); - return self.cache.getHosts().then(function(hosts) { - return _.filter(hosts, function (hostObj) { - return _.intersection(groupids, hostObj.groups).length; + var hostids = _.flatten(_.map(groups, 'hosts')); + if (hostids.length) { + return self.cache.getHostsExtend().then(function(hosts) { + return _.map(hostids, function(hostid) { + return hosts[hostid]; }); }); } else { - return hosts; + return []; } }); }; @@ -129,14 +128,12 @@ function (angular, _, utils) { var promises = [ this.filterHosts(groupFilter), - this.filterApplications(groupFilter, hostFilter), - this.cache.getItems(itemType), + this.filterApplications(groupFilter, hostFilter) ]; return $q.all(promises).then(function(results) { var hostList = results[0]; var applicationList = results[1]; - var cachedItems = results[2]; // Filter hosts by regex if (utils.isRegex(hostFilter)) { @@ -144,8 +141,7 @@ function (angular, _, utils) { hosts = _.filter(hostList, function (hostObj) { return hostFilterPattern.test(hostObj.name); }); - } - else { + } else { var findedHosts = _.find(hostList, {'name': hostFilter}); if (findedHosts) { hosts.push(findedHosts); @@ -172,20 +168,18 @@ function (angular, _, utils) { } else { apps = undefined; if (hosts) { - items = _.filter(cachedItems, function (itemObj) { - return _.find(hosts, {'hostid': itemObj.hostid }); - }); + items = _.flatten(_.map(hosts, 'items'), true); } } if (apps) { - var appids = _.flatten(_.map(apps, 'applicationids')); + /*var appids = _.flatten(_.map(apps, 'applicationids')); items = _.filter(cachedItems, function (itemObj) { return _.intersection(appids, itemObj.applications).length; }); items = _.filter(items, function (itemObj) { return _.find(hosts, {'hostid': itemObj.hostid }); - }); + });*/ } if (!showDisabledItems) { diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js index 5c1b61d..eac7589 100644 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ b/plugins/datasource-zabbix/zabbixAPI.js @@ -123,7 +123,8 @@ function (angular, _) { p.getGroups = function() { var params = { output: ['name'], - sortfield: 'name' + sortfield: 'name', + selectHosts: [] }; return this.request('hostgroup.get', params); @@ -168,6 +169,27 @@ function (angular, _) { return this.request('item.get', params); }; + /** + * Get Hosts list with host's items. + * @return {[type]} [description] + */ + p.getHostsExtend = function() { + var params = { + output: ['name', 'host'], + sortfield: 'name', + selectGroups: [], + selectItems: [ + 'name', 'key_', + 'value_type', + 'hostid', + 'status', + 'state' + ] + }; + + return this.request('host.get', params); + }; + p.getLastValue = function(itemid) { var params = { output: ['lastvalue'], diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index 8c8aafe..6427d07 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -21,6 +21,7 @@ function (angular, _, utils) { this._hosts = undefined; this._applications = undefined; this._items = undefined; + this._hostsExtend = undefined; this.storage = { history: {}, trends: {} @@ -51,15 +52,17 @@ function (angular, _, utils) { this.zabbixAPI.getGroups(), this.zabbixAPI.getHosts(), this.zabbixAPI.getApplications(), - this.zabbixAPI.getItems() + this.zabbixAPI.getItems(), + this.zabbixAPI.getHostsExtend() ]; return $q.all(promises).then(function(results) { if (results.length) { - self._groups = results[0]; + self._groups = convertGroups(results[0]); self._hosts = convertHosts(results[1]); self._applications = convertApplications(results[2]); self._items = convertItems(results[3]); + self._hostsExtend = convertHostsExtend(results[4]); } self._initialized = true; }); @@ -87,6 +90,17 @@ function (angular, _, utils) { } }; + p.getHostsExtend = function() { + var self = this; + if (this._hostsExtend) { + return $q.when(self._hostsExtend); + } else { + return this.refresh().then(function() { + return self._hostsExtend; + }); + } + }; + p.getApplications = function() { var self = this; if (this._applications) { @@ -181,6 +195,25 @@ function (angular, _, utils) { }); } + function convertGroups(groups) { + return _.forEach(groups, function(group) { + group.hosts = _.map(group.hosts, 'hostid'); + return group; + }); + } + + function convertHostsExtend(hosts) { + return _.indexBy(_.map(hosts, function(host) { + host.items = _.forEach(host.items, function(item) { + item.applications = _.map(item.applications, 'applicationid'); + item.item = item.name; + item.name = utils.expandItemName(item.item, item.key_); + return item; + }); + return host; + }), 'hostid'); + } + /** * Group Zabbix applications by name * host.hosts - array of host ids