Fixed Triggers panel editor.

This commit is contained in:
Alexander Zobnin
2016-04-14 13:21:36 +03:00
parent fb24637364
commit 5b6a83e255
2 changed files with 75 additions and 31 deletions

View File

@@ -19,20 +19,23 @@ import '../datasource-zabbix/css/query-editor.css!';
class TriggerPanelEditorCtrl { class TriggerPanelEditorCtrl {
/** @ngInject */ /** @ngInject */
constructor($scope, $q, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) { constructor($scope, $rootScope, $q, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
$scope.editor = this; $scope.editor = this;
this.panelCtrl = $scope.ctrl; this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel; this.panel = this.panelCtrl.panel;
this.$q = $q;
this.datasourceSrv = datasourceSrv; this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv; this.templateSrv = templateSrv;
this.popoverSrv = popoverSrv; this.popoverSrv = popoverSrv;
// Map functions for bs-typeahead // Map functions for bs-typeahead
this.getGroupNames = _.partial(getMetricNames, this, 'groupList'); this.getGroupNames = _.partial(getMetricNames, this, 'groupList');
this.getHostNames = _.partial(getMetricNames, this, 'filteredHosts'); this.getHostNames = _.partial(getMetricNames, this, 'hostList');
this.getApplicationNames = _.partial(getMetricNames, this, 'filteredApplications'); this.getApplicationNames = _.partial(getMetricNames, this, 'appList');
this.getItemNames = _.partial(getMetricNames, this, 'filteredItems');
// Update metric suggestion when template variable was changed
$rootScope.$on('template-variable-value-updated', () => this.onVariableChange());
this.ackFilters = [ this.ackFilters = [
'all triggers', 'all triggers',
@@ -80,36 +83,77 @@ class TriggerPanelEditorCtrl {
} }
initFilters() { initFilters() {
this.filterGroups(); var self = this;
this.filterHosts(); return this.$q
this.filterApplications(); .when(this.suggestGroups())
.then(() => {return self.suggestHosts();})
.then(() => {return self.suggestApps();});
} }
filterGroups() { suggestGroups() {
var self = this; var self = this;
this.datasource.queryProcessor.getGroups().then(function(groups) { return this.datasource.zabbixCache
self.metric.groupList = groups; .getGroups()
}); .then(groups => {
} self.metric.groupList = groups;
return groups;
filterHosts() {
var self = this;
var groupFilter = this.templateSrv.replace(this.panel.triggers.group.filter);
this.datasource.queryProcessor.getHosts(groupFilter).then(function(hosts) {
self.metric.filteredHosts = hosts;
});
}
filterApplications() {
var self = this;
var groupFilter = this.templateSrv.replace(this.panel.triggers.group.filter);
var hostFilter = this.templateSrv.replace(this.panel.triggers.host.filter);
this.datasource.queryProcessor.getApps(groupFilter, hostFilter)
.then(function(apps) {
self.metric.filteredApplications = apps;
}); });
} }
suggestHosts() {
var self = this;
var groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
return this.datasource.queryProcessor
.filterGroups(self.metric.groupList, groupFilter)
.then(groups => {
var groupids = _.map(groups, 'groupid');
return self.datasource.zabbixAPI
.getHosts(groupids)
.then(hosts => {
self.metric.hostList = hosts;
return hosts;
});
});
}
suggestApps() {
var self = this;
var hostFilter = this.datasource.replaceTemplateVars(this.panel.triggers.host.filter);
return this.datasource.queryProcessor
.filterHosts(self.metric.hostList, hostFilter)
.then(hosts => {
var hostids = _.map(hosts, 'hostid');
return self.datasource.zabbixAPI
.getApps(hostids)
.then(apps => {
return self.metric.appList = apps;
});
});
}
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'], field => {
return self.templateSrv.containsVariable(self.panel.triggers[field].filter, variable.name);
});
});
}
targetChanged() {
this.initFilters();
this.panelCtrl.refresh();
}
parseTarget() { parseTarget() {
this.initFilters(); this.initFilters();
var newTarget = _.cloneDeep(this.panel.triggers); var newTarget = _.cloneDeep(this.panel.triggers);

View File

@@ -110,9 +110,9 @@ class TriggerPanelCtrl extends MetricsPanelCtrl {
var triggerFilter = self.panel.triggers; var triggerFilter = self.panel.triggers;
// Replace template variables // Replace template variables
var groupFilter = self.templateSrv.replace(triggerFilter.group.filter); var groupFilter = datasource.replaceTemplateVars(triggerFilter.group.filter);
var hostFilter = self.templateSrv.replace(triggerFilter.host.filter); var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
var appFilter = self.templateSrv.replace(triggerFilter.application.filter); var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
var buildQuery = queryProcessor.buildTriggerQuery(groupFilter, hostFilter, appFilter); var buildQuery = queryProcessor.buildTriggerQuery(groupFilter, hostFilter, appFilter);
return buildQuery.then(query => { return buildQuery.then(query => {