Multiple data sources support for triggers panel, #431

This commit is contained in:
Alexander Zobnin
2017-11-16 12:25:17 +03:00
parent 18ffee3765
commit 51c2185cde
22 changed files with 880 additions and 652 deletions

View File

@@ -0,0 +1,93 @@
'use strict';
System.register(['angular', 'lodash'], function (_export, _context) {
"use strict";
var angular, _, _createClass, template, DatasourceSelectorCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
return {
setters: [function (_angular) {
angular = _angular.default;
}, function (_lodash) {
_ = _lodash.default;
}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
template = '\n<value-select-dropdown variable="ctrl.dsOptions" on-updated="ctrl.onChange(ctrl.dsOptions)">\n</value-select-dropdown>\n';
angular.module('grafana.directives').directive('datasourceSelector', function () {
return {
scope: {
datasources: "=",
options: "=",
onChange: "&"
},
controller: DatasourceSelectorCtrl,
controllerAs: 'ctrl',
template: template
};
});
DatasourceSelectorCtrl = function () {
/** @ngInject */
function DatasourceSelectorCtrl($scope) {
_classCallCheck(this, DatasourceSelectorCtrl);
this.scope = $scope;
var datasources = $scope.datasources;
var options = $scope.options;
this.dsOptions = {
multi: true,
current: { value: datasources, text: datasources.join(" + ") },
options: _.map(options, function (ds) {
return { text: ds, value: ds, selected: _.includes(datasources, ds) };
})
};
}
_createClass(DatasourceSelectorCtrl, [{
key: 'onChange',
value: function onChange(updatedOptions) {
var _this = this;
var newDataSources = updatedOptions.current.value;
this.scope.datasources = newDataSources;
// Run after model was changed
this.scope.$$postDigest(function () {
_this.scope.onChange();
});
}
}]);
return DatasourceSelectorCtrl;
}();
}
};
});
//# sourceMappingURL=datasource-selector.directive.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../src/panel-triggers/datasource-selector.directive.js"],"names":["angular","_","template","module","directive","scope","datasources","options","onChange","controller","DatasourceSelectorCtrl","controllerAs","$scope","dsOptions","multi","current","value","text","join","map","ds","selected","includes","updatedOptions","newDataSources","$$postDigest"],"mappings":";;;;;;;;;;;;;;;AAAOA,a;;AACAC,O;;;;;;;;;;;;;;;;;;;;;AAEDC,c;;;AAKNF,cACCG,MADD,CACQ,oBADR,EAECC,SAFD,CAEW,oBAFX,EAEiC,YAAM;AACrC,eAAO;AACLC,iBAAO;AACLC,yBAAa,GADR;AAELC,qBAAS,GAFJ;AAGLC,sBAAU;AAHL,WADF;AAMLC,sBAAYC,sBANP;AAOLC,wBAAc,MAPT;AAQLT,oBAAUA;AARL,SAAP;AAUD,OAbD;;AAeMQ,4B;;AAEJ;AACA,wCAAYE,MAAZ,EAAoB;AAAA;;AAClB,eAAKP,KAAL,GAAaO,MAAb;AACA,cAAIN,cAAcM,OAAON,WAAzB;AACA,cAAIC,UAAUK,OAAOL,OAArB;AACA,eAAKM,SAAL,GAAiB;AACfC,mBAAO,IADQ;AAEfC,qBAAS,EAACC,OAAOV,WAAR,EAAqBW,MAAMX,YAAYY,IAAZ,CAAiB,KAAjB,CAA3B,EAFM;AAGfX,qBAASN,EAAEkB,GAAF,CAAMZ,OAAN,EAAe,UAACa,EAAD,EAAQ;AAC9B,qBAAO,EAACH,MAAMG,EAAP,EAAWJ,OAAOI,EAAlB,EAAsBC,UAAUpB,EAAEqB,QAAF,CAAWhB,WAAX,EAAwBc,EAAxB,CAAhC,EAAP;AACD,aAFQ;AAHM,WAAjB;AAOD;;;;mCAEQG,c,EAAgB;AAAA;;AACvB,gBAAIC,iBAAiBD,eAAeR,OAAf,CAAuBC,KAA5C;AACA,iBAAKX,KAAL,CAAWC,WAAX,GAAyBkB,cAAzB;;AAEA;AACA,iBAAKnB,KAAL,CAAWoB,YAAX,CAAwB,YAAM;AAC5B,oBAAKpB,KAAL,CAAWG,QAAX;AACD,aAFD;AAGD","file":"datasource-selector.directive.js","sourcesContent":["import angular from 'angular';\nimport _ from 'lodash';\n\nconst template = `\n<value-select-dropdown variable=\"ctrl.dsOptions\" on-updated=\"ctrl.onChange(ctrl.dsOptions)\">\n</value-select-dropdown>\n`;\n\nangular\n.module('grafana.directives')\n.directive('datasourceSelector', () => {\n return {\n scope: {\n datasources: \"=\",\n options: \"=\",\n onChange: \"&\"\n },\n controller: DatasourceSelectorCtrl,\n controllerAs: 'ctrl',\n template: template\n };\n});\n\nclass DatasourceSelectorCtrl {\n\n /** @ngInject */\n constructor($scope) {\n this.scope = $scope;\n let datasources = $scope.datasources;\n let options = $scope.options;\n this.dsOptions = {\n multi: true,\n current: {value: datasources, text: datasources.join(\" + \")},\n options: _.map(options, (ds) => {\n return {text: ds, value: ds, selected: _.includes(datasources, ds)};\n })\n };\n }\n\n onChange(updatedOptions) {\n let newDataSources = updatedOptions.current.value;\n this.scope.datasources = newDataSources;\n\n // Run after model was changed\n this.scope.$$postDigest(() => {\n this.scope.onChange();\n });\n }\n}\n"]}

View File

