From f696760c321b95cc1e5e2e8b42a290ec2557ab5a Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 21:21:14 +0300 Subject: [PATCH] Refactor queryCtrl - filter metrics using queryProcessor and support templating. --- plugins/datasource-zabbix/queryCtrl.js | 142 +++----------------- plugins/datasource-zabbix/queryProcessor.js | 2 +- 2 files changed, 20 insertions(+), 124 deletions(-) diff --git a/plugins/datasource-zabbix/queryCtrl.js b/plugins/datasource-zabbix/queryCtrl.js index b263f43..d5dc69f 100644 --- a/plugins/datasource-zabbix/queryCtrl.js +++ b/plugins/datasource-zabbix/queryCtrl.js @@ -70,9 +70,9 @@ define([ }; $scope.initFilters = function () { - $scope.metric.filteredHosts = $scope.filterHosts(); - $scope.metric.filteredApplications = $scope.filterApplications(); - $scope.metric.filteredItems = $scope.filterItems(); + $scope.filterHosts(); + $scope.filterApplications(); + $scope.filterItems(); }; $scope.getMetricsFromCache = function() { @@ -105,130 +105,26 @@ define([ $scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems'); $scope.filterHosts = function () { - var group = $scope.target.group; - var groups = []; - var hosts = []; - - // Filter groups by regex - if (group.isRegex) { - var filterPattern = Utils.buildRegex(group.filter); - groups = _.filter($scope.metric.groupList, function (groupObj) { - return filterPattern.test(groupObj.name); - }); - } - // Find hosts in selected group - else { - var finded = _.find($scope.metric.groupList, {'name': group.filter}); - if (finded) { - groups.push(finded); - } else { - groups = undefined; - } - } - - if (groups) { - var groupids = _.map(groups, 'groupid'); - hosts = _.filter($scope.metric.hostList, function (hostObj) { - return _.intersection(groupids, hostObj.groups).length; - }); - } - return hosts; + var groupFilter = templateSrv.replace($scope.target.group.filter); + $scope.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) { + $scope.metric.filteredHosts = hosts; + }); }; $scope.filterApplications = function () { - var host = $scope.target.host; - var hosts = []; - var apps = []; - - // Filter hosts by regex - if (host.isRegex) { - var filterPattern = Utils.buildRegex(host.filter); - hosts = _.filter($scope.metric.hostList, function (hostObj) { - return filterPattern.test(hostObj.name); - }); - } - // Find applications in selected host - else { - var finded = _.find($scope.metric.hostList, {'name': host.filter}); - if (finded) { - hosts.push(finded); - } else { - hosts = undefined; - } - } - - if (hosts) { - var hostsids = _.map(hosts, 'hostid'); - apps = _.filter($scope.metric.applicationList, function (appObj) { - return _.intersection(hostsids, appObj.hosts).length; - }); - } - - return apps; + var hostFilter = templateSrv.replace($scope.target.host.filter); + $scope.datasource.queryProcessor.filterApplications(hostFilter).then(function(apps) { + $scope.metric.filteredApplications = apps; + }); }; $scope.filterItems = function () { - var app = $scope.target.application; - var host = $scope.target.host; - var hosts = []; - var apps = []; - var items = []; - - // Filter hosts by regex - if (host.isRegex) { - var hostFilterPattern = Utils.buildRegex(host.filter); - hosts = _.filter($scope.metric.hostList, function (hostObj) { - return hostFilterPattern.test(hostObj.name); + var hostFilter = templateSrv.replace($scope.target.host.filter); + var appFilter = templateSrv.replace($scope.target.application.filter); + $scope.datasource.queryProcessor.filterItems(hostFilter, appFilter, $scope.target.showDisabledItems) + .then(function(items) { + $scope.metric.filteredItems = items; }); - } - else { - var findedHosts = _.find($scope.metric.hostList, {'name': host.filter}); - if (findedHosts) { - hosts.push(findedHosts); - } else { - hosts = undefined; - } - } - - // Filter applications by regex - if (app.isRegex) { - var filterPattern = Utils.buildRegex(app.filter); - apps = _.filter($scope.metric.applicationList, function (appObj) { - return filterPattern.test(appObj.name); - }); - } - // Find items in selected application - else if (app.filter) { - var finded = _.find($scope.metric.applicationList, {'name': app.filter}); - if (finded) { - apps.push(finded); - } else { - apps = undefined; - } - } else { - apps = undefined; - if (hosts) { - items = _.filter($scope.metric.itemList, function (itemObj) { - return _.find(hosts, {'hostid': itemObj.hostid }); - }); - } - } - - if (apps) { - var appids = _.flatten(_.map(apps, 'applicationids')); - items = _.filter($scope.metric.itemList, function (itemObj) { - return _.intersection(appids, itemObj.applications).length; - }); - items = _.filter(items, function (itemObj) { - return _.find(hosts, {'hostid': itemObj.hostid }); - }); - } - - if (!$scope.target.showDisabledItems) { - items = _.filter(items, {'status': '0'}); - } - - return items; }; $scope.onTargetPartChange = function (targetPart) { @@ -239,21 +135,21 @@ define([ // Handle group blur and filter hosts $scope.onGroupBlur = function() { - $scope.metric.filteredHosts = $scope.filterHosts(); + $scope.filterHosts(); $scope.parseTarget(); $scope.get_data(); }; // Handle host blur and filter applications $scope.onHostBlur = function() { - $scope.metric.filteredApplications = $scope.filterApplications(); + $scope.filterApplications(); $scope.parseTarget(); $scope.get_data(); }; // Handle application blur and filter items $scope.onApplicationBlur = function() { - $scope.metric.filteredItems = $scope.filterItems(); + $scope.filterItems(); $scope.parseTarget(); $scope.get_data(); }; diff --git a/plugins/datasource-zabbix/queryProcessor.js b/plugins/datasource-zabbix/queryProcessor.js index 1af2d87..9947a17 100644 --- a/plugins/datasource-zabbix/queryProcessor.js +++ b/plugins/datasource-zabbix/queryProcessor.js @@ -153,7 +153,7 @@ function (angular, _, utils) { } else { apps = undefined; if (hosts) { - items = _.filter(this.cache.getItems(), function (itemObj) { + items = _.filter(cachedItems, function (itemObj) { return _.find(hosts, {'hostid': itemObj.hostid }); }); }