Query editor: templated variables support.
This commit is contained in:
@@ -433,7 +433,7 @@ function (angular, _, kbn) {
|
||||
|
||||
// Get items
|
||||
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) {
|
||||
var itemname = expandItemName(item)
|
||||
return {
|
||||
@@ -446,7 +446,7 @@ function (angular, _, kbn) {
|
||||
}
|
||||
// Get applications
|
||||
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 {
|
||||
text: app.name,
|
||||
@@ -457,7 +457,7 @@ function (angular, _, kbn) {
|
||||
}
|
||||
// Get hosts
|
||||
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 {
|
||||
text: host.name,
|
||||
@@ -499,20 +499,20 @@ function (angular, _, kbn) {
|
||||
*
|
||||
* @return {Array} Array of Zabbix API item objects
|
||||
*/
|
||||
ZabbixAPIDatasource.prototype.itemFindQuery = function(template) {
|
||||
ZabbixAPIDatasource.prototype.itemFindQuery = function(groups, hosts, apps) {
|
||||
var promises = [];
|
||||
|
||||
// Get hostids from names
|
||||
if (template.host && template.host != '*') {
|
||||
promises.push(this.findZabbixHost(template.host));
|
||||
if (hosts && hosts != '*') {
|
||||
promises.push(this.findZabbixHost(hosts));
|
||||
}
|
||||
// Get groupids from names
|
||||
else if (template.group && template.group != '*') {
|
||||
promises.push(this.findZabbixGroup(template.group));
|
||||
else if (groups && groups != '*') {
|
||||
promises.push(this.findZabbixGroup(groups));
|
||||
}
|
||||
// Get applicationids from names
|
||||
if (template.app && template.app != '*') {
|
||||
promises.push(this.findZabbixApp(template.app));
|
||||
if (apps && apps != '*') {
|
||||
promises.push(this.findZabbixApp(apps));
|
||||
}
|
||||
|
||||
var self = this;
|
||||
@@ -533,16 +533,16 @@ function (angular, _, kbn) {
|
||||
};
|
||||
|
||||
|
||||
ZabbixAPIDatasource.prototype.appFindQuery = function(template) {
|
||||
ZabbixAPIDatasource.prototype.appFindQuery = function(hosts, groups) {
|
||||
var promises = [];
|
||||
|
||||
// Get hostids from names
|
||||
if (template.host && template.host != '*') {
|
||||
promises.push(this.findZabbixHost(template.host));
|
||||
if (hosts && hosts != '*') {
|
||||
promises.push(this.findZabbixHost(hosts));
|
||||
}
|
||||
// Get groupids from names
|
||||
else if (template.group && template.group != '*') {
|
||||
promises.push(this.findZabbixGroup(template.group));
|
||||
else if (groups && groups != '*') {
|
||||
promises.push(this.findZabbixGroup(groups));
|
||||
}
|
||||
|
||||
var self = this;
|
||||
@@ -560,9 +560,9 @@ function (angular, _, kbn) {
|
||||
};
|
||||
|
||||
|
||||
ZabbixAPIDatasource.prototype.hostFindQuery = function(template) {
|
||||
ZabbixAPIDatasource.prototype.hostFindQuery = function(groups) {
|
||||
var self = this;
|
||||
return this.findZabbixGroup(template.group).then(function (results) {
|
||||
return this.findZabbixGroup(groups).then(function (results) {
|
||||
results = _.flatten(results);
|
||||
var groupids = _.map(_.filter(results, function (object) {
|
||||
return object.groupid;
|
||||
@@ -573,6 +573,11 @@ function (angular, _, kbn) {
|
||||
};
|
||||
|
||||
|
||||
/////////////////
|
||||
// Annotations //
|
||||
/////////////////
|
||||
|
||||
|
||||
ZabbixAPIDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
|
||||
var from = Math.ceil(kbn.parseDate(rangeUnparsed.from).getTime() / 1000);
|
||||
var to = Math.ceil(kbn.parseDate(rangeUnparsed.to).getTime() / 1000);
|
||||
|
||||
@@ -150,9 +150,9 @@ function (angular, _) {
|
||||
$scope.metric.hostList = [];
|
||||
addTemplatedVariables($scope.metric.hostList);
|
||||
|
||||
var groupid = $scope.target.hostGroup ? $scope.target.hostGroup.groupid: null;
|
||||
$scope.datasource.performHostSuggestQuery(groupid).then(function (series) {
|
||||
$scope.metric.hostList = $scope.metric.hostList.concat(series);
|
||||
var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : [];
|
||||
$scope.datasource.hostFindQuery(groups).then(function (hosts) {
|
||||
$scope.metric.hostList = $scope.metric.hostList.concat(hosts);
|
||||
|
||||
if ($scope.target.host) {
|
||||
$scope.target.host = $scope.metric.hostList.filter(function (item, index, array) {
|
||||
@@ -171,10 +171,10 @@ function (angular, _) {
|
||||
$scope.metric.applicationList = [];
|
||||
addTemplatedVariables($scope.metric.applicationList);
|
||||
|
||||
var hostid = $scope.target.host ? $scope.target.host.hostid : null;
|
||||
var groupid = $scope.target.hostGroup ? $scope.target.hostGroup.groupid: null;
|
||||
$scope.datasource.performAppSuggestQuery(hostid, groupid).then(function (series) {
|
||||
var apps = _.map(_.uniq(_.map(series, 'name')), function (appname) {
|
||||
var groups = $scope.target.hostGroup ? splitMetrics(templateSrv.replace($scope.target.hostGroup.name)) : [];
|
||||
var hosts = $scope.target.host ? splitMetrics(templateSrv.replace($scope.target.host.name)) : [];
|
||||
$scope.datasource.appFindQuery(hosts, groups).then(function (apps) {
|
||||
var apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
|
||||
return {name: appname};
|
||||
});
|
||||
$scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
|
||||
@@ -196,29 +196,24 @@ function (angular, _) {
|
||||
$scope.metric.itemList = [];
|
||||
addTemplatedVariables($scope.metric.itemList);
|
||||
|
||||
var groupids = $scope.target.hostGroup ? $scope.target.hostGroup.groupid: null;
|
||||
var hostids = $scope.target.host ? $scope.target.host.hostid : null;
|
||||
var application = $scope.target.application || null;
|
||||
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)) : [];
|
||||
$scope.datasource.itemFindQuery(groups, hosts, apps).then(function (items) {
|
||||
$scope.metric.itemList = $scope.metric.itemList.concat(items);
|
||||
|
||||
// Get application ids from name
|
||||
$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
|
||||
$scope.metric.itemList.forEach(function (item, index, array) {
|
||||
if (item && item.key_ && item.name) {
|
||||
item.name = expandItemName(item);
|
||||
}
|
||||
});
|
||||
if ($scope.target.item) {
|
||||
$scope.target.item = $scope.metric.itemList.filter(function (item, index, array) {
|
||||
// Find selected item in metric.hostList
|
||||
return item.name == $scope.target.item.name;
|
||||
}).pop();
|
||||
// Expand item parameters
|
||||
$scope.metric.itemList.forEach(function (item, index, array) {
|
||||
if (item && item.key_ && item.name) {
|
||||
item.name = expandItemName(item);
|
||||
}
|
||||
});
|
||||
if ($scope.target.item) {
|
||||
$scope.target.item = $scope.metric.itemList.filter(function (item, index, array) {
|
||||
// Find selected item in metric.hostList
|
||||
return item.name == $scope.target.item.name;
|
||||
}).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)
|
||||
}
|
||||
Reference in New Issue
Block a user