alert_def.ts 3.9 KB

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