Fix query editor for work with templated variables.

This commit is contained in:
Alexander Zobnin
2015-05-30 20:54:03 +03:00
parent 81f10ea6a9
commit 46c9fa90be
3 changed files with 41 additions and 74 deletions

View File

@@ -49,7 +49,7 @@ function (angular, _, kbn) {
name: target.alias name: target.alias
}]; }];
return this.performTimeSeriesQuery(item, from, to).then(_.partial( return this.performTimeSeriesQuery(item, from, to).then(_.partial(
this.handleZabbixAPIResponse, alias)); this.handleHistoryResponse, alias));
} else { } else {
// Handle templated target // Handle templated target
@@ -73,13 +73,14 @@ function (angular, _, kbn) {
var self = this; var self = this;
return $q.all(_.map(hosts, function (hostname) { return $q.all(_.map(hosts, function (hostname) {
if (hosts.length > 1) { if (hosts.length > 1) {
// Select the host that the item belongs for multiple hosts request
var selectHosts = true; var selectHosts = true;
} }
return this.findZabbixItem(hostname, itemnames, selectHosts); return this.findZabbixItem(hostname, itemnames, selectHosts);
}, this)).then(function (items) { }, this)).then(function (items) {
items = _.flatten(items); items = _.flatten(items);
return self.performTimeSeriesQuery(items, from, to) return self.performTimeSeriesQuery(items, from, to)
.then(_.partial(self.handleZabbixAPIResponse, items)); .then(_.partial(self.handleHistoryResponse, items));
}); });
} else { } else {
return []; return [];
@@ -127,7 +128,7 @@ function (angular, _, kbn) {
// Convert Zabbix API data to Grafana format // Convert Zabbix API data to Grafana format
ZabbixAPIDatasource.prototype.handleZabbixAPIResponse = function(items, response) { ZabbixAPIDatasource.prototype.handleHistoryResponse = function(items, response) {
/** /**
* Response should be in the format: * Response should be in the format:
* data: [ * data: [

View File

@@ -59,7 +59,7 @@
ng-change="selectHostGroup()" ng-change="selectHostGroup()"
ng-model="target.hostGroup" ng-model="target.hostGroup"
bs-tooltip="target.hostGroup.name.length > 25 ? target.hostGroup.name : ''" bs-tooltip="target.hostGroup.name.length > 25 ? target.hostGroup.name : ''"
ng-options="hostgroup.name for hostgroup in metric.hostGroupList" > ng-options="hostgroup.name for hostgroup in metric.groupList" >
<option value="">All</option> <option value="">All</option>
</select> </select>
<a bs-tooltip="target.errors.metric" <a bs-tooltip="target.errors.metric"

View File

@@ -20,20 +20,9 @@ function (angular, _) {
}; };
// Update host group, host, application and item lists // Update host group, host, application and item lists
$scope.updateHostGroupList(); $scope.updateGroupList();
if ($scope.target.hostGroup) { $scope.updateHostList()
$scope.updateHostList($scope.target.hostGroup.groupid); $scope.updateItemList();
} else {
$scope.updateHostList();
}
if ($scope.target.host) {
$scope.updateAppList($scope.target.host.hostid);
if ($scope.target.application) {
$scope.updateItemList($scope.target.host.hostid, $scope.target.application.applicationid);
} else {
$scope.updateItemList($scope.target.host.hostid, null);
}
}
setItemAlias(); setItemAlias();
@@ -58,13 +47,8 @@ function (angular, _) {
// Call when host group selected // Call when host group selected
$scope.selectHostGroup = function() { $scope.selectHostGroup = function() {
$scope.updateHostList()
// Update host list $scope.updateAppList();
if ($scope.target.hostGroup) {
$scope.updateHostList($scope.target.hostGroup.groupid);
} else {
$scope.updateHostList('');
}
$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)) {
@@ -75,17 +59,8 @@ function (angular, _) {
// Call when host selected // Call when host selected
$scope.selectHost = function() { $scope.selectHost = function() {
if ($scope.target.host) { $scope.updateItemList();
// Update item list $scope.updateAppList();
if ($scope.target.application) {
$scope.updateItemList($scope.target.host.hostid, $scope.target.application.applicationid);
} else {
$scope.updateItemList($scope.target.host.hostid, null);
}
// Update application list
$scope.updateAppList($scope.target.host.hostid);
}
$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)) {
@@ -97,13 +72,7 @@ function (angular, _) {
// Call when application selected // Call when application selected
$scope.selectApplication = function() { $scope.selectApplication = function() {
$scope.updateItemList();
// Update item list
if ($scope.target.application) {
$scope.updateItemList($scope.target.host.hostid, $scope.target.application.applicationid);
} else {
$scope.updateItemList($scope.target.host.hostid, null);
}
$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)) {
@@ -142,15 +111,15 @@ function (angular, _) {
/** /**
* Update list of host groups * Update list of host groups
*/ */
$scope.updateHostGroupList = function() { $scope.updateGroupList = function() {
$scope.metric.hostGroupList = []; $scope.metric.groupList = [];
addTemplatedVariables($scope.metric.hostGroupList); addTemplatedVariables($scope.metric.groupList);
$scope.datasource.performHostGroupSuggestQuery().then(function (series) { $scope.datasource.performHostGroupSuggestQuery().then(function (series) {
$scope.metric.hostGroupList = $scope.metric.hostGroupList.concat(series); $scope.metric.groupList = $scope.metric.groupList.concat(series);
if ($scope.target.hostGroup) { if ($scope.target.hostGroup) {
$scope.target.hostGroup = $scope.metric.hostGroupList.filter(function (item, index, array) { $scope.target.hostGroup = $scope.metric.groupList.filter(function (item, index, array) {
// Find selected host in metric.hostList // Find selected host in metric.hostList
return item.name == $scope.target.hostGroup.name; return item.name == $scope.target.hostGroup.name;
}).pop(); }).pop();
@@ -162,10 +131,11 @@ function (angular, _) {
/** /**
* Update list of hosts * Update list of hosts
*/ */
$scope.updateHostList = function(groupid) { $scope.updateHostList = function() {
$scope.metric.hostList = []; $scope.metric.hostList = [];
addTemplatedVariables($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.datasource.performHostSuggestQuery(groupid).then(function (series) {
$scope.metric.hostList = $scope.metric.hostList.concat(series); $scope.metric.hostList = $scope.metric.hostList.concat(series);
@@ -182,11 +152,13 @@ function (angular, _) {
/** /**
* Update list of host applications * Update list of host applications
*/ */
$scope.updateAppList = function(hostid) { $scope.updateAppList = function() {
$scope.metric.applicationList = []; $scope.metric.applicationList = [];
addTemplatedVariables($scope.metric.applicationList); addTemplatedVariables($scope.metric.applicationList);
$scope.datasource.performAppSuggestQuery(hostid).then(function (series) { 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) {
$scope.metric.applicationList = $scope.metric.applicationList.concat(series); $scope.metric.applicationList = $scope.metric.applicationList.concat(series);
if ($scope.target.application) { if ($scope.target.application) {
@@ -202,36 +174,30 @@ function (angular, _) {
/** /**
* Update list of items * Update list of items
*/ */
$scope.updateItemList = function(hostid, applicationid) { $scope.updateItemList = function() {
$scope.metric.itemList = []; $scope.metric.itemList = [];
addTemplatedVariables($scope.metric.itemList); addTemplatedVariables($scope.metric.itemList);
// Update only if host selected var groupids = $scope.target.hostGroup ? $scope.target.hostGroup.groupid: null;
if (hostid) { var hostids = $scope.target.host ? $scope.target.host.hostid : null;
$scope.datasource.performItemSuggestQuery(hostid, applicationid).then(function (series) { var applicationids = $scope.target.application ? $scope.target.application.applicationid : null;
$scope.metric.itemList = $scope.metric.itemList.concat(series);
// Expand item parameters $scope.datasource.performItemSuggestQuery(hostids, applicationids, groupids).then(function (series) {
$scope.metric.itemList.forEach(function (item, index, array) { $scope.metric.itemList = $scope.metric.itemList.concat(series);
if (item && item.key_ && item.name) {
item.name = expandItemName(item); // Expand item parameters
} $scope.metric.itemList.forEach(function (item, index, array) {
}); if (item && item.key_ && item.name) {
if ($scope.target.item) { item.name = expandItemName(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();
}
}); });
} else {
if ($scope.target.item) { if ($scope.target.item) {
$scope.target.item = $scope.metric.itemList.filter(function (item, index, array) { $scope.target.item = $scope.metric.itemList.filter(function (item, index, array) {
// Find selected item in metric.hostList // Find selected item in metric.hostList
return (item.name == $scope.target.item.name); return item.name == $scope.target.item.name;
}).pop(); }).pop();
}
} }
});
}; };