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. this.panelCtrl.editingAlert = true;
  69. this.panelCtrl.render();
  70. $scope.$on("$destroy", () => {
  71. this.panelCtrl.editingAlert = false;
  72. this.panelCtrl.render();
  73. });
  74. }
  75. initAlertModel() {
  76. this.alert = this.panel.alert = this.panel.alert || {};
  77. // set defaults
  78. _.defaults(this.alert, this.defaultValues);
  79. var defaultName = (this.panelCtrl.dashboard.title + ' ' + this.panel.title + ' alert');
  80. this.alert.name = this.alert.name || defaultName;
  81. this.alert.description = this.alert.description || defaultName;
  82. // great temp working model
  83. this.queryParams = {
  84. params: [
  85. this.alert.query.refId,
  86. this.alert.query.from,
  87. this.alert.query.to
  88. ]
  89. };
  90. // init the query part components model
  91. this.query = new QueryPart(this.queryParams, alertQueryDef);
  92. this.convertThresholdsToAlertThresholds();
  93. this.transformDef = _.findWhere(this.transforms, {type: this.alert.transform.type});
  94. }
  95. queryUpdated() {
  96. this.alert.query = {
  97. refId: this.query.params[0],
  98. from: this.query.params[1],
  99. to: this.query.params[2],
  100. };
  101. }
  102. transformChanged() {
  103. // clear model
  104. this.alert.transform = {type: this.alert.transform.type};
  105. this.transformDef = _.findWhere(this.transforms, {type: this.alert.transform.type});
  106. switch (this.alert.transform.type) {
  107. case 'aggregation': {
  108. this.alert.transform.method = 'avg';
  109. break;
  110. }
  111. case "forecast": {
  112. this.alert.transform.timespan = '7d';
  113. break;
  114. }
  115. }
  116. }
  117. convertThresholdsToAlertThresholds() {
  118. // if (this.panel.grid
  119. // && this.panel.grid.threshold1
  120. // && this.alert.warnLevel === undefined
  121. // ) {
  122. // this.alert.warning.op = '>';
  123. // this.alert.warning.level = this.panel.grid.threshold1;
  124. // }
  125. //
  126. // if (this.panel.grid
  127. // && this.panel.grid.threshold2
  128. // && this.alert.critical.level === undefined
  129. // ) {
  130. // this.alert.critical.op = '>';
  131. // this.alert.critical.level = this.panel.grid.threshold2;
  132. // }
  133. }
  134. delete() {
  135. this.alert = this.panel.alert = {};
  136. this.alert.deleted = true;
  137. this.initAlertModel();
  138. }
  139. enable() {
  140. delete this.alert.deleted;
  141. this.alert.enabled = true;
  142. }
  143. disable() {
  144. this.alert.enabled = false;
  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. }