alert_tab_ctrl.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import {ThresholdMapper} from './threshold_mapper';
  4. import {QueryPart} from 'app/core/components/query_part/query_part';
  5. import alertDef from './alert_def';
  6. export class AlertTabCtrl {
  7. panel: any;
  8. panelCtrl: any;
  9. testing: boolean;
  10. testResult: any;
  11. subTabIndex: number;
  12. conditionTypes: any;
  13. alert: any;
  14. conditionModels: any;
  15. evalFunctions: any;
  16. severityLevels: any;
  17. addNotificationSegment;
  18. notifications;
  19. alertNotifications;
  20. /** @ngInject */
  21. constructor(private $scope, private $timeout, private backendSrv, private dashboardSrv, private uiSegmentSrv, private $q) {
  22. this.panelCtrl = $scope.ctrl;
  23. this.panel = this.panelCtrl.panel;
  24. this.$scope.ctrl = this;
  25. this.subTabIndex = 0;
  26. this.evalFunctions = alertDef.evalFunctions;
  27. this.conditionTypes = alertDef.conditionTypes;
  28. this.severityLevels = alertDef.severityLevels;
  29. }
  30. $onInit() {
  31. this.addNotificationSegment = this.uiSegmentSrv.newPlusButton();
  32. this.initModel();
  33. // set panel alert edit mode
  34. this.$scope.$on("$destroy", () => {
  35. this.panelCtrl.editingThresholds = false;
  36. this.panelCtrl.render();
  37. });
  38. // subscribe to graph threshold handle changes
  39. this.panelCtrl.events.on('threshold-changed', this.graphThresholdChanged.bind(this));
  40. // build notification model
  41. this.notifications = [];
  42. this.alertNotifications = [];
  43. return this.backendSrv.get('/api/alert-notifications').then(res => {
  44. this.notifications = res;
  45. _.each(this.alert.notifications, item => {
  46. var model = _.findWhere(this.notifications, {id: item.id});
  47. if (model) {
  48. model.iconClass = this.getNotificationIcon(model.type);
  49. this.alertNotifications.push(model);
  50. }
  51. });
  52. });
  53. }
  54. getNotificationIcon(type) {
  55. switch (type) {
  56. case "email": return "fa fa-envelope";
  57. case "slack": return "fa fa-slack";
  58. case "webhook": return "fa fa-cubes";
  59. }
  60. }
  61. getNotifications() {
  62. return Promise.resolve(this.notifications.map(item => {
  63. return this.uiSegmentSrv.newSegment(item.name);
  64. }));
  65. }
  66. notificationAdded() {
  67. var model = _.findWhere(this.notifications, {name: this.addNotificationSegment.value});
  68. if (!model) {
  69. return;
  70. }
  71. this.alertNotifications.push({name: model.name, iconClass: this.getNotificationIcon(model.type)});
  72. this.alert.notifications.push({id: model.id});
  73. // reset plus button
  74. this.addNotificationSegment.value = this.uiSegmentSrv.newPlusButton().value;
  75. this.addNotificationSegment.html = this.uiSegmentSrv.newPlusButton().html;
  76. }
  77. removeNotification(index) {
  78. this.alert.notifications.splice(index, 1);
  79. this.alertNotifications.splice(index, 1);
  80. }
  81. initModel() {
  82. var alert = this.alert = this.panel.alert = this.panel.alert || {};
  83. alert.conditions = alert.conditions || [];
  84. if (alert.conditions.length === 0) {
  85. alert.conditions.push(this.buildDefaultCondition());
  86. }
  87. alert.severity = alert.severity || 'critical';
  88. alert.frequency = alert.frequency || '60s';
  89. alert.handler = alert.handler || 1;
  90. alert.notifications = alert.notifications || [];
  91. var defaultName = this.panel.title + ' alert';
  92. alert.name = alert.name || defaultName;
  93. this.conditionModels = _.reduce(alert.conditions, (memo, value) => {
  94. memo.push(this.buildConditionModel(value));
  95. return memo;
  96. }, []);
  97. if (this.alert.enabled) {
  98. this.panelCtrl.editingThresholds = true;
  99. }
  100. ThresholdMapper.alertToGraphThresholds(this.panel);
  101. this.panelCtrl.render();
  102. }
  103. graphThresholdChanged(evt) {
  104. for (var condition of this.alert.conditions) {
  105. if (condition.type === 'query') {
  106. condition.evaluator.params[evt.handleIndex] = evt.threshold.value;
  107. this.evaluatorParamsChanged();
  108. break;
  109. }
  110. }
  111. }
  112. buildDefaultCondition() {
  113. return {
  114. type: 'query',
  115. query: {params: ['A', '5m', 'now']},
  116. reducer: {type: 'avg', params: []},
  117. evaluator: {type: 'gt', params: [null]},
  118. };
  119. }
  120. buildConditionModel(source) {
  121. var cm: any = {source: source, type: source.type};
  122. cm.queryPart = new QueryPart(source.query, alertDef.alertQueryDef);
  123. cm.reducerPart = alertDef.createReducerPart(source.reducer);
  124. cm.evaluator = source.evaluator;
  125. return cm;
  126. }
  127. handleQueryPartEvent(conditionModel, evt) {
  128. switch (evt.name) {
  129. case "action-remove-part": {
  130. break;
  131. }
  132. case "get-part-actions": {
  133. return this.$q.when([]);
  134. }
  135. }
  136. }
  137. handleReducerPartEvent(conditionModel, evt) {
  138. switch (evt.name) {
  139. case "action": {
  140. conditionModel.source.reducer.type = evt.action.value;
  141. conditionModel.reducerPart = alertDef.createReducerPart(conditionModel.source.reducer);
  142. break;
  143. }
  144. case "get-part-actions": {
  145. var result = [];
  146. for (var type of alertDef.reducerTypes) {
  147. if (type.value !== conditionModel.source.reducer.type) {
  148. result.push(type);
  149. }
  150. }
  151. return this.$q.when(result);
  152. }
  153. }
  154. }
  155. addCondition(type) {
  156. var condition = this.buildDefaultCondition();
  157. // add to persited model
  158. this.panelCtrl.conditions.push(condition);
  159. // add to view model
  160. this.conditionModels.push(this.buildConditionModel(condition));
  161. }
  162. removeCondition(index) {
  163. this.alert.conditions.splice(index, 1);
  164. this.conditionModels.splice(index, 1);
  165. }
  166. delete() {
  167. this.panel.alert = {enabled: false};
  168. this.panel.thresholds = [];
  169. this.conditionModels = [];
  170. this.panelCtrl.render();
  171. }
  172. enable() {
  173. this.alert.enabled = true;
  174. this.initModel();
  175. }
  176. evaluatorParamsChanged() {
  177. ThresholdMapper.alertToGraphThresholds(this.panel);
  178. this.panelCtrl.render();
  179. }
  180. severityChanged() {
  181. ThresholdMapper.alertToGraphThresholds(this.panel);
  182. this.panelCtrl.render();
  183. }
  184. evaluatorTypeChanged(evaluator) {
  185. // ensure params array is correct length
  186. switch (evaluator.type) {
  187. case "lt":
  188. case "gt": {
  189. evaluator.params = [evaluator.params[0]];
  190. break;
  191. }
  192. case "within_range":
  193. case "outside_range": {
  194. evaluator.params = [evaluator.params[0], evaluator.params[1]];
  195. break;
  196. }
  197. case "no_value": {
  198. evaluator.params = [];
  199. }
  200. }
  201. this.evaluatorParamsChanged();
  202. }
  203. test() {
  204. this.testing = true;
  205. var payload = {
  206. dashboard: this.dashboardSrv.getCurrent().getSaveModelClone(),
  207. panelId: this.panelCtrl.panel.id,
  208. };
  209. return this.backendSrv.post('/api/alerts/test', payload).then(res => {
  210. this.testResult = res;
  211. this.testing = false;
  212. });
  213. }
  214. }
  215. /** @ngInject */
  216. export function alertTab() {
  217. 'use strict';
  218. return {
  219. restrict: 'E',
  220. scope: true,
  221. templateUrl: 'public/app/features/alerting/partials/alert_tab.html',
  222. controller: AlertTabCtrl,
  223. };
  224. }