module.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. contentHeight: string;
  16. stateFilter: any = {};
  17. currentAlerts: any = [];
  18. alertHistory: any = [];
  19. // Set and populate defaults
  20. panelDefaults = {
  21. show: 'current',
  22. limit: 10,
  23. stateFilter: [],
  24. onlyAlertsOnDashboard: false
  25. };
  26. /** @ngInject */
  27. constructor($scope, $injector, private $location, private backendSrv, private timeSrv, private templateSrv) {
  28. super($scope, $injector);
  29. _.defaults(this.panel, this.panelDefaults);
  30. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  31. this.events.on('render', this.onRender.bind(this));
  32. this.events.on('refresh', this.onRender.bind(this));
  33. for (let key in this.panel.stateFilter) {
  34. this.stateFilter[this.panel.stateFilter[key]] = true;
  35. }
  36. }
  37. updateStateFilter() {
  38. var result = [];
  39. for (let key in this.stateFilter) {
  40. if (this.stateFilter[key]) {
  41. result.push(key);
  42. }
  43. }
  44. this.panel.stateFilter = result;
  45. this.onRender();
  46. }
  47. onRender() {
  48. this.contentHeight = "max-height: " + this.height + "px;";
  49. if (this.panel.show === 'current') {
  50. this.getCurrentAlertState();
  51. }
  52. if (this.panel.show === 'changes') {
  53. this.getStateChanges();
  54. }
  55. }
  56. getStateChanges() {
  57. var params: any = {
  58. limit: this.panel.limit,
  59. type: 'alert',
  60. newState: this.panel.stateFilter,
  61. };
  62. if (this.panel.onlyAlertsOnDashboard) {
  63. params.dashboardId = this.dashboard.id;
  64. }
  65. params.from = dateMath.parse(this.dashboard.time.from).unix() * 1000;
  66. params.to = dateMath.parse(this.dashboard.time.to).unix() * 1000;
  67. this.backendSrv.get(`/api/annotations`, params)
  68. .then(res => {
  69. this.alertHistory = _.map(res, al => {
  70. al.time = moment(al.time).format('MMM D, YYYY HH:mm:ss');
  71. al.stateModel = alertDef.getStateDisplayModel(al.newState);
  72. al.metrics = alertDef.joinEvalMatches(al.data, ', ');
  73. return al;
  74. });
  75. });
  76. }
  77. getCurrentAlertState() {
  78. var params: any = {
  79. state: this.panel.stateFilter
  80. };
  81. if (this.panel.onlyAlertsOnDashboard) {
  82. params.dashboardId = this.dashboard.id;
  83. }
  84. this.backendSrv.get(`/api/alerts`, params)
  85. .then(res => {
  86. this.currentAlerts = _.map(res, al => {
  87. al.stateModel = alertDef.getStateDisplayModel(al.state);
  88. al.newStateDateAgo = moment(al.newStateDate).fromNow().replace(" ago", "");
  89. return al;
  90. });
  91. });
  92. }
  93. onInitEditMode() {
  94. this.addEditorTab('Options', 'public/app/plugins/panel/alertlist/editor.html');
  95. }
  96. }
  97. export {
  98. AlertListPanel,
  99. AlertListPanel as PanelCtrl
  100. }