alert_tab_ctrl.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. {name: "aggregation", type: "select", options: ['sum', 'avg', 'min', 'max', 'last']},
  16. ],
  17. defaultParams: ['#A', '5m', 'now', 'avg']
  18. });
  19. export class AlertTabCtrl {
  20. panel: any;
  21. panelCtrl: any;
  22. alerting: any;
  23. metricTargets = [{ refId: '- select query -' } ];
  24. evalFuncs = [
  25. {
  26. text: 'Static Threshold',
  27. value: 'static',
  28. },
  29. {
  30. text: 'Percent Change Compared To',
  31. value: 'percent_change',
  32. secondParam: "query",
  33. },
  34. {
  35. text: 'Forcast',
  36. value: 'forcast',
  37. secondParam: "duration",
  38. }
  39. ];
  40. aggregators = ['avg', 'sum', 'min', 'max', 'median'];
  41. rule: any;
  42. valueQuery: any;
  43. evalQuery: any;
  44. secondParam: any;
  45. defaultValues = {
  46. frequency: 10,
  47. warning: { op: '>', level: 10 },
  48. critical: { op: '>', level: 20 },
  49. function: 'static',
  50. valueQuery: {
  51. queryRefId: 'A',
  52. from: '5m',
  53. to: 'now',
  54. agg: 'avg',
  55. },
  56. evalQuery: {
  57. queryRefId: 'A',
  58. from: '5m',
  59. to: 'now',
  60. agg: 'avg',
  61. },
  62. evalStringParam1: '',
  63. };
  64. /** @ngInject */
  65. constructor($scope, private $timeout) {
  66. this.panelCtrl = $scope.ctrl;
  67. this.panel = this.panelCtrl.panel;
  68. $scope.ctrl = this;
  69. _.defaults(this.panel.alerting, this.defaultValues);
  70. this.rule = this.panel.alerting;
  71. this.valueQuery = new QueryPart(this.rule.valueQuery, alertQueryDef);
  72. this.evalQuery = new QueryPart(this.rule.evalQuery, alertQueryDef);
  73. var defaultName = (this.panelCtrl.dashboard.title + ' ' + this.panel.title + ' alert');
  74. this.panel.alerting.name = this.panel.alerting.name || defaultName;
  75. this.panel.targets.map(target => {
  76. this.metricTargets.push(target);
  77. });
  78. this.panel.alerting.queryRef = this.panel.alerting.queryRef || this.metricTargets[0].refId;
  79. this.convertThresholdsToAlertThresholds();
  80. this.evalFuncChanged();
  81. }
  82. evalFuncChanged() {
  83. var evalFuncDef = _.findWhere(this.evalFuncs, { value: this.rule.expression.evalFunc });
  84. console.log(evalFuncDef);
  85. this.secondParam = evalFuncDef.secondParam;
  86. }
  87. convertThresholdsToAlertThresholds() {
  88. if (this.panel.grid
  89. && this.panel.grid.threshold1
  90. && this.panel.alerting.warnLevel === undefined
  91. ) {
  92. this.panel.alerting.warnOperator = '>';
  93. this.panel.alerting.warnLevel = this.panel.grid.threshold1;
  94. }
  95. if (this.panel.grid
  96. && this.panel.grid.threshold2
  97. && this.panel.alerting.critLevel === undefined
  98. ) {
  99. this.panel.alerting.critOperator = '>';
  100. this.panel.alerting.critLevel = this.panel.grid.threshold2;
  101. }
  102. }
  103. markAsDeleted() {
  104. if (this.panel.alerting) {
  105. this.panel.alerting = this.defaultValues;
  106. }
  107. }
  108. thresholdsUpdated() {
  109. if (this.panel.alerting.warnLevel) {
  110. this.panel.grid.threshold1 = parseInt(this.panel.alerting.warnLevel);
  111. }
  112. if (this.panel.alerting.critLevel) {
  113. this.panel.grid.threshold2 = parseInt(this.panel.alerting.critLevel);
  114. }
  115. this.panelCtrl.render();
  116. }
  117. }
  118. /** @ngInject */
  119. export function graphAlertEditor() {
  120. 'use strict';
  121. return {
  122. restrict: 'E',
  123. scope: true,
  124. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  125. controller: AlertTabCtrl,
  126. };
  127. }