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: 'Alert state', value: 'alert_state'},
  31. ];
  32. alert: any;
  33. conditionModels: any;
  34. levelOpList = [
  35. {text: '>', value: '>'},
  36. {text: '<', value: '<'},
  37. {text: '=', value: '='},
  38. ];
  39. /** @ngInject */
  40. constructor($scope, private $timeout) {
  41. this.panelCtrl = $scope.ctrl;
  42. this.panel = this.panelCtrl.panel;
  43. $scope.ctrl = this;
  44. this.metricTargets = this.panel.targets.map(val => val);
  45. this.initModel();
  46. // set panel alert edit mode
  47. $scope.$on("$destroy", () => {
  48. this.panelCtrl.editingAlert = false;
  49. this.panelCtrl.render();
  50. });
  51. }
  52. getThresholdWithDefaults(threshold) {
  53. threshold = threshold || {};
  54. threshold.op = threshold.op || '>';
  55. threshold.value = threshold.value || undefined;
  56. return threshold;
  57. }
  58. initModel() {
  59. var alert = this.alert = this.panel.alert = this.panel.alert || {};
  60. // set threshold defaults
  61. alert.warn = this.getThresholdWithDefaults(alert.warn);
  62. alert.crit = this.getThresholdWithDefaults(alert.crit);
  63. alert.conditions = alert.conditions || [];
  64. if (alert.conditions.length === 0) {
  65. alert.conditions.push(this.buildDefaultCondition());
  66. }
  67. alert.frequency = alert.frequency || '60s';
  68. alert.handler = alert.handler || 1;
  69. alert.notifications = alert.notifications || [];
  70. var defaultName = this.panel.title + ' alert';
  71. alert.name = alert.name || defaultName;
  72. alert.description = alert.description || defaultName;
  73. this.conditionModels = _.reduce(alert.conditions, (memo, value) => {
  74. memo.push(this.buildConditionModel(value));
  75. return memo;
  76. }, []);
  77. this.panelCtrl.editingAlert = true;
  78. this.panelCtrl.render();
  79. }
  80. buildDefaultCondition() {
  81. return {
  82. type: 'query',
  83. refId: 'A',
  84. from: '5m',
  85. to: 'now',
  86. reducer: 'avg',
  87. reducerParams: [],
  88. };
  89. }
  90. buildConditionModel(source) {
  91. var cm: any = {source: source, type: source.type};
  92. var queryPartModel = {
  93. params: [source.refId, source.from, source.to]
  94. };
  95. cm.queryPart = new QueryPart(queryPartModel, alertQueryDef);
  96. cm.reducerPart = new QueryPart({params: []}, reducerAvgDef);
  97. return cm;
  98. }
  99. queryPartUpdated(conditionModel) {
  100. conditionModel.source.refId = conditionModel.queryPart.params[0];
  101. conditionModel.source.from = conditionModel.queryPart.params[1];
  102. conditionModel.source.to = conditionModel.queryPart.params[2];
  103. }
  104. addCondition(type) {
  105. var condition = this.buildDefaultCondition();
  106. // add to persited model
  107. this.alert.conditions.push(condition);
  108. // add to view model
  109. this.conditionModels.push(this.buildConditionModel(condition));
  110. }
  111. removeCondition(index) {
  112. this.alert.conditions.splice(index, 1);
  113. this.conditionModels.splice(index, 1);
  114. }
  115. delete() {
  116. this.alert.enabled = false;
  117. this.alert.warn.value = undefined;
  118. this.alert.crit.value = undefined;
  119. // reset model but keep thresholds instance
  120. this.initModel();
  121. }
  122. enable() {
  123. this.alert.enabled = true;
  124. this.initModel();
  125. }
  126. thresholdsUpdated() {
  127. this.panelCtrl.render();
  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. }