@@ -1,224 +0,0 @@
'use strict';
System.register(['lodash', '../datasource-zabbix/utils', '../datasource-zabbix/css/query-editor.css!'], function (_export, _context) {
"use strict";
var _, utils, _createClass, TriggerPanelEditorCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _.uniq(_.map(scope.metric[metricList], 'name'));
}
function triggerPanelEditor() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/alexanderzobnin-zabbix-app/panel-triggers/editor.html',
controller: TriggerPanelEditorCtrl
};
}
_export('triggerPanelEditor', triggerPanelEditor);
return {
setters: [function (_lodash) {
_ = _lodash.default;
}, function (_datasourceZabbixUtils) {
utils = _datasourceZabbixUtils;
}, function (_datasourceZabbixCssQueryEditorCss) {}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
TriggerPanelEditorCtrl = function () {
/** @ngInject */
function TriggerPanelEditorCtrl($scope, $rootScope, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
var _this = this;
_classCallCheck(this, TriggerPanelEditorCtrl);
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
this.popoverSrv = popoverSrv;
// Map functions for bs-typeahead
this.getGroupNames = _.partial(getMetricNames, this, 'groupList');
this.getHostNames = _.partial(getMetricNames, this, 'hostList');
this.getApplicationNames = _.partial(getMetricNames, this, 'appList');
// Update metric suggestion when template variable was changed
$rootScope.$on('template-variable-value-updated', function () {
return _this.onVariableChange();
});
this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
this.ackFilters = ['all triggers', 'unacknowledged', 'acknowledged'];
this.sortByFields = [{ text: 'last change', value: 'lastchange' }, { text: 'severity', value: 'priority' }];
this.showEventsFields = [{ text: 'All', value: [0, 1] }, { text: 'OK', value: [0] }, { text: 'Problems', value: 1 }];
// Load scope defaults
var scopeDefaults = {
metric: {},
inputStyles: {},
oldTarget: _.cloneDeep(this.panel.triggers)
};
_.defaults(this, scopeDefaults);
// Set default datasource
this.datasources = _.map(this.getZabbixDataSources(), 'name');
if (!this.panel.datasource) {
this.panel.datasource = this.datasources[0];
}
// Load datasource
this.datasourceSrv.get(this.panel.datasource).then(function (datasource) {
_this.datasource = datasource;
_this.zabbix = datasource.zabbix;
_this.queryBuilder = datasource.queryBuilder;
_this.initFilters();
_this.panelCtrl.refresh();
});
}
_createClass(TriggerPanelEditorCtrl, [{
key: 'initFilters',
value: function initFilters() {
return Promise.all([this.suggestGroups(), this.suggestHosts(), this.suggestApps()]);
}
}, {
key: 'suggestGroups',
value: function suggestGroups() {
var _this2 = this;
return this.zabbix.getAllGroups().then(function (groups) {
_this2.metric.groupList = groups;
return groups;
});
}
}, {
key: 'suggestHosts',
value: function suggestHosts() {
var _this3 = this;
var groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
return this.zabbix.getAllHosts(groupFilter).then(function (hosts) {
_this3.metric.hostList = hosts;
return hosts;
});
}
}, {
key: 'suggestApps',
value: function suggestApps() {
var _this4 = this;
var groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
var hostFilter = this.datasource.replaceTemplateVars(this.panel.triggers.host.filter);
return this.zabbix.getAllApps(groupFilter, hostFilter).then(function (apps) {
_this4.metric.appList = apps;
return apps;
});
}
}, {
key: 'onVariableChange',
value: function onVariableChange() {
if (this.isContainsVariables()) {
this.targetChanged();
}
}
}, {
key: 'isContainsVariables',
value: function isContainsVariables() {
var _this5 = this;
return _.some(['group', 'host', 'application'], function (field) {
return utils.isTemplateVariable(_this5.panel.triggers[field].filter, _this5.templateSrv.variables);
});
}
}, {
key: 'targetChanged',
value: function targetChanged() {
this.initFilters();
this.panelCtrl.refresh();
}
}, {
key: 'parseTarget',
value: function parseTarget() {
this.initFilters();
var newTarget = _.cloneDeep(this.panel.triggers);
if (!_.isEqual(this.oldTarget, this.panel.triggers)) {
this.oldTarget = newTarget;
this.panelCtrl.refresh();
}
}
}, {
key: 'refreshTriggerSeverity',
value: function refreshTriggerSeverity() {
_.each(this.triggerList, function (trigger) {
trigger.color = this.panel.triggerSeverity[trigger.priority].color;
trigger.severity = this.panel.triggerSeverity[trigger.priority].severity;
});
this.panelCtrl.refresh();
}
}, {
key: 'datasourceChanged',
value: function datasourceChanged() {
this.panelCtrl.refresh();
}
}, {
key: 'changeTriggerSeverityColor',
value: function changeTriggerSeverityColor(trigger, color) {
this.panel.triggerSeverity[trigger.priority].color = color;
this.refreshTriggerSeverity();
}
}, {
key: 'isRegex',
value: function isRegex(str) {
return utils.isRegex(str);
}
}, {
key: 'isVariable',
value: function isVariable(str) {
return utils.isTemplateVariable(str, this.templateSrv.variables);
}
}, {
key: 'getZabbixDataSources',
value: function getZabbixDataSources() {
var ZABBIX_DS_ID = 'alexanderzobnin-zabbix-datasource';
return _.filter(this.datasourceSrv.getMetricSources(), function (datasource) {
return datasource.meta.id === ZABBIX_DS_ID && datasource.value;
});
}
}]);
return TriggerPanelEditorCtrl;
}();
}
};
});
//# sourceMappingURL=editor.js.map

File diff suppressed because one or more lines are too long

11
dist/panel-triggers/migrations.js vendored Normal file
View File

@@ -0,0 +1,11 @@
"use strict";
System.register([], function (_export, _context) {
"use strict";
return {
setters: [],
execute: function () {}
};
});
//# sourceMappingURL=migrations.js.map

1
dist/panel-triggers/migrations.js.map vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"sources":[],"names":[],"mappings":"","file":"migrations.js","sourcesContent":[]}

View File

