Browse Source

ref: Move intervalToMilliseconds to utils/duration/intervalToMilliseconds (#70011)

Ryan Albrecht 10 months ago
parent
commit
9d09a5b2f6

+ 1 - 1
static/app/components/feedback/useFeedbackListQueryKey.tsx

@@ -2,7 +2,7 @@ import {useMemo} from 'react';
 
 import decodeMailbox from 'sentry/components/feedback/decodeMailbox';
 import type {Organization} from 'sentry/types/organization';
-import {intervalToMilliseconds} from 'sentry/utils/dates';
+import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
 import type {ApiQueryKey} from 'sentry/utils/queryClient';
 import {decodeList, decodeScalar} from 'sentry/utils/queryString';
 import useLocationQuery from 'sentry/utils/url/useLocationQuery';

+ 0 - 20
static/app/utils/dates.spec.tsx

@@ -3,7 +3,6 @@ import {UserFixture} from 'sentry-fixture/user';
 import ConfigStore from 'sentry/stores/configStore';
 import {
   getTimeFormat,
-  intervalToMilliseconds,
   parsePeriodToHours,
   setDateToTime,
   shouldUse24Hours,
@@ -30,25 +29,6 @@ describe('utils.dates', function () {
     });
   });
 
-  describe('intervalToMilliseconds()', function () {
-    it('can convert standard formats', function () {
-      expect(intervalToMilliseconds('24h')).toBe(86400000);
-      expect(intervalToMilliseconds('30m')).toBe(1800000);
-      expect(intervalToMilliseconds('15m')).toBe(900000);
-      expect(intervalToMilliseconds('5m')).toBe(300000);
-      expect(intervalToMilliseconds('1m')).toBe(60000);
-    });
-
-    it('can convert arbitrary formats', function () {
-      expect(intervalToMilliseconds('8w')).toBe(4838400000);
-      expect(intervalToMilliseconds('30d')).toBe(2592000000);
-      expect(intervalToMilliseconds('7d')).toBe(604800000);
-      expect(intervalToMilliseconds('1d')).toBe(86400000);
-      expect(intervalToMilliseconds('1h')).toBe(3600000);
-      expect(intervalToMilliseconds('2m')).toBe(120000);
-    });
-  });
-
   describe('parsePeriodToHours()', function () {
     it('can convert standard formats', function () {
       expect(parsePeriodToHours('30s').toFixed(4)).toBe('0.0083');

+ 0 - 24
static/app/utils/dates.tsx

@@ -185,30 +185,6 @@ export function getStartOfPeriodAgo(
   return getStartOfDay(getPeriodAgo(period, unit));
 }
 
-/**
- * Convert an interval string into a number of seconds.
- * This allows us to create end timestamps from starting ones
- * enabling us to find events in narrow windows.
- *
- * @param {String} interval The interval to convert.
- * @return {Integer}
- */
-export function intervalToMilliseconds(interval: string): number {
-  const pattern = /^(\d+)(w|d|h|m)$/;
-  const matches = pattern.exec(interval);
-  if (!matches) {
-    return 0;
-  }
-  const [, value, unit] = matches;
-  const multipliers = {
-    w: 60 * 60 * 24 * 7,
-    d: 60 * 60 * 24,
-    h: 60 * 60,
-    m: 60,
-  };
-  return parseInt(value, 10) * multipliers[unit] * 1000;
-}
-
 /**
  * This parses our period shorthand strings (e.g. <int><unit>)
  * and converts it into hours

+ 20 - 0
static/app/utils/duration/intervalToMilliseconds.spec.tsx

@@ -0,0 +1,20 @@
+import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
+
+describe('intervalToMilliseconds()', () => {
+  it('can convert standard formats', () => {
+    expect(intervalToMilliseconds('24h')).toBe(86400000);
+    expect(intervalToMilliseconds('30m')).toBe(1800000);
+    expect(intervalToMilliseconds('15m')).toBe(900000);
+    expect(intervalToMilliseconds('5m')).toBe(300000);
+    expect(intervalToMilliseconds('1m')).toBe(60000);
+  });
+
+  it('can convert arbitrary formats', () => {
+    expect(intervalToMilliseconds('8w')).toBe(4838400000);
+    expect(intervalToMilliseconds('30d')).toBe(2592000000);
+    expect(intervalToMilliseconds('7d')).toBe(604800000);
+    expect(intervalToMilliseconds('1d')).toBe(86400000);
+    expect(intervalToMilliseconds('1h')).toBe(3600000);
+    expect(intervalToMilliseconds('2m')).toBe(120000);
+  });
+});

+ 24 - 0
static/app/utils/duration/intervalToMilliseconds.tsx

@@ -0,0 +1,24 @@
+/**
+ * Convert an interval string into a number of seconds.
+ * This allows us to create end timestamps from starting ones
+ * enabling us to find events in narrow windows.
+ *
+ * @param {String} interval The interval to convert.
+ * @return {Integer}
+ */
+
+export function intervalToMilliseconds(interval: string): number {
+  const pattern = /^(\d+)(w|d|h|m)$/;
+  const matches = pattern.exec(interval);
+  if (!matches) {
+    return 0;
+  }
+  const [, value, unit] = matches;
+  const multipliers = {
+    w: 60 * 60 * 24 * 7,
+    d: 60 * 60 * 24,
+    h: 60 * 60,
+    m: 60,
+  };
+  return parseInt(value, 10) * multipliers[unit] * 1000;
+}

+ 1 - 1
static/app/views/monitors/components/monitorStats.tsx

@@ -13,9 +13,9 @@ import PanelBody from 'sentry/components/panels/panelBody';
 import Placeholder from 'sentry/components/placeholder';
 import {t} from 'sentry/locale';
 import {space} from 'sentry/styles/space';
-import {intervalToMilliseconds} from 'sentry/utils/dates';
 import {axisLabelFormatter, tooltipFormatter} from 'sentry/utils/discover/charts';
 import type {AggregationOutputType} from 'sentry/utils/discover/fields';
+import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
 import {useApiQuery} from 'sentry/utils/queryClient';
 import theme from 'sentry/utils/theme';
 import usePageFilters from 'sentry/utils/usePageFilters';

+ 1 - 1
static/app/views/monitors/components/timeline/hooks/useMonitorDates.tsx

@@ -1,7 +1,7 @@
 import {useRef} from 'react';
 import moment from 'moment';
 
-import {intervalToMilliseconds} from 'sentry/utils/dates';
+import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
 import usePageFilters from 'sentry/utils/usePageFilters';
 
 /**

+ 1 - 1
static/app/views/starfish/queries/getSeriesEventView.tsx

@@ -1,10 +1,10 @@
 import sortBy from 'lodash/sortBy';
 
 import type {PageFilters} from 'sentry/types/core';
-import {intervalToMilliseconds} from 'sentry/utils/dates';
 import EventView from 'sentry/utils/discover/eventView';
 import {parseFunction} from 'sentry/utils/discover/fields';
 import {DiscoverDatasets} from 'sentry/utils/discover/types';
+import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
 import type {MutableSearch} from 'sentry/utils/tokenizeSearch';
 import {getIntervalForMetricFunction} from 'sentry/views/performance/database/getIntervalForMetricFunction';
 import {DEFAULT_INTERVAL} from 'sentry/views/performance/database/settings';