index.spec.tsx 4.5 KB

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