@@ -1,9 +1,9 @@
'use strict';
System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource-zabbix/utils', './editor', './ack-tooltip.directive'], function (_export, _context) {
System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource-zabbix/utils', './options_tab', './triggers_tab', './ack-tooltip.directive'], function (_export, _context) {
"use strict";
var _, $, moment, loadPluginCss, utils, PanelCtrl, triggerPanelEditor, _createClass, defaultSeverity, panelDefaults, triggerStatusMap, defaultTimeFormat, TriggerPanelCtrl;
var _, $, moment, loadPluginCss, utils, PanelCtrl, triggerPanelOptionsTab, triggerPanelTriggersTab, _createClass, defaultSeverity, panelDefaults, triggerStatusMap, defaultTimeFormat, TriggerPanelCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
@@ -59,8 +59,10 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
PanelCtrl = _appPluginsSdk.PanelCtrl;
}, function (_datasourceZabbixUtils) {
utils = _datasourceZabbixUtils;
}, function (_editor) {
triggerPanelEditor = _editor.triggerPanelEditor;
}, function (_options_tab) {
triggerPanelOptionsTab = _options_tab.triggerPanelOptionsTab;
}, function (_triggers_tab) {
triggerPanelTriggersTab = _triggers_tab.triggerPanelTriggersTab;
}, function (_ackTooltipDirective) {}],
execute: function () {
_createClass = function () {
@@ -101,13 +103,14 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
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 = {
datasource: null,
datasources: [],
triggers: {
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
trigger: { filter: "" }
},
targets: {},
hostField: true,
statusField: false,
severityField: false,
@@ -124,7 +127,8 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
ackEventColor: 'rgba(0, 0, 0, 0)',
scroll: true,
pageSize: 10,
fontSize: '100%'
fontSize: '100%',
schemaVersion: 2
};
triggerStatusMap = {
'0': 'OK',
@@ -151,26 +155,44 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
_this.pageIndex = 0;
_this.triggerList = [];
_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.initDatasources();
_this.events.on('init-edit-mode', _this.onInitEditMode.bind(_this));
_this.events.on('refresh', _this.onRefresh.bind(_this));
return _this;
}
_createClass(TriggerPanelCtrl, [{
key: 'initDatasources',
value: function initDatasources() {
var _this2 = this;
_.each(this.panel.datasources, function (ds) {
// Load datasource
_this2.datasourceSrv.get(ds).then(function (datasource) {
_this2.datasources[ds] = datasource;
_this2.datasources[ds].queryBuilder = datasource.queryBuilder;
});
});
}
}, {
key: 'onInitEditMode',
value: function onInitEditMode() {
this.addEditorTab('Options', triggerPanelEditor, 2);
this.addEditorTab('Triggers', triggerPanelTriggersTab, 2);
this.addEditorTab('Options', triggerPanelOptionsTab, 3);
}
}, {
key: 'onRefresh',
value: function onRefresh() {
var _this2 = this;
var _this3 = this;
// ignore fetching data if another panel is in fullscreen
if (this.otherPanelInFullscreenMode()) {
@@ -183,60 +205,80 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
return this.refreshData().then(function (triggerList) {
// Limit triggers number
_this2.triggerList = triggerList.slice(0, _this2.panel.limit);
_this3.triggerList = triggerList.slice(0, _this3.panel.limit);
_this2.getCurrentTriggersPage();
_this3.getCurrentTriggersPage();
// Notify panel that request is finished
_this2.loading = false;
_this3.loading = false;
_this2.render(_this2.triggerList);
_this3.render(_this3.triggerList);
});
}
}, {
key: 'refreshData',
value: function refreshData() {
return this.getTriggers().then(this.getAcknowledges.bind(this)).then(this.filterTriggers.bind(this));
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() {
var _this3 = this;
var _this4 = this;
return this.datasourceSrv.get(this.panel.datasource).then(function (datasource) {
var zabbix = datasource.zabbix;
_this3.zabbix = zabbix;
_this3.datasource = datasource;
var showEvents = _this3.panel.showEvents.value;
var triggerFilter = _this3.panel.triggers;
var hideHostsInMaintenance = _this3.panel.hideHostsInMaintenance;
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;
// Replace template variables
var groupFilter = datasource.replaceTemplateVars(triggerFilter.group.filter);
var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
// Replace template variables
var groupFilter = datasource.replaceTemplateVars(triggerFilter.group.filter);
var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
var triggersOptions = {
showTriggers: showEvents,
hideHostsInMaintenance: hideHostsInMaintenance
};
var triggersOptions = {
showTriggers: showEvents,
hideHostsInMaintenance: hideHostsInMaintenance
};
return zabbix.getTriggers(groupFilter, hostFilter, appFilter, triggersOptions);
return zabbix.getTriggers(groupFilter, hostFilter, appFilter, triggersOptions);
}).then(function (triggers) {
return _this4.getAcknowledges(triggers, ds);
}).then(function (triggers) {
return _this4.filterTriggers(triggers, ds);
});
});
return Promise.all(promises).then(function (results) {
return _.flatten(results);
}).then(function (triggers) {
return _.map(triggers, _this3.formatTrigger.bind(_this3));
return _.map(triggers, _this4.formatTrigger.bind(_this4));
});
}
}, {
key: 'getAcknowledges',
value: function getAcknowledges(triggerList) {
var _this4 = this;
value: function getAcknowledges(triggerList, ds) {
var _this5 = this;
// Request acknowledges for trigger
var eventids = _.map(triggerList, function (trigger) {
return trigger.lastEvent.eventid;
});
return this.zabbix.getAcknowledges(eventids).then(function (events) {
return this.datasources[ds].zabbix.getAcknowledges(eventids).then(function (events) {
// Map events to triggers
_.each(triggerList, function (trigger) {
@@ -247,18 +289,18 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
if (event) {
trigger.acknowledges = _.map(event.acknowledges, function (ack) {
var timestamp = moment.unix(ack.clock);
if (_this4.panel.customLastChangeFormat) {
ack.time = timestamp.format(_this4.panel.lastChangeFormat);
if (_this5.panel.customLastChangeFormat) {
ack.time = timestamp.format(_this5.panel.lastChangeFormat);
} else {
ack.time = timestamp.format(_this4.defaultTimeFormat);
ack.time = timestamp.format(_this5.defaultTimeFormat);
}
ack.user = ack.alias + ' (' + ack.name + ' ' + ack.surname + ')';
return ack;
});
// Mark acknowledged triggers with different color
if (_this4.panel.markAckEvents && trigger.acknowledges.length) {
trigger.color = _this4.panel.ackEventColor;
if (_this5.panel.markAckEvents && trigger.acknowledges.length) {
trigger.color = _this5.panel.ackEventColor;
}
}
});
@@ -268,12 +310,12 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
}
}, {
key: 'filterTriggers',
value: function filterTriggers(triggerList) {
var _this5 = this;
value: function filterTriggers(triggerList, ds) {
var _this6 = this;
// Filter triggers by description
var triggerFilter = this.panel.triggers.trigger.filter;
triggerFilter = this.datasource.replaceTemplateVars(triggerFilter);
var triggerFilter = this.panel.targets[ds].trigger.filter;
triggerFilter = this.datasources[ds].replaceTemplateVars(triggerFilter);
if (triggerFilter) {
triggerList = _filterTriggers(triggerList, triggerFilter);
}
@@ -291,7 +333,7 @@ System.register(['lodash', 'jquery', 'moment', 'app/plugins/sdk', '../datasource
// Filter triggers by severity
triggerList = _.filter(triggerList, function (trigger) {
return _this5.panel.triggerSeverity[trigger.priority].show;
return _this6.panel.triggerSeverity[trigger.priority].show;
});
// Sort triggers

File diff suppressed because one or more lines are too long

86
dist/panel-triggers/options_tab.js vendored Normal file
View File

@@ -0,0 +1,86 @@
'use strict';
System.register(['lodash', './datasource-selector.directive', '../datasource-zabbix/css/query-editor.css!'], function (_export, _context) {
"use strict";
var _, _createClass, TriggerPanelOptionsCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function triggerPanelOptionsTab() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/alexanderzobnin-zabbix-app/panel-triggers/partials/options_tab.html',
controller: TriggerPanelOptionsCtrl
};
}
_export('triggerPanelOptionsTab', triggerPanelOptionsTab);
return {
setters: [function (_lodash) {
_ = _lodash.default;
}, function (_datasourceSelectorDirective) {}, function (_datasourceZabbixCssQueryEditorCss) {}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
TriggerPanelOptionsCtrl = function () {
/** @ngInject */
function TriggerPanelOptionsCtrl($scope) {
_classCallCheck(this, TriggerPanelOptionsCtrl);
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
this.ackFilters = ['all triggers', 'unacknowledged', 'acknowledged'];
this.sortByFields = [{ text: 'last change', value: 'lastchange' }, { text: 'severity', value: 'priority' }];
this.showEventsFields = [{ text: 'All', value: [0, 1] }, { text: 'OK', value: [0] }, { text: 'Problems', value: 1 }];
}
_createClass(TriggerPanelOptionsCtrl, [{
key: 'refreshTriggerSeverity',
value: function refreshTriggerSeverity() {
_.each(this.triggerList, function (trigger) {
trigger.color = this.panel.triggerSeverity[trigger.priority].color;
trigger.severity = this.panel.triggerSeverity[trigger.priority].severity;
});
this.panelCtrl.refresh();
}
}, {
key: 'changeTriggerSeverityColor',
value: function changeTriggerSeverityColor(trigger, color) {
this.panel.triggerSeverity[trigger.priority].color = color;
this.refreshTriggerSeverity();
}
}]);
return TriggerPanelOptionsCtrl;
}();
}
};
});
//# sourceMappingURL=options_tab.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../src/panel-triggers/options_tab.js"],"names":["triggerPanelOptionsTab","restrict","scope","templateUrl","controller","TriggerPanelOptionsCtrl","_","$scope","editor","panelCtrl","ctrl","panel","fontSizes","ackFilters","sortByFields","text","value","showEventsFields","each","triggerList","trigger","color","triggerSeverity","priority","severity","refresh","refreshTriggerSeverity"],"mappings":";;;;;;;;;;;;;AAyDO,WAASA,sBAAT,GAAkC;AACvC,WAAO;AACLC,gBAAU,GADL;AAELC,aAAO,IAFF;AAGLC,mBAAa,oFAHR;AAILC,kBAAYC;AAJP,KAAP;AAMD;;oCAPeL,sB;;;;AA5CTM,O;;;;;;;;;;;;;;;;;;;;;AAKDD,6B;;AAEJ;AACA,yCAAYE,MAAZ,EAAoB;AAAA;;AAClBA,iBAAOC,MAAP,GAAgB,IAAhB;AACA,eAAKC,SAAL,GAAiBF,OAAOG,IAAxB;AACA,eAAKC,KAAL,GAAa,KAAKF,SAAL,CAAeE,KAA5B;;AAEA,eAAKC,SAAL,GAAiB,CAAC,KAAD,EAAQ,KAAR,EAAe,MAAf,EAAuB,MAAvB,EAA+B,MAA/B,EAAuC,MAAvC,EAA+C,MAA/C,EAAuD,MAAvD,EAA+D,MAA/D,EAAuE,MAAvE,EAA+E,MAA/E,EAAuF,MAAvF,CAAjB;AACA,eAAKC,UAAL,GAAkB,CAChB,cADgB,EAEhB,gBAFgB,EAGhB,cAHgB,CAAlB;AAKA,eAAKC,YAAL,GAAoB,CAClB,EAAEC,MAAM,aAAR,EAAwBC,OAAO,YAA/B,EADkB,EAElB,EAAED,MAAM,UAAR,EAAwBC,OAAO,UAA/B,EAFkB,CAApB;AAIA,eAAKC,gBAAL,GAAwB,CACtB,EAAEF,MAAM,KAAR,EAAmBC,OAAO,CAAC,CAAD,EAAG,CAAH,CAA1B,EADsB,EAEtB,EAAED,MAAM,IAAR,EAAmBC,OAAO,CAAC,CAAD,CAA1B,EAFsB,EAGtB,EAAED,MAAM,UAAR,EAAoBC,OAAO,CAA3B,EAHsB,CAAxB;AAKD;;;;mDAEwB;AACvBV,cAAEY,IAAF,CAAO,KAAKC,WAAZ,EAAyB,UAASC,OAAT,EAAkB;AACzCA,sBAAQC,KAAR,GAAgB,KAAKV,KAAL,CAAWW,eAAX,CAA2BF,QAAQG,QAAnC,EAA6CF,KAA7D;AACAD,sBAAQI,QAAR,GAAmB,KAAKb,KAAL,CAAWW,eAAX,CAA2BF,QAAQG,QAAnC,EAA6CC,QAAhE;AACD,aAHD;AAIA,iBAAKf,SAAL,CAAegB,OAAf;AACD;;;qDAE0BL,O,EAASC,K,EAAO;AACzC,iBAAKV,KAAL,CAAWW,eAAX,CAA2BF,QAAQG,QAAnC,EAA6CF,KAA7C,GAAqDA,KAArD;AACA,iBAAKK,sBAAL;AACD","file":"options_tab.js","sourcesContent":["/**\n * Grafana-Zabbix\n * Zabbix plugin for Grafana.\n * http://github.com/alexanderzobnin/grafana-zabbix\n *\n * Trigger panel.\n * This feature sponsored by CORE IT\n * http://www.coreit.fr\n *\n * Copyright 2015 Alexander Zobnin alexanderzobnin@gmail.com\n * Licensed under the Apache License, Version 2.0\n */\n\nimport _ from 'lodash';\nimport './datasource-selector.directive';\n\nimport '../datasource-zabbix/css/query-editor.css!';\n\nclass TriggerPanelOptionsCtrl {\n\n /** @ngInject */\n constructor($scope) {\n $scope.editor = this;\n this.panelCtrl = $scope.ctrl;\n this.panel = this.panelCtrl.panel;\n\n this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];\n this.ackFilters = [\n 'all triggers',\n 'unacknowledged',\n 'acknowledged'\n ];\n this.sortByFields = [\n { text: 'last change', value: 'lastchange' },\n { text: 'severity', value: 'priority' }\n ];\n this.showEventsFields = [\n { text: 'All', value: [0,1] },\n { text: 'OK', value: [0] },\n { text: 'Problems', value: 1 }\n ];\n }\n\n refreshTriggerSeverity() {\n _.each(this.triggerList, function(trigger) {\n trigger.color = this.panel.triggerSeverity[trigger.priority].color;\n trigger.severity = this.panel.triggerSeverity[trigger.priority].severity;\n });\n this.panelCtrl.refresh();\n }\n\n changeTriggerSeverityColor(trigger, color) {\n this.panel.triggerSeverity[trigger.priority].color = color;\n this.refreshTriggerSeverity();\n }\n}\n\nexport function triggerPanelOptionsTab() {\n return {\n restrict: 'E',\n scope: true,\n templateUrl: 'public/plugins/alexanderzobnin-zabbix-app/panel-triggers/partials/options_tab.html',\n controller: TriggerPanelOptionsCtrl,\n };\n}\n"]}

View File

@@ -1,85 +1,3 @@
<div class="editor-row">
<div class="section gf-form-group">
<h5 class="section-heading">Select triggers</h5>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Group</label>
<input type="text"
ng-model="editor.panel.triggers.group.filter"
bs-typeahead="editor.getGroupNames"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.group.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.group.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Host</label>
<input type="text"
ng-model="editor.panel.triggers.host.filter"
bs-typeahead="editor.getHostNames"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.host.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.host.filter)
}">
</div>
</div>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Application</label>
<input type="text"
ng-model="editor.panel.triggers.application.filter"
bs-typeahead="editor.getApplicationNames"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.application.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.application.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Trigger</label>
<input type="text"
ng-model="editor.panel.triggers.trigger.filter"
ng-blur="editor.parseTarget()"
placeholder="trigger name"
class="gf-form-input"
ng-style="editor.panel.triggers.trigger.style"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.trigger.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.trigger.filter)
}"
empty-to-null>
</div>
</div>
</div>
<div class="section gf-form-group">
<h5 class="section-heading">Data source</h5>
<div class="gf-form-inline">
<div class="gf-form">
<div class="gf-form-select-wrapper">
<select class="gf-form-input"
ng-model="editor.panel.datasource"
ng-options="ds for ds in editor.datasources"
ng-change="editor.datasourceChanged()">
</select>
</div>
</div>
</div>
</div>
</div>
<div class="editor-row">
<div class="section gf-form-group">
<h5 class="section-heading">Options</h5>

View File

@@ -0,0 +1,83 @@
<div class="editor-row">
<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>
</div>
<div class="gf-form">
<datasource-selector
datasources="editor.panel.datasources"
options="editor.available_datasources"
on-change="editor.datasourcesChanged()">
</datasource-selector>
</div>
</div>
</div>
</div>
<div class="editor-row" ng-repeat="ds in editor.panel.datasources">
<div class="section gf-form-group">
<h5 class="section-heading">{{ ds }}</h5>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Group</label>
<input type="text"
ng-model="editor.panel.targets[ds].group.filter"
bs-typeahead="editor.getGroupNames[ds]"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].group.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].group.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Host</label>
<input type="text"
ng-model="editor.panel.targets[ds].host.filter"
bs-typeahead="editor.getHostNames[ds]"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].host.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].host.filter)
}">
</div>
</div>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Application</label>
<input type="text"
ng-model="editor.panel.targets[ds].application.filter"
bs-typeahead="editor.getApplicationNames[ds]"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].application.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].application.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Trigger</label>
<input type="text"
ng-model="editor.panel.targets[ds].trigger.filter"
ng-blur="editor.parseTarget()"
placeholder="trigger name"
class="gf-form-input"
ng-style="editor.panel.targets[ds].trigger.style"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].trigger.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].trigger.filter)
}"
empty-to-null>
</div>
</div>
</div>
</div>

