diff --git a/plugins/datasource-zabbix/queryCtrl.js b/plugins/datasource-zabbix/queryCtrl.js index 6c95110..b263f43 100644 --- a/plugins/datasource-zabbix/queryCtrl.js +++ b/plugins/datasource-zabbix/queryCtrl.js @@ -10,13 +10,16 @@ define([ var module = angular.module('grafana.controllers'); var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; - module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) { + module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, $q, templateSrv) { var zabbixCache = $scope.datasource.zabbixCache; $scope.init = function () { $scope.targetLetters = targetLetters; - $scope.metric = {}; + + if (!$scope.metric) { + $scope.metric = {}; + } // Load default values var targetDefaults = { @@ -49,17 +52,9 @@ define([ } // Load metrics from cache - if (zabbixCache._initialized) { - $scope.getMetricsFromCache(); + $scope.getMetricsFromCache().then(function() { $scope.initFilters(); - //console.log("Cached", $scope.metric); - } else { - zabbixCache.refresh().then(function () { - $scope.getMetricsFromCache(); - $scope.initFilters(); - //console.log("From server", $scope.metric); - }); - } + }); } else if ($scope.target.mode === 1) { $scope.slaPropertyList = [ @@ -80,14 +75,22 @@ define([ $scope.metric.filteredItems = $scope.filterItems(); }; - $scope.getMetricsFromCache = function () { + $scope.getMetricsFromCache = function() { var item_type = $scope.editorModes[$scope.target.mode]; - $scope.metric = { - groupList: zabbixCache.getGroups(), - hostList: zabbixCache.getHosts(), - applicationList: zabbixCache.getApplications(), - itemList: zabbixCache.getItems(item_type) - }; + var promises = [ + zabbixCache.getGroups(), + zabbixCache.getHosts(), + zabbixCache.getApplications(), + zabbixCache.getItems(item_type) + ]; + return $q.all(promises).then(function(results) { + $scope.metric = { + groupList: results[0], + hostList: results[1], + applicationList: results[2], + itemList: results[3] + }; + }); }; // Get list of metric names for bs-typeahead directive diff --git a/plugins/datasource-zabbix/queryProcessor.js b/plugins/datasource-zabbix/queryProcessor.js index 81a6492..1af2d87 100644 --- a/plugins/datasource-zabbix/queryProcessor.js +++ b/plugins/datasource-zabbix/queryProcessor.js @@ -66,97 +66,115 @@ function (angular, _, utils) { this.filterApplications = function(hostFilter) { var hosts = []; var apps = []; - var hostList = this.cache.getHosts(); - // Filter hosts by regex - if (utils.isRegex(hostFilter)) { - var filterPattern = utils.buildRegex(hostFilter); - hosts = _.filter(hostList, function (hostObj) { - return filterPattern.test(hostObj.name); - }); - } - // Find applications in selected host - else { - var finded = _.find(hostList, {'name': hostFilter}); - if (finded) { - hosts.push(finded); - } else { - hosts = undefined; + var promises = [ + this.cache.getHosts(), + this.cache.getApplications() + ]; + + return $q.all(promises).then(function(results) { + var hostList = results[0]; + var applicationList = results[1]; + + // Filter hosts by regex + if (utils.isRegex(hostFilter)) { + var filterPattern = utils.buildRegex(hostFilter); + hosts = _.filter(hostList, function (hostObj) { + return filterPattern.test(hostObj.name); + }); + } + // Find applications in selected host + else { + var finded = _.find(hostList, {'name': hostFilter}); + if (finded) { + hosts.push(finded); + } else { + hosts = undefined; + } } - } - if (hosts) { - var hostsids = _.map(hosts, 'hostid'); - apps = _.filter(this.cache.getApplications(), function (appObj) { - return _.intersection(hostsids, appObj.hosts).length; - }); - } - - return apps; + if (hosts) { + var hostsids = _.map(hosts, 'hostid'); + apps = _.filter(applicationList, function (appObj) { + return _.intersection(hostsids, appObj.hosts).length; + }); + } + return apps; + }); }; this.filterItems = function (hostFilter, appFilter, showDisabledItems) { var hosts = []; var apps = []; var items = []; - var hostList = this.cache.getHosts(); - var applicationList = this.cache.getApplications(); - // Filter hosts by regex - if (utils.isRegex(hostFilter)) { - var hostFilterPattern = utils.buildRegex(hostFilter); - hosts = _.filter(hostList, function (hostObj) { - return hostFilterPattern.test(hostObj.name); - }); - } - else { - var findedHosts = _.find(hostList, {'name': hostFilter}); - if (findedHosts) { - hosts.push(findedHosts); - } else { - hosts = undefined; + var promises = [ + this.cache.getHosts(), + this.cache.getApplications(), + this.cache.getItems() + ]; + + 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)) { + var hostFilterPattern = utils.buildRegex(hostFilter); + hosts = _.filter(hostList, function (hostObj) { + return hostFilterPattern.test(hostObj.name); + }); + } + else { + var findedHosts = _.find(hostList, {'name': hostFilter}); + if (findedHosts) { + hosts.push(findedHosts); + } else { + hosts = undefined; + } } - } - // Filter applications by regex - if (utils.isRegex(appFilter)) { - var filterPattern = utils.buildRegex(appFilter); - apps = _.filter(applicationList, function (appObj) { - return filterPattern.test(appObj.name); - }); - } - // Find items in selected application - else if (appFilter) { - var finded = _.find(applicationList, {'name': appFilter}); - if (finded) { - apps.push(finded); + // Filter applications by regex + if (utils.isRegex(appFilter)) { + var filterPattern = utils.buildRegex(appFilter); + apps = _.filter(applicationList, function (appObj) { + return filterPattern.test(appObj.name); + }); + } + // Find items in selected application + else if (appFilter) { + var finded = _.find(applicationList, {'name': appFilter}); + if (finded) { + apps.push(finded); + } else { + apps = undefined; + } } else { apps = undefined; + if (hosts) { + items = _.filter(this.cache.getItems(), function (itemObj) { + return _.find(hosts, {'hostid': itemObj.hostid }); + }); + } } - } else { - apps = undefined; - if (hosts) { - items = _.filter(this.cache.getItems(), function (itemObj) { + + if (apps) { + 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 (apps) { - var appids = _.flatten(_.map(apps, 'applicationids')); - items = _.filter(this.cache.getItems(), function (itemObj) { - return _.intersection(appids, itemObj.applications).length; - }); - items = _.filter(items, function (itemObj) { - return _.find(hosts, {'hostid': itemObj.hostid }); - }); - } + if (!showDisabledItems) { + items = _.filter(items, {'status': '0'}); + } - if (!showDisabledItems) { - items = _.filter(items, {'status': '0'}); - } - - return items; + return items; + }); }; /** diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index 60715c0..fb35b27 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -56,38 +56,64 @@ function (angular, _, utils) { p.getGroups = function() { var self = this; if (this._groups) { + return $q.when(self._groups); + } else { return this.refresh().then(function() { return self._groups; }); } - return $q.when(this._groups); }; p.getHosts = function() { - return this._hosts; + var self = this; + if (this._hosts) { + return $q.when(self._hosts); + } else { + return this.refresh().then(function() { + return self._hosts; + }); + } }; p.getApplications = function() { - return this._applications; + var self = this; + if (this._applications) { + return $q.when(self._applications); + } else { + return this.refresh().then(function() { + return self._applications; + }); + } }; p.getItems = function(type) { + var self = this; + if (this._items) { + return $q.when(filterItems(self._items, type)); + } else { + return this.refresh().then(function() { + return filterItems(self._items, type); + }); + } + }; + + function filterItems(items, type) { switch (type) { case 'num': - return _.filter(this._items, function(item) { + return _.filter(items, function(item) { return (item.value_type === '0' || item.value_type === '3'); }); case 'text': - return _.filter(this._items, function(item) { + return _.filter(items, function(item) { return (item.value_type === '1' || item.value_type === '2' || item.value_type === '4'); }); default: - return this._items; + return items; } - }; + } p.getHost = function(hostid) { return _.find(this._hosts, {'hostid': hostid});