alert_tab_ctrl.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /** @ngInject */
  11. constructor($scope, private $timeout) {
  12. $scope.alertTab = this; //HACK ATTACK!
  13. this.panelCtrl = $scope.ctrl;
  14. this.panel = this.panelCtrl.panel;
  15. this.panel.alerting = this.panel.alerting || {};
  16. this.panel.alerting.aggregator = this.panel.alerting.aggregator || 'avg';
  17. this.panel.alerting.interval = this.panel.alerting.interval || '60s';
  18. this.panel.alerting.queryRange = this.panel.alerting.queryRange || '10m';
  19. this.panel.alerting.warnOperator = this.panel.alerting.warnOperator || '>';
  20. this.panel.alerting.critOperator = this.panel.alerting.critOperator || '>';
  21. var defaultTitle = (this.panelCtrl.dashboard.title + ' ' + this.panel.title + ' alert');
  22. this.panel.alerting.title = this.panel.alerting.title || defaultTitle;
  23. this.panel.targets.map(target => {
  24. this.metricTargets.push(target);
  25. });
  26. this.panel.alerting.queryRef = this.panel.alerting.queryRef || this.metricTargets[0].refId;
  27. this.convertThresholdsToAlertThresholds();
  28. }
  29. convertThresholdsToAlertThresholds() {
  30. if (this.panel.grid && this.panel.grid.threshold1) {
  31. this.panel.alerting.warnOperator = '>';
  32. this.panel.alerting.warnLevel = this.panel.grid.threshold1;
  33. }
  34. if (this.panel.grid && this.panel.grid.threshold2) {
  35. this.panel.alerting.critOperator = '>';
  36. this.panel.alerting.critLevel = this.panel.grid.threshold2;
  37. }
  38. }
  39. thresholdsUpdated() {
  40. if (this.panel.alerting.warnLevel) {
  41. this.panel.grid.threshold1 = parseInt(this.panel.alerting.warnLevel);
  42. }
  43. if (this.panel.alerting.critLevel) {
  44. this.panel.grid.threshold2 = parseInt(this.panel.alerting.critLevel);
  45. }
  46. this.panelCtrl.render();
  47. }
  48. }
  49. /** @ngInject */
  50. export function graphAlertEditor() {
  51. 'use strict';
  52. return {
  53. restrict: 'E',
  54. scope: true,
  55. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  56. controller: AlertTabCtrl,
  57. };
  58. }