177
dist/panel-triggers/triggers_tab.js vendored Normal file
View File

@@ -0,0 +1,177 @@
'use strict';
System.register(['lodash', '../datasource-zabbix/utils', './datasource-selector.directive', '../datasource-zabbix/css/query-editor.css!'], function (_export, _context) {
"use strict";
var _, utils, _createClass, ZABBIX_DS_ID, DEFAULT_TARGET, TriggersTabCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function triggerPanelTriggersTab() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/alexanderzobnin-zabbix-app/panel-triggers/partials/triggers_tab.html',
controller: TriggersTabCtrl
};
}
_export('triggerPanelTriggersTab', triggerPanelTriggersTab);
return {
setters: [function (_lodash) {
_ = _lodash.default;
}, function (_datasourceZabbixUtils) {
utils = _datasourceZabbixUtils;
}, function (_datasourceSelectorDirective) {}, function (_datasourceZabbixCssQueryEditorCss) {}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
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) {
_classCallCheck(this, TriggersTabCtrl);
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
// Load scope defaults
var scopeDefaults = {
datasources: {},
getGroupNames: {},
getHostNames: {},
getApplicationNames: {},
oldTarget: _.cloneDeep(this.panel.targets)
};
_.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();
}
_createClass(TriggersTabCtrl, [{
key: 'initDatasources',
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);
});
});
}
}, {
key: 'getZabbixDataSources',
value: function getZabbixDataSources() {
return _.filter(this.datasourceSrv.getMetricSources(), function (datasource) {
return datasource.meta.id === ZABBIX_DS_ID && datasource.value;
});
}
}, {
key: 'suggestGroups',
value: function suggestGroups(ds, query, callback) {
return ds.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) {
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) {
return _.map(apps, 'name');
}).then(callback);
}
}, {
key: 'datasourcesChanged',
value: function datasourcesChanged() {
var _this2 = this;
_.each(this.panel.datasources, function (ds) {
if (!_this2.panel.targets[ds]) {
_this2.panel.targets[ds] = DEFAULT_TARGET;
}
});
this.parseTarget();
}
}, {
key: 'parseTarget',
value: function parseTarget() {
this.initDatasources();
var newTarget = _.cloneDeep(this.panel.targets);
if (!_.isEqual(this.oldTarget, newTarget)) {
this.oldTarget = newTarget;
}
this.panelCtrl.refresh();
}
}, {
key: 'isRegex',
value: function isRegex(str) {
return utils.isRegex(str);
}
}, {
key: 'isVariable',
value: function isVariable(str) {
return utils.isTemplateVariable(str, this.templateSrv.variables);
}
}]);
return TriggersTabCtrl;
}();
}
};
});
//# sourceMappingURL=triggers_tab.js.map

