2017-12-21 08:39:31 +01:00
|
|
|
import _ from 'lodash';
|
2020-02-12 09:37:36 +01:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
|
|
|
import { dateMath, dateTime, PanelEvents } from '@grafana/data';
|
|
|
|
|
import { auto, IScope } from 'angular';
|
|
|
|
|
|
2018-09-02 07:11:21 -07:00
|
|
|
import alertDef from '../../../features/alerting/state/alertDef';
|
2017-12-21 08:39:31 +01:00
|
|
|
import { PanelCtrl } from 'app/plugins/sdk';
|
2020-01-21 09:08:07 +00:00
|
|
|
import { promiseToDigest } from 'app/core/utils/promiseToDigest';
|
2016-09-14 14:12:19 +02:00
|
|
|
|
2016-09-12 13:16:29 +02:00
|
|
|
class AlertListPanel extends PanelCtrl {
|
2017-12-21 08:39:31 +01:00
|
|
|
static templateUrl = 'module.html';
|
2017-11-21 14:30:33 +01:00
|
|
|
static scrollable = true;
|
2016-09-12 13:16:29 +02:00
|
|
|
|
2019-11-19 13:59:39 +00:00
|
|
|
showOptions = [
|
|
|
|
|
{ text: 'Current state', value: 'current' },
|
|
|
|
|
{ text: 'Recent state changes', value: 'changes' },
|
|
|
|
|
];
|
2016-09-12 13:16:29 +02:00
|
|
|
|
2016-12-15 16:29:40 +01:00
|
|
|
sortOrderOptions = [
|
2017-12-21 08:39:31 +01:00
|
|
|
{ text: 'Alphabetical (asc)', value: 1 },
|
|
|
|
|
{ text: 'Alphabetical (desc)', value: 2 },
|
|
|
|
|
{ text: 'Importance', value: 3 },
|
2016-12-15 16:29:40 +01:00
|
|
|
];
|
|
|
|
|
|
2016-09-14 14:12:19 +02:00
|
|
|
stateFilter: any = {};
|
2016-09-12 13:16:29 +02:00
|
|
|
currentAlerts: any = [];
|
|
|
|
|
alertHistory: any = [];
|
2017-11-03 10:16:25 +01:00
|
|
|
noAlertsMessage: string;
|
2019-05-06 12:51:09 +05:30
|
|
|
templateSrv: string;
|
2018-06-01 14:36:40 +02:00
|
|
|
|
2016-09-12 13:16:29 +02:00
|
|
|
// Set and populate defaults
|
2019-06-27 15:56:02 +02:00
|
|
|
panelDefaults: any = {
|
2017-12-21 08:39:31 +01:00
|
|
|
show: 'current',
|
2016-09-14 08:36:44 +02:00
|
|
|
limit: 10,
|
2016-11-10 16:07:13 +01:00
|
|
|
stateFilter: [],
|
2016-12-15 16:29:40 +01:00
|
|
|
onlyAlertsOnDashboard: false,
|
2017-12-21 08:39:31 +01:00
|
|
|
sortOrder: 1,
|
2018-06-01 14:36:40 +02:00
|
|
|
dashboardFilter: '',
|
|
|
|
|
nameFilter: '',
|
|
|
|
|
folderId: null,
|
2016-09-12 13:16:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** @ngInject */
|
2020-01-21 09:08:07 +00:00
|
|
|
constructor($scope: IScope, $injector: auto.IInjectorService) {
|
2016-09-12 13:16:29 +02:00
|
|
|
super($scope, $injector);
|
|
|
|
|
_.defaults(this.panel, this.panelDefaults);
|
|
|
|
|
|
2019-10-14 09:27:47 +01:00
|
|
|
this.events.on(PanelEvents.editModeInitialized, this.onInitEditMode.bind(this));
|
|
|
|
|
this.events.on(PanelEvents.refresh, this.onRefresh.bind(this));
|
2019-05-06 12:51:09 +05:30
|
|
|
this.templateSrv = this.$injector.get('templateSrv');
|
2016-09-14 14:12:19 +02:00
|
|
|
|
2018-08-26 17:14:40 +02:00
|
|
|
for (const key in this.panel.stateFilter) {
|
2016-09-14 14:12:19 +02:00
|
|
|
this.stateFilter[this.panel.stateFilter[key]] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
sortResult(alerts: any[]) {
|
2016-12-15 16:29:40 +01:00
|
|
|
if (this.panel.sortOrder === 3) {
|
2017-12-19 16:06:54 +01:00
|
|
|
return _.sortBy(alerts, a => {
|
2019-06-27 15:56:02 +02:00
|
|
|
// @ts-ignore
|
2017-12-19 16:06:54 +01:00
|
|
|
return alertDef.alertStateSortScore[a.state];
|
|
|
|
|
});
|
2016-12-15 16:29:40 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 14:27:29 +02:00
|
|
|
const result = _.sortBy(alerts, a => {
|
2017-12-19 16:06:54 +01:00
|
|
|
return a.name.toLowerCase();
|
|
|
|
|
});
|
2016-12-15 16:29:40 +01:00
|
|
|
if (this.panel.sortOrder === 2) {
|
|
|
|
|
result.reverse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-14 14:12:19 +02:00
|
|
|
updateStateFilter() {
|
2018-08-29 14:27:29 +02:00
|
|
|
const result = [];
|
2016-09-14 14:12:19 +02:00
|
|
|
|
2018-08-26 17:14:40 +02:00
|
|
|
for (const key in this.stateFilter) {
|
2016-09-14 14:12:19 +02:00
|
|
|
if (this.stateFilter[key]) {
|
|
|
|
|
result.push(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.panel.stateFilter = result;
|
2017-11-03 10:16:25 +01:00
|
|
|
this.onRefresh();
|
2016-09-12 13:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-03 10:16:25 +01:00
|
|
|
onRefresh() {
|
2018-01-09 15:58:24 +03:00
|
|
|
let getAlertsPromise;
|
|
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
if (this.panel.show === 'current') {
|
2018-01-09 15:58:24 +03:00
|
|
|
getAlertsPromise = this.getCurrentAlertState();
|
2016-09-12 13:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
if (this.panel.show === 'changes') {
|
2018-01-09 15:58:24 +03:00
|
|
|
getAlertsPromise = this.getStateChanges();
|
2016-09-12 13:16:29 +02:00
|
|
|
}
|
2018-01-09 15:58:24 +03:00
|
|
|
|
|
|
|
|
getAlertsPromise.then(() => {
|
|
|
|
|
this.renderingCompleted();
|
|
|
|
|
});
|
2016-09-12 13:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 10:13:03 +00:00
|
|
|
onFolderChange = (folder: any) => {
|
2018-06-01 14:36:40 +02:00
|
|
|
this.panel.folderId = folder.id;
|
|
|
|
|
this.refresh();
|
2020-02-13 10:13:03 +00:00
|
|
|
};
|
2018-06-01 14:36:40 +02:00
|
|
|
|
2016-09-14 08:36:44 +02:00
|
|
|
getStateChanges() {
|
2018-08-29 14:27:29 +02:00
|
|
|
const params: any = {
|
2016-09-14 08:36:44 +02:00
|
|
|
limit: this.panel.limit,
|
2017-12-21 08:39:31 +01:00
|
|
|
type: 'alert',
|
|
|
|
|
newState: this.panel.stateFilter,
|
2016-09-14 08:36:44 +02:00
|
|
|
};
|
|
|
|
|
|
2016-11-10 16:07:13 +01:00
|
|
|
if (this.panel.onlyAlertsOnDashboard) {
|
|
|
|
|
params.dashboardId = this.dashboard.id;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 10:07:53 +02:00
|
|
|
params.from = dateMath.parse(this.dashboard.time.from).unix() * 1000;
|
|
|
|
|
params.to = dateMath.parse(this.dashboard.time.to).unix() * 1000;
|
2016-09-16 15:55:38 +02:00
|
|
|
|
2020-01-21 09:08:07 +00:00
|
|
|
return promiseToDigest(this.$scope)(
|
|
|
|
|
getBackendSrv()
|
2020-02-12 09:37:36 +01:00
|
|
|
.get('/api/annotations', params, `alert-list-get-state-changes-${this.panel.id}`)
|
|
|
|
|
.then(data => {
|
|
|
|
|
this.alertHistory = _.map(data, al => {
|
2020-01-21 09:08:07 +00:00
|
|
|
al.time = this.dashboard.formatDate(al.time, 'MMM D, YYYY HH:mm:ss');
|
|
|
|
|
al.stateModel = alertDef.getStateDisplayModel(al.newState);
|
|
|
|
|
al.info = alertDef.getAlertAnnotationInfo(al);
|
|
|
|
|
return al;
|
|
|
|
|
});
|
2018-06-01 14:36:40 +02:00
|
|
|
|
2020-01-21 09:08:07 +00:00
|
|
|
this.noAlertsMessage = this.alertHistory.length === 0 ? 'No alerts in current time range' : '';
|
2018-01-09 15:58:24 +03:00
|
|
|
|
2020-01-21 09:08:07 +00:00
|
|
|
return this.alertHistory;
|
|
|
|
|
})
|
|
|
|
|
);
|
2016-09-12 13:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCurrentAlertState() {
|
2018-08-29 14:27:29 +02:00
|
|
|
const params: any = {
|
2017-12-21 08:39:31 +01:00
|
|
|
state: this.panel.stateFilter,
|
2016-09-14 14:12:19 +02:00
|
|
|
};
|
|
|
|
|
|
2018-06-01 14:36:40 +02:00
|
|
|
if (this.panel.nameFilter) {
|
2019-05-06 12:51:09 +05:30
|
|
|
params.query = this.templateSrv.replace(this.panel.nameFilter, this.panel.scopedVars);
|
2018-06-01 14:36:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.panel.folderId >= 0) {
|
|
|
|
|
params.folderId = this.panel.folderId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.panel.dashboardFilter) {
|
|
|
|
|
params.dashboardQuery = this.panel.dashboardFilter;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 16:07:13 +01:00
|
|
|
if (this.panel.onlyAlertsOnDashboard) {
|
|
|
|
|
params.dashboardId = this.dashboard.id;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 14:36:40 +02:00
|
|
|
if (this.panel.dashboardTags) {
|
|
|
|
|
params.dashboardTag = this.panel.dashboardTags;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 09:08:07 +00:00
|
|
|
return promiseToDigest(this.$scope)(
|
|
|
|
|
getBackendSrv()
|
2020-02-12 09:37:36 +01:00
|
|
|
.get('/api/alerts', params, `alert-list-get-current-alert-state-${this.panel.id}`)
|
|
|
|
|
.then(data => {
|
2020-01-21 09:08:07 +00:00
|
|
|
this.currentAlerts = this.sortResult(
|
2020-02-12 09:37:36 +01:00
|
|
|
_.map(data, al => {
|
2020-01-21 09:08:07 +00:00
|
|
|
al.stateModel = alertDef.getStateDisplayModel(al.state);
|
|
|
|
|
al.newStateDateAgo = dateTime(al.newStateDate)
|
|
|
|
|
.locale('en')
|
|
|
|
|
.fromNow(true);
|
|
|
|
|
return al;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
if (this.currentAlerts.length > this.panel.limit) {
|
|
|
|
|
this.currentAlerts = this.currentAlerts.slice(0, this.panel.limit);
|
|
|
|
|
}
|
|
|
|
|
this.noAlertsMessage = this.currentAlerts.length === 0 ? 'No alerts' : '';
|
|
|
|
|
|
|
|
|
|
return this.currentAlerts;
|
2017-12-19 16:06:54 +01:00
|
|
|
})
|
2020-01-21 09:08:07 +00:00
|
|
|
);
|
2016-09-12 13:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onInitEditMode() {
|
2017-12-21 08:39:31 +01:00
|
|
|
this.addEditorTab('Options', 'public/app/plugins/panel/alertlist/editor.html');
|
2016-09-12 13:16:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
export { AlertListPanel, AlertListPanel as PanelCtrl };
|