alertDef.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import _ from 'lodash';
  2. import { QueryPartDef, QueryPart } from 'app/core/components/query_part/query_part';
  3. const alertQueryDef = new QueryPartDef({
  4. type: 'query',
  5. params: [
  6. { name: 'queryRefId', type: 'string', dynamicLookup: true },
  7. {
  8. name: 'from',
  9. type: 'string',
  10. options: ['10s', '1m', '5m', '10m', '15m', '1h', '24h', '48h'],
  11. },
  12. { name: 'to', type: 'string', options: ['now', 'now-1m', 'now-5m', 'now-10m', 'now-1h'] },
  13. ],
  14. defaultParams: ['#A', '15m', 'now', 'avg'],
  15. });
  16. const conditionTypes = [{ text: 'Query', value: 'query' }];
  17. const alertStateSortScore = {
  18. alerting: 1,
  19. no_data: 2,
  20. pending: 3,
  21. ok: 4,
  22. paused: 5,
  23. };
  24. const evalFunctions = [
  25. { text: 'IS ABOVE', value: 'gt' },
  26. { text: 'IS BELOW', value: 'lt' },
  27. { text: 'IS OUTSIDE RANGE', value: 'outside_range' },
  28. { text: 'IS WITHIN RANGE', value: 'within_range' },
  29. { text: 'HAS NO VALUE', value: 'no_value' },
  30. ];
  31. const evalOperators = [{ text: 'OR', value: 'or' }, { text: 'AND', value: 'and' }];
  32. const reducerTypes = [
  33. { text: 'avg()', value: 'avg' },
  34. { text: 'min()', value: 'min' },
  35. { text: 'max()', value: 'max' },
  36. { text: 'sum()', value: 'sum' },
  37. { text: 'count()', value: 'count' },
  38. { text: 'last()', value: 'last' },
  39. { text: 'median()', value: 'median' },
  40. { text: 'diff()', value: 'diff' },
  41. { text: 'percent_diff()', value: 'percent_diff' },
  42. { text: 'count_non_null()', value: 'count_non_null' },
  43. ];
  44. const noDataModes = [
  45. { text: 'Alerting', value: 'alerting' },
  46. { text: 'No Data', value: 'no_data' },
  47. { text: 'Keep Last State', value: 'keep_state' },
  48. { text: 'Ok', value: 'ok' },
  49. ];
  50. const executionErrorModes = [{ text: 'Alerting', value: 'alerting' }, { text: 'Keep Last State', value: 'keep_state' }];
  51. function createReducerPart(model) {
  52. const def = new QueryPartDef({ type: model.type, defaultParams: [] });
  53. return new QueryPart(model, def);
  54. }
  55. function getStateDisplayModel(state) {
  56. switch (state) {
  57. case 'ok': {
  58. return {
  59. text: 'OK',
  60. iconClass: 'icon-gf icon-gf-online',
  61. stateClass: 'alert-state-ok',
  62. };
  63. }
  64. case 'alerting': {
  65. return {
  66. text: 'ALERTING',
  67. iconClass: 'icon-gf icon-gf-critical',
  68. stateClass: 'alert-state-critical',
  69. };
  70. }
  71. case 'no_data': {
  72. return {
  73. text: 'NO DATA',
  74. iconClass: 'fa fa-question',
  75. stateClass: 'alert-state-warning',
  76. };
  77. }
  78. case 'paused': {
  79. return {
  80. text: 'PAUSED',
  81. iconClass: 'fa fa-pause',
  82. stateClass: 'alert-state-paused',
  83. };
  84. }
  85. case 'pending': {
  86. return {
  87. text: 'PENDING',
  88. iconClass: 'fa fa-exclamation',
  89. stateClass: 'alert-state-warning',
  90. };
  91. }
  92. case 'unknown': {
  93. return {
  94. text: 'UNKNOWN',
  95. iconClass: 'fa fa-question',
  96. stateClass: 'alert-state-paused',
  97. };
  98. }
  99. }
  100. throw { message: 'Unknown alert state' };
  101. }
  102. function joinEvalMatches(matches, separator: string) {
  103. return _.reduce(
  104. matches,
  105. (res, ev) => {
  106. if (ev.metric !== undefined && ev.value !== undefined) {
  107. res.push(ev.metric + '=' + ev.value);
  108. }
  109. // For backwards compatibility . Should be be able to remove this after ~2017-06-01
  110. if (ev.Metric !== undefined && ev.Value !== undefined) {
  111. res.push(ev.Metric + '=' + ev.Value);
  112. }
  113. return res;
  114. },
  115. []
  116. ).join(separator);
  117. }
  118. function getAlertAnnotationInfo(ah) {
  119. // backward compatibility, can be removed in grafana 5.x
  120. // old way stored evalMatches in data property directly,
  121. // new way stores it in evalMatches property on new data object
  122. if (_.isArray(ah.data)) {
  123. return joinEvalMatches(ah.data, ', ');
  124. } else if (_.isArray(ah.data.evalMatches)) {
  125. return joinEvalMatches(ah.data.evalMatches, ', ');
  126. }
  127. if (ah.data.error) {
  128. return 'Error: ' + ah.data.error;
  129. }
  130. return '';
  131. }
  132. export default {
  133. alertQueryDef: alertQueryDef,
  134. getStateDisplayModel: getStateDisplayModel,
  135. conditionTypes: conditionTypes,
  136. evalFunctions: evalFunctions,
  137. evalOperators: evalOperators,
  138. noDataModes: noDataModes,
  139. executionErrorModes: executionErrorModes,
  140. reducerTypes: reducerTypes,
  141. createReducerPart: createReducerPart,
  142. getAlertAnnotationInfo: getAlertAnnotationInfo,
  143. alertStateSortScore: alertStateSortScore,
  144. };