From 006d74d3c576fc16d81ca5ba70cab7bb0f220c24 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 11:02:14 +0300 Subject: [PATCH 01/12] Renamend ZabbixCache service to ZabbixCachingProxy. --- plugins/datasource-zabbix/datasource.js | 4 ++-- plugins/datasource-zabbix/zabbixCache.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 40f1378..facae7a 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -19,7 +19,7 @@ function (angular, _, dateMath, utils, metricFunctions) { /** @ngInject */ function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv, - ZabbixAPI, ZabbixCache, QueryProcessor, DataProcessingService) { + ZabbixAPI, ZabbixCachingProxy, QueryProcessor, DataProcessingService) { // General data source settings this.name = instanceSettings.name; @@ -39,7 +39,7 @@ function (angular, _, dateMath, utils, metricFunctions) { this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials); // Initialize cache service - this.zabbixCache = new ZabbixCache(this.zabbixAPI); + this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI); // Initialize query builder this.queryProcessor = new QueryProcessor(this.zabbixCache); diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index 57405cc..dc68ff4 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -10,9 +10,9 @@ function (angular, _, utils) { // Use factory() instead service() for multiple datasources support. // Each datasource instance must initialize its own cache. - module.factory('ZabbixCache', function($q) { + module.factory('ZabbixCachingProxy', function($q) { - function ZabbixCache(zabbixAPI, ttl) { + function ZabbixCachingProxy(zabbixAPI, ttl) { this.zabbixAPI = zabbixAPI; this.ttl = ttl; @@ -26,7 +26,7 @@ function (angular, _, utils) { this._initialized = undefined; } - var p = ZabbixCache.prototype; + var p = ZabbixCachingProxy.prototype; p.refresh = function () { var self = this; @@ -126,7 +126,7 @@ function (angular, _, utils) { }); } - return ZabbixCache; + return ZabbixCachingProxy; }); From a139131c26f20093bddcb71ed83b0672ad2ed557 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 11:11:56 +0300 Subject: [PATCH 02/12] Little ZabbixAPIService refactor. --- plugins/datasource-zabbix/zabbixAPI.js | 6 +++--- plugins/datasource-zabbix/zabbixAPIService.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js index 00b41dd..81ca4a1 100644 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ b/plugins/datasource-zabbix/zabbixAPI.js @@ -32,7 +32,7 @@ function (angular, _) { p.request = function(method, params) { var self = this; if (this.auth) { - return ZabbixAPIService._request(this.url, method, params, this.requestOptions, this.auth) + return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth) .then(function(result) { return result; }, @@ -43,7 +43,7 @@ function (angular, _) { return ZabbixAPIService.login(self.url, self.username, self.password, self.requestOptions) .then(function(auth) { self.auth = auth; - return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth); + return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); }); } }); @@ -53,7 +53,7 @@ function (angular, _) { return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions) .then(function(auth) { self.auth = auth; - return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth); + return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); }); } }; diff --git a/plugins/datasource-zabbix/zabbixAPIService.js b/plugins/datasource-zabbix/zabbixAPIService.js index 2212a5a..0b12b8d 100644 --- a/plugins/datasource-zabbix/zabbixAPIService.js +++ b/plugins/datasource-zabbix/zabbixAPIService.js @@ -16,7 +16,7 @@ function (angular) { * Request data from Zabbix API * @return {object} response.result */ - this._request = function(api_url, method, params, options, auth) { + this.request = function(api_url, method, params, options, auth) { var requestData = { jsonrpc: '2.0', method: method, @@ -70,7 +70,7 @@ function (angular) { user: username, password: password }; - return this._request(api_url, 'user.login', params, options, null); + return this.request(api_url, 'user.login', params, options, null); }; /** @@ -78,7 +78,7 @@ function (angular) { * Matches the version of Zabbix starting from Zabbix 2.0.4 */ this.getVersion = function(api_url, options) { - return this._request(api_url, 'apiinfo.version', [], options); + return this.request(api_url, 'apiinfo.version', [], options); }; }); From 625096cba858496b0761e7788d493d6d3b89c19c Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 11:17:14 +0300 Subject: [PATCH 03/12] ZabbixAPI refactor. --- plugins/datasource-zabbix/zabbixAPI.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js index 81ca4a1..34b4092 100644 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ b/plugins/datasource-zabbix/zabbixAPI.js @@ -8,6 +8,11 @@ function (angular, _) { var module = angular.module('grafana.services'); + /** + * Zabbix API Wrapper. + * Creates Zabbix API instance with given parameters (url, credentials and other). + * Wraps API calls and provides high-level methods. + */ module.factory('ZabbixAPI', function($q, backendSrv, ZabbixAPIService) { // Initialize Zabbix API. From 1d3e2337a2cf73bc0a3406240e7d7a48ff2b9669 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 13:40:24 +0300 Subject: [PATCH 04/12] Call login() once. --- plugins/datasource-zabbix/zabbixAPI.js | 43 ++++++++++++++++++++------ 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js index 34b4092..c1ddd9c 100644 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ b/plugins/datasource-zabbix/zabbixAPI.js @@ -26,6 +26,8 @@ function (angular, _) { basicAuth: basicAuth, withCredentials: withCredentials }; + + this.loginPromise = null; } var p = ZabbixAPI.prototype; @@ -45,24 +47,45 @@ function (angular, _) { // Handle errors function(error) { if (error.message === "Session terminated, re-login, please.") { - return ZabbixAPIService.login(self.url, self.username, self.password, self.requestOptions) - .then(function(auth) { - self.auth = auth; - return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); - }); + throw 'expired'; + return self.login().then(function(auth) { + self.auth = auth; + return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); + }); } }); } else { // Login first - return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions) - .then(function(auth) { - self.auth = auth; - return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); - }); + //throw 'unauthenticated'; + return self.loginOnce().then(function(auth) { + self.auth = auth; + return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); + }); } }; + /** + * When API unauthenticated or auth token expired each request produce login() + * call. But auth token is common to all requests. This function wraps login() method + * and call it once. If login() already called just wait for it (return its promise). + * @return login promise + */ + p.loginOnce = function() { + var self = this; + var deferred = $q.defer(); + if (!self.loginPromise) { + self.loginPromise = deferred.promise; + self.login().then(function(auth) { + self.loginPromise = null; + deferred.resolve(auth); + }); + } else { + return self.loginPromise; + } + return deferred.promise; + }; + /** * Get authentication token. */ From 63cb31003cde209b99b9c671436c3ece980a0b30 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 15:06:36 +0300 Subject: [PATCH 05/12] Improved both api request() in zabbixAPI and zabbixAPIService. Improved api request error handling. --- plugins/datasource-zabbix/zabbixAPI.js | 45 +++++++++---------- plugins/datasource-zabbix/zabbixAPIService.js | 18 +++++--- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js index c1ddd9c..c84d829 100644 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ b/plugins/datasource-zabbix/zabbixAPI.js @@ -20,7 +20,7 @@ function (angular, _) { this.url = api_url; this.username = username; this.password = password; - this.auth = null; + this.auth = ""; this.requestOptions = { basicAuth: basicAuth, @@ -38,33 +38,29 @@ function (angular, _) { p.request = function(method, params) { var self = this; - if (this.auth) { - return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth) - .then(function(result) { - return result; - }, + return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth) + .then(function(result) { + return result; + }, - // Handle errors - function(error) { - if (error.message === "Session terminated, re-login, please.") { - throw 'expired'; - return self.login().then(function(auth) { - self.auth = auth; - return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); - }); - } - }); - } else { - - // Login first - //throw 'unauthenticated'; - return self.loginOnce().then(function(auth) { - self.auth = auth; - return ZabbixAPIService.request(self.url, method, params, self.requestOptions, self.auth); + // Handle errors + function(error) { + if (isAuthError(error.data)) { + return self.loginOnce().then(function() { + return self.request(method, params); + }); + } }); - } }; + function isAuthError(message) { + return ( + message === "Session terminated, re-login, please." || + message === "Not authorised." || + message === "Not authorized." + ); + } + /** * When API unauthenticated or auth token expired each request produce login() * call. But auth token is common to all requests. This function wraps login() method @@ -78,6 +74,7 @@ function (angular, _) { self.loginPromise = deferred.promise; self.login().then(function(auth) { self.loginPromise = null; + self.auth = auth; deferred.resolve(auth); }); } else { diff --git a/plugins/datasource-zabbix/zabbixAPIService.js b/plugins/datasource-zabbix/zabbixAPIService.js index 0b12b8d..03f7789 100644 --- a/plugins/datasource-zabbix/zabbixAPIService.js +++ b/plugins/datasource-zabbix/zabbixAPIService.js @@ -17,6 +17,7 @@ function (angular) { * @return {object} response.result */ this.request = function(api_url, method, params, options, auth) { + var deferred = $q.defer(); var requestData = { jsonrpc: '2.0', method: method, @@ -24,8 +25,12 @@ function (angular) { id: 1 }; - // Set auth parameter only if it needed - if (auth) { + if (auth === "") { + // Reject immediately if not authenticated + deferred.reject({data: "Not authorised."}); + return deferred.promise; + } else if (auth) { + // Set auth parameter only if it needed requestData.auth = auth; } @@ -46,19 +51,20 @@ function (angular) { requestOptions.headers.Authorization = options.basicAuth; } - return backendSrv.datasourceRequest(requestOptions).then(function (response) { + backendSrv.datasourceRequest(requestOptions).then(function (response) { // General connection issues if (!response.data) { - return []; + deferred.reject(response); } // Handle Zabbix API errors else if (response.data.error) { - throw new ZabbixException(response.data.error); + deferred.reject(response.data.error); } - return response.data.result; + deferred.resolve(response.data.result); }); + return deferred.promise; }; /** From e9d29c63bafe9322856ed17cc02e8ad848003701 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 15:14:48 +0300 Subject: [PATCH 06/12] Wrap cache refresh() method to call it once. --- plugins/datasource-zabbix/zabbixCache.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index dc68ff4..1fe6e31 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -24,11 +24,31 @@ function (angular, _, utils) { // Check is a service initialized or not this._initialized = undefined; + + this.refreshPromise = false; } var p = ZabbixCachingProxy.prototype; - p.refresh = function () { + /** + * Wrap _refresh() method to call it once. + */ + p.refresh = function() { + var self = this; + var deferred = $q.defer(); + if (!self.refreshPromise) { + self.refreshPromise = deferred.promise; + self._refresh().then(function() { + deferred.resolve(); + self.refreshPromise = null; + }); + } else { + return self.refreshPromise; + } + return deferred.promise; + }; + + p._refresh = function() { var self = this; var promises = [ this.zabbixAPI.getGroups(), From 521e3a2a089791e36f44dea9b484cc86d86a515e Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 16:20:09 +0300 Subject: [PATCH 07/12] ZabbixCachingProxy: Wrap _refresh() method to call it once. --- plugins/datasource-zabbix/zabbixCache.js | 41 ++++++++++++------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index 1fe6e31..24baac9 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -26,28 +26,13 @@ function (angular, _, utils) { this._initialized = undefined; this.refreshPromise = false; + + // Wrap _refresh() method to call it once. + this.refresh = callOnce(p._refresh, this.refreshPromise); } var p = ZabbixCachingProxy.prototype; - /** - * Wrap _refresh() method to call it once. - */ - p.refresh = function() { - var self = this; - var deferred = $q.defer(); - if (!self.refreshPromise) { - self.refreshPromise = deferred.promise; - self._refresh().then(function() { - deferred.resolve(); - self.refreshPromise = null; - }); - } else { - return self.refreshPromise; - } - return deferred.promise; - }; - p._refresh = function() { var self = this; var promises = [ @@ -57,7 +42,7 @@ function (angular, _, utils) { this.zabbixAPI.getItems() ]; - return $q.all(promises).then(function (results) { + return $q.all(promises).then(function(results) { if (results.length) { self._groups = results[0]; self._hosts = convertHosts(results[1]); @@ -122,7 +107,7 @@ function (angular, _, utils) { * host.hosts - array of host ids */ function convertApplications(applications) { - return _.map(_.groupBy(applications, 'name'), function (value, key) { + return _.map(_.groupBy(applications, 'name'), function(value, key) { return { name: key, applicationids: _.map(value, 'applicationid'), @@ -146,6 +131,22 @@ function (angular, _, utils) { }); } + function callOnce(func, promiseKeeper) { + return function() { + var deferred = $q.defer(); + if (!promiseKeeper) { + promiseKeeper = deferred.promise; + func.apply(this, arguments).then(function(result) { + deferred.resolve(result); + promiseKeeper = null; + }); + } else { + return promiseKeeper; + } + return deferred.promise; + }; + } + return ZabbixCachingProxy; }); From 1de4b4bc5c4222a5171087fac43310a7d50c7b81 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 16:43:59 +0300 Subject: [PATCH 08/12] 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() { From 5b3fe1559cc109c1306bcc8ad83f6c3e16a825d3 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 17:20:16 +0300 Subject: [PATCH 09/12] Refacrot ZabbixCachingProxy. Now returns all data in async manner. --- plugins/datasource-zabbix/queryCtrl.js | 41 ++--- plugins/datasource-zabbix/queryProcessor.js | 160 +++++++++++--------- plugins/datasource-zabbix/zabbixCache.js | 40 ++++- 3 files changed, 144 insertions(+), 97 deletions(-) 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}); From e532b3c37f3a4fbf277cb70e4a19a837fd5efa2b Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 19:00:30 +0300 Subject: [PATCH 10/12] Added method for getting history from cache. --- plugins/datasource-zabbix/datasource.js | 2 +- plugins/datasource-zabbix/zabbixCache.js | 46 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index facae7a..8d995fb 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -140,7 +140,7 @@ function (angular, _, dateMath, utils, metricFunctions) { } else { // Use history - getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) { + getHistory = self.zabbixCache.getHistory(items, from, to).then(function(history) { return self.queryProcessor.handleHistory(history, addHostName); }); } diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index fb35b27..bc9f6ca 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -21,6 +21,10 @@ function (angular, _, utils) { this._hosts = undefined; this._applications = undefined; this._items = undefined; + this.storage = { + history: {}, + trends: {} + }; // Check is a service initialized or not this._initialized = undefined; @@ -115,6 +119,48 @@ function (angular, _, utils) { } } + p.getHistory = function(items, time_from, time_till) { + var itemids = _.map(arguments[0], 'itemid'); + var stamp = itemids.join() + arguments[1] + arguments[2]; + //console.log(arguments, stamp); + return this.zabbixAPI.getHistory(items, time_from, time_till); + }; + + p.getHistory_ = function(items, time_from, time_till) { + var deferred = $q.defer(); + var historyStorage = this.storage.history; + var full_history; + var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) { + return !historyStorage[itemid]; + }); + if (expired.length) { + this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) { + var grouped_history = _.groupBy(history, 'itemid'); + _.forEach(expired, function(item) { + var itemid = item.itemid; + historyStorage[itemid] = item; + historyStorage[itemid].time_from = time_from; + historyStorage[itemid].time_till = time_till; + historyStorage[itemid].history = grouped_history[itemid]; + }); + full_history = _.map(items, function(item) { + return historyStorage[item.itemid].history; + }); + deferred.resolve(_.flatten(full_history, true)); + }); + } else { + full_history = _.map(items, function(item) { + return historyStorage[item.itemid].history; + }); + deferred.resolve(_.flatten(full_history, true)); + } + return deferred.promise; + }; + + p.getHistoryFromAPI = function(items, time_from, time_till) { + return this.zabbixAPI.getHistory(items, time_from, time_till); + }; + p.getHost = function(hostid) { return _.find(this._hosts, {'hostid': hostid}); }; From 594bbbddb95e85c85e19c6ff1bfc17bbc510918e Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 20:03:02 +0300 Subject: [PATCH 11/12] Fixed templated variables query. --- plugins/datasource-zabbix/datasource.js | 31 ++++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 8d995fb..64726fd 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -277,8 +277,6 @@ function (angular, _, dateMath, utils, metricFunctions) { * of metrics in "{metric1,metcic2,...,metricN}" format. */ this.metricFindQuery = function (query) { - var metrics; - // Split query. Query structure: // group.host.app.item var parts = []; @@ -296,31 +294,36 @@ function (angular, _, dateMath, utils, metricFunctions) { // Get items if (parts.length === 4) { - var items = this.queryProcessor.filterItems(template.host, template.app, true); - metrics = _.map(items, formatMetric); + //var items = this.queryProcessor.filterItems(template.host, template.app, true); + return this.queryProcessor.filterItems(template.host, template.app, true) + .then(function(items) { + return _.map(items, formatMetric); + }); } // Get applications else if (parts.length === 3) { - var apps = this.queryProcessor.filterApplications(template.host); - metrics = _.map(apps, formatMetric); + return this.queryProcessor.filterApplications(template.host) + .then(function(apps) { + return _.map(apps, formatMetric); + }); } // Get hosts else if (parts.length === 2) { - var hosts = this.queryProcessor.filterHosts(template.group); - metrics = _.map(hosts, formatMetric); + return this.queryProcessor.filterHosts(template.group) + .then(function(hosts) { + return _.map(hosts, formatMetric); + }); } // Get groups else if (parts.length === 1) { - metrics = _.map(this.zabbixCache.getGroups(template.group), formatMetric); + return this.zabbixCache.getGroups(template.group).then(function(groups) { + return _.map(groups, formatMetric); + }); } // Return empty object for invalid request else { - var d = $q.defer(); - d.resolve([]); - return d.promise; + return $q.when([]); } - - return $q.when(metrics); }; function formatMetric(metricObj) { From f696760c321b95cc1e5e2e8b42a290ec2557ab5a Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 21:21:14 +0300 Subject: [PATCH 12/12] 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 }); }); }