From 1de4b4bc5c4222a5171087fac43310a7d50c7b81 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 16:43:59 +0300 Subject: [PATCH] Refactor buildFromCache() method. --- plugins/datasource-zabbix/queryProcessor.js | 237 +++++++++++--------- plugins/datasource-zabbix/zabbixCache.js | 8 +- 2 files changed, 134 insertions(+), 111 deletions(-) diff --git a/plugins/datasource-zabbix/queryProcessor.js b/plugins/datasource-zabbix/queryProcessor.js index aa6cf7b..81a6492 100644 --- a/plugins/datasource-zabbix/queryProcessor.js +++ b/plugins/datasource-zabbix/queryProcessor.js @@ -31,32 +31,36 @@ function (angular, _, utils) { this.filterHosts = function(groupFilter) { var groups = []; var hosts = []; - var groupList = self.cache.getGroups(); - // Filter groups by regex - if (utils.isRegex(groupFilter)) { - var filterPattern = utils.buildRegex(groupFilter); - groups = _.filter(groupList, function (groupObj) { - return filterPattern.test(groupObj.name); - }); - } - // Find hosts in selected group - else { - var finded = _.find(groupList, {'name': groupFilter}); - if (finded) { - groups.push(finded); - } else { - groups = undefined; + return self.cache.getGroups().then(function(groupList) { + // Filter groups by regex + if (utils.isRegex(groupFilter)) { + var filterPattern = utils.buildRegex(groupFilter); + groups = _.filter(groupList, function (groupObj) { + return filterPattern.test(groupObj.name); + }); + } + // Find hosts in selected group + else { + var finded = _.find(groupList, {'name': groupFilter}); + if (finded) { + groups.push(finded); + } else { + groups = undefined; + } } - } - if (groups) { - var groupids = _.map(groups, 'groupid'); - hosts = _.filter(self.cache.getHosts(), function (hostObj) { - return _.intersection(groupids, hostObj.groups).length; - }); - } - return hosts; + 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; + }); + }); + } else { + return hosts; + } + }); }; this.filterApplications = function(hostFilter) { @@ -165,107 +169,120 @@ function (angular, _, utils) { var hosts = []; var apps = []; var items = []; + var promises = [ + this.cache.getGroups(), + this.cache.getHosts(), + this.cache.getApplications(), + this.cache.getItems() + ]; - if (utils.isRegex(hostFilter)) { + return $q.all(promises).then(function(results) { + var cachedGroups = results[0]; + var cachedHosts = results[1]; + var cachedApps = results[2]; + var cachedItems = results[3]; - // Filter groups - if (utils.isRegex(groupFilter)) { - var groupPattern = utils.buildRegex(groupFilter); - groups = _.filter(this.cache.getGroups(), function (groupObj) { - return groupPattern.test(groupObj.name); - }); - } else { - var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter}); - if (findedGroup) { - groups.push(findedGroup); + if (utils.isRegex(hostFilter)) { + + // Filter groups + if (utils.isRegex(groupFilter)) { + var groupPattern = utils.buildRegex(groupFilter); + groups = _.filter(cachedGroups, function (groupObj) { + return groupPattern.test(groupObj.name); + }); } else { - groups = undefined; + var findedGroup = _.find(cachedGroups, {'name': groupFilter}); + if (findedGroup) { + groups.push(findedGroup); + } else { + groups = undefined; + } } - } - if (groups) { - var groupids = _.map(groups, 'groupid'); - hosts = _.filter(this.cache.getHosts(), function (hostObj) { - return _.intersection(groupids, hostObj.groups).length; - }); - } else { - // No groups finded - return []; - } - - // Filter hosts - var hostPattern = utils.buildRegex(hostFilter); - hosts = _.filter(hosts, function (hostObj) { - return hostPattern.test(hostObj.name); - }); - } else { - var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter}); - if (findedHost) { - hosts.push(findedHost); - } else { - // No hosts finded - return []; - } - } - - // Find items belongs to selected hosts - items = _.filter(this.cache.getItems(), function (itemObj) { - return _.contains(_.map(hosts, 'hostid'), itemObj.hostid); - }); - - if (utils.isRegex(itemFilter)) { - - // Filter applications - if (utils.isRegex(appFilter)) { - var appPattern = utils.buildRegex(appFilter); - apps = _.filter(this.cache.getApplications(), function (appObj) { - return appPattern.test(appObj.name); - }); - } - // Don't use application filter if it empty - else if (appFilter === "") { - apps = undefined; - } - else { - var findedApp = _.find(this.cache.getApplications(), {'name': appFilter}); - if (findedApp) { - apps.push(findedApp); + if (groups) { + var groupids = _.map(groups, 'groupid'); + hosts = _.filter(cachedHosts, function (hostObj) { + return _.intersection(groupids, hostObj.groups).length; + }); } else { - // No applications finded + // No groups finded + return []; + } + + // Filter hosts + var hostPattern = utils.buildRegex(hostFilter); + hosts = _.filter(hosts, function (hostObj) { + return hostPattern.test(hostObj.name); + }); + } else { + var findedHost = _.find(cachedHosts, {'name': hostFilter}); + if (findedHost) { + hosts.push(findedHost); + } else { + // No hosts finded return []; } } - // Find items belongs to selected applications - if (apps) { - var appids = _.flatten(_.map(apps, 'applicationids')); - items = _.filter(items, function (itemObj) { - return _.intersection(appids, itemObj.applications).length; - }); - } + // Find items belongs to selected hosts + items = _.filter(cachedItems, function (itemObj) { + return _.contains(_.map(hosts, 'hostid'), itemObj.hostid); + }); - if (items) { - var itemPattern = utils.buildRegex(itemFilter); - items = _.filter(items, function (itemObj) { - return itemPattern.test(itemObj.name); - }); + if (utils.isRegex(itemFilter)) { + + // Filter applications + if (utils.isRegex(appFilter)) { + var appPattern = utils.buildRegex(appFilter); + apps = _.filter(cachedApps, function (appObj) { + return appPattern.test(appObj.name); + }); + } + // Don't use application filter if it empty + else if (appFilter === "") { + apps = undefined; + } + else { + var findedApp = _.find(cachedApps, {'name': appFilter}); + if (findedApp) { + apps.push(findedApp); + } else { + // No applications finded + return []; + } + } + + // Find items belongs to selected applications + if (apps) { + var appids = _.flatten(_.map(apps, 'applicationids')); + items = _.filter(items, function (itemObj) { + return _.intersection(appids, itemObj.applications).length; + }); + } + + if (items) { + var itemPattern = utils.buildRegex(itemFilter); + items = _.filter(items, function (itemObj) { + return itemPattern.test(itemObj.name); + }); + } else { + // No items finded + return []; + } } else { - // No items finded - return []; + items = _.filter(items, {'name': itemFilter}); + if (!items.length) { + // No items finded + return []; + } } - } else { - items = _.filter(items, {'name': itemFilter}); - if (!items.length) { - // No items finded - return []; - } - } - // Set host as host name for each item - items = _.each(items, function (itemObj) { - itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name; + // Set host as host name for each item + items = _.each(items, function (itemObj) { + itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name; + }); + + return items; }); - - return items; }; /** diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index 24baac9..60715c0 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -54,7 +54,13 @@ function (angular, _, utils) { }; p.getGroups = function() { - return this._groups; + var self = this; + if (this._groups) { + return this.refresh().then(function() { + return self._groups; + }); + } + return $q.when(this._groups); }; p.getHosts = function() {