| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- ///<reference path="../../../headers/common.d.ts" />
- import _ from "lodash";
- import moment from "moment";
- import alertDef from "../../../features/alerting/alert_def";
- import { PanelCtrl } from "app/plugins/sdk";
- import * as dateMath from "app/core/utils/datemath";
- class AlertListPanel extends PanelCtrl {
- static templateUrl = "module.html";
- static scrollable = true;
- showOptions = [
- { text: "Current state", value: "current" },
- { text: "Recent state changes", value: "changes" }
- ];
- sortOrderOptions = [
- { text: "Alphabetical (asc)", value: 1 },
- { text: "Alphabetical (desc)", value: 2 },
- { text: "Importance", value: 3 }
- ];
- stateFilter: any = {};
- currentAlerts: any = [];
- alertHistory: any = [];
- noAlertsMessage: string;
- // Set and populate defaults
- panelDefaults = {
- show: "current",
- limit: 10,
- stateFilter: [],
- onlyAlertsOnDashboard: false,
- sortOrder: 1
- };
- /** @ngInject */
- constructor($scope, $injector, private backendSrv) {
- super($scope, $injector);
- _.defaults(this.panel, this.panelDefaults);
- this.events.on("init-edit-mode", this.onInitEditMode.bind(this));
- this.events.on("refresh", this.onRefresh.bind(this));
- for (let key in this.panel.stateFilter) {
- this.stateFilter[this.panel.stateFilter[key]] = true;
- }
- }
- sortResult(alerts) {
- if (this.panel.sortOrder === 3) {
- return _.sortBy(alerts, a => {
- return alertDef.alertStateSortScore[a.state];
- });
- }
- var result = _.sortBy(alerts, a => {
- return a.name.toLowerCase();
- });
- if (this.panel.sortOrder === 2) {
- result.reverse();
- }
- return result;
- }
- updateStateFilter() {
- var result = [];
- for (let key in this.stateFilter) {
- if (this.stateFilter[key]) {
- result.push(key);
- }
- }
- this.panel.stateFilter = result;
- this.onRefresh();
- }
- onRefresh() {
- if (this.panel.show === "current") {
- this.getCurrentAlertState();
- }
- if (this.panel.show === "changes") {
- this.getStateChanges();
- }
- }
- getStateChanges() {
- var params: any = {
- limit: this.panel.limit,
- type: "alert",
- newState: this.panel.stateFilter
- };
- if (this.panel.onlyAlertsOnDashboard) {
- params.dashboardId = this.dashboard.id;
- }
- params.from = dateMath.parse(this.dashboard.time.from).unix() * 1000;
- params.to = dateMath.parse(this.dashboard.time.to).unix() * 1000;
- this.backendSrv.get(`/api/annotations`, params).then(res => {
- this.alertHistory = _.map(res, al => {
- 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;
- });
- this.noAlertsMessage =
- this.alertHistory.length === 0 ? "No alerts in current time range" : "";
- });
- }
- getCurrentAlertState() {
- var params: any = {
- state: this.panel.stateFilter
- };
- if (this.panel.onlyAlertsOnDashboard) {
- params.dashboardId = this.dashboard.id;
- }
- this.backendSrv.get(`/api/alerts`, params).then(res => {
- this.currentAlerts = this.sortResult(
- _.map(res, al => {
- al.stateModel = alertDef.getStateDisplayModel(al.state);
- al.newStateDateAgo = moment(al.newStateDate)
- .locale("en")
- .fromNow(true);
- return al;
- })
- );
- this.noAlertsMessage = this.currentAlerts.length === 0 ? "No alerts" : "";
- });
- }
- onInitEditMode() {
- this.addEditorTab(
- "Options",
- "public/app/plugins/panel/alertlist/editor.html"
- );
- }
- }
- export { AlertListPanel, AlertListPanel as PanelCtrl };
|