notification_edit_ctrl.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import _ from 'lodash';
  2. import { appEvents, coreModule } from 'app/core/core';
  3. export class AlertNotificationEditCtrl {
  4. theForm: any;
  5. navModel: any;
  6. testSeverity = 'critical';
  7. notifiers: any;
  8. notifierTemplateId: string;
  9. isNew: boolean;
  10. model: any;
  11. defaults: any = {
  12. type: 'email',
  13. sendReminder: false,
  14. frequency: '15m',
  15. settings: {
  16. httpMethod: 'POST',
  17. autoResolve: true,
  18. uploadImage: true,
  19. },
  20. isDefault: false,
  21. };
  22. getFrequencySuggestion: any;
  23. /** @ngInject */
  24. constructor(private $routeParams, private backendSrv, private $location, private $templateCache, navModelSrv) {
  25. this.navModel = navModelSrv.getNav('alerting', 'channels', 0);
  26. this.isNew = !this.$routeParams.id;
  27. this.getFrequencySuggestion = () => {
  28. return ['1m', '5m', '10m', '15m', '30m', '1h'];
  29. };
  30. this.backendSrv
  31. .get(`/api/alert-notifiers`)
  32. .then(notifiers => {
  33. this.notifiers = notifiers;
  34. // add option templates
  35. for (const notifier of this.notifiers) {
  36. this.$templateCache.put(this.getNotifierTemplateId(notifier.type), notifier.optionsTemplate);
  37. }
  38. if (!this.$routeParams.id) {
  39. this.navModel.breadcrumbs.push({ text: 'New channel' });
  40. this.navModel.node = { text: 'New channel' };
  41. return _.defaults(this.model, this.defaults);
  42. }
  43. return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
  44. this.navModel.breadcrumbs.push({ text: result.name });
  45. this.navModel.node = { text: result.name };
  46. result.settings = _.defaults(result.settings, this.defaults.settings);
  47. return result;
  48. });
  49. })
  50. .then(model => {
  51. this.model = model;
  52. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  53. });
  54. }
  55. save() {
  56. if (!this.theForm.$valid) {
  57. return;
  58. }
  59. if (this.model.id) {
  60. this.backendSrv
  61. .put(`/api/alert-notifications/${this.model.id}`, this.model)
  62. .then(res => {
  63. this.model = res;
  64. appEvents.emit('alert-success', ['Notification updated', '']);
  65. })
  66. .catch(err => {
  67. if (err.data && err.data.error) {
  68. appEvents.emit('alert-error', [err.data.error]);
  69. }
  70. });
  71. } else {
  72. this.backendSrv
  73. .post(`/api/alert-notifications`, this.model)
  74. .then(res => {
  75. appEvents.emit('alert-success', ['Notification created', '']);
  76. this.$location.path('alerting/notifications');
  77. })
  78. .catch(err => {
  79. if (err.data && err.data.error) {
  80. appEvents.emit('alert-error', [err.data.error]);
  81. }
  82. });
  83. }
  84. }
  85. getNotifierTemplateId(type) {
  86. return `notifier-options-${type}`;
  87. }
  88. typeChanged() {
  89. this.model.settings = _.defaults({}, this.defaults.settings);
  90. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  91. }
  92. testNotification() {
  93. if (!this.theForm.$valid) {
  94. return;
  95. }
  96. const payload = {
  97. name: this.model.name,
  98. type: this.model.type,
  99. frequency: this.model.frequency,
  100. settings: this.model.settings,
  101. };
  102. this.backendSrv.post(`/api/alert-notifications/test`, payload).then(res => {
  103. appEvents.emit('alert-success', ['Test notification sent', '']);
  104. });
  105. }
  106. }
  107. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);