alert_tab_ctrl.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 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. levelOpList = [
  37. {text: '>', value: '>'},
  38. {text: '<', value: '<'},
  39. {text: '=', value: '='},
  40. ];
  41. /** @ngInject */
  42. constructor($scope, private $timeout) {
  43. this.panelCtrl = $scope.ctrl;
  44. this.panel = this.panelCtrl.panel;
  45. $scope.ctrl = this;
  46. this.metricTargets = this.panel.targets.map(val => val);
  47. this.initModel();
  48. // set panel alert edit mode
  49. $scope.$on("$destroy", () => {
  50. this.panelCtrl.editingAlert = false;
  51. this.panelCtrl.render();
  52. });
  53. }
  54. getThresholdWithDefaults(threshold) {
  55. threshold = threshold || {};
  56. threshold.op = threshold.op || '>';
  57. threshold.value = threshold.value || undefined;
  58. return threshold;
  59. }
  60. initModel() {
  61. var alert = this.alert = this.panel.alert = this.panel.alert || {};
  62. alert.conditions = alert.conditions || [];
  63. if (alert.conditions.length === 0) {
  64. alert.conditions.push(this.buildDefaultCondition());
  65. }
  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. refId: 'A',
  83. from: '5m',
  84. to: 'now',
  85. reducer: 'avg',
  86. reducerParams: [],
  87. warn: this.getThresholdWithDefaults({}),
  88. crit: this.getThresholdWithDefaults({}),
  89. };
  90. }
  91. buildConditionModel(source) {
  92. var cm: any = {source: source, type: source.type};
  93. var queryPartModel = {
  94. params: [source.refId, source.from, source.to]
  95. };
  96. cm.queryPart = new QueryPart(queryPartModel, alertQueryDef);
  97. cm.reducerPart = new QueryPart({params: []}, reducerAvgDef);
  98. return cm;
  99. }
  100. queryPartUpdated(conditionModel) {
  101. conditionModel.source.refId = conditionModel.queryPart.params[0];
  102. conditionModel.source.from = conditionModel.queryPart.params[1];
  103. conditionModel.source.to = conditionModel.queryPart.params[2];
  104. }
  105. addCondition(type) {
  106. var condition = this.buildDefaultCondition();
  107. // add to persited model
  108. this.alert.conditions.push(condition);
  109. // add to view model
  110. this.conditionModels.push(this.buildConditionModel(condition));
  111. }
  112. removeCondition(index) {
  113. this.alert.conditions.splice(index, 1);
  114. this.conditionModels.splice(index, 1);
  115. }
  116. delete() {
  117. this.alert.enabled = false;
  118. this.alert.warn.value = undefined;
  119. this.alert.crit.value = undefined;
  120. // reset model but keep thresholds instance
  121. this.initModel();
  122. }
  123. enable() {
  124. this.alert.enabled = true;
  125. this.initModel();
  126. }
  127. thresholdsUpdated() {
  128. this.panelCtrl.render();
  129. }
  130. }
  131. /** @ngInject */
  132. export function graphAlertEditor() {
  133. 'use strict';
  134. return {
  135. restrict: 'E',
  136. scope: true,
  137. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  138. controller: AlertTabCtrl,
  139. };
  140. }