module.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from "lodash";
  3. import moment from "moment";
  4. import alertDef from "../../../features/alerting/alert_def";
  5. import { PanelCtrl } from "app/plugins/sdk";
  6. import * as dateMath from "app/core/utils/datemath";
  7. class AlertListPanel extends PanelCtrl {
  8. static templateUrl = "module.html";
  9. static scrollable = true;
  10. showOptions = [
  11. { text: "Current state", value: "current" },
  12. { text: "Recent state changes", value: "changes" }
  13. ];
  14. sortOrderOptions = [
  15. { text: "Alphabetical (asc)", value: 1 },
  16. { text: "Alphabetical (desc)", value: 2 },
  17. { text: "Importance", value: 3 }
  18. ];
  19. stateFilter: any = {};
  20. currentAlerts: any = [];
  21. alertHistory: any = [];
  22. noAlertsMessage: string;
  23. // Set and populate defaults
  24. panelDefaults = {
  25. show: "current",
  26. limit: 10,
  27. stateFilter: [],
  28. onlyAlertsOnDashboard: false,
  29. sortOrder: 1
  30. };
  31. /** @ngInject */
  32. constructor($scope, $injector, private backendSrv) {
  33. super($scope, $injector);
  34. _.defaults(this.panel, this.panelDefaults);
  35. this.events.on("init-edit-mode", this.onInitEditMode.bind(this));
  36. this.events.on("refresh", this.onRefresh.bind(this));
  37. for (let key in this.panel.stateFilter) {
  38. this.stateFilter[this.panel.stateFilter[key]] = true;
  39. }
  40. }
  41. sortResult(alerts) {
  42. if (this.panel.sortOrder === 3) {
  43. return _.sortBy(alerts, a => {
  44. return alertDef.alertStateSortScore[a.state];
  45. });
  46. }
  47. var result = _.sortBy(alerts, a => {
  48. return a.name.toLowerCase();
  49. });
  50. if (this.panel.sortOrder === 2) {
  51. result.reverse();
  52. }
  53. return result;
  54. }
  55. updateStateFilter() {
  56. var result = [];
  57. for (let key in this.stateFilter) {
  58. if (this.stateFilter[key]) {
  59. result.push(key);
  60. }
  61. }
  62. this.panel.stateFilter = result;
  63. this.onRefresh();
  64. }
  65. onRefresh() {
  66. if (this.panel.show === "current") {
  67. this.getCurrentAlertState();
  68. }
  69. if (this.panel.show === "changes") {
  70. this.getStateChanges();
  71. }
  72. }
  73. getStateChanges() {
  74. var params: any = {
  75. limit: this.panel.limit,
  76. type: "alert",
  77. newState: this.panel.stateFilter
  78. };
  79. if (this.panel.onlyAlertsOnDashboard) {
  80. params.dashboardId = this.dashboard.id;
  81. }
  82. params.from = dateMath.parse(this.dashboard.time.from).unix() * 1000;
  83. params.to = dateMath.parse(this.dashboard.time.to).unix() * 1000;
  84. this.backendSrv.get(`/api/annotations`, params).then(res => {
  85. this.alertHistory = _.map(res, al => {
  86. al.time = this.dashboard.formatDate(al.time, "MMM D, YYYY HH:mm:ss");
  87. al.stateModel = alertDef.getStateDisplayModel(al.newState);
  88. al.info = alertDef.getAlertAnnotationInfo(al);
  89. return al;
  90. });
  91. this.noAlertsMessage =
  92. this.alertHistory.length === 0 ? "No alerts in current time range" : "";
  93. });
  94. }
  95. getCurrentAlertState() {
  96. var params: any = {
  97. state: this.panel.stateFilter
  98. };
  99. if (this.panel.onlyAlertsOnDashboard) {
  100. params.dashboardId = this.dashboard.id;
  101. }
  102. this.backendSrv.get(`/api/alerts`, params).then(res => {
  103. this.currentAlerts = this.sortResult(
  104. _.map(res, al => {
  105. al.stateModel = alertDef.getStateDisplayModel(al.state);
  106. al.newStateDateAgo = moment(al.newStateDate)
  107. .locale("en")
  108. .fromNow(true);
  109. return al;
  110. })
  111. );
  112. this.noAlertsMessage = this.currentAlerts.length === 0 ? "No alerts" : "";
  113. });
  114. }
  115. onInitEditMode() {
  116. this.addEditorTab(
  117. "Options",
  118. "public/app/plugins/panel/alertlist/editor.html"
  119. );
  120. }
  121. }
  122. export { AlertListPanel, AlertListPanel as PanelCtrl };