triggers panel multi-ds supoort: refactor

This commit is contained in:
Alexander Zobnin
2017-12-02 22:32:50 +03:00
parent 51c2185cde
commit e0064da209
11 changed files with 279 additions and 184 deletions

View File

@@ -3,6 +3,23 @@
System.register([], function (_export, _context) {
"use strict";
function migratePanelSchema(panel) {
var schemaVersion = getSchemaVersion(panel);
switch (schemaVersion) {
case 1:
panel.datasources = [panel.datasource];
panel.targets[panel.datasources[0]] = panel.triggers;
break;
}
return panel;
}
_export("migratePanelSchema", migratePanelSchema);
function getSchemaVersion(panel) {
return panel.schemaVersion || 1;
}
return {
setters: [],
execute: function () {}

View File

@@ -1 +1 @@
{"version":3,"sources":[],"names":[],"mappings":"","file":"migrations.js","sourcesContent":[]}
{"version":3,"sources":["../../src/panel-triggers/migrations.js"],"names":["migratePanelSchema","panel","schemaVersion","getSchemaVersion","datasources","datasource","targets","triggers"],"mappings":";;;;;AAAO,WAASA,kBAAT,CAA4BC,KAA5B,EAAmC;AACxC,QAAMC,gBAAgBC,iBAAiBF,KAAjB,CAAtB;AACA,YAAQC,aAAR;AACE,WAAK,CAAL;AACED,cAAMG,WAAN,GAAoB,CAACH,MAAMI,UAAP,CAApB;AACAJ,cAAMK,OAAN,CAAcL,MAAMG,WAAN,CAAkB,CAAlB,CAAd,IAAsCH,MAAMM,QAA5C;AACA;AAJJ;;AAOA,WAAON,KAAP;AACD;;gCAVeD,kB;;AAYhB,WAASG,gBAAT,CAA0BF,KAA1B,EAAiC;AAC/B,WAAOA,MAAMC,aAAN,IAAuB,CAA9B;AACD","file":"migrations.js","sourcesContent":["export function migratePanelSchema(panel) {\n const schemaVersion = getSchemaVersion(panel);\n switch (schemaVersion) {\n case 1:\n panel.datasources = [panel.datasource];\n panel.targets[panel.datasources[0]] = panel.triggers;\n break;\n }\n\n return panel;\n}\n\nfunction getSchemaVersion(panel) {\n return panel.schemaVersion || 1;\n}\n"]}

View File

@@ -1,9 +1,9 @@
'use strict';
System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource-zabbix/utils', './options_tab', './triggers_tab', './ack-tooltip.directive'], function (_export, _context) {
System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource-zabbix/utils', './options_tab', './triggers_tab', './migrations', './ack-tooltip.directive'], function (_export, _context) {
"use strict";
var _, $, moment, loadPluginCss, utils, PanelCtrl, triggerPanelOptionsTab, triggerPanelTriggersTab, _createClass, defaultSeverity, panelDefaults, triggerStatusMap, defaultTimeFormat, TriggerPanelCtrl;
var _, $, moment, loadPluginCss, utils, PanelCtrl, triggerPanelOptionsTab, triggerPanelTriggersTab, migratePanelSchema, _createClass, ZABBIX_DS_ID, DEFAULT_TARGET, defaultSeverity, panelDefaults, triggerStatusMap, defaultTimeFormat, TriggerPanelCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
@@ -63,6 +63,8 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
triggerPanelOptionsTab = _options_tab.triggerPanelOptionsTab;
}, function (_triggers_tab) {
triggerPanelTriggersTab = _triggers_tab.triggerPanelTriggersTab;
}, function (_migrations) {
migratePanelSchema = _migrations.migratePanelSchema;
}, function (_ackTooltipDirective) {}],
execute: function () {
_createClass = function () {
@@ -101,6 +103,17 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
light: 'plugins/alexanderzobnin-zabbix-app/css/grafana-zabbix.light.css'
});
ZABBIX_DS_ID = 'alexanderzobnin-zabbix-datasource';
_export('DEFAULT_TARGET', DEFAULT_TARGET = {
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
trigger: { filter: "" }
});
_export('DEFAULT_TARGET', DEFAULT_TARGET);
defaultSeverity = [{ priority: 0, severity: 'Not classified', color: '#B7DBAB', show: true }, { priority: 1, severity: 'Information', color: '#82B5D8', show: true }, { priority: 2, severity: 'Warning', color: '#E5AC0E', show: true }, { priority: 3, severity: 'Average', color: '#C15C17', show: true }, { priority: 4, severity: 'High', color: '#BF1B00', show: true }, { priority: 5, severity: 'Disaster', color: '#890F02', show: true }];
panelDefaults = {
datasources: [],
@@ -150,6 +163,7 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
_this.contextSrv = contextSrv;
_this.dashboardSrv = dashboardSrv;
_this.editorTabIndex = 1;
_this.triggerStatusMap = triggerStatusMap;
_this.defaultTimeFormat = defaultTimeFormat;
_this.pageIndex = 0;
@@ -157,12 +171,16 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
_this.currentTriggersPage = [];
_this.datasources = {};
_this.migratePanelConfig();
// Load panel defaults
// _.cloneDeep() need for prevent changing shared defaultSeverity.
// Load object "by value" istead "by reference".
_.defaults(_this.panel, _.cloneDeep(panelDefaults));
_this.panel = migratePanelSchema(_this.panel);
_this.available_datasources = _.map(_this.getZabbixDataSources(), 'name');
if (_this.panel.datasources.length === 0) {
_this.panel.datasources.push(_this.available_datasources[0]);
}
if (_.isEmpty(_this.panel.targets)) {
_this.panel.targets[_this.panel.datasources[0]] = DEFAULT_TARGET;
}
_this.initDatasources();
_this.events.on('init-edit-mode', _this.onInitEditMode.bind(_this));
@@ -175,19 +193,37 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
value: function initDatasources() {
var _this2 = this;
_.each(this.panel.datasources, function (ds) {
var promises = _.map(this.panel.datasources, function (ds) {
// Load datasource
_this2.datasourceSrv.get(ds).then(function (datasource) {
return _this2.datasourceSrv.get(ds).then(function (datasource) {
_this2.datasources[ds] = datasource;
_this2.datasources[ds].queryBuilder = datasource.queryBuilder;
return datasource;
});
});
return Promise.all(promises);
}
}, {
key: 'getZabbixDataSources',
value: function getZabbixDataSources() {
return _.filter(this.datasourceSrv.getMetricSources(), function (datasource) {
return datasource.meta.id === ZABBIX_DS_ID && datasource.value;
});
}
}, {
key: 'onInitEditMode',
value: function onInitEditMode() {
this.addEditorTab('Triggers', triggerPanelTriggersTab, 2);
this.addEditorTab('Options', triggerPanelOptionsTab, 3);
this.addEditorTab('Triggers', triggerPanelTriggersTab, 1);
this.addEditorTab('Options', triggerPanelOptionsTab, 2);
}
}, {
key: 'setTimeQueryStart',
value: function setTimeQueryStart() {
this.timing.queryStart = new Date().getTime();
}
}, {
key: 'setTimeQueryEnd',
value: function setTimeQueryEnd() {
this.timing.queryEnd = new Date().getTime();
}
}, {
key: 'onRefresh',
@@ -202,34 +238,40 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
// clear loading/error state
delete this.error;
this.loading = true;
this.setTimeQueryStart();
return this.refreshData().then(function (triggerList) {
// Limit triggers number
_this3.triggerList = triggerList.slice(0, _this3.panel.limit);
_this3.getCurrentTriggersPage();
return this.getTriggers().then(function (triggerList) {
// Notify panel that request is finished
_this3.loading = false;
_this3.setTimeQueryEnd();
// Limit triggers number
_this3.triggerList = triggerList.slice(0, _this3.panel.limit);
_this3.getCurrentTriggersPage();
_this3.render(_this3.triggerList);
}).catch(function (err) {
// if cancelled keep loading set to true
if (err.cancelled) {
console.log('Panel request cancelled', err);
return;
}
_this3.loading = false;
_this3.error = err.message || "Request Error";
if (err.data) {
if (err.data.message) {
_this3.error = err.data.message;
}
if (err.data.error) {
_this3.error = err.data.error;
}
}
_this3.events.emit('data-error', err);
console.log('Panel data error:', err);
});
}
}, {
key: 'refreshData',
value: function refreshData() {
return this.getTriggers();
}
}, {
key: 'migratePanelConfig',
value: function migratePanelConfig() {
if (!this.panel.datasources || this.panel.datasource && !this.panel.datasources.length) {
this.panel.datasources = [this.panel.datasource];
this.panel.targets[this.panel.datasource] = this.panel.triggers;
} else if (_.isEmpty(this.panel.targets)) {
this.panel.targets[this.panel.datasources[0]] = this.panel.triggers;
}
}
}, {
key: 'getTriggers',
value: function getTriggers() {
@@ -238,8 +280,6 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
var promises = _.map(this.panel.datasources, function (ds) {
return _this4.datasourceSrv.get(ds).then(function (datasource) {
var zabbix = datasource.zabbix;
_this4.zabbix = zabbix;
_this4.datasource = datasource;
var showEvents = _this4.panel.showEvents.value;
var triggerFilter = _this4.panel.targets[ds];
var hideHostsInMaintenance = _this4.panel.hideHostsInMaintenance;
@@ -264,6 +304,8 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
return Promise.all(promises).then(function (results) {
return _.flatten(results);
}).then(function (triggers) {
return _this4.sortTriggers(triggers);
}).then(function (triggers) {
return _.map(triggers, _this4.formatTrigger.bind(_this4));
});
@@ -336,13 +378,16 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
return _this6.panel.triggerSeverity[trigger.priority].show;
});
// Sort triggers
return triggerList;
}
}, {
key: 'sortTriggers',
value: function sortTriggers(triggerList) {
if (this.panel.sortTriggersBy.value === 'priority') {
triggerList = _.sortBy(triggerList, 'priority').reverse();
} else {
triggerList = _.sortBy(triggerList, 'lastchangeUnix').reverse();
}
return triggerList;
}
}, {
@@ -431,8 +476,9 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
var endPos = Math.min(startPos + pageSize, ctrl.triggerList.length);
ctrl.currentTriggersPage = ctrl.triggerList.slice(startPos, endPos);
scope.$apply();
renderPanel();
scope.$apply(function () {
renderPanel();
});
}
function appendPaginationControls(footerElem) {
@@ -468,6 +514,7 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
appendPaginationControls(footerElem);
rootElem.css({ 'max-height': panel.scroll ? getTableHeight() : '' });
ctrl.renderingCompleted();
}
elem.on('click', '.triggers-panel-page-link', switchPage);
@@ -480,9 +527,10 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
ctrl.events.on('render', function (renderData) {
data = renderData || data;
if (data) {
renderPanel();
scope.$apply(function () {
renderPanel();
});
}
ctrl.renderingCompleted();
});
}
}]);

File diff suppressed because one or more lines are too long

View File

@@ -2,12 +2,12 @@
<div class="section gf-form-group">
<div class="gf-form-inline">
<div class="gf-form">
<label class="gf-form-label width-9">Data sources</label>
<label class="gf-form-label width-7">Data sources</label>
</div>
<div class="gf-form">
<datasource-selector
datasources="editor.panel.datasources"
options="editor.available_datasources"
options="editor.panelCtrl.available_datasources"
on-change="editor.datasourcesChanged()">
</datasource-selector>
</div>

View File

@@ -1,9 +1,9 @@
'use strict';
System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.directive', '../datasource-zabbix/css/query-editor.css!'], function (_export, _context) {
System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.directive', '../datasource-zabbix/css/query-editor.css!', './module'], function (_export, _context) {
"use strict";
var _, utils, _createClass, ZABBIX_DS_ID, DEFAULT_TARGET, TriggersTabCtrl;
var _, utils, DEFAULT_TARGET, _createClass, TriggersTabCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
@@ -27,7 +27,9 @@ System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.
_ = _lodash.default;
}, function (_datasourceZabbixUtils) {
utils = _datasourceZabbixUtils;
}, function (_datasourceSelectorDirective) {}, function (_datasourceZabbixCssQueryEditorCss) {}],
}, function (_datasourceSelectorDirective) {}, function (_datasourceZabbixCssQueryEditorCss) {}, function (_module) {
DEFAULT_TARGET = _module.DEFAULT_TARGET;
}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {
@@ -47,29 +49,20 @@ System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.
};
}();
ZABBIX_DS_ID = 'alexanderzobnin-zabbix-datasource';
DEFAULT_TARGET = {
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
trigger: { filter: "" }
};
TriggersTabCtrl = function () {
/** @ngInject */
function TriggersTabCtrl($scope, $rootScope, uiSegmentSrv, datasourceSrv, templateSrv) {
function TriggersTabCtrl($scope, $rootScope, uiSegmentSrv, templateSrv) {
_classCallCheck(this, TriggersTabCtrl);
$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: {},
@@ -77,11 +70,6 @@ System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.
};
_.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();
}
@@ -91,47 +79,42 @@ System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.
value: function initDatasources() {
var _this = this;
_.each(this.panel.datasources, function (ds) {
// Load datasource
_this.datasourceSrv.get(ds).then(function (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(function (datasources) {
_.each(datasources, function (datasource) {
_this.bindSuggestionFunctions(datasource);
});
});
}
}, {
key: 'getZabbixDataSources',
value: function getZabbixDataSources() {
return _.filter(this.datasourceSrv.getMetricSources(), function (datasource) {
return datasource.meta.id === ZABBIX_DS_ID && datasource.value;
});
key: 'bindSuggestionFunctions',
value: function bindSuggestionFunctions(datasource) {
// Map functions for bs-typeahead
var 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);
}
}, {
key: 'suggestGroups',
value: function suggestGroups(ds, query, callback) {
return ds.zabbix.getAllGroups().then(function (groups) {
value: function suggestGroups(datasource, query, callback) {
return datasource.zabbix.getAllGroups().then(function (groups) {
return _.map(groups, 'name');
}).then(callback);
}
}, {
key: 'suggestHosts',
value: function suggestHosts(ds, query, callback) {
var groupFilter = ds.replaceTemplateVars(this.panel.targets[ds.name].group.filter);
return ds.zabbix.getAllHosts(groupFilter).then(function (hosts) {
value: function suggestHosts(datasource, query, callback) {
var groupFilter = datasource.replaceTemplateVars(this.panel.targets[datasource.name].group.filter);
return datasource.zabbix.getAllHosts(groupFilter).then(function (hosts) {
return _.map(hosts, 'name');
}).then(callback);
}
}, {
key: 'suggestApps',
value: function suggestApps(ds, query, callback) {
var groupFilter = ds.replaceTemplateVars(this.panel.targets[ds.name].group.filter);
var hostFilter = ds.replaceTemplateVars(this.panel.targets[ds.name].host.filter);
return ds.zabbix.getAllApps(groupFilter, hostFilter).then(function (apps) {
value: function suggestApps(datasource, query, callback) {
var groupFilter = datasource.replaceTemplateVars(this.panel.targets[datasource.name].group.filter);
var hostFilter = datasource.replaceTemplateVars(this.panel.targets[datasource.name].host.filter);
return datasource.zabbix.getAllApps(groupFilter, hostFilter).then(function (apps) {
return _.map(apps, 'name');
}).then(callback);
}
@@ -150,12 +133,15 @@ System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.
}, {
key: 'parseTarget',
value: function parseTarget() {
this.initDatasources();
var newTarget = _.cloneDeep(this.panel.targets);
if (!_.isEqual(this.oldTarget, newTarget)) {
this.oldTarget = newTarget;
}
this.panelCtrl.refresh();
var _this3 = this;
this.initDatasources().then(function () {
var newTarget = _.cloneDeep(_this3.panel.targets);
if (!_.isEqual(_this3.oldTarget, newTarget)) {
_this3.oldTarget = newTarget;
}
_this3.panelCtrl.refresh();
});
}
}, {
key: 'isRegex',

File diff suppressed because one or more lines are too long