From beee9973d7452e76277e7db0cbff43125110984e Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 8 Jun 2015 16:25:20 +0300 Subject: [PATCH] Improved query editor - more flexible metric selection. --- .gitignore | 2 + zabbix/datasource.js | 125 ++++++++++++++++++++++--------------------- zabbix/queryCtrl.js | 15 +++--- 3 files changed, 73 insertions(+), 69 deletions(-) diff --git a/.gitignore b/.gitignore index b543f8f..20d58b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.sublime-workspace *.sublime-project + +.idea/ \ No newline at end of file diff --git a/zabbix/datasource.js b/zabbix/datasource.js index 64bc7df..4cc8ec6 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -48,61 +48,51 @@ function (angular, _, kbn) { var promises = _.map(options.targets, function(target) { // Remove undefined and hidden targets - if (target.hide || !target.item) { + if (target.hide) { return []; } - // From !target.item.templated for testing - if (false) { + var groupname = target.hostGroup ? templateSrv.replace(target.hostGroup.name) : undefined; + var hostname = target.host ? templateSrv.replace(target.host.name) : undefined; + var appname = target.application ? templateSrv.replace(target.application.name) : undefined; + var itemname = target.item ? templateSrv.replace(target.item.name) : undefined; - // Perform request and then handle result - var item = [target.item]; - var alias = [{ - itemid: target.item.itemid, - key_: '', - name: target.alias - }]; - return this.performTimeSeriesQuery(item, from, to).then(_.partial( - this.handleHistoryResponse, alias)); - } else { - /** - * Handle templated target - */ + // Extract zabbix hosts from hosts string: + // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN] + var hosts = hostname ? splitMetrics(hostname) : undefined; - var itemname = templateSrv.replace(target.item.name); - var hostname = templateSrv.replace(target.host.name); + var groups = groupname ? splitMetrics(groupname) : undefined; + var apps = appname ? splitMetrics(appname) : undefined; - // Extract zabbix hosts from hosts string: - // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN] - var hosts = splitMetrics(hostname); + // Remove hostnames from item names and then + // extract item names + // "hostname: itemname" --> "itemname" + var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g; + var itemnames = itemname ? splitMetrics(itemname.replace(delete_hostname_pattern, '')) : []; + //var aliases = itemname.match(itemname_pattern); - // Remove hostnames from item names and then - // extract item names - // "hostname: itemname" --> "itemname" - var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g; - var itemnames = splitMetrics(itemname.replace(delete_hostname_pattern, '')); - //var aliases = itemname.match(itemname_pattern); + // Don't perform query for high number of items + // to prevent Grafana slowdown + if (itemnames.length < this.limitmetrics) { - // Don't perform query for high number of items - // to prevent Grafana slowdown - if (itemnames && (itemnames.length < this.limitmetrics)) { - - // Select the host that the item belongs for multiple hosts request - if (hosts.length > 1) { - var selectHosts = true; - } - - // Find items by item names and perform queries - var self = this; - return this.findZabbixItem(hosts, itemnames, selectHosts) - .then(function (items) { - items = _.flatten(items); - return self.performTimeSeriesQuery(items, from, to) + // Find items by item names and perform queries + var self = this; + return this.itemFindQuery(groups, hosts, apps) + .then(function (items) { + if (itemnames.length) { + return _.filter(items, function (item) { + return _.contains(itemnames, expandItemName(item)); + }); + } else { + return items; + } + }).then(function (items) { + items = _.flatten(items); + return self.performTimeSeriesQuery(items, from, to) .then(_.partial(self.handleHistoryResponse, items)); - }); - } else { - return []; - } + }); + } else { + return []; } }, this); @@ -394,7 +384,8 @@ function (angular, _, kbn) { var hostids = _.map(hosts, 'hostid'); var params = { output: ['name', 'key_', 'value_type'], - hostids: hostids + hostids: hostids, + searchWildcardsEnabled: true }; if (selectHosts) { params.selectHosts = ['name']; @@ -519,15 +510,21 @@ function (angular, _, kbn) { var self = this; return $q.all(promises).then(function (results) { results = _.flatten(results); - var groupids = _.map(_.filter(results, function (object) { - return object.groupid; - }), 'groupid'); - var hostids = _.map(_.filter(results, function (object) { - return object.hostid; - }), 'hostid'); - var applicationids = _.map(_.filter(results, function (object) { - return object.applicationid; - }), 'applicationid'); + if (groups) { + var groupids = _.map(_.filter(results, function (object) { + return object.groupid; + }), 'groupid'); + } + if (hosts) { + var hostids = _.map(_.filter(results, function (object) { + return object.hostid; + }), 'hostid'); + } + if (apps) { + var applicationids = _.map(_.filter(results, function (object) { + return object.applicationid; + }), 'applicationid'); + } return self.performItemSuggestQuery(hostids, applicationids, groupids); }); @@ -549,12 +546,16 @@ function (angular, _, kbn) { var self = this; return $q.all(promises).then(function (results) { results = _.flatten(results); - var groupids = _.map(_.filter(results, function (object) { - return object.groupid; - }), 'groupid'); - var hostids = _.map(_.filter(results, function (object) { - return object.hostid; - }), 'hostid'); + if (groups) { + var groupids = _.map(_.filter(results, function (object) { + return object.groupid; + }), 'groupid'); + } + if (hosts) { + var hostids = _.map(_.filter(results, function (object) { + return object.hostid; + }), 'hostid'); + } return self.performAppSuggestQuery(hostids, groupids); }); diff --git a/zabbix/queryCtrl.js b/zabbix/queryCtrl.js index af7dd10..e44f379 100644 --- a/zabbix/queryCtrl.js +++ b/zabbix/queryCtrl.js @@ -57,6 +57,7 @@ function (angular, _) { $scope.selectHostGroup = function() { $scope.updateHostList() $scope.updateAppList(); + $scope.updateItemList(); $scope.target.errors = validateTarget($scope.target); if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { @@ -70,8 +71,8 @@ function (angular, _) { * Call when host selected */ $scope.selectHost = function() { - $scope.updateItemList(); $scope.updateAppList(); + $scope.updateItemList(); $scope.target.errors = validateTarget($scope.target); if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { @@ -150,7 +151,7 @@ function (angular, _) { $scope.metric.hostList = []; addTemplatedVariables($scope.metric.hostList); - var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : []; + var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : undefined; $scope.datasource.hostFindQuery(groups).then(function (hosts) { $scope.metric.hostList = $scope.metric.hostList.concat(hosts); @@ -171,8 +172,8 @@ function (angular, _) { $scope.metric.applicationList = []; addTemplatedVariables($scope.metric.applicationList); - var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : []; - var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : []; + var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : undefined; + var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; $scope.datasource.appFindQuery(hosts, groups).then(function (apps) { // TODO: work with app names, not objects var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { @@ -197,9 +198,9 @@ function (angular, _) { $scope.metric.itemList = []; addTemplatedVariables($scope.metric.itemList); - var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : []; - var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : []; - var apps = $scope.target.application ? splitMetrics(templateSrv.replace($scope.target.application.name)) : []; + var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : undefined; + var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; + var apps = $scope.target.application ? splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined; $scope.datasource.itemFindQuery(groups, hosts, apps).then(function (items) { // Show only unique item names var uniq_items = _.uniq(items, function (item) {