alert_tab_ctrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import angular from 'angular';
  5. import {
  6. QueryPartDef,
  7. QueryPart,
  8. } from 'app/core/components/query_part/query_part';
  9. var alertQueryDef = new QueryPartDef({
  10. type: 'query',
  11. params: [
  12. {name: "queryRefId", type: 'string', options: ['#A', '#B', '#C', '#D']},
  13. {name: "from", type: "string", options: ['1s', '10s', '1m', '5m', '10m', '15m', '1h']},
  14. {name: "to", type: "string", options: ['now']},
  15. ],
  16. defaultParams: ['#A', '5m', 'now', 'avg']
  17. });
  18. var reducerAvgDef = new QueryPartDef({
  19. type: 'avg',
  20. params: [],
  21. defaultParams: []
  22. });
  23. export class AlertTabCtrl {
  24. panel: any;
  25. panelCtrl: any;
  26. metricTargets;
  27. testing: boolean;
  28. testResult: any;
  29. handlers = [{text: 'Grafana', value: 1}, {text: 'External', value: 0}];
  30. conditionTypes = [
  31. {text: 'Query', value: 'query'},
  32. {text: 'Other alert', value: 'other_alert'},
  33. {text: 'Time of day', value: 'time_of_day'},
  34. {text: 'Day of week', value: 'day_of_week'},
  35. ];
  36. alert: any;
  37. conditionModels: any;
  38. evalFunctions = [
  39. {text: '>', value: '>'},
  40. {text: '<', value: '<'},
  41. ];
  42. severityLevels = [
  43. {text: 'Critical', value: 'critical'},
  44. {text: 'Warning', value: 'warning'},
  45. ];
  46. /** @ngInject */
  47. constructor($scope, private $timeout, private backendSrv, private dashboardSrv) {
  48. this.panelCtrl = $scope.ctrl;
  49. this.panel = this.panelCtrl.panel;
  50. $scope.ctrl = this;
  51. this.metricTargets = this.panel.targets.map(val => val);
  52. this.initModel();
  53. // set panel alert edit mode
  54. $scope.$on("$destroy", () => {
  55. this.panelCtrl.editingAlert = false;
  56. this.panelCtrl.render();
  57. });
  58. }
  59. initModel() {
  60. var alert = this.alert = this.panel.alert = this.panel.alert || {};
  61. alert.conditions = alert.conditions || [];
  62. if (alert.conditions.length === 0) {
  63. alert.conditions.push(this.buildDefaultCondition());
  64. }
  65. alert.severity = alert.severity || 'critical';
  66. alert.frequency = alert.frequency || '60s';
  67. alert.handler = alert.handler || 1;
  68. alert.notifications = alert.notifications || [];
  69. var defaultName = this.panel.title + ' alert';
  70. alert.name = alert.name || defaultName;
  71. alert.description = alert.description || defaultName;
  72. this.conditionModels = _.reduce(alert.conditions, (memo, value) => {
  73. memo.push(this.buildConditionModel(value));
  74. return memo;
  75. }, []);
  76. this.panelCtrl.editingAlert = true;
  77. this.panelCtrl.render();
  78. }
  79. buildDefaultCondition() {
  80. return {
  81. type: 'query',
  82. query: {params: ['A', '5m', 'now']},
  83. reducer: {type: 'avg', params: []},
  84. evaluator: {type: '>', params: [null]},
  85. };
  86. }
  87. buildConditionModel(source) {
  88. var cm: any = {source: source, type: source.type};
  89. cm.queryPart = new QueryPart(source.query, alertQueryDef);
  90. cm.reducerPart = new QueryPart({params: []}, reducerAvgDef);
  91. cm.evaluator = source.evaluator;
  92. return cm;
  93. }
  94. queryPartUpdated(conditionModel) {
  95. }
  96. addCondition(type) {
  97. var condition = this.buildDefaultCondition();
  98. // add to persited model
  99. this.alert.conditions.push(condition);
  100. // add to view model
  101. this.conditionModels.push(this.buildConditionModel(condition));
  102. }
  103. removeCondition(index) {
  104. this.alert.conditions.splice(index, 1);
  105. this.conditionModels.splice(index, 1);
  106. }
  107. delete() {
  108. this.alert.enabled = false;
  109. this.initModel();
  110. }
  111. enable() {
  112. this.alert.enabled = true;
  113. this.initModel();
  114. }
  115. thresholdsUpdated() {
  116. this.panelCtrl.render();
  117. }
  118. test() {
  119. this.testing = true;
  120. var payload = {
  121. dashboard: this.dashboardSrv.getCurrent().getSaveModelClone(),
  122. panelId: this.panelCtrl.panel.id,
  123. };
  124. return this.backendSrv.post('/api/alerts/test', payload).then(res => {
  125. this.testResult = res;
  126. this.testing = false;
  127. });
  128. }
  129. }
  130. /** @ngInject */
  131. export function graphAlertEditor() {
  132. 'use strict';
  133. return {
  134. restrict: 'E',
  135. scope: true,
  136. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  137. controller: AlertTabCtrl,
  138. };
  139. }