module.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. class AlertListPanel extends PanelCtrl {
  8. static templateUrl = 'module.html';
  9. showOptions = [
  10. {text: 'Current state', value: 'current'},
  11. {text: 'State changes', value: 'changes'},
  12. ];
  13. currentAlerts: any = [];
  14. alertHistory: any = [];
  15. // Set and populate defaults
  16. panelDefaults = {
  17. show: 'current'
  18. };
  19. /** @ngInject */
  20. constructor($scope, $injector, private $location, private backendSrv) {
  21. super($scope, $injector);
  22. _.defaults(this.panel, this.panelDefaults);
  23. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  24. this.events.on('render', this.onRender.bind(this));
  25. this.events.on('refresh', this.onRender.bind(this));
  26. }
  27. onRender() {
  28. if (this.panel.show === 'current') {
  29. this.getCurrentAlertState();
  30. }
  31. if (this.panel.show === 'changes') {
  32. this.getAlertHistory();
  33. }
  34. }
  35. getAlertHistory() {
  36. this.backendSrv.get(`/api/alert-history?dashboardId=32&panelId=1`)
  37. .then(res => {
  38. this.alertHistory = _.map(res, al => {
  39. al.time = moment(al.timestamp).format('MMM D, YYYY HH:mm:ss');
  40. al.stateModel = alertDef.getStateDisplayModel(al.newState);
  41. return al;
  42. });
  43. });
  44. }
  45. getCurrentAlertState() {
  46. this.backendSrv.get(`/api/alerts`)
  47. .then(res => {
  48. this.currentAlerts = _.map(res, al => {
  49. al.stateModel = alertDef.getStateDisplayModel(al.state);
  50. al.newStateDateAgo = moment(al.newStateDate).fromNow().replace(" ago", "");
  51. return al;
  52. });
  53. });
  54. }
  55. onInitEditMode() {
  56. this.addEditorTab('Options', 'public/app/plugins/panel/alertlist/editor.html');
  57. }
  58. }
  59. export {
  60. AlertListPanel,
  61. AlertListPanel as PanelCtrl
  62. }