module.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 => { return alertDef.alertStateSortScore[a.state]; });
  44. }
  45. var result = _.sortBy(alerts, a => { return a.name.toLowerCase(); });
  46. if (this.panel.sortOrder === 2) {
  47. result.reverse();
  48. }
  49. return result;
  50. }
  51. updateStateFilter() {
  52. var result = [];
  53. for (let key in this.stateFilter) {
  54. if (this.stateFilter[key]) {
  55. result.push(key);
  56. }
  57. }
  58. this.panel.stateFilter = result;
  59. this.onRefresh();
  60. }
  61. onRefresh() {
  62. if (this.panel.show === 'current') {
  63. this.getCurrentAlertState();
  64. }
  65. if (this.panel.show === 'changes') {
  66. this.getStateChanges();
  67. }
  68. }
  69. getStateChanges() {
  70. var params: any = {
  71. limit: this.panel.limit,
  72. type: 'alert',
  73. newState: this.panel.stateFilter,
  74. };
  75. if (this.panel.onlyAlertsOnDashboard) {
  76. params.dashboardId = this.dashboard.id;
  77. }
  78. params.from = dateMath.parse(this.dashboard.time.from).unix() * 1000;
  79. params.to = dateMath.parse(this.dashboard.time.to).unix() * 1000;
  80. this.backendSrv.get(`/api/annotations`, params)
  81. .then(res => {
  82. this.alertHistory = _.map(res, al => {
  83. al.time = this.dashboard.formatDate(al.time, 'MMM D, YYYY HH:mm:ss');
  84. al.stateModel = alertDef.getStateDisplayModel(al.newState);
  85. al.info = alertDef.getAlertAnnotationInfo(al);
  86. return al;
  87. });
  88. this.noAlertsMessage = this.alertHistory.length === 0 ? 'No alerts in current time range' : '';
  89. });
  90. }
  91. getCurrentAlertState() {
  92. var params: any = {
  93. state: this.panel.stateFilter
  94. };
  95. if (this.panel.onlyAlertsOnDashboard) {
  96. params.dashboardId = this.dashboard.id;
  97. }
  98. this.backendSrv.get(`/api/alerts`, params)
  99. .then(res => {
  100. this.currentAlerts = this.sortResult(_.map(res, al => {
  101. al.stateModel = alertDef.getStateDisplayModel(al.state);
  102. al.newStateDateAgo = moment(al.newStateDate).locale('en').fromNow(true);
  103. return al;
  104. }));
  105. this.noAlertsMessage = this.currentAlerts.length === 0 ? 'No alerts' : '';
  106. });
  107. }
  108. onInitEditMode() {
  109. this.addEditorTab('Options', 'public/app/plugins/panel/alertlist/editor.html');
  110. }
  111. }
  112. export {
  113. AlertListPanel,
  114. AlertListPanel as PanelCtrl
  115. };