index.spec.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import {PageFilters} from 'sentry/types';
  2. import {
  3. formatMetricsUsingUnitAndOp,
  4. formatMetricUsingFixedUnit,
  5. formattingSupportedMetricUnits,
  6. getDateTimeParams,
  7. getMetricsApiRequestQuery,
  8. getMetricsInterval,
  9. } from 'sentry/utils/metrics';
  10. describe('formatMetricsUsingUnitAndOp', () => {
  11. it('should format the value according to the unit', () => {
  12. // Test cases for different units
  13. expect(formatMetricsUsingUnitAndOp(123456, 'millisecond')).toEqual('2.06min');
  14. expect(formatMetricsUsingUnitAndOp(5000, 'second')).toEqual('1.39hr');
  15. expect(formatMetricsUsingUnitAndOp(600, 'byte')).toEqual('600 B');
  16. expect(formatMetricsUsingUnitAndOp(4096, 'kibibyte')).toEqual('4.0 MiB');
  17. expect(formatMetricsUsingUnitAndOp(3145728, 'megabyte')).toEqual('3.15 TB');
  18. });
  19. it('should handle value as null', () => {
  20. expect(formatMetricsUsingUnitAndOp(null, 'millisecond')).toEqual('—');
  21. expect(formatMetricsUsingUnitAndOp(null, 'byte')).toEqual('—');
  22. expect(formatMetricsUsingUnitAndOp(null, 'megabyte')).toEqual('—');
  23. });
  24. it('should format count operation as a number', () => {
  25. expect(formatMetricsUsingUnitAndOp(99, 'none', 'count')).toEqual('99');
  26. expect(formatMetricsUsingUnitAndOp(null, 'none', 'count')).toEqual('');
  27. });
  28. });
  29. describe('getMetricsApiRequestQuery', () => {
  30. it('should return the correct query object with default values', () => {
  31. const metric = {field: 'sessions', query: 'error', groupBy: ['project']};
  32. const filters = {
  33. projects: [1],
  34. environments: ['production'],
  35. datetime: {start: '2023-01-01', end: '2023-01-31', period: null, utc: true},
  36. };
  37. const overrides = {};
  38. const result = getMetricsApiRequestQuery(metric, filters, overrides);
  39. expect(result).toEqual({
  40. start: '2023-01-01T00:00:00.000Z',
  41. end: '2023-01-31T00:00:00.000Z',
  42. query: 'error',
  43. project: [1],
  44. environment: ['production'],
  45. field: 'sessions',
  46. useCase: 'custom',
  47. interval: '12h',
  48. groupBy: ['project'],
  49. allowPrivate: true,
  50. per_page: 10,
  51. });
  52. });
  53. it('should return the correct query object with default values (period)', () => {
  54. const metric = {field: 'sessions', query: 'error', groupBy: ['project']};
  55. const filters = {
  56. projects: [1],
  57. environments: ['production'],
  58. datetime: {period: '7d', utc: true} as PageFilters['datetime'],
  59. };
  60. const overrides = {};
  61. const result = getMetricsApiRequestQuery(metric, filters, overrides);
  62. expect(result).toEqual({
  63. statsPeriod: '7d',
  64. query: 'error',
  65. project: [1],
  66. environment: ['production'],
  67. field: 'sessions',
  68. useCase: 'custom',
  69. interval: '30m',
  70. groupBy: ['project'],
  71. allowPrivate: true,
  72. per_page: 10,
  73. });
  74. });
  75. it('should return the correct query object with overridden values', () => {
  76. const metric = {field: 'sessions', query: 'error', groupBy: ['project']};
  77. const filters = {
  78. projects: [1],
  79. environments: ['production'],
  80. datetime: {start: '2023-01-01', end: '2023-01-02', period: null, utc: true},
  81. };
  82. const overrides = {interval: '5m', groupBy: ['environment']};
  83. const result = getMetricsApiRequestQuery(metric, filters, overrides);
  84. expect(result).toEqual({
  85. start: '2023-01-01T00:00:00.000Z',
  86. end: '2023-01-02T00:00:00.000Z',
  87. query: 'error',
  88. project: [1],
  89. environment: ['production'],
  90. field: 'sessions',
  91. useCase: 'custom',
  92. interval: '5m',
  93. groupBy: ['environment'],
  94. allowPrivate: true,
  95. per_page: 10,
  96. });
  97. });
  98. });
  99. describe('getMetricsInterval', () => {
  100. it('should return the correct interval for non-"1m" intervals', () => {
  101. const dateTimeObj = {start: '2023-01-01', end: '2023-01-31'};
  102. const useCase = 'sessions';
  103. const result = getMetricsInterval(dateTimeObj, useCase);
  104. expect(result).toBe('12h');
  105. });
  106. it('should return "10s" interval for "1m" interval within 60 minutes and custom use case', () => {
  107. const dateTimeObj = {start: '2023-01-01', end: '2023-01-01T00:59:00.000Z'};
  108. const useCase = 'custom';
  109. const result = getMetricsInterval(dateTimeObj, useCase);
  110. expect(result).toBe('10s');
  111. });
  112. it('should return "1m" interval for "1m" interval beyond 60 minutes', () => {
  113. const dateTimeObj = {start: '2023-01-01', end: '2023-01-01T01:05:00.000Z'};
  114. const useCase = 'sessions';
  115. const result = getMetricsInterval(dateTimeObj, useCase);
  116. expect(result).toBe('1m');
  117. });
  118. });
  119. describe('formatMetricUsingFixedUnit', () => {
  120. it('should return the formatted value with the short form of the given unit', () => {
  121. expect(formatMetricUsingFixedUnit(123456, 'millisecond')).toBe('123,456ms');
  122. expect(formatMetricUsingFixedUnit(2.1231245, 'kibibyte')).toBe('2.123KiB');
  123. expect(formatMetricUsingFixedUnit(1222.1231245, 'megabyte')).toBe('1,222.123MB');
  124. });
  125. it.each(formattingSupportedMetricUnits.filter(unit => unit !== 'none'))(
  126. 'appends a unit for every supported one (except none)',
  127. unit => {
  128. expect(formatMetricUsingFixedUnit(1234.56, unit)).toMatch(/1,234\.56.+/);
  129. }
  130. );
  131. it('does not append a unit for unsupported units and "none"', () => {
  132. expect(formatMetricUsingFixedUnit(1234.56, 'randomunitname')).toBe('1,234.56');
  133. expect(formatMetricUsingFixedUnit(1234.56, 'none')).toBe('1,234.56');
  134. });
  135. it.each(['sum', 'count_unique', 'avg', 'max', 'p50', 'p75', 'p95', 'p99'])(
  136. 'does append a unit for every operation (except count)',
  137. op => {
  138. expect(formatMetricUsingFixedUnit(1234.56, 'second', op)).toMatch(/1,234\.56s/);
  139. }
  140. );
  141. it('does not append a unit for count operation', () => {
  142. expect(formatMetricUsingFixedUnit(1234.56, 'second', 'count')).toBe('1,234.56');
  143. });
  144. });
  145. describe('getDateTimeParams', () => {
  146. it('should return the correct object with "statsPeriod" when period is provided', () => {
  147. const datetime = {start: '2023-01-01', end: '2023-01-31', period: '7d', utc: true};
  148. const result = getDateTimeParams(datetime);
  149. expect(result).toEqual({statsPeriod: '7d'});
  150. });
  151. it('should return the correct object with "start" and "end" when period is not provided', () => {
  152. const datetime = {start: '2023-01-01', end: '2023-01-31', period: null, utc: true};
  153. const result = getDateTimeParams(datetime);
  154. expect(result).toEqual({
  155. start: '2023-01-01T00:00:00.000Z',
  156. end: '2023-01-31T00:00:00.000Z',
  157. });
  158. });
  159. });