alert_def.ts 4.2 KB

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