timeRange.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { ReducerID } from '@grafana/data';
  2. import { ExpressionQuery, ExpressionQueryType } from 'app/features/expressions/types';
  3. import { defaultCondition } from 'app/features/expressions/utils/expressionTypes';
  4. import { AlertQuery } from 'app/types/unified-alerting-dto';
  5. import { getTimeRangeForExpression } from './timeRange';
  6. describe('timeRange', () => {
  7. describe('getTimeRangeForExpression', () => {
  8. describe('classic condition', () => {
  9. it('should return referenced query timeRange for classic condition', () => {
  10. const expressionQuery: AlertQuery = {
  11. refId: 'B',
  12. queryType: 'expression',
  13. datasourceUid: '-100',
  14. model: {
  15. queryType: 'query',
  16. datasource: '__expr__',
  17. refId: 'B',
  18. conditions: [{ ...defaultCondition, query: { params: ['A'] } }],
  19. type: ExpressionQueryType.classic,
  20. } as ExpressionQuery,
  21. };
  22. const query: AlertQuery = {
  23. refId: 'A',
  24. relativeTimeRange: { from: 300, to: 0 },
  25. queryType: 'query',
  26. datasourceUid: 'dsuid',
  27. model: { refId: 'A' },
  28. };
  29. const queries: AlertQuery[] = [query, expressionQuery];
  30. expect(getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, queries)).toEqual({
  31. from: 300,
  32. to: 0,
  33. });
  34. });
  35. it('should return the min and max time range', () => {
  36. const expressionQuery: AlertQuery = {
  37. refId: 'C',
  38. queryType: 'expression',
  39. datasourceUid: '-100',
  40. model: {
  41. queryType: 'query',
  42. datasource: '__expr__',
  43. refId: 'C',
  44. conditions: [
  45. { ...defaultCondition, query: { params: ['A'] } },
  46. { ...defaultCondition, query: { params: ['B'] } },
  47. ],
  48. type: ExpressionQueryType.classic,
  49. } as ExpressionQuery,
  50. };
  51. const queryA: AlertQuery = {
  52. refId: 'A',
  53. relativeTimeRange: { from: 300, to: 0 },
  54. datasourceUid: 'dsuid',
  55. model: { refId: 'A' },
  56. queryType: 'query',
  57. };
  58. const queryB: AlertQuery = {
  59. refId: 'B',
  60. relativeTimeRange: { from: 600, to: 300 },
  61. datasourceUid: 'dsuid',
  62. model: { refId: 'B' },
  63. queryType: 'query',
  64. };
  65. const queries: AlertQuery[] = [queryA, queryB, expressionQuery];
  66. expect(getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, queries)).toEqual({
  67. from: 600,
  68. to: 0,
  69. });
  70. });
  71. });
  72. });
  73. describe('math', () => {
  74. it('should get timerange for referenced query', () => {
  75. const expressionQuery: AlertQuery = {
  76. refId: 'B',
  77. queryType: 'expression',
  78. datasourceUid: '-100',
  79. model: {
  80. queryType: 'query',
  81. datasource: '__expr__',
  82. refId: 'B',
  83. expression: '$A > 10',
  84. type: ExpressionQueryType.math,
  85. } as ExpressionQuery,
  86. };
  87. const query: AlertQuery = {
  88. refId: 'A',
  89. datasourceUid: 'dsuid',
  90. relativeTimeRange: { from: 300, to: 0 },
  91. model: { refId: 'A' },
  92. queryType: 'query',
  93. };
  94. expect(getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, [expressionQuery, query]));
  95. });
  96. it('should get time ranges for multiple referenced queries', () => {
  97. const expressionQuery: AlertQuery = {
  98. refId: 'C',
  99. queryType: 'expression',
  100. datasourceUid: '-100',
  101. model: {
  102. queryType: 'query',
  103. datasource: '__expr__',
  104. refId: 'C',
  105. expression: '$A > 10 && $queryB > 20',
  106. type: ExpressionQueryType.math,
  107. } as ExpressionQuery,
  108. };
  109. const queryA: AlertQuery = {
  110. refId: 'A',
  111. relativeTimeRange: { from: 300, to: 0 },
  112. datasourceUid: 'dsuid',
  113. model: { refId: 'A' },
  114. queryType: 'query',
  115. };
  116. const queryB: AlertQuery = {
  117. refId: 'queryB',
  118. relativeTimeRange: { from: 600, to: 300 },
  119. datasourceUid: 'dsuid',
  120. model: { refId: 'queryB' },
  121. queryType: 'query',
  122. };
  123. expect(
  124. getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, [expressionQuery, queryA, queryB])
  125. ).toEqual({ from: 600, to: 0 });
  126. });
  127. });
  128. describe('resample', () => {
  129. it('should get referenced timerange for resample expression', () => {
  130. const expressionQuery: AlertQuery = {
  131. refId: 'B',
  132. queryType: 'expression',
  133. datasourceUid: '-100',
  134. model: {
  135. queryType: 'query',
  136. datasource: '__expr__',
  137. refId: 'B',
  138. expression: 'A',
  139. type: ExpressionQueryType.resample,
  140. window: '10s',
  141. } as ExpressionQuery,
  142. };
  143. const queryA: AlertQuery = {
  144. refId: 'A',
  145. relativeTimeRange: { from: 300, to: 0 },
  146. datasourceUid: 'dsuid',
  147. model: { refId: 'A' },
  148. queryType: 'query',
  149. };
  150. const queries = [queryA, expressionQuery];
  151. expect(getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, queries)).toEqual({
  152. from: 300,
  153. to: 0,
  154. });
  155. });
  156. });
  157. describe('reduce', () => {
  158. it('should get referenced timerange for reduce expression', () => {
  159. const expressionQuery: AlertQuery = {
  160. refId: 'B',
  161. queryType: 'expression',
  162. datasourceUid: '-100',
  163. model: {
  164. queryType: 'query',
  165. datasource: '__expr__',
  166. refId: 'B',
  167. expression: 'A',
  168. type: ExpressionQueryType.reduce,
  169. reducer: ReducerID.max,
  170. } as ExpressionQuery,
  171. };
  172. const queryA: AlertQuery = {
  173. refId: 'A',
  174. relativeTimeRange: { from: 300, to: 0 },
  175. datasourceUid: 'dsuid',
  176. model: { refId: 'A' },
  177. queryType: 'query',
  178. };
  179. const queries = [queryA, expressionQuery];
  180. expect(getTimeRangeForExpression(expressionQuery.model as ExpressionQuery, queries)).toEqual({
  181. from: 300,
  182. to: 0,
  183. });
  184. });
  185. });
  186. });