triggers panel multi-ds supoort: refactor
This commit is contained in:
@@ -2,28 +2,20 @@ import _ from 'lodash';
|
||||
import * as utils from '../datasource-zabbix/utils';
|
||||
import './datasource-selector.directive';
|
||||
import '../datasource-zabbix/css/query-editor.css!';
|
||||
|
||||
const ZABBIX_DS_ID = 'alexanderzobnin-zabbix-datasource';
|
||||
const DEFAULT_TARGET = {
|
||||
group: {filter: ""},
|
||||
host: {filter: ""},
|
||||
application: {filter: ""},
|
||||
trigger: {filter: ""}
|
||||
};
|
||||
import {DEFAULT_TARGET} from './module';
|
||||
|
||||
class TriggersTabCtrl {
|
||||
|
||||
/** @ngInject */
|
||||
constructor($scope, $rootScope, uiSegmentSrv, datasourceSrv, templateSrv) {
|
||||
constructor($scope, $rootScope, uiSegmentSrv, templateSrv) {
|
||||
$scope.editor = this;
|
||||
this.panelCtrl = $scope.ctrl;
|
||||
this.panel = this.panelCtrl.panel;
|
||||
this.datasourceSrv = datasourceSrv;
|
||||
this.templateSrv = templateSrv;
|
||||
this.datasources = this.panelCtrl.datasources;
|
||||
|
||||
// Load scope defaults
|
||||
var scopeDefaults = {
|
||||
datasources: {},
|
||||
getGroupNames: {},
|
||||
getHostNames: {},
|
||||
getApplicationNames: {},
|
||||
@@ -31,58 +23,48 @@ class TriggersTabCtrl {
|
||||
};
|
||||
_.defaultsDeep(this, scopeDefaults);
|
||||
|
||||
this.available_datasources = _.map(this.getZabbixDataSources(), 'name');
|
||||
if (!this.panel.datasource) {
|
||||
this.panel.datasource = this.available_datasources[0];
|
||||
}
|
||||
|
||||
this.initDatasources();
|
||||
this.panelCtrl.refresh();
|
||||
}
|
||||
|
||||
initDatasources() {
|
||||
_.each(this.panel.datasources, (ds) => {
|
||||
// Load datasource
|
||||
this.datasourceSrv.get(ds)
|
||||
.then(datasource => {
|
||||
this.panelCtrl.datasources[ds] = datasource;
|
||||
this.datasources[ds] = datasource;
|
||||
|
||||
// Map functions for bs-typeahead
|
||||
this.getGroupNames[ds] = _.bind(this.suggestGroups, this, datasource);
|
||||
this.getHostNames[ds] = _.bind(this.suggestHosts, this, datasource);
|
||||
this.getApplicationNames[ds] = _.bind(this.suggestApps, this, datasource);
|
||||
return this.panelCtrl.initDatasources()
|
||||
.then((datasources) => {
|
||||
_.each(datasources, (datasource) => {
|
||||
this.bindSuggestionFunctions(datasource);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getZabbixDataSources() {
|
||||
return _.filter(this.datasourceSrv.getMetricSources(), datasource => {
|
||||
return datasource.meta.id === ZABBIX_DS_ID && datasource.value;
|
||||
});
|
||||
bindSuggestionFunctions(datasource) {
|
||||
// Map functions for bs-typeahead
|
||||
let ds = datasource.name;
|
||||
this.getGroupNames[ds] = _.bind(this.suggestGroups, this, datasource);
|
||||
this.getHostNames[ds] = _.bind(this.suggestHosts, this, datasource);
|
||||
this.getApplicationNames[ds] = _.bind(this.suggestApps, this, datasource);
|
||||
}
|
||||
|
||||
suggestGroups(ds, query, callback) {
|
||||
return ds.zabbix.getAllGroups()
|
||||
suggestGroups(datasource, query, callback) {
|
||||
return datasource.zabbix.getAllGroups()
|
||||
.then(groups => {
|
||||
return _.map(groups, 'name');
|
||||
})
|
||||
.then(callback);
|
||||
}
|
||||
|
||||
suggestHosts(ds, query, callback) {
|
||||
let groupFilter = ds.replaceTemplateVars(this.panel.targets[ds.name].group.filter);
|
||||
return ds.zabbix.getAllHosts(groupFilter)
|
||||
suggestHosts(datasource, query, callback) {
|
||||
let groupFilter = datasource.replaceTemplateVars(this.panel.targets[datasource.name].group.filter);
|
||||
return datasource.zabbix.getAllHosts(groupFilter)
|
||||
.then(hosts => {
|
||||
return _.map(hosts, 'name');
|
||||
})
|
||||
.then(callback);
|
||||
}
|
||||
|
||||
suggestApps(ds, query, callback) {
|
||||
let groupFilter = ds.replaceTemplateVars(this.panel.targets[ds.name].group.filter);
|
||||
let hostFilter = ds.replaceTemplateVars(this.panel.targets[ds.name].host.filter);
|
||||
return ds.zabbix.getAllApps(groupFilter, hostFilter)
|
||||
suggestApps(datasource, query, callback) {
|
||||
let groupFilter = datasource.replaceTemplateVars(this.panel.targets[datasource.name].group.filter);
|
||||
let hostFilter = datasource.replaceTemplateVars(this.panel.targets[datasource.name].host.filter);
|
||||
return datasource.zabbix.getAllApps(groupFilter, hostFilter)
|
||||
.then(apps => {
|
||||
return _.map(apps, 'name');
|
||||
})
|
||||
@@ -99,12 +81,14 @@ class TriggersTabCtrl {
|
||||
}
|
||||
|
||||
parseTarget() {
|
||||
this.initDatasources();
|
||||
var newTarget = _.cloneDeep(this.panel.targets);
|
||||
if (!_.isEqual(this.oldTarget, newTarget)) {
|
||||
this.oldTarget = newTarget;
|
||||
}
|
||||
this.panelCtrl.refresh();
|
||||
this.initDatasources()
|
||||
.then(() => {
|
||||
var newTarget = _.cloneDeep(this.panel.targets);
|
||||
if (!_.isEqual(this.oldTarget, newTarget)) {
|
||||
this.oldTarget = newTarget;
|
||||
}
|
||||
this.panelCtrl.refresh();
|
||||
});
|
||||
}
|
||||
|
||||
isRegex(str) {
|
||||
|
||||
Reference in New Issue
Block a user