module.ts 3.5 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. contentHeight: string;
  19. stateFilter: any = {};
  20. currentAlerts: any = [];
  21. alertHistory: any = [];
  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('render', this.onRender.bind(this));
  36. this.events.on('refresh', this.onRender.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.onRender();
  60. }
  61. onRender() {
  62. this.contentHeight = "max-height: " + this.height + "px;";
  63. if (this.panel.show === 'current') {
  64. this.getCurrentAlertState();
  65. }
  66. if (this.panel.show === 'changes') {
  67. this.getStateChanges();
  68. }
  69. }
  70. getStateChanges() {
  71. var params: any = {
  72. limit: this.panel.limit,
  73. type: 'alert',
  74. newState: this.panel.stateFilter,
  75. };
  76. if (this.panel.onlyAlertsOnDashboard) {
  77. params.dashboardId = this.dashboard.id;
  78. }
  79. params.from = dateMath.parse(this.dashboard.time.from).unix() * 1000;
  80. params.to = dateMath.parse(this.dashboard.time.to).unix() * 1000;
  81. this.backendSrv.get(`/api/annotations`, params)
  82. .then(res => {
  83. this.alertHistory = _.map(res, al => {
  84. al.time = this.dashboard.formatDate(al.time, 'MMM D, YYYY HH:mm:ss');
  85. al.stateModel = alertDef.getStateDisplayModel(al.newState);
  86. al.info = alertDef.getAlertAnnotationInfo(al);
  87. return al;
  88. });
  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. });
  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. };