alert_tab_ctrl.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. evalFunctions = [
  37. {text: '>', value: '>'},
  38. {text: '<', value: '<'},
  39. ];
  40. severityLevels = [
  41. {text: 'Critical', value: 'critical'},
  42. {text: 'Warning', value: 'warning'},
  43. ];
  44. /** @ngInject */
  45. constructor($scope, private $timeout) {
  46. this.panelCtrl = $scope.ctrl;
  47. this.panel = this.panelCtrl.panel;
  48. $scope.ctrl = this;
  49. this.metricTargets = this.panel.targets.map(val => val);
  50. this.initModel();
  51. // set panel alert edit mode
  52. $scope.$on("$destroy", () => {
  53. this.panelCtrl.editingAlert = false;
  54. this.panelCtrl.render();
  55. });
  56. }
  57. initModel() {
  58. var alert = this.alert = this.panel.alert = this.panel.alert || {};
  59. alert.conditions = [];
  60. if (alert.conditions.length === 0) {
  61. alert.conditions.push(this.buildDefaultCondition());
  62. }
  63. alert.severity = alert.severity || 'critical';
  64. alert.frequency = alert.frequency || '60s';
  65. alert.handler = alert.handler || 1;
  66. alert.notifications = alert.notifications || [];
  67. var defaultName = this.panel.title + ' alert';
  68. alert.name = alert.name || defaultName;
  69. alert.description = alert.description || defaultName;
  70. this.conditionModels = _.reduce(alert.conditions, (memo, value) => {
  71. memo.push(this.buildConditionModel(value));
  72. return memo;
  73. }, []);
  74. this.panelCtrl.editingAlert = true;
  75. this.panelCtrl.render();
  76. }
  77. buildDefaultCondition() {
  78. return {
  79. type: 'query',
  80. query: {params: ['A', '5m', 'now']},
  81. reducer: {type: 'avg', params: []},
  82. evaluator: {type: '>', params: [null]},
  83. };
  84. }
  85. buildConditionModel(source) {
  86. var cm: any = {source: source, type: source.type};
  87. cm.queryPart = new QueryPart(source.query, alertQueryDef);
  88. cm.reducerPart = new QueryPart({params: []}, reducerAvgDef);
  89. cm.evaluator = source.evaluator;
  90. return cm;
  91. }
  92. queryPartUpdated(conditionModel) {
  93. }
  94. addCondition(type) {
  95. var condition = this.buildDefaultCondition();
  96. // add to persited model
  97. this.alert.conditions.push(condition);
  98. // add to view model
  99. this.conditionModels.push(this.buildConditionModel(condition));
  100. }
  101. removeCondition(index) {
  102. this.alert.conditions.splice(index, 1);
  103. this.conditionModels.splice(index, 1);
  104. }
  105. delete() {
  106. this.alert.enabled = false;
  107. this.initModel();
  108. }
  109. enable() {
  110. this.alert.enabled = true;
  111. this.initModel();
  112. }
  113. thresholdsUpdated() {
  114. this.panelCtrl.render();
  115. }
  116. }
  117. /** @ngInject */
  118. export function graphAlertEditor() {
  119. 'use strict';
  120. return {
  121. restrict: 'E',
  122. scope: true,
  123. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  124. controller: AlertTabCtrl,
  125. };
  126. }