alert_tab_ctrl.ts 4.1 KB

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