alert_tab_ctrl.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. query: {
  50. refId: 'A',
  51. from: '5m',
  52. to: 'now',
  53. },
  54. transform: {
  55. type: 'aggregation',
  56. method: 'avg'
  57. }
  58. };
  59. /** @ngInject */
  60. constructor($scope, private $timeout) {
  61. this.panelCtrl = $scope.ctrl;
  62. this.panel = this.panelCtrl.panel;
  63. $scope.ctrl = this;
  64. _.defaults(this.panel.alerting, this.defaultValues);
  65. this.rule = this.panel.alerting;
  66. this.valueQuery = new QueryPart(this.rule.valueQuery, alertQueryDef);
  67. this.evalQuery = new QueryPart(this.rule.evalQuery, alertQueryDef);
  68. var defaultName = (this.panelCtrl.dashboard.title + ' ' + this.panel.title + ' alert');
  69. this.panel.alerting.name = this.panel.alerting.name || defaultName;
  70. this.panel.alerting.description = this.panel.alerting.description || defaultName;
  71. this.panel.targets.map(target => {
  72. this.metricTargets.push(target);
  73. });
  74. this.panel.alerting.queryRef = this.panel.alerting.queryRef || this.metricTargets[0].refId;
  75. this.convertThresholdsToAlertThresholds();
  76. this.evalFuncChanged();
  77. }
  78. evalFuncChanged() {
  79. var evalFuncDef = _.findWhere(this.evalFuncs, { value: this.rule.evalFunc });
  80. console.log(evalFuncDef);
  81. this.secondParam = evalFuncDef.secondParam;
  82. }
  83. convertThresholdsToAlertThresholds() {
  84. if (this.panel.grid
  85. && this.panel.grid.threshold1
  86. && this.panel.alerting.warnLevel === undefined
  87. ) {
  88. this.panel.alerting.warnOperator = '>';
  89. this.panel.alerting.warnLevel = this.panel.grid.threshold1;
  90. }
  91. if (this.panel.grid
  92. && this.panel.grid.threshold2
  93. && this.panel.alerting.critLevel === undefined
  94. ) {
  95. this.panel.alerting.critOperator = '>';
  96. this.panel.alerting.critLevel = this.panel.grid.threshold2;
  97. }
  98. }
  99. markAsDeleted() {
  100. if (this.panel.alerting) {
  101. this.panel.alerting = this.defaultValues;
  102. }
  103. }
  104. thresholdsUpdated() {
  105. if (this.panel.alerting.warnLevel) {
  106. this.panel.grid.threshold1 = parseInt(this.panel.alerting.warnLevel);
  107. }
  108. if (this.panel.alerting.critLevel) {
  109. this.panel.grid.threshold2 = parseInt(this.panel.alerting.critLevel);
  110. }
  111. this.panelCtrl.render();
  112. }
  113. }
  114. /** @ngInject */
  115. export function graphAlertEditor() {
  116. 'use strict';
  117. return {
  118. restrict: 'E',
  119. scope: true,
  120. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  121. controller: AlertTabCtrl,
  122. };
  123. }