Query editor: templated variables support.

This commit is contained in:
Alexander Zobnin
2015-06-04 22:38:02 +03:00
parent 53ad2a07f5
commit a75af217fd
2 changed files with 58 additions and 44 deletions

View File

@@ -433,7 +433,7 @@ function (angular, _, kbn) {
// Get items // Get items
if (parts.length === 4) { if (parts.length === 4) {
return this.itemFindQuery(template).then(function (result) { return this.itemFindQuery(template.group, template.host, template.app).then(function (result) {
return _.map(result, function (item) { return _.map(result, function (item) {
var itemname = expandItemName(item) var itemname = expandItemName(item)
return { return {
@@ -446,7 +446,7 @@ function (angular, _, kbn) {
} }
// Get applications // Get applications
else if (parts.length === 3) { else if (parts.length === 3) {
return this.appFindQuery(template).then(function (result) { return this.appFindQuery(template.host, template.group).then(function (result) {
return _.map(result, function (app) { return _.map(result, function (app) {
return { return {
text: app.name, text: app.name,
@@ -457,7 +457,7 @@ function (angular, _, kbn) {
} }
// Get hosts // Get hosts
else if (parts.length === 2) { else if (parts.length === 2) {
return this.hostFindQuery(template).then(function (result) { return this.hostFindQuery(template.group).then(function (result) {
return _.map(result, function (host) { return _.map(result, function (host) {
return { return {
text: host.name, text: host.name,
@@ -499,20 +499,20 @@ function (angular, _, kbn) {
* *
* @return {Array} Array of Zabbix API item objects * @return {Array} Array of Zabbix API item objects
*/ */
ZabbixAPIDatasource.prototype.itemFindQuery = function(template) { ZabbixAPIDatasource.prototype.itemFindQuery = function(groups, hosts, apps) {
var promises = []; var promises = [];
// Get hostids from names // Get hostids from names
if (template.host && template.host != '*') { if (hosts && hosts != '*') {
promises.push(this.findZabbixHost(template.host)); promises.push(this.findZabbixHost(hosts));
} }
// Get groupids from names // Get groupids from names
else if (template.group && template.group != '*') { else if (groups && groups != '*') {
promises.push(this.findZabbixGroup(template.group)); promises.push(this.findZabbixGroup(groups));
} }
// Get applicationids from names // Get applicationids from names
if (template.app && template.app != '*') { if (apps && apps != '*') {
promises.push(this.findZabbixApp(template.app)); promises.push(this.findZabbixApp(apps));
} }
var self = this; var self = this;
@@ -533,16 +533,16 @@ function (angular, _, kbn) {
}; };
ZabbixAPIDatasource.prototype.appFindQuery = function(template) { ZabbixAPIDatasource.prototype.appFindQuery = function(hosts, groups) {
var promises = []; var promises = [];
// Get hostids from names // Get hostids from names
if (template.host && template.host != '*') { if (hosts && hosts != '*') {
promises.push(this.findZabbixHost(template.host)); promises.push(this.findZabbixHost(hosts));
} }
// Get groupids from names // Get groupids from names
else if (template.group && template.group != '*') { else if (groups && groups != '*') {
promises.push(this.findZabbixGroup(template.group)); promises.push(this.findZabbixGroup(groups));
} }
var self = this; var self = this;
@@ -560,9 +560,9 @@ function (angular, _, kbn) {
}; };
ZabbixAPIDatasource.prototype.hostFindQuery = function(template) { ZabbixAPIDatasource.prototype.hostFindQuery = function(groups) {
var self = this; var self = this;
return this.findZabbixGroup(template.group).then(function (results) { return this.findZabbixGroup(groups).then(function (results) {
results = _.flatten(results); results = _.flatten(results);
var groupids = _.map(_.filter(results, function (object) { var groupids = _.map(_.filter(results, function (object) {
return object.groupid; return object.groupid;
@@ -573,6 +573,11 @@ function (angular, _, kbn) {
}; };
/////////////////
// Annotations //
/////////////////
ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) { ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
var from = Math.ceil(kbn.parseDate(rangeUnparsed.from).getTime() / 1000); var from = Math.ceil(kbn.parseDate(rangeUnparsed.from).getTime() / 1000);
var to = Math.ceil(kbn.parseDate(rangeUnparsed.to).getTime() / 1000); var to = Math.ceil(kbn.parseDate(rangeUnparsed.to).getTime() / 1000);

View File

@@ -150,9 +150,9 @@ function (angular, _) {
$scope.metric.hostList = []; $scope.metric.hostList = [];
addTemplatedVariables($scope.metric.hostList); addTemplatedVariables($scope.metric.hostList);
var groupid = $scope.target.hostGroup ? $scope.target.hostGroup.groupid: null; var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : [];
$scope.datasource.performHostSuggestQuery(groupid).then(function (series) { $scope.datasource.hostFindQuery(groups).then(function (hosts) {
$scope.metric.hostList = $scope.metric.hostList.concat(series); $scope.metric.hostList = $scope.metric.hostList.concat(hosts);
if ($scope.target.host) { if ($scope.target.host) {
$scope.target.host = $scope.metric.hostList.filter(function (item, index, array) { $scope.target.host = $scope.metric.hostList.filter(function (item, index, array) {
@@ -171,10 +171,10 @@ function (angular, _) {
$scope.metric.applicationList = []; $scope.metric.applicationList = [];
addTemplatedVariables($scope.metric.applicationList); addTemplatedVariables($scope.metric.applicationList);
var hostid = $scope.target.host ? $scope.target.host.hostid : null; var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : [];
var groupid = $scope.target.hostGroup ? $scope.target.hostGroup.groupid: null; var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : [];
$scope.datasource.performAppSuggestQuery(hostid, groupid).then(function (series) { $scope.datasource.appFindQuery(hosts, groups).then(function (apps) {
var apps = _.map(_.uniq(_.map(series, 'name')), function (appname) { var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
return {name: appname}; return {name: appname};
}); });
$scope.metric.applicationList = $scope.metric.applicationList.concat(apps); $scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
@@ -196,15 +196,11 @@ function (angular, _) {
$scope.metric.itemList = []; $scope.metric.itemList = [];
addTemplatedVariables($scope.metric.itemList); addTemplatedVariables($scope.metric.itemList);
var groupids = $scope.target.hostGroup ? $scope.target.hostGroup.groupid: null; var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : [];
var hostids = $scope.target.host ? $scope.target.host.hostid : null; var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : [];
var application = $scope.target.application || null; var apps = $scope.target.application ? splitMetrics(templateSrv.replace($scope.target.application.name)) : [];
$scope.datasource.itemFindQuery(groups, hosts, apps).then(function (items) {
// Get application ids from name $scope.metric.itemList = $scope.metric.itemList.concat(items);
$scope.datasource.findZabbixApp(application).then(function (result) {
var applicationids = _.map(result, 'applicationid');
$scope.datasource.performItemSuggestQuery(hostids, applicationids, groupids).then(function (series) {
$scope.metric.itemList = $scope.metric.itemList.concat(series);
// Expand item parameters // Expand item parameters
$scope.metric.itemList.forEach(function (item, index, array) { $scope.metric.itemList.forEach(function (item, index, array) {
@@ -219,7 +215,6 @@ function (angular, _) {
}).pop(); }).pop();
} }
}); });
});
}; };
@@ -276,3 +271,17 @@ function (angular, _) {
}); });
}); });
/**
* Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
*
* @param {string} metrics "{metric1,metcic2,...,metricN}"
* @return {Array} [metric1, metcic2,..., metricN]
*/
function splitMetrics(metrics) {
var remove_brackets_pattern = /^{|}$/g;
var metric_split_pattern = /,(?!\s)/g;
return metrics.replace(remove_brackets_pattern, '').split(metric_split_pattern)
}