index.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import {resetMockDate, setMockDate} from 'sentry-test/utils';
  2. import type {MetricAggregation, MetricType, MRI} from 'sentry/types/metrics';
  3. import {
  4. getAbsoluteDateTimeRange,
  5. getDateTimeParams,
  6. getDefaultAggregation,
  7. getFormattedMQL,
  8. getMetricsInterval,
  9. isFormattedMQL,
  10. } from 'sentry/utils/metrics';
  11. import {DEFAULT_AGGREGATES} from 'sentry/utils/metrics/constants';
  12. describe('getDDMInterval', () => {
  13. it('should return the correct interval for non-"1m" intervals', () => {
  14. const dateTimeObj = {start: '2023-01-01', end: '2023-01-31'};
  15. const useCase = 'sessions';
  16. const result = getMetricsInterval(dateTimeObj, useCase);
  17. expect(result).toBe('2h');
  18. });
  19. it('should return "10s" interval for "1m" interval within 60 minutes and custom use case', () => {
  20. const dateTimeObj = {
  21. start: '2023-01-01T00:00:00.000Z',
  22. end: '2023-01-01T00:59:00.000Z',
  23. };
  24. const useCase = 'custom';
  25. const result = getMetricsInterval(dateTimeObj, useCase);
  26. expect(result).toBe('10s');
  27. });
  28. it('should return "1m" interval for "1m" interval beyond 60 minutes', () => {
  29. const dateTimeObj = {start: '2023-01-01', end: '2023-01-01T01:05:00.000Z'};
  30. const useCase = 'sessions';
  31. const result = getMetricsInterval(dateTimeObj, useCase);
  32. expect(result).toBe('1m');
  33. });
  34. });
  35. describe('getDateTimeParams', () => {
  36. it('should return the correct object with "statsPeriod" when period is provided', () => {
  37. const datetime = {start: '2023-01-01', end: '2023-01-31', period: '7d', utc: true};
  38. const result = getDateTimeParams(datetime);
  39. expect(result).toEqual({statsPeriod: '7d'});
  40. });
  41. it('should return the correct object with "start" and "end" when period is not provided', () => {
  42. const datetime = {start: '2023-01-01', end: '2023-01-31', period: null, utc: true};
  43. const result = getDateTimeParams(datetime);
  44. expect(result).toEqual({
  45. start: '2023-01-01T00:00:00.000Z',
  46. end: '2023-01-31T00:00:00.000Z',
  47. });
  48. });
  49. });
  50. describe('getFormattedMQL', () => {
  51. it('should format metric widget object into a string', () => {
  52. const result = getFormattedMQL({
  53. aggregation: 'avg',
  54. mri: 'd:custom/sentry.process_profile.symbolicate.process@second',
  55. groupBy: ['result'],
  56. query: 'result:success',
  57. });
  58. expect(result).toEqual(
  59. 'avg(sentry.process_profile.symbolicate.process){result:success} by result'
  60. );
  61. });
  62. it('defaults to an empty string', () => {
  63. const result = getFormattedMQL({
  64. aggregation: '' as MetricAggregation,
  65. mri: 'd:custom/sentry.process_profile.symbolicate.process@second',
  66. groupBy: [],
  67. query: '',
  68. });
  69. expect(result).toEqual('');
  70. });
  71. });
  72. describe('isFormattedMQL', () => {
  73. it('should return true for a valid MQL string - simple', () => {
  74. const result = isFormattedMQL('avg(sentry.process_profile.symbolicate.process)');
  75. expect(result).toBe(true);
  76. });
  77. it('should return true for a valid MQL string - filter', () => {
  78. const result = isFormattedMQL(
  79. 'avg(sentry.process_profile.symbolicate.process){result:success}'
  80. );
  81. expect(result).toBe(true);
  82. });
  83. it('should return true for a valid MQL string - groupy by', () => {
  84. const result = isFormattedMQL(
  85. 'avg(sentry.process_profile.symbolicate.process) by result'
  86. );
  87. expect(result).toBe(true);
  88. });
  89. it('should return true for a valid MQL string - filter and group by', () => {
  90. const result = isFormattedMQL(
  91. 'avg(sentry.process_profile.symbolicate.process){result:success} by result'
  92. );
  93. expect(result).toBe(true);
  94. });
  95. it('should return false for an invalid MQL string', () => {
  96. const result = isFormattedMQL('not MQL string');
  97. expect(result).toBe(false);
  98. });
  99. });
  100. describe('getAbsoluteDateTimeRange', () => {
  101. beforeEach(() => {
  102. setMockDate(new Date('2024-01-01T00:00:00Z'));
  103. });
  104. afterEach(() => {
  105. resetMockDate();
  106. });
  107. it('should return the correct object with "start" and "end" when period is not provided', () => {
  108. const datetime = {
  109. start: '2023-01-01T00:00:00.000Z',
  110. end: '2023-01-01T00:00:00.000Z',
  111. period: null,
  112. utc: true,
  113. };
  114. const result = getAbsoluteDateTimeRange(datetime);
  115. expect(result).toEqual({
  116. start: '2023-01-01T00:00:00.000Z',
  117. end: '2023-01-01T00:00:00.000Z',
  118. });
  119. });
  120. it('should return the correct object with "start" and "end" when period is provided', () => {
  121. const datetime = {start: null, end: null, period: '7d', utc: true};
  122. const result = getAbsoluteDateTimeRange(datetime);
  123. expect(result).toEqual({
  124. start: '2023-12-25T00:00:00.000Z',
  125. end: '2024-01-01T00:00:00.000Z',
  126. });
  127. });
  128. });
  129. describe('getDefaultAggregation', () => {
  130. it.each(['c', 'd', 'g', 's'])(
  131. 'should give default aggregation - metric type %s',
  132. metricType => {
  133. const mri = `${metricType as MetricType}:custom/xyz@test` as MRI;
  134. expect(getDefaultAggregation(mri)).toBe(DEFAULT_AGGREGATES[metricType]);
  135. }
  136. );
  137. it('should fallback to sum', () => {
  138. expect(getDefaultAggregation('b:roken/MRI@none' as MRI)).toBe('sum');
  139. });
  140. });