alert_tab_ctrl.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import angular from 'angular';
  5. export class AlertTabCtrl {
  6. panel: any;
  7. panelCtrl: any;
  8. alerting: any;
  9. metricTargets = [{ refId: '- select query -' } ];
  10. evalFuncs = ['Greater Then', 'Percent Change'];
  11. aggregators = ['avg', 'sum', 'min', 'max', 'median'];
  12. rule: any;
  13. defaultValues = {
  14. aggregator: 'avg',
  15. frequency: 10,
  16. queryRange: 3600,
  17. warnOperator: '>',
  18. critOperator: '>',
  19. queryRef: '- select query -',
  20. valueExpr: 'query(#A, 5m, now, avg)',
  21. evalFunc: 'Greater Then',
  22. evalExpr: '',
  23. critLevel: 20,
  24. warnLevel: 10,
  25. };
  26. /** @ngInject */
  27. constructor($scope, private $timeout) {
  28. this.panelCtrl = $scope.ctrl;
  29. this.panel = this.panelCtrl.panel;
  30. $scope.ctrl = this;
  31. _.defaults(this.panel.alerting, this.defaultValues);
  32. this.rule = this.panel.alerting;
  33. var defaultName = (this.panelCtrl.dashboard.title + ' ' + this.panel.title + ' alert');
  34. this.panel.alerting.name = this.panel.alerting.name || defaultName;
  35. this.panel.targets.map(target => {
  36. this.metricTargets.push(target);
  37. });
  38. this.panel.alerting.queryRef = this.panel.alerting.queryRef || this.metricTargets[0].refId;
  39. this.convertThresholdsToAlertThresholds();
  40. }
  41. convertThresholdsToAlertThresholds() {
  42. if (this.panel.grid
  43. && this.panel.grid.threshold1
  44. && this.panel.alerting.warnLevel === undefined
  45. ) {
  46. this.panel.alerting.warnOperator = '>';
  47. this.panel.alerting.warnLevel = this.panel.grid.threshold1;
  48. }
  49. if (this.panel.grid
  50. && this.panel.grid.threshold2
  51. && this.panel.alerting.critLevel === undefined
  52. ) {
  53. this.panel.alerting.critOperator = '>';
  54. this.panel.alerting.critLevel = this.panel.grid.threshold2;
  55. }
  56. }
  57. markAsDeleted() {
  58. if (this.panel.alerting) {
  59. this.panel.alerting = this.defaultValues;
  60. }
  61. }
  62. thresholdsUpdated() {
  63. if (this.panel.alerting.warnLevel) {
  64. this.panel.grid.threshold1 = parseInt(this.panel.alerting.warnLevel);
  65. }
  66. if (this.panel.alerting.critLevel) {
  67. this.panel.grid.threshold2 = parseInt(this.panel.alerting.critLevel);
  68. }
  69. this.panelCtrl.render();
  70. }
  71. }
  72. /** @ngInject */
  73. export function graphAlertEditor() {
  74. 'use strict';
  75. return {
  76. restrict: 'E',
  77. scope: true,
  78. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  79. controller: AlertTabCtrl,
  80. };
  81. }