File diff suppressed because one or more lines are too long

View File

@@ -32,7 +32,7 @@ class DatasourceSelectorCtrl {
multi: true,
current: {value: datasources, text: datasources.join(" + ")},
options: _.map(options, (ds) => {
return {text: ds, value: ds};
return {text: ds, value: ds, selected: _.includes(datasources, ds)};
})
};
}

View File

@@ -1,188 +0,0 @@
/**
* Grafana-Zabbix
* Zabbix plugin for Grafana.
* http://github.com/alexanderzobnin/grafana-zabbix
*
* Trigger panel.
* This feature sponsored by CORE IT
* http://www.coreit.fr
*
* Copyright 2015 Alexander Zobnin alexanderzobnin@gmail.com
* Licensed under the Apache License, Version 2.0
*/
import _ from 'lodash';
import * as utils from '../datasource-zabbix/utils';
import '../datasource-zabbix/css/query-editor.css!';
class TriggerPanelEditorCtrl {
/** @ngInject */
constructor($scope, $rootScope, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
this.popoverSrv = popoverSrv;
// Map functions for bs-typeahead
this.getGroupNames = _.partial(getMetricNames, this, 'groupList');
this.getHostNames = _.partial(getMetricNames, this, 'hostList');
this.getApplicationNames = _.partial(getMetricNames, this, 'appList');
// Update metric suggestion when template variable was changed
$rootScope.$on('template-variable-value-updated', () => this.onVariableChange());
this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
this.ackFilters = [
'all triggers',
'unacknowledged',
'acknowledged'
];
this.sortByFields = [
{ text: 'last change', value: 'lastchange' },
{ text: 'severity', value: 'priority' }
];
this.showEventsFields = [
{ text: 'All', value: [0,1] },
{ text: 'OK', value: [0] },
{ text: 'Problems', value: 1 }
];
// Load scope defaults
var scopeDefaults = {
metric: {},
inputStyles: {},
oldTarget: _.cloneDeep(this.panel.triggers)
};
_.defaults(this, scopeDefaults);
// Set default datasource
this.datasources = _.map(this.getZabbixDataSources(), 'name');
if (!this.panel.datasource) {
this.panel.datasource = this.datasources[0];
}
// Load datasource
this.datasourceSrv.get(this.panel.datasource)
.then(datasource => {
this.datasource = datasource;
this.zabbix = datasource.zabbix;
this.queryBuilder = datasource.queryBuilder;
this.initFilters();
this.panelCtrl.refresh();
});
}
initFilters() {
return Promise.all([
this.suggestGroups(),
this.suggestHosts(),
this.suggestApps()
]);
}
suggestGroups() {
return this.zabbix.getAllGroups()
.then(groups => {
this.metric.groupList = groups;
return groups;
});
}
suggestHosts() {
let groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
return this.zabbix.getAllHosts(groupFilter)
.then(hosts => {
this.metric.hostList = hosts;
return hosts;
});
}
suggestApps() {
let groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
let hostFilter = this.datasource.replaceTemplateVars(this.panel.triggers.host.filter);
return this.zabbix.getAllApps(groupFilter, hostFilter)
.then(apps => {
this.metric.appList = apps;
return apps;
});
}
onVariableChange() {
if (this.isContainsVariables()) {
this.targetChanged();
}
}
/**
* Check query for template variables
*/
isContainsVariables() {
return _.some(['group', 'host', 'application'], field => {
return utils.isTemplateVariable(this.panel.triggers[field].filter, this.templateSrv.variables);
});
}
targetChanged() {
this.initFilters();
this.panelCtrl.refresh();
}
parseTarget() {
this.initFilters();
var newTarget = _.cloneDeep(this.panel.triggers);
if (!_.isEqual(this.oldTarget, this.panel.triggers)) {
this.oldTarget = newTarget;
this.panelCtrl.refresh();
}
}
refreshTriggerSeverity() {
_.each(this.triggerList, function(trigger) {
trigger.color = this.panel.triggerSeverity[trigger.priority].color;
trigger.severity = this.panel.triggerSeverity[trigger.priority].severity;
});
this.panelCtrl.refresh();
}
datasourceChanged() {
this.panelCtrl.refresh();
}
changeTriggerSeverityColor(trigger, color) {
this.panel.triggerSeverity[trigger.priority].color = color;
this.refreshTriggerSeverity();
}
isRegex(str) {
return utils.isRegex(str);
}
isVariable(str) {
return utils.isTemplateVariable(str, this.templateSrv.variables);
}
getZabbixDataSources() {
let ZABBIX_DS_ID = 'alexanderzobnin-zabbix-datasource';
return _.filter(this.datasourceSrv.getMetricSources(), datasource => {
return datasource.meta.id === ZABBIX_DS_ID && datasource.value;
});
}
}
// Get list of metric names for bs-typeahead directive
function getMetricNames(scope, metricList) {
return _.uniq(_.map(scope.metric[metricList], 'name'));
}
export function triggerPanelEditor() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/alexanderzobnin-zabbix-app/panel-triggers/editor.html',
controller: TriggerPanelEditorCtrl,
};
}

