alert_tab_ctrl.ts 6.6 KB

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