alert_tab_ctrl.ts 4.0 KB

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