Update metric suggestion when template variable was changed.

This commit is contained in:
Alexander Zobnin
2016-04-12 13:29:54 +03:00
parent a6c2774334
commit a9f86f38f2

View File

@@ -12,7 +12,7 @@ import './css/query-editor.css!';
export class ZabbixQueryController extends QueryCtrl { export class ZabbixQueryController extends QueryCtrl {
// ZabbixQueryCtrl constructor // ZabbixQueryCtrl constructor
constructor($scope, $injector, $sce, $q, templateSrv) { constructor($scope, $injector, $rootScope, $sce, $q, templateSrv) {
// Call superclass constructor // Call superclass constructor
super($scope, $injector); super($scope, $injector);
@@ -21,6 +21,10 @@ export class ZabbixQueryController extends QueryCtrl {
this.cache = this.datasource.zabbixCache; this.cache = this.datasource.zabbixCache;
this.$q = $q; this.$q = $q;
// Use custom format for template variables
this.replaceTemplateVars = this.datasource.replaceTemplateVars;
this.templateSrv = templateSrv;
this.editorModes = { this.editorModes = {
0: 'num', 0: 'num',
1: 'itservice', 1: 'itservice',
@@ -33,9 +37,10 @@ export class ZabbixQueryController extends QueryCtrl {
this.getApplicationNames = _.partial(getMetricNames, this, 'appList'); this.getApplicationNames = _.partial(getMetricNames, this, 'appList');
this.getItemNames = _.partial(getMetricNames, this, 'itemList'); this.getItemNames = _.partial(getMetricNames, this, 'itemList');
this.init = function() { // Update metric suggestion when template variable was changed
$rootScope.$on('template-variable-value-updated', () => this.onVariableChange());
this.templateSrv = templateSrv; this.init = function() {
var target = this.target; var target = this.target;
// Migrate old targets // Migrate old targets
@@ -110,7 +115,8 @@ export class ZabbixQueryController extends QueryCtrl {
suggestHosts() { suggestHosts() {
var self = this; var self = this;
var groupFilter = this.templateSrv.replace(this.target.group.filter); var groupFilter = this.replaceTemplateVars(this.target.group.filter);
console.log(groupFilter);
return this.datasource.queryProcessor return this.datasource.queryProcessor
.filterGroups(self.metric.groupList, groupFilter) .filterGroups(self.metric.groupList, groupFilter)
.then(groups => { .then(groups => {
@@ -126,7 +132,7 @@ export class ZabbixQueryController extends QueryCtrl {
suggestApps() { suggestApps() {
var self = this; var self = this;
var hostFilter = this.templateSrv.replace(this.target.host.filter); var hostFilter = this.replaceTemplateVars(this.target.host.filter);
return this.datasource.queryProcessor return this.datasource.queryProcessor
.filterHosts(self.metric.hostList, hostFilter) .filterHosts(self.metric.hostList, hostFilter)
.then(hosts => { .then(hosts => {
@@ -141,7 +147,7 @@ export class ZabbixQueryController extends QueryCtrl {
suggestItems(itemtype='num') { suggestItems(itemtype='num') {
var self = this; var self = this;
var appFilter = this.templateSrv.replace(this.target.application.filter); var appFilter = this.replaceTemplateVars(this.target.application.filter);
if (appFilter) { if (appFilter) {
// Filter by applications // Filter by applications
return this.datasource.queryProcessor return this.datasource.queryProcessor
@@ -199,12 +205,28 @@ export class ZabbixQueryController extends QueryCtrl {
var newTarget = _.cloneDeep(this.target); var newTarget = _.cloneDeep(this.target);
if (!_.isEqual(this.oldTarget, this.target)) { if (!_.isEqual(this.oldTarget, this.target)) {
this.oldTarget = newTarget; this.oldTarget = newTarget;
this.initFilters(); this.targetChanged();
this.parseTarget();
this.panelCtrl.refresh();
} }
} }
onVariableChange() {
if (this.isContainsVariables()) {
this.targetChanged();
}
}
/**
* Check query for template variables
*/
isContainsVariables() {
var self = this;
return _.some(self.templateSrv.variables, variable => {
return _.some(['group', 'host', 'application', 'item'], field => {
return self.templateSrv.containsVariable(self.target[field].filter, variable.name);
});
});
}
parseTarget() { parseTarget() {
// Parse target // Parse target
} }
@@ -215,6 +237,8 @@ export class ZabbixQueryController extends QueryCtrl {
} }
targetChanged() { targetChanged() {
this.initFilters();
this.parseTarget();
this.panelCtrl.refresh(); this.panelCtrl.refresh();
} }