Skip to content

Commit 692e278

Browse files
committed
fix tests roundTimeByTimeRange
1 parent 83cffc4 commit 692e278

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

datahub-web-react/src/app/entityV2/shared/tabs/Dataset/Stats/StatsTabV2/graphs/QueryCountGraph/useQueryCountData.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ export default function useQueryCountData(urn: string | undefined, timeRange?: T
8181

8282
useEffect(() => {
8383
if (timeRange && urn !== undefined && canViewDatasetUsage) {
84-
const startTime = roundTimeByTimeRange(getCalendarStartTimeByTimeRange(Date.now(), timeRange), timeRange);
84+
const startTime = roundTimeByTimeRange(
85+
getCalendarStartTimeByTimeRange(Date.now(), timeRange),
86+
timeRange,
87+
moment.tz.guess(),
88+
);
8589

8690
getTimeRangeUsageAggregations({
8791
variables: {

datahub-web-react/src/app/entityV2/shared/tabs/Dataset/Stats/StatsTabV2/graphs/__tests__/utils/roundTimeByTimeRange.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,47 @@ import { roundTimeByTimeRange } from '@app/entityV2/shared/tabs/Dataset/Stats/St
22
import { TimeRange } from '@src/types.generated';
33

44
const MOCKED_NOW = 1739791888317;
5+
const MOCKED_TZ = 'UTC';
56

67
describe('roundTimeByTimeRange', () => {
78
it('should return correct value for Day of TimeRange', () => {
8-
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Day);
9+
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Day, MOCKED_TZ);
910

10-
expect(response).toEqual(1739739600000);
11+
expect(response).toEqual(1739750400000);
1112
});
1213

1314
it('should return correct value for Week of TimeRange', () => {
14-
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Week);
15+
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Week, MOCKED_TZ);
1516

16-
expect(response).toEqual(1739739600000);
17+
expect(response).toEqual(1739750400000);
1718
});
1819

1920
it('should return correct value for Month of TimeRange', () => {
20-
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Month);
21+
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Month, MOCKED_TZ);
2122

22-
expect(response).toEqual(1739739600000);
23+
expect(response).toEqual(1739750400000);
2324
});
2425

2526
it('should return correct value for Quarter of TimeRange', () => {
26-
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Quarter);
27+
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Quarter, MOCKED_TZ);
2728

28-
expect(response).toEqual(1739653200000);
29+
expect(response).toEqual(1739664000000);
2930
});
3031

3132
it('should return correct value for HalfYear of TimeRange', () => {
32-
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.HalfYear);
33+
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.HalfYear, MOCKED_TZ);
3334

34-
expect(response).toEqual(1738357200000);
35+
expect(response).toEqual(1738368000000);
3536
});
3637

3738
it('should return correct value for Year of TimeRange', () => {
38-
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Year);
39+
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.Year, MOCKED_TZ);
3940

40-
expect(response).toEqual(1738357200000);
41+
expect(response).toEqual(1738368000000);
4142
});
4243

4344
it('should return the same value for All of TimeRange', () => {
44-
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.All);
45+
const response = roundTimeByTimeRange(MOCKED_NOW, TimeRange.All, MOCKED_TZ);
4546

4647
expect(response).toEqual(MOCKED_NOW);
4748
});

datahub-web-react/src/app/entityV2/shared/tabs/Dataset/Stats/StatsTabV2/graphs/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import dayjs from 'dayjs';
2+
import timezone from 'dayjs/plugin/timezone';
23
import utc from 'dayjs/plugin/utc';
34

45
import { LookbackWindow } from '@app/entityV2/shared/tabs/Dataset/Stats/lookbackWindows';
56
import { getTimeWindowStart } from '@src/app/shared/time/timeUtils';
67
import { DateInterval, TimeRange } from '@src/types.generated';
78

89
dayjs.extend(utc);
10+
dayjs.extend(timezone);
911

1012
export type TimeSeriesDatum = {
1113
time: number;
@@ -175,10 +177,11 @@ export function getCalendarStartTimeByTimeRange(time: number, range: TimeRange):
175177
}
176178
}
177179

178-
export function roundTimeByTimeRange(time: number | undefined, range: TimeRange) {
180+
export function roundTimeByTimeRange(time: number | undefined, range: TimeRange, tz?: string) {
179181
if (time === undefined) return time;
180182

181-
const dayjsTime = dayjs(time);
183+
let dayjsTime = dayjs(time);
184+
if (tz) dayjsTime = dayjsTime.tz(tz);
182185

183186
switch (range) {
184187
case TimeRange.Day:

0 commit comments

Comments
 (0)