alert_tab_ctrl.ts 8.3 KB

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