Improved query editor - more flexible metric selection.

This commit is contained in:
Alexander Zobnin
2015-06-08 16:25:20 +03:00
parent 1e3af4c742
commit beee9973d7
3 changed files with 73 additions and 69 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
*.sublime-workspace *.sublime-workspace
*.sublime-project *.sublime-project
.idea/

View File

@@ -48,54 +48,45 @@ function (angular, _, kbn) {
var promises = _.map(options.targets, function(target) { var promises = _.map(options.targets, function(target) {
// Remove undefined and hidden targets // Remove undefined and hidden targets
if (target.hide || !target.item) { if (target.hide) {
return []; return [];
} }
// From !target.item.templated for testing var groupname = target.hostGroup ? templateSrv.replace(target.hostGroup.name) : undefined;
if (false) { var hostname = target.host ? templateSrv.replace(target.host.name) : undefined;
var appname = target.application ? templateSrv.replace(target.application.name) : undefined;
// Perform request and then handle result var itemname = target.item ? templateSrv.replace(target.item.name) : undefined;
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
*/
var itemname = templateSrv.replace(target.item.name);
var hostname = templateSrv.replace(target.host.name);
// Extract zabbix hosts from hosts string: // Extract zabbix hosts from hosts string:
// "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN] // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN]
var hosts = splitMetrics(hostname); var hosts = hostname ? splitMetrics(hostname) : undefined;
var groups = groupname ? splitMetrics(groupname) : undefined;
var apps = appname ? splitMetrics(appname) : undefined;
// Remove hostnames from item names and then // Remove hostnames from item names and then
// extract item names // extract item names
// "hostname: itemname" --> "itemname" // "hostname: itemname" --> "itemname"
var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g; var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g;
var itemnames = splitMetrics(itemname.replace(delete_hostname_pattern, '')); var itemnames = itemname ? splitMetrics(itemname.replace(delete_hostname_pattern, '')) : [];
//var aliases = itemname.match(itemname_pattern); //var aliases = itemname.match(itemname_pattern);
// Don't perform query for high number of items // Don't perform query for high number of items
// to prevent Grafana slowdown // to prevent Grafana slowdown
if (itemnames && (itemnames.length < this.limitmetrics)) { if (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 // Find items by item names and perform queries
var self = this; var self = this;
return this.findZabbixItem(hosts, itemnames, selectHosts) return this.itemFindQuery(groups, hosts, apps)
.then(function (items) { .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); items = _.flatten(items);
return self.performTimeSeriesQuery(items, from, to) return self.performTimeSeriesQuery(items, from, to)
.then(_.partial(self.handleHistoryResponse, items)); .then(_.partial(self.handleHistoryResponse, items));
@@ -103,7 +94,6 @@ function (angular, _, kbn) {
} else { } else {
return []; return [];
} }
}
}, this); }, this);
return $q.all(_.flatten(promises)).then(function (results) { return $q.all(_.flatten(promises)).then(function (results) {
@@ -394,7 +384,8 @@ function (angular, _, kbn) {
var hostids = _.map(hosts, 'hostid'); var hostids = _.map(hosts, 'hostid');
var params = { var params = {
output: ['name', 'key_', 'value_type'], output: ['name', 'key_', 'value_type'],
hostids: hostids hostids: hostids,
searchWildcardsEnabled: true
}; };
if (selectHosts) { if (selectHosts) {
params.selectHosts = ['name']; params.selectHosts = ['name'];
@@ -519,15 +510,21 @@ function (angular, _, kbn) {
var self = this; var self = this;
return $q.all(promises).then(function (results) { return $q.all(promises).then(function (results) {
results = _.flatten(results); results = _.flatten(results);
if (groups) {
var groupids = _.map(_.filter(results, function (object) { var groupids = _.map(_.filter(results, function (object) {
return object.groupid; return object.groupid;
}), 'groupid'); }), 'groupid');
}
if (hosts) {
var hostids = _.map(_.filter(results, function (object) { var hostids = _.map(_.filter(results, function (object) {
return object.hostid; return object.hostid;
}), 'hostid'); }), 'hostid');
}
if (apps) {
var applicationids = _.map(_.filter(results, function (object) { var applicationids = _.map(_.filter(results, function (object) {
return object.applicationid; return object.applicationid;
}), 'applicationid'); }), 'applicationid');
}
return self.performItemSuggestQuery(hostids, applicationids, groupids); return self.performItemSuggestQuery(hostids, applicationids, groupids);
}); });
@@ -549,12 +546,16 @@ function (angular, _, kbn) {
var self = this; var self = this;
return $q.all(promises).then(function (results) { return $q.all(promises).then(function (results) {
results = _.flatten(results); results = _.flatten(results);
if (groups) {
var groupids = _.map(_.filter(results, function (object) { var groupids = _.map(_.filter(results, function (object) {
return object.groupid; return object.groupid;
}), 'groupid'); }), 'groupid');
}
if (hosts) {
var hostids = _.map(_.filter(results, function (object) { var hostids = _.map(_.filter(results, function (object) {
return object.hostid; return object.hostid;
}), 'hostid'); }), 'hostid');
}
return self.performAppSuggestQuery(hostids, groupids); return self.performAppSuggestQuery(hostids, groupids);
}); });

View File

@@ -57,6 +57,7 @@ function (angular, _) {
$scope.selectHostGroup = function() { $scope.selectHostGroup = function() {
$scope.updateHostList() $scope.updateHostList()
$scope.updateAppList(); $scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
@@ -70,8 +71,8 @@ function (angular, _) {
* Call when host selected * Call when host selected
*/ */
$scope.selectHost = function() { $scope.selectHost = function() {
$scope.updateItemList();
$scope.updateAppList(); $scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target); $scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
@@ -150,7 +151,7 @@ function (angular, _) {
$scope.metric.hostList = []; $scope.metric.hostList = [];
addTemplatedVariables($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.datasource.hostFindQuery(groups).then(function (hosts) {
$scope.metric.hostList = $scope.metric.hostList.concat(hosts); $scope.metric.hostList = $scope.metric.hostList.concat(hosts);
@@ -171,8 +172,8 @@ function (angular, _) {
$scope.metric.applicationList = []; $scope.metric.applicationList = [];
addTemplatedVariables($scope.metric.applicationList); addTemplatedVariables($scope.metric.applicationList);
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;
var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : []; var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
$scope.datasource.appFindQuery(hosts, groups).then(function (apps) { $scope.datasource.appFindQuery(hosts, groups).then(function (apps) {
// TODO: work with app names, not objects // TODO: work with app names, not objects
var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
@@ -197,9 +198,9 @@ function (angular, _) {
$scope.metric.itemList = []; $scope.metric.itemList = [];
addTemplatedVariables($scope.metric.itemList); addTemplatedVariables($scope.metric.itemList);
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;
var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : []; 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)) : []; var apps = $scope.target.application ? splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined;
$scope.datasource.itemFindQuery(groups, hosts, apps).then(function (items) { $scope.datasource.itemFindQuery(groups, hosts, apps).then(function (items) {
// Show only unique item names // Show only unique item names
var uniq_items = _.uniq(items, function (item) { var uniq_items = _.uniq(items, function (item) {