View File

View File

@@ -17,7 +17,8 @@ import moment from 'moment';
import {loadPluginCss} from 'app/plugins/sdk';
import * as utils from '../datasource-zabbix/utils';
import {PanelCtrl} from 'app/plugins/sdk';
import {triggerPanelEditor} from './editor';
import {triggerPanelOptionsTab} from './options_tab';
import {triggerPanelTriggersTab} from './triggers_tab';
import './ack-tooltip.directive';
loadPluginCss({
@@ -35,13 +36,14 @@ var defaultSeverity = [
];
var panelDefaults = {
datasource: null,
datasources: [],
triggers: {
group: {filter: ""},
host: {filter: ""},
application: {filter: ""},
trigger: {filter: ""}
},
targets: {},
hostField: true,
statusField: false,
severityField: false,
@@ -59,6 +61,7 @@ var panelDefaults = {
scroll: true,
pageSize: 10,
fontSize: '100%',
schemaVersion: 2
};
var triggerStatusMap = {
@@ -83,18 +86,34 @@ class TriggerPanelCtrl extends PanelCtrl {
this.pageIndex = 0;
this.triggerList = [];
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.initDatasources();
this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
this.events.on('refresh', this.onRefresh.bind(this));
}
initDatasources() {
_.each(this.panel.datasources, (ds) => {
// Load datasource
this.datasourceSrv.get(ds)
.then(datasource => {
this.datasources[ds] = datasource;
this.datasources[ds].queryBuilder = datasource.queryBuilder;
});
});
}
onInitEditMode() {
this.addEditorTab('Options', triggerPanelEditor, 2);
this.addEditorTab('Triggers', triggerPanelTriggersTab, 2);
this.addEditorTab('Options', triggerPanelOptionsTab, 3);
}
onRefresh() {
@@ -120,45 +139,61 @@ class TriggerPanelCtrl extends PanelCtrl {
}
refreshData() {
return this.getTriggers()
.then(this.getAcknowledges.bind(this))
.then(this.filterTriggers.bind(this));
return this.getTriggers();
}
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;
}
}
getTriggers() {
return this.datasourceSrv.get(this.panel.datasource)
.then(datasource => {
var zabbix = datasource.zabbix;
this.zabbix = zabbix;
this.datasource = datasource;
var showEvents = this.panel.showEvents.value;
var triggerFilter = this.panel.triggers;
var hideHostsInMaintenance = this.panel.hideHostsInMaintenance;
let promises = _.map(this.panel.datasources, (ds) => {
return this.datasourceSrv.get(ds)
.then(datasource => {
var zabbix = datasource.zabbix;
this.zabbix = zabbix;
this.datasource = datasource;
var showEvents = this.panel.showEvents.value;
var triggerFilter = this.panel.targets[ds];
var hideHostsInMaintenance = this.panel.hideHostsInMaintenance;
// Replace template variables
var groupFilter = datasource.replaceTemplateVars(triggerFilter.group.filter);
var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
// Replace template variables
var groupFilter = datasource.replaceTemplateVars(triggerFilter.group.filter);
var hostFilter = datasource.replaceTemplateVars(triggerFilter.host.filter);
var appFilter = datasource.replaceTemplateVars(triggerFilter.application.filter);
let triggersOptions = {
showTriggers: showEvents,
hideHostsInMaintenance: hideHostsInMaintenance
};
let triggersOptions = {
showTriggers: showEvents,
hideHostsInMaintenance: hideHostsInMaintenance
};
return zabbix.getTriggers(groupFilter, hostFilter, appFilter, triggersOptions);
})
return zabbix.getTriggers(groupFilter, hostFilter, appFilter, triggersOptions);
}).then((triggers) => {
return this.getAcknowledges(triggers, ds);
}).then((triggers) => {
return this.filterTriggers(triggers, ds);
});
});
return Promise.all(promises)
.then(results => _.flatten(results))
.then(triggers => {
return _.map(triggers, this.formatTrigger.bind(this));
});
}
getAcknowledges(triggerList) {
getAcknowledges(triggerList, ds) {
// Request acknowledges for trigger
var eventids = _.map(triggerList, trigger => {
return trigger.lastEvent.eventid;
});
return this.zabbix.getAcknowledges(eventids)
return this.datasources[ds].zabbix.getAcknowledges(eventids)
.then(events => {
// Map events to triggers
@@ -190,10 +225,10 @@ class TriggerPanelCtrl extends PanelCtrl {
});
}
filterTriggers(triggerList) {
filterTriggers(triggerList, ds) {
// Filter triggers by description
var triggerFilter = this.panel.triggers.trigger.filter;
triggerFilter = this.datasource.replaceTemplateVars(triggerFilter);
var triggerFilter = this.panel.targets[ds].trigger.filter;
triggerFilter = this.datasources[ds].replaceTemplateVars(triggerFilter);
if (triggerFilter) {
triggerList = filterTriggers(triggerList, triggerFilter);
}

View File

@@ -0,0 +1,65 @@
/**
* Grafana-Zabbix
* Zabbix plugin for Grafana.
* http://github.com/alexanderzobnin/grafana-zabbix
*
* Trigger panel.
* This feature sponsored by CORE IT
* http://www.coreit.fr
*
* Copyright 2015 Alexander Zobnin alexanderzobnin@gmail.com
* Licensed under the Apache License, Version 2.0
*/
import _ from 'lodash';
import './datasource-selector.directive';
import '../datasource-zabbix/css/query-editor.css!';
class TriggerPanelOptionsCtrl {
/** @ngInject */
constructor($scope) {
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
this.ackFilters = [
'all triggers',
'unacknowledged',
'acknowledged'
];
this.sortByFields = [
{ text: 'last change', value: 'lastchange' },
{ text: 'severity', value: 'priority' }
];
this.showEventsFields = [
{ text: 'All', value: [0,1] },
{ text: 'OK', value: [0] },
{ text: 'Problems', value: 1 }
];
}
refreshTriggerSeverity() {
_.each(this.triggerList, function(trigger) {
trigger.color = this.panel.triggerSeverity[trigger.priority].color;
trigger.severity = this.panel.triggerSeverity[trigger.priority].severity;
});
this.panelCtrl.refresh();
}
changeTriggerSeverityColor(trigger, color) {
this.panel.triggerSeverity[trigger.priority].color = color;
this.refreshTriggerSeverity();
}
}
export function triggerPanelOptionsTab() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/alexanderzobnin-zabbix-app/panel-triggers/partials/options_tab.html',
controller: TriggerPanelOptionsCtrl,
};
}

View File

@@ -1,85 +1,3 @@
<div class="editor-row">
<div class="section gf-form-group">
<h5 class="section-heading">Select triggers</h5>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Group</label>
<input type="text"
ng-model="editor.panel.triggers.group.filter"
bs-typeahead="editor.getGroupNames"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.group.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.group.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Host</label>
<input type="text"
ng-model="editor.panel.triggers.host.filter"
bs-typeahead="editor.getHostNames"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.host.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.host.filter)
}">
</div>
</div>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Application</label>
<input type="text"
ng-model="editor.panel.triggers.application.filter"
bs-typeahead="editor.getApplicationNames"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.application.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.application.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Trigger</label>
<input type="text"
ng-model="editor.panel.triggers.trigger.filter"
ng-blur="editor.parseTarget()"
placeholder="trigger name"
class="gf-form-input"
ng-style="editor.panel.triggers.trigger.style"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.triggers.trigger.filter),
'zbx-regex': editor.isRegex(editor.panel.triggers.trigger.filter)
}"
empty-to-null>
</div>
</div>
</div>
<div class="section gf-form-group">
<h5 class="section-heading">Data source</h5>
<div class="gf-form-inline">
<div class="gf-form">
<div class="gf-form-select-wrapper">
<select class="gf-form-input"
ng-model="editor.panel.datasource"
ng-options="ds for ds in editor.datasources"
ng-change="editor.datasourceChanged()">
</select>
</div>
</div>
</div>
</div>
</div>
<div class="editor-row">
<div class="section gf-form-group">
<h5 class="section-heading">Options</h5>

View File

@@ -0,0 +1,83 @@
<div class="editor-row">
<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>
</div>
<div class="gf-form">
<datasource-selector
datasources="editor.panel.datasources"
options="editor.available_datasources"
on-change="editor.datasourcesChanged()">
</datasource-selector>
</div>
</div>
</div>
</div>
<div class="editor-row" ng-repeat="ds in editor.panel.datasources">
<div class="section gf-form-group">
<h5 class="section-heading">{{ ds }}</h5>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Group</label>
<input type="text"
ng-model="editor.panel.targets[ds].group.filter"
bs-typeahead="editor.getGroupNames[ds]"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].group.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].group.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Host</label>
<input type="text"
ng-model="editor.panel.targets[ds].host.filter"
bs-typeahead="editor.getHostNames[ds]"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].host.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].host.filter)
}">
</div>
</div>
<div class="gf-form-inline">
<div class="gf-form max-width-20">
<label class="gf-form-label query-keyword width-7">Application</label>
<input type="text"
ng-model="editor.panel.targets[ds].application.filter"
bs-typeahead="editor.getApplicationNames[ds]"
ng-blur="editor.parseTarget()"
data-min-length=0
data-items=100
class="gf-form-input"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].application.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].application.filter)
}">
</div>
<div class="gf-form">
<label class="gf-form-label query-keyword width-7">Trigger</label>
<input type="text"
ng-model="editor.panel.targets[ds].trigger.filter"
ng-blur="editor.parseTarget()"
placeholder="trigger name"
class="gf-form-input"
ng-style="editor.panel.targets[ds].trigger.style"
ng-class="{
'zbx-variable': editor.isVariable(editor.panel.targets[ds].trigger.filter),
'zbx-regex': editor.isRegex(editor.panel.targets[ds].trigger.filter)
}"
empty-to-null>
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,126 @@
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: ""}
};
class TriggersTabCtrl {
/** @ngInject */
constructor($scope, $rootScope, uiSegmentSrv, datasourceSrv, templateSrv) {
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
// Load scope defaults
var scopeDefaults = {
datasources: {},
getGroupNames: {},
getHostNames: {},
getApplicationNames: {},
oldTarget: _.cloneDeep(this.panel.targets)
};
_.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);
});
});
}
getZabbixDataSources() {
return _.filter(this.datasourceSrv.getMetricSources(), datasource => {
return datasource.meta.id === ZABBIX_DS_ID && datasource.value;
});
}
suggestGroups(ds, query, callback) {
return ds.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)
.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)
.then(apps => {
return _.map(apps, 'name');
})
.then(callback);
}
datasourcesChanged() {
_.each(this.panel.datasources, (ds) => {
if (!this.panel.targets[ds]) {
this.panel.targets[ds] = DEFAULT_TARGET;
}
});
this.parseTarget();
}
parseTarget() {
this.initDatasources();
var newTarget = _.cloneDeep(this.panel.targets);
if (!_.isEqual(this.oldTarget, newTarget)) {
this.oldTarget = newTarget;
}
this.panelCtrl.refresh();
}
isRegex(str) {
return utils.isRegex(str);
}
isVariable(str) {
return utils.isTemplateVariable(str, this.templateSrv.variables);
}
}
export function triggerPanelTriggersTab() {
return {
restrict: 'E',
scope: true,
templateUrl: 'public/plugins/alexanderzobnin-zabbix-app/panel-triggers/partials/triggers_tab.html',
controller: TriggersTabCtrl,
};
}