alert_def.ts 4.1 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. {text: 'count_non_null()', value: 'count_non_null'},
  48. ];
  49. var noDataModes = [
  50. {text: 'Alerting', value: 'alerting'},
  51. {text: 'No Data', value: 'no_data'},
  52. {text: 'Keep Last State', value: 'keep_state'},
  53. {text: 'Ok', value: 'ok'},
  54. ];
  55. var executionErrorModes = [
  56. {text: 'Alerting', value: 'alerting'},
  57. {text: 'Keep Last State', value: 'keep_state'},
  58. ];
  59. function createReducerPart(model) {
  60. var def = new QueryPartDef({type: model.type, defaultParams: []});
  61. return new QueryPart(model, def);
  62. }
  63. function getStateDisplayModel(state) {
  64. switch (state) {
  65. case 'ok': {
  66. return {
  67. text: 'OK',
  68. iconClass: 'icon-gf icon-gf-online',
  69. stateClass: 'alert-state-ok'
  70. };
  71. }
  72. case 'alerting': {
  73. return {
  74. text: 'ALERTING',
  75. iconClass: 'icon-gf icon-gf-critical',
  76. stateClass: 'alert-state-critical'
  77. };
  78. }
  79. case 'no_data': {
  80. return {
  81. text: 'NO DATA',
  82. iconClass: "fa fa-question",
  83. stateClass: 'alert-state-warning'
  84. };
  85. }
  86. case 'paused': {
  87. return {
  88. text: 'PAUSED',
  89. iconClass: "fa fa-pause",
  90. stateClass: 'alert-state-paused'
  91. };
  92. }
  93. case 'pending': {
  94. return {
  95. text: 'PENDING',
  96. iconClass: "fa fa-exclamation",
  97. stateClass: 'alert-state-warning'
  98. };
  99. }
  100. }
  101. throw {message: 'Unknown alert state'};
  102. }
  103. function joinEvalMatches(matches, separator: string) {
  104. return _.reduce(matches, (res, ev)=> {
  105. if (ev.metric !== undefined && ev.value !== undefined) {
  106. res.push(ev.metric + '=' + ev.value);
  107. }
  108. // For backwards compatibility . Should be be able to remove this after ~2017-06-01
  109. if (ev.Metric !== undefined && ev.Value !== undefined) {
  110. res.push(ev.Metric + '=' + ev.Value);
  111. }
  112. return res;
  113. }, []).join(separator);
  114. }
  115. function getAlertAnnotationInfo(ah) {
  116. // backward compatability, can be removed in grafana 5.x
  117. // old way stored evalMatches in data property directly,
  118. // new way stores it in evalMatches property on new data object
  119. if (_.isArray(ah.data)) {
  120. return joinEvalMatches(ah.data, ', ');
  121. } else if (_.isArray(ah.data.evalMatches)) {
  122. return joinEvalMatches(ah.data.evalMatches, ', ');
  123. }
  124. if (ah.data.error) {
  125. return "Error: " + ah.data.error;
  126. }
  127. if (ah.data.noData || ah.data.no_data) {
  128. return "No Data";
  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. };