Skip to content

Commit 6ee53de

Browse files
committed
fix: Handle date ranges with same start and end date
1 parent ade5381 commit 6ee53de

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

packages/app/src/hooks/__tests__/useChartConfig.test.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,32 @@ describe('useChartConfig', () => {
286286
},
287287
]);
288288
});
289+
290+
it('returns a single window matching the input date range if the input date range is empty', () => {
291+
expect(
292+
getGranularityAlignedTimeWindows(
293+
{
294+
dateRange: [
295+
new Date('2023-01-10 00:00:30'),
296+
new Date('2023-01-10 00:00:30'),
297+
],
298+
granularity: '1 minute',
299+
timestampValueExpression: 'TimestampTime',
300+
} as ChartConfigWithDateRange & { granularity: string },
301+
[
302+
60, // 1m
303+
5 * 60, // 5m
304+
],
305+
),
306+
).toEqual([
307+
{
308+
dateRange: [
309+
new Date('2023-01-10 00:00:30'),
310+
new Date('2023-01-10 00:00:30'),
311+
],
312+
},
313+
]);
314+
});
289315
});
290316

291317
describe('useQueriedChartConfig', () => {
@@ -936,5 +962,36 @@ describe('useChartConfig', () => {
936962
// The original query should still have its chunked data
937963
expect(result1.current.data?.rows).toBeGreaterThan(1);
938964
});
965+
966+
it('returns an empty result if the given date range is empty', async () => {
967+
const config = createMockChartConfig({
968+
dateRange: [
969+
new Date('2025-10-01 00:00:00Z'),
970+
new Date('2025-10-01 00:00:00Z'),
971+
],
972+
granularity: '1 hour',
973+
});
974+
975+
const mockResponse = createMockQueryResponse([]);
976+
mockClickhouseClient.queryChartConfig.mockResolvedValue(mockResponse);
977+
978+
const { result } = renderHook(() => useQueriedChartConfig(config), {
979+
wrapper,
980+
});
981+
982+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
983+
await waitFor(() => expect(result.current.isFetching).toBe(false));
984+
985+
expect(mockClickhouseClient.queryChartConfig).toHaveBeenCalledTimes(1);
986+
987+
expect(result.current.data).toEqual({
988+
data: [],
989+
meta: mockResponse.meta,
990+
rows: 0,
991+
isComplete: true,
992+
});
993+
expect(result.current.isLoading).toBe(false);
994+
expect(result.current.isPending).toBe(false);
995+
});
939996
});
940997
});

packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,40 @@ describe('useOffsetPaginatedQuery', () => {
309309
// Should have more pages available due to large time range
310310
expect(result.current.hasNextPage).toBe(true);
311311
});
312+
313+
it('should handle a time range with the same start and end date by generating one window', async () => {
314+
const config = createMockChartConfig({
315+
dateRange: [
316+
new Date('2024-01-01T00:00:00Z'),
317+
new Date('2024-01-01T00:00:00Z'), // same start and end date
318+
] as [Date, Date],
319+
});
320+
321+
// Mock the reader to return data for first window
322+
mockReader.read
323+
.mockResolvedValueOnce({
324+
done: false,
325+
value: [
326+
{ json: () => ['timestamp', 'message'] },
327+
{ json: () => ['DateTime', 'String'] },
328+
{ json: () => ['2024-01-01T01:00:00Z', 'test log 1'] },
329+
],
330+
})
331+
.mockResolvedValueOnce({ done: true });
332+
333+
const { result } = renderHook(() => useOffsetPaginatedQuery(config), {
334+
wrapper,
335+
});
336+
337+
await waitFor(() => expect(result.current.isLoading).toBe(false));
338+
339+
// Should have data from the first window
340+
expect(result.current.data).toBeDefined();
341+
expect(result.current.data?.window.windowIndex).toBe(0);
342+
343+
// Should have more pages available due to large time range
344+
expect(result.current.hasNextPage).toBe(true);
345+
});
312346
});
313347

314348
describe('Pagination Within Time Windows', () => {

packages/app/src/utils/searchWindows.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ export function generateTimeWindowsDescending(
1818
endDate: Date,
1919
windowDurationsSeconds: number[] = DEFAULT_TIME_WINDOWS_SECONDS,
2020
): TimeWindow[] {
21+
if (startDate.getTime() === endDate.getTime()) {
22+
return [
23+
{
24+
startTime: startDate,
25+
endTime: endDate,
26+
windowIndex: 0,
27+
direction: 'DESC',
28+
},
29+
];
30+
}
31+
2132
const windows: TimeWindow[] = [];
2233
let currentEnd = new Date(endDate);
2334
let windowIndex = 0;
@@ -50,7 +61,18 @@ export function generateTimeWindowsAscending(
5061
startDate: Date,
5162
endDate: Date,
5263
windowDurationsSeconds: number[] = DEFAULT_TIME_WINDOWS_SECONDS,
53-
) {
64+
): TimeWindow[] {
65+
if (startDate.getTime() === endDate.getTime()) {
66+
return [
67+
{
68+
startTime: startDate,
69+
endTime: endDate,
70+
windowIndex: 0,
71+
direction: 'ASC',
72+
},
73+
];
74+
}
75+
5476
const windows: TimeWindow[] = [];
5577
let currentStart = new Date(startDate);
5678
let windowIndex = 0;

0 commit comments

Comments
 (0)