alert_list_ctrl.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import coreModule from '../../core/core_module';
  5. import appEvents from '../../core/app_events';
  6. import moment from 'moment';
  7. import alertDef from './alert_def';
  8. export class AlertListCtrl {
  9. alerts: any;
  10. stateFilters = [
  11. {text: 'All', value: null},
  12. {text: 'OK', value: 'ok'},
  13. {text: 'Alerting', value: 'alerting'},
  14. {text: 'No Data', value: 'no_data'},
  15. {text: 'Execution Error', value: 'execution_error'},
  16. ];
  17. filters = {
  18. state: 'ALL'
  19. };
  20. /** @ngInject */
  21. constructor(private backendSrv, private $location, private $scope) {
  22. var params = $location.search();
  23. this.filters.state = params.state || null;
  24. this.loadAlerts();
  25. }
  26. filtersChanged() {
  27. this.$location.search(this.filters);
  28. }
  29. loadAlerts() {
  30. this.backendSrv.get('/api/alerts', this.filters).then(result => {
  31. this.alerts = _.map(result, alert => {
  32. alert.stateModel = alertDef.getStateDisplayModel(alert.state);
  33. alert.newStateDateAgo = moment(alert.newStateDate).fromNow().replace(" ago", "");
  34. return alert;
  35. });
  36. });
  37. }
  38. pauseAlertRule(alertId: any) {
  39. var alert = _.find(this.alerts, {id: alertId});
  40. var payload = {
  41. paused: alert.state !== "paused"
  42. };
  43. this.backendSrv.post(`/api/alerts/${alert.id}/pause`, payload).then(result => {
  44. alert.state = result.state;
  45. alert.stateModel = alertDef.getStateDisplayModel(result.state);
  46. });
  47. }
  48. openHowTo() {
  49. appEvents.emit('show-modal', {
  50. src: 'public/app/features/alerting/partials/alert_howto.html',
  51. modalClass: 'confirm-modal',
  52. model: {}
  53. });
  54. }
  55. }
  56. coreModule.controller('AlertListCtrl', AlertListCtrl);