alert_tab_ctrl.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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']},
  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. handlers = [{text: 'Grafana', value: 1}, {text: 'External', value: 0}];
  27. conditionTypes = [
  28. {text: 'Query', value: 'query'},
  29. {text: 'Other alert', value: 'other_alert'},
  30. {text: 'Time of day', value: 'time_of_day'},
  31. {text: 'Day of week', value: 'day_of_week'},
  32. ];
  33. alert: any;
  34. conditionModels: any;
  35. evalFunctions = [
  36. {text: '>', value: '>'},
  37. {text: '<', value: '<'},
  38. ];
  39. severityLevels = [
  40. {text: 'Critical', value: 'critical'},
  41. {text: 'Warning', value: 'warning'},
  42. ];
  43. addNotificationSegment;
  44. notifications;
  45. alertNotifications;
  46. /** @ngInject */
  47. constructor(private $scope, private $timeout, private backendSrv, private dashboardSrv, private uiSegmentSrv) {
  48. this.panelCtrl = $scope.ctrl;
  49. this.panel = this.panelCtrl.panel;
  50. this.$scope.ctrl = this;
  51. }
  52. $onInit() {
  53. this.addNotificationSegment = this.uiSegmentSrv.newPlusButton();
  54. this.initModel();
  55. // set panel alert edit mode
  56. this.$scope.$on("$destroy", () => {
  57. this.panelCtrl.editingAlert = false;
  58. this.panelCtrl.render();
  59. });
  60. // build notification model
  61. this.notifications = [];
  62. this.alertNotifications = [];
  63. return this.backendSrv.get('/api/alert-notifications').then(res => {
  64. this.notifications = res;
  65. _.each(this.alert.notifications, item => {
  66. var model = _.findWhere(this.notifications, {id: item.id});
  67. if (model) {
  68. this.alertNotifications.push(model);
  69. }
  70. });
  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});
  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. alert.description = alert.description || defaultName;
  106. this.conditionModels = _.reduce(alert.conditions, (memo, value) => {
  107. memo.push(this.buildConditionModel(value));
  108. return memo;
  109. }, []);
  110. ///this.panelCtrl.editingAlert = true;
  111. this.syncThresholds();
  112. this.panelCtrl.render();
  113. }
  114. syncThresholds() {
  115. var threshold: any = {};
  116. if (this.panel.thresholds && this.panel.thresholds.length > 0) {
  117. threshold = this.panel.thresholds[0];
  118. } else {
  119. this.panel.thresholds = [threshold];
  120. }
  121. var updated = false;
  122. for (var condition of this.conditionModels) {
  123. if (condition.type === 'query') {
  124. var value = condition.evaluator.params[0];
  125. if (!_.isNumber(value)) {
  126. continue;
  127. }
  128. if (value !== threshold.from) {
  129. threshold.from = value;
  130. updated = true;
  131. }
  132. if (condition.evaluator.type === '<' && threshold.to !== -Infinity) {
  133. threshold.to = -Infinity;
  134. updated = true;
  135. } else if (condition.evaluator.type === '>' && threshold.to !== Infinity) {
  136. threshold.to = Infinity;
  137. updated = true;
  138. }
  139. }
  140. }
  141. return updated;
  142. }
  143. buildDefaultCondition() {
  144. return {
  145. type: 'query',
  146. query: {params: ['A', '5m', 'now']},
  147. reducer: {type: 'avg', params: []},
  148. evaluator: {type: '>', params: [null]},
  149. };
  150. }
  151. buildConditionModel(source) {
  152. var cm: any = {source: source, type: source.type};
  153. cm.queryPart = new QueryPart(source.query, alertQueryDef);
  154. cm.reducerPart = new QueryPart({params: []}, reducerAvgDef);
  155. cm.evaluator = source.evaluator;
  156. return cm;
  157. }
  158. queryPartUpdated(conditionModel) {
  159. }
  160. addCondition(type) {
  161. var condition = this.buildDefaultCondition();
  162. // add to persited model
  163. this.alert.conditions.push(condition);
  164. // add to view model
  165. this.conditionModels.push(this.buildConditionModel(condition));
  166. }
  167. removeCondition(index) {
  168. this.alert.conditions.splice(index, 1);
  169. this.conditionModels.splice(index, 1);
  170. }
  171. delete() {
  172. this.alert.enabled = false;
  173. this.initModel();
  174. }
  175. enable() {
  176. this.alert.enabled = true;
  177. this.initModel();
  178. }
  179. thresholdUpdated() {
  180. if (this.syncThresholds()) {
  181. this.panelCtrl.render();
  182. }
  183. }
  184. test() {
  185. this.testing = true;
  186. var payload = {
  187. dashboard: this.dashboardSrv.getCurrent().getSaveModelClone(),
  188. panelId: this.panelCtrl.panel.id,
  189. };
  190. return this.backendSrv.post('/api/alerts/test', payload).then(res => {
  191. this.testResult = res;
  192. this.testing = false;
  193. });
  194. }
  195. }
  196. /** @ngInject */
  197. export function graphAlertEditor() {
  198. 'use strict';
  199. return {
  200. restrict: 'E',
  201. scope: true,
  202. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  203. controller: AlertTabCtrl,
  204. };
  205. }