module.ts 3.6 KB

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