alert_def.ts 4.0 KB

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