alert_tab_ctrl.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. defaultValues = {
  13. aggregator: 'avg',
  14. frequency: 10,
  15. queryRange: 3600,
  16. warnOperator: '>',
  17. critOperator: '>',
  18. queryRef: '- select query -'
  19. };
  20. /** @ngInject */
  21. constructor($scope, private $timeout) {
  22. $scope.alertTab = this; //HACK ATTACK!
  23. this.panelCtrl = $scope.ctrl;
  24. this.panel = this.panelCtrl.panel;
  25. _.defaults(this.panel.alerting, this.defaultValues);
  26. var defaultName = (this.panelCtrl.dashboard.title + ' ' + this.panel.title + ' alert');
  27. this.panel.alerting.name = this.panel.alerting.name || defaultName;
  28. this.panel.targets.map(target => {
  29. this.metricTargets.push(target);
  30. });
  31. this.panel.alerting.queryRef = this.panel.alerting.queryRef || this.metricTargets[0].refId;
  32. this.convertThresholdsToAlertThresholds();
  33. }
  34. convertThresholdsToAlertThresholds() {
  35. if (this.panel.grid
  36. && this.panel.grid.threshold1
  37. && this.panel.alerting.warnLevel === undefined
  38. ) {
  39. this.panel.alerting.warnOperator = '>';
  40. this.panel.alerting.warnLevel = this.panel.grid.threshold1;
  41. }
  42. if (this.panel.grid
  43. && this.panel.grid.threshold2
  44. && this.panel.alerting.critLevel === undefined
  45. ) {
  46. this.panel.alerting.critOperator = '>';
  47. this.panel.alerting.critLevel = this.panel.grid.threshold2;
  48. }
  49. }
  50. markAsDeleted() {
  51. if (this.panel.alerting) {
  52. this.panel.alerting = this.defaultValues;
  53. }
  54. }
  55. thresholdsUpdated() {
  56. if (this.panel.alerting.warnLevel) {
  57. this.panel.grid.threshold1 = parseInt(this.panel.alerting.warnLevel);
  58. }
  59. if (this.panel.alerting.critLevel) {
  60. this.panel.grid.threshold2 = parseInt(this.panel.alerting.critLevel);
  61. }
  62. this.panelCtrl.render();
  63. }
  64. }
  65. /** @ngInject */
  66. export function graphAlertEditor() {
  67. 'use strict';
  68. return {
  69. restrict: 'E',
  70. scope: true,
  71. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  72. controller: AlertTabCtrl,
  73. };
  74. }