alert_tab_ctrl.ts 8.6 KB

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