alert_tab_ctrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. ],
  16. defaultParams: ['#A', '5m', 'now', 'avg']
  17. });
  18. export class AlertTabCtrl {
  19. panel: any;
  20. panelCtrl: any;
  21. metricTargets;
  22. handlers = [{text: 'Grafana', value: 1}, {text: 'External', value: 0}];
  23. transforms = [
  24. {
  25. text: 'Aggregation',
  26. type: 'aggregation',
  27. },
  28. {
  29. text: 'Linear Forecast',
  30. type: 'forecast',
  31. },
  32. ];
  33. aggregators = ['avg', 'sum', 'min', 'max', 'last'];
  34. alert: any;
  35. thresholds: any;
  36. query: any;
  37. queryParams: any;
  38. transformDef: any;
  39. levelOpList = [
  40. {text: '>', value: '>'},
  41. {text: '<', value: '<'},
  42. {text: '=', value: '='},
  43. ];
  44. /** @ngInject */
  45. constructor($scope, private $timeout) {
  46. this.panelCtrl = $scope.ctrl;
  47. this.panel = this.panelCtrl.panel;
  48. $scope.ctrl = this;
  49. this.metricTargets = this.panel.targets.map(val => val);
  50. this.initModel();
  51. // set panel alert edit mode
  52. $scope.$on("$destroy", () => {
  53. this.panelCtrl.editingAlert = false;
  54. this.panelCtrl.render();
  55. });
  56. }
  57. getThresholdWithDefaults(threshold) {
  58. threshold = threshold || {};
  59. threshold.op = threshold.op || '>';
  60. threshold.value = threshold.value || undefined;
  61. return threshold;
  62. }
  63. initModel() {
  64. var alert = this.alert = this.panel.alert = this.panel.alert || {};
  65. // set threshold defaults
  66. alert.warn = this.getThresholdWithDefaults(alert.warn);
  67. alert.crit = this.getThresholdWithDefaults(alert.crit);
  68. alert.query = alert.query || {};
  69. alert.query.refId = alert.query.refId || 'A';
  70. alert.query.from = alert.query.from || '5m';
  71. alert.query.to = alert.query.to || 'now';
  72. alert.transform = alert.transform || {};
  73. alert.transform.type = alert.transform.type || 'aggregation';
  74. alert.transform.method = alert.transform.method || 'avg';
  75. alert.frequency = alert.frequency || '60s';
  76. alert.handler = alert.handler || 1;
  77. alert.notifications = alert.notifications || [];
  78. var defaultName = this.panel.title + ' alert';
  79. alert.name = alert.name || defaultName;
  80. alert.description = alert.description || defaultName;
  81. // great temp working model
  82. this.queryParams = {
  83. params: [alert.query.refId, alert.query.from, alert.query.to]
  84. };
  85. // init the query part components model
  86. this.query = new QueryPart(this.queryParams, alertQueryDef);
  87. this.transformDef = _.findWhere(this.transforms, {type: alert.transform.type});
  88. this.panelCtrl.editingAlert = true;
  89. this.panelCtrl.render();
  90. }
  91. queryUpdated() {
  92. this.alert.query = {
  93. refId: this.query.params[0],
  94. from: this.query.params[1],
  95. to: this.query.params[2],
  96. };
  97. }
  98. transformChanged() {
  99. // clear model
  100. this.alert.transform = {type: this.alert.transform.type};
  101. this.transformDef = _.findWhere(this.transforms, {type: this.alert.transform.type});
  102. switch (this.alert.transform.type) {
  103. case 'aggregation': {
  104. this.alert.transform.method = 'avg';
  105. break;
  106. }
  107. case "forecast": {
  108. this.alert.transform.timespan = '7d';
  109. break;
  110. }
  111. }
  112. }
  113. delete() {
  114. this.alert.enabled = false;
  115. this.alert.warn.value = undefined;
  116. this.alert.crit.value = undefined;
  117. // reset model but keep thresholds instance
  118. this.initModel();
  119. }
  120. enable() {
  121. this.alert.enabled = true;
  122. this.initModel();
  123. }
  124. thresholdsUpdated() {
  125. this.panelCtrl.render();
  126. }
  127. }
  128. /** @ngInject */
  129. export function graphAlertEditor() {
  130. 'use strict';
  131. return {
  132. restrict: 'E',
  133. scope: true,
  134. templateUrl: 'public/app/plugins/panel/graph/partials/tab_alerting.html',
  135. controller: AlertTabCtrl,
  136. };
  137. }