useMetricsQuery.spec.tsx 6.9 KB

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