useMetricsQuery.spec.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import type {PageFilters} from 'sentry/types/core';
  2. import type {MetricAggregation} from 'sentry/types/metrics';
  3. import {
  4. createMqlQuery,
  5. getMetricsQueryApiRequestPayload,
  6. } from 'sentry/utils/metrics/useMetricsQuery';
  7. describe('createMqlQuery', () => {
  8. it('should create a basic mql query', () => {
  9. const field = 'avg(transaction.duration)';
  10. const result = createMqlQuery({field});
  11. expect(result).toEqual(`avg(transaction.duration)`);
  12. });
  13. it('should create a mql query with a query', () => {
  14. const field = 'avg(transaction.duration)';
  15. const query = 'event.type:error';
  16. const result = createMqlQuery({field, query});
  17. expect(result).toEqual(`avg(transaction.duration){event.type:error}`);
  18. });
  19. it('should create a mql query with a groupBy', () => {
  20. const field = 'avg(transaction.duration)';
  21. const groupBy = ['environment'];
  22. const result = createMqlQuery({field, groupBy});
  23. expect(result).toEqual(`avg(transaction.duration) by (environment)`);
  24. });
  25. it('should create a mql query with a query and groupBy', () => {
  26. const field = 'avg(transaction.duration)';
  27. const query = 'event.type:error';
  28. const groupBy = ['environment', 'project'];
  29. const result = createMqlQuery({field, query, groupBy});
  30. expect(result).toEqual(
  31. `avg(transaction.duration){event.type:error} by (environment,project)`
  32. );
  33. });
  34. });
  35. describe('getMetricsQueryApiRequestPayload', () => {
  36. it('should return the correct query object with default values', () => {
  37. const metric = {
  38. query: 'error',
  39. groupBy: ['project'],
  40. mri: 'c:custom/sessions@none' as const,
  41. aggregation: 'avg' as MetricAggregation,
  42. name: 'query_1',
  43. };
  44. const filters = {
  45. projects: [1],
  46. environments: ['production'],
  47. datetime: {start: '2023-01-01', end: '2023-01-31', period: null, utc: true},
  48. };
  49. const result = getMetricsQueryApiRequestPayload([metric], filters);
  50. expect(result.query).toEqual({
  51. start: '2023-01-01T00:00:00.000Z',
  52. end: '2023-01-31T00:00:00.000Z',
  53. project: [1],
  54. environment: ['production'],
  55. includeSeries: true,
  56. interval: '2h',
  57. });
  58. expect(result.body).toEqual({
  59. queries: [
  60. {
  61. name: 'query_1',
  62. mql: 'avg(c:custom/sessions@none){error} by (project)',
  63. },
  64. ],
  65. formulas: [{mql: '$query_1', limit: undefined, order: undefined}],
  66. });
  67. });
  68. it('should return the correct query object with default values (period)', () => {
  69. const metric = {
  70. mri: 'c:custom/sessions@none' as const,
  71. aggregation: 'avg' as MetricAggregation,
  72. query: 'error',
  73. groupBy: ['project'],
  74. name: 'query_1',
  75. };
  76. const filters = {
  77. projects: [1],
  78. environments: ['production'],
  79. datetime: {period: '7d', utc: true} as PageFilters['datetime'],
  80. };
  81. const result = getMetricsQueryApiRequestPayload([metric], filters);
  82. expect(result.query).toEqual({
  83. statsPeriod: '7d',
  84. project: [1],
  85. environment: ['production'],
  86. includeSeries: true,
  87. interval: '30m',
  88. });
  89. expect(result.body).toEqual({
  90. queries: [
  91. {
  92. name: 'query_1',
  93. mql: 'avg(c:custom/sessions@none){error} by (project)',
  94. },
  95. ],
  96. formulas: [{mql: '$query_1', limit: undefined, order: undefined}],
  97. });
  98. });
  99. it('should return the correct query object with overridden values', () => {
  100. const metric = {
  101. mri: 'c:custom/sessions@none' as const,
  102. aggregation: 'avg' as MetricAggregation,
  103. query: 'error',
  104. groupBy: ['project'],
  105. name: 'query_1',
  106. };
  107. const filters = {
  108. projects: [1],
  109. environments: ['production'],
  110. datetime: {start: '2023-01-01', end: '2023-01-02', period: null, utc: true},
  111. };
  112. const result = getMetricsQueryApiRequestPayload([metric], filters, {
  113. interval: '123m',
  114. includeSeries: false,
  115. });
  116. expect(result.query).toEqual({
  117. start: '2023-01-01T00:00:00.000Z',
  118. end: '2023-01-02T00:00:00.000Z',
  119. project: [1],
  120. environment: ['production'],
  121. includeSeries: false,
  122. interval: '123m',
  123. });
  124. expect(result.body).toEqual({
  125. queries: [
  126. {
  127. name: 'query_1',
  128. mql: 'avg(c:custom/sessions@none){error} by (project)',
  129. },
  130. ],
  131. formulas: [{mql: '$query_1', limit: undefined, order: undefined}],
  132. });
  133. });
  134. it('should not add a default orderBy if one is already present', () => {
  135. const metric = {
  136. mri: 'c:custom/sessions@none' as const,
  137. aggregation: 'avg' as MetricAggregation,
  138. query: 'error',
  139. groupBy: ['project'],
  140. orderBy: 'asc' as const,
  141. name: 'query_1',
  142. };
  143. const filters = {
  144. projects: [1],
  145. environments: ['production'],
  146. datetime: {start: '2023-01-01', end: '2023-01-02', period: null, utc: true},
  147. };
  148. const result = getMetricsQueryApiRequestPayload([metric], filters);
  149. expect(result.query).toEqual({
  150. start: '2023-01-01T00:00:00.000Z',
  151. end: '2023-01-02T00:00:00.000Z',
  152. project: [1],
  153. environment: ['production'],
  154. includeSeries: true,
  155. interval: '5m',
  156. });
  157. expect(result.body).toEqual({
  158. queries: [
  159. {
  160. name: 'query_1',
  161. mql: 'avg(c:custom/sessions@none){error} by (project)',
  162. },
  163. ],
  164. formulas: [{mql: '$query_1', limit: undefined, order: 'asc'}],
  165. });
  166. });
  167. it('should not add a default orderBy if there are no groups', () => {
  168. const metric = {
  169. mri: 'c:custom/sessions@none' as const,
  170. aggregation: 'avg' as MetricAggregation,
  171. query: 'error',
  172. groupBy: [],
  173. name: 'query_1',
  174. };
  175. const filters = {
  176. projects: [1],
  177. environments: ['production'],
  178. datetime: {start: '2023-01-01', end: '2023-01-02', period: null, utc: true},
  179. };
  180. const result = getMetricsQueryApiRequestPayload([metric], filters);
  181. expect(result.query).toEqual({
  182. start: '2023-01-01T00:00:00.000Z',
  183. end: '2023-01-02T00:00:00.000Z',
  184. project: [1],
  185. environment: ['production'],
  186. includeSeries: true,
  187. interval: '5m',
  188. });
  189. expect(result.body).toEqual({
  190. queries: [
  191. {
  192. name: 'query_1',
  193. mql: 'avg(c:custom/sessions@none){error}',
  194. },
  195. ],
  196. formulas: [{mql: '$query_1', limit: undefined, order: undefined}],
  197. });
  198. });
  199. it('should not add intervalLadder override into the request', () => {
  200. const metric = {
  201. mri: 'c:custom/test@seconds' as const,
  202. aggregation: 'sum' as MetricAggregation,
  203. query: 'error',
  204. groupBy: [],
  205. name: 'query_1',
  206. };
  207. const filters = {
  208. projects: [1],
  209. environments: ['production'],
  210. datetime: {start: '2023-01-01', end: '2023-01-02', period: null, utc: true},
  211. };
  212. const result = getMetricsQueryApiRequestPayload([metric], filters, {
  213. intervalLadder: 'metrics',
  214. });
  215. expect(result.query).toEqual({
  216. start: '2023-01-01T00:00:00.000Z',
  217. end: '2023-01-02T00:00:00.000Z',
  218. project: [1],
  219. environment: ['production'],
  220. includeSeries: true,
  221. interval: '5m',
  222. });
  223. expect(result.body).toEqual({
  224. queries: [
  225. {
  226. name: 'query_1',
  227. mql: 'sum(c:custom/test@seconds){error}',
  228. },
  229. ],
  230. formulas: [{mql: '$query_1', limit: undefined, order: undefined}],
  231. });
  232. });
  233. });