alert_tab_ctrl.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. export class AlertTabCtrl {
  19. panel: any;
  20. panelCtrl: any;
  21. metricTargets = [{ refId: '- select query -' } ];
  22. schedulers = [{text: 'Grafana', value: 1}, {text: 'External', value: 0}];
  23. transforms = [
  24. {
  25. text: 'Aggregation',
  26. type: 'aggregation',
  27. },
  28. {
  29. text: 'Linear Forecast',
  30. type: 'forecast',
  31. },
  32. ];
  33. aggregators = ['avg', 'sum', 'min', 'max', 'last'];
  34. alert: any;
  35. query: any;
  36. queryParams: any;
  37. transformDef: any;
  38. levelOpList = [
  39. {text: '>', value: '>'},
  40. {text: '<', value: '<'},
  41. {text: '=', value: '='},
  42. ];
  43. defaultValues = {
  44. frequency: '60s',
  45. notify: [],
  46. enabled: false,
  47. scheduler: 1,
  48. warn: { op: '>', level: undefined },
  49. critical: { op: '>', level: undefined },
  50. query: {
  51. refId: 'A',
  52. from: '5m',
  53. to: 'now',
  54. },
  55. transform: {
  56. type: 'aggregation',
  57. method: 'avg'
  58. }
  59. };
  60. /** @ngInject */
  61. constructor($scope, private $timeout) {
  62. this.panelCtrl = $scope.ctrl;
  63. this.panel = this.panelCtrl.panel;
  64. $scope.ctrl = this;
  65. this.metricTargets = this.panel.targets.map(val => val);
  66. this.initAlertModel();
  67. // set panel alert edit mode
  68. $scope.$on("$destroy", () => {
  69. this.panelCtrl.editingAlert = false;
  70. this.panelCtrl.render();
  71. });
  72. }
  73. initAlertModel() {
  74. if (!this.panel.alert) {
  75. return;
  76. }
  77. this.alert = this.panel.alert;
  78. // set defaults
  79. _.defaults(this.alert, this.defaultValues);
  80. var defaultName = (this.panelCtrl.dashboard.title + ' ' + this.panel.title + ' alert');
  81. this.alert.name = this.alert.name || defaultName;
  82. this.alert.description = this.alert.description || defaultName;
  83. // great temp working model
  84. this.queryParams = {
  85. params: [
  86. this.alert.query.refId,
  87. this.alert.query.from,
  88. this.alert.query.to
  89. ]
  90. };
  91. // init the query part components model
  92. this.query = new QueryPart(this.queryParams, alertQueryDef);
  93. this.convertThresholdsToAlertThresholds();
  94. this.transformDef = _.findWhere(this.transforms, {type: this.alert.transform.type});
  95. this.panelCtrl.editingAlert = true;
  96. this.panelCtrl.render();
  97. }
  98. queryUpdated() {
  99. this.alert.query = {
  100. refId: this.query.params[0],
  101. from: this.query.params[1],
  102. to: this.query.params[2],
  103. };
  104. }
  105. transformChanged() {
  106. // clear model
  107. this.alert.transform = {type: this.alert.transform.type};
  108. this.transformDef = _.findWhere(this.transforms, {type: this.alert.transform.type});
  109. switch (this.alert.transform.type) {
  110. case 'aggregation': {
  111. this.alert.transform.method = 'avg';
  112. break;
  113. }
  114. case "forecast": {
  115. this.alert.transform.timespan = '7d';
  116. break;
  117. }
  118. }
  119. }
  120. convertThresholdsToAlertThresholds() {
  121. // if (this.panel.grid
  122. // && this.panel.grid.threshold1
  123. // && this.alert.warnLevel === undefined
  124. // ) {
  125. // this.alert.warning.op = '>';
  126. // this.alert.warning.level = this.panel.grid.threshold1;
  127. // }
  128. //
  129. // if (this.panel.grid
  130. // && this.panel.grid.threshold2
  131. // && this.alert.critical.level === undefined
  132. // ) {
  133. // this.alert.critical.op = '>';
  134. // this.alert.critical.level = this.panel.grid.threshold2;
  135. // }
  136. }
  137. delete() {
  138. delete this.panel.alert;
  139. this.panelCtrl.editingAlert = false;
  140. this.panelCtrl.render();
  141. }
  142. enable() {
  143. this.panel.alert = {};
  144. this.initAlertModel();
  145. }
  146. levelsUpdated() {
  147. this.panelCtrl.render();
  148. }
  149. }
  150. /** @ngInject */
  151. export function graphAlertEditor() {
  152. 'use strict';
  153. return {
  154. restrict: 'E',
  155. scope: true,
  156. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  157. controller: AlertTabCtrl,
  158. };
  159. }