alert_tab_ctrl.ts 2.3 KB

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