alert_def.ts 4.2 KB

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