diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index 6225cc6..e25349d 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -2,19 +2,19 @@ define([ 'angular', 'lodash', 'app/core/utils/datemath', + './queryBuilder', './directives', './zabbixAPI', - './utils', './helperFunctions', - './zabbixCacheSrv', + './zabbixCache', './queryCtrl' ], -function (angular, _, dateMath) { +function (angular, _, dateMath, QueryBuilder) { 'use strict'; /** @ngInject */ - function ZabbixAPIDatasource(instanceSettings, $q, backendSrv, templateSrv, alertSrv, - ZabbixAPI, Utils, zabbixHelperSrv, ZabbixCache) { + function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, + ZabbixAPI, zabbixHelperSrv, ZabbixCache) { // General data source settings this.name = instanceSettings.name; @@ -35,7 +35,13 @@ function (angular, _, dateMath) { // Initialize cache service this.zabbixCache = new ZabbixCache(this.zabbixAPI); - console.log(this.zabbixCache); + + // Initialize query builder + this.queryBuilder = new QueryBuilder(this.zabbixCache); + + //////////////////////// + // Datasource methods // + //////////////////////// /** * Test connection to Zabbix API @@ -79,7 +85,7 @@ function (angular, _, dateMath) { }; /** - * Calls for each panel in dashboard. + * Query panel data. Calls for each panel in dashboard. * * @param {Object} options Query options. Contains time range, targets * and other info. @@ -112,112 +118,9 @@ function (angular, _, dateMath) { var itemFilter = templateSrv.replace(target.item.filter, options.scopedVars); // Query numeric data - if (!target.mode) { + if (!target.mode || target.mode === 0) { - // Find items by item names and perform queries - var groups = []; - var hosts = []; - var apps = []; - var items = []; - - if (target.host.isRegex) { - - // Filter groups - if (target.group.isRegex) { - var groupPattern = Utils.buildRegex(groupFilter); - groups = _.filter(self.zabbixCache.getGroups(), function (groupObj) { - return groupPattern.test(groupObj.name); - }); - } else { - var findedGroup = _.find(self.zabbixCache.getGroups(), {'name': groupFilter}); - if (findedGroup) { - groups.push(findedGroup); - } else { - groups = undefined; - } - } - if (groups) { - var groupids = _.map(groups, 'groupid'); - hosts = _.filter(self.zabbixCache.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(self.zabbixCache.getHosts(), {'name': hostFilter}); - if (findedHost) { - hosts.push(findedHost); - } else { - // No hosts finded - return []; - } - } - - // Find items belongs to selected hosts - items = _.filter(self.zabbixCache.getItems(), function (itemObj) { - return _.contains(_.map(hosts, 'hostid'), itemObj.hostid); - }); - - if (target.item.isRegex) { - - // Filter applications - if (target.application.isRegex) { - var appPattern = Utils.buildRegex(appFilter); - apps = _.filter(self.zabbixCache.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(self.zabbixCache.getApplications(), {'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 { - items = _.filter(items, {'name': hostFilter}); - 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; - }); + var items = self.queryBuilder.build(groupFilter, hostFilter, appFilter, itemFilter); // Use alias only for single metric, otherwise use item names var alias; diff --git a/plugins/datasource-zabbix/queryBuilder.js b/plugins/datasource-zabbix/queryBuilder.js index 5293409..8bdba8a 100644 --- a/plugins/datasource-zabbix/queryBuilder.js +++ b/plugins/datasource-zabbix/queryBuilder.js @@ -1,15 +1,14 @@ define([ 'angular', 'lodash', - './zabbixCacheSrv', './utils' ], -function (angular, _) { +function (angular, _, utils) { 'use strict'; - var module = angular.module('grafana.services'); + function QueryBuilder(zabbixCacheInstance) { - module.service('QueryBuilderSrv', function(ZabbixCache, Utils) { + this.cache = zabbixCacheInstance; this.build = function (groupFilter, hostFilter, appFilter, itemFilter) { @@ -19,16 +18,16 @@ function (angular, _) { var apps = []; var items = []; - if (Utils.isRegex(hostFilter)) { + if (utils.isRegex(hostFilter)) { // Filter groups - if (Utils.isRegex(groupFilter)) { - var groupPattern = Utils.buildRegex(groupFilter); - groups = _.filter(ZabbixCache.getGroups(), function (groupObj) { + 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(ZabbixCache.getGroups(), {'name': groupFilter}); + var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter}); if (findedGroup) { groups.push(findedGroup); } else { @@ -37,7 +36,7 @@ function (angular, _) { } if (groups) { var groupids = _.map(groups, 'groupid'); - hosts = _.filter(ZabbixCache.getHosts(), function (hostObj) { + hosts = _.filter(this.cache.getHosts(), function (hostObj) { return _.intersection(groupids, hostObj.groups).length; }); } else { @@ -46,12 +45,12 @@ function (angular, _) { } // Filter hosts - var hostPattern = Utils.buildRegex(hostFilter); + var hostPattern = utils.buildRegex(hostFilter); hosts = _.filter(hosts, function (hostObj) { return hostPattern.test(hostObj.name); }); } else { - var findedHost = _.find(ZabbixCache.getHosts(), {'name': hostFilter}); + var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter}); if (findedHost) { hosts.push(findedHost); } else { @@ -61,16 +60,16 @@ function (angular, _) { } // Find items belongs to selected hosts - items = _.filter(ZabbixCache.getItems(), function (itemObj) { + items = _.filter(this.cache.getItems(), function (itemObj) { return _.contains(_.map(hosts, 'hostid'), itemObj.hostid); }); - if (Utils.isRegex(itemFilter)) { + if (utils.isRegex(itemFilter)) { // Filter applications - if (Utils.isRegex(appFilter)) { - var appPattern = Utils.buildRegex(appFilter); - apps = _.filter(ZabbixCache.getApplications(), function (appObj) { + if (utils.isRegex(appFilter)) { + var appPattern = utils.buildRegex(appFilter); + apps = _.filter(this.cache.getApplications(), function (appObj) { return appPattern.test(appObj.name); }); } @@ -79,7 +78,7 @@ function (angular, _) { apps = undefined; } else { - var findedApp = _.find(ZabbixCache.getApplications(), {'name': appFilter}); + var findedApp = _.find(this.cache.getApplications(), {'name': appFilter}); if (findedApp) { apps.push(findedApp); } else { @@ -97,7 +96,7 @@ function (angular, _) { } if (items) { - var itemPattern = Utils.buildRegex(itemFilter); + var itemPattern = utils.buildRegex(itemFilter); items = _.filter(items, function (itemObj) { return itemPattern.test(itemObj.name); }); @@ -118,40 +117,11 @@ function (angular, _) { itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name; }); - // Use alias only for single metric, otherwise use item names - var alias; - if (items.length === 1) { - alias = templateSrv.replace(alias, options.scopedVars); - } - - var history; - if ((from < useTrendsFrom) && self.trends) { - // Use trends - var points = downsampleFunction ? downsampleFunction.value : "avg"; - history = self.zabbixAPI.getTrends(items, from, to) - .then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, scale, points)); - } else { - // Use history - history = self.zabbixAPI.getHistory(items, from, to) - .then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, scale)); - } - - return history.then(function (timeseries) { - var timeseries_data = _.flatten(timeseries); - return _.map(timeseries_data, function (timeseries) { - - // Series downsampling - if (timeseries.datapoints.length > options.maxDataPoints) { - var ms_interval = Math.floor((to - from) / options.maxDataPoints) * 1000; - var downsampleFunc = downsampleFunction ? downsampleFunction.value : "avg"; - timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval, downsampleFunc); - } - return timeseries; - }); - }); - - return itemFilter; + return items; }; - }); + } + + return QueryBuilder; + }); \ No newline at end of file diff --git a/plugins/datasource-zabbix/queryCtrl.js b/plugins/datasource-zabbix/queryCtrl.js index 6d05da9..4c8dbc4 100644 --- a/plugins/datasource-zabbix/queryCtrl.js +++ b/plugins/datasource-zabbix/queryCtrl.js @@ -3,13 +3,13 @@ define([ 'lodash', './utils' ], - function (angular, _) { + function (angular, _, Utils) { 'use strict'; var module = angular.module('grafana.controllers'); var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; - module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, Utils) { + module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) { var zabbixCache = $scope.datasource.zabbixCache; diff --git a/plugins/datasource-zabbix/utils.js b/plugins/datasource-zabbix/utils.js index 900bba7..003ce53 100644 --- a/plugins/datasource-zabbix/utils.js +++ b/plugins/datasource-zabbix/utils.js @@ -1,13 +1,10 @@ define([ - 'angular', 'lodash' ], -function (angular, _) { +function () { 'use strict'; - var module = angular.module('grafana.services'); - - module.service('Utils', function() { + function utils() { /** * Expand Zabbix item name @@ -43,5 +40,7 @@ function (angular, _) { return new RegExp(pattern, flags); }; - }); + } + + return new utils(); }); \ No newline at end of file diff --git a/plugins/datasource-zabbix/zabbixCacheSrv.js b/plugins/datasource-zabbix/zabbixCache.js similarity index 95% rename from plugins/datasource-zabbix/zabbixCacheSrv.js rename to plugins/datasource-zabbix/zabbixCache.js index 96bd424..e5d63f8 100644 --- a/plugins/datasource-zabbix/zabbixCacheSrv.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -3,14 +3,14 @@ define([ 'lodash', './utils' ], -function (angular, _) { +function (angular, _, utils) { 'use strict'; var module = angular.module('grafana.services'); // Use factory() instead service() for multiple datasources support. // Each datasource instance must initialize its own cache. - module.factory('ZabbixCache', function($q, Utils) { + module.factory('ZabbixCache', function($q) { function ZabbixCache(zabbixAPI, ttl) { var self = this; @@ -105,7 +105,7 @@ function (angular, _) { return _.forEach(items, function(item) { item.applications = _.map(item.applications, 'applicationid'); item.item = item.name; - item.name = Utils.expandItemName(item.item, item.key_); + item.name = utils.expandItemName(item.item, item.key_); return item; }); }