module.ts 3.6 KB

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