diff --git a/.changeset/table-tile-refresh-pulse.md b/.changeset/table-tile-refresh-pulse.md new file mode 100644 index 0000000000..12c73f5d08 --- /dev/null +++ b/.changeset/table-tile-refresh-pulse.md @@ -0,0 +1,9 @@ +--- +'@hyperdx/app': patch +--- + +fix: keep table tile rows on screen while a dashboard refreshes + +A dashboard refresh cleared the table tile and showed "Loading Chart Data..." +until the new result arrived. The tile now keeps its current rows and pulses +while the refetch runs, like the other dashboard tiles. diff --git a/packages/app/src/HDXMultiSeriesTableChart.tsx b/packages/app/src/HDXMultiSeriesTableChart.tsx index 9a48664d92..fb035d76ea 100644 --- a/packages/app/src/HDXMultiSeriesTableChart.tsx +++ b/packages/app/src/HDXMultiSeriesTableChart.tsx @@ -60,6 +60,7 @@ export const Table = ({ onSortingChange, variant = 'default', alternateRowBackground = false, + className, }: { data: any[]; columns: { @@ -101,6 +102,9 @@ export const Table = ({ // dedicated `.stripedRow` hover rule keeps row hover visible over a stripe // (see the CSS module). alternateRowBackground?: boolean; + // Extra classes for the scroll container, e.g. `effect-pulse` while the + // tile is refreshing. + className?: string; }) => { const brandName = useBrandDisplayName(); const MIN_COLUMN_WIDTH_PX = 100; @@ -403,7 +407,10 @@ export const Table = ({ ); return ( -
+
) : data?.data.length === 0 ? ( -
+
No data found within time range.
) : (
diff --git a/packages/app/src/components/__tests__/DBTableChart.test.tsx b/packages/app/src/components/__tests__/DBTableChart.test.tsx index 980524cccc..9063f7744e 100644 --- a/packages/app/src/components/__tests__/DBTableChart.test.tsx +++ b/packages/app/src/components/__tests__/DBTableChart.test.tsx @@ -6,6 +6,7 @@ import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizati import { Table } from '@/HDXMultiSeriesTableChart'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import useOffsetPaginatedQuery from '@/hooks/useOffsetPaginatedQuery'; +import { useOnClickLinkBuilder } from '@/hooks/useOnClickLinkBuilder'; import { useSource } from '@/source'; // Mock dependencies @@ -170,6 +171,88 @@ describe('DBTableChart', () => { expect(dateRangeIndicatorCall.mvGranularity).toBe('1 minute'); }); + describe('refresh indicator', () => { + it('asks the query to keep the previous rows during a refetch', () => { + renderWithMantine(); + + const options = jest.mocked(useOffsetPaginatedQuery).mock.calls[0][1]; + expect(options?.keepPreviousData).toBe(true); + }); + + it('pulses while a refetch is showing the previous rows', () => { + jest.mocked(useOffsetPaginatedQuery).mockReturnValue({ + ...jest.mocked(useOffsetPaginatedQuery)(baseTestConfig), + isPlaceholderData: true, + }); + + renderWithMantine(); + expect(jest.mocked(Table).mock.calls.at(-1)![0].className).toBe( + 'effect-pulse', + ); + }); + + it('does not pulse once fresh rows have loaded', () => { + renderWithMantine(); + expect( + jest.mocked(Table).mock.calls.at(-1)![0].className, + ).toBeUndefined(); + }); + + it('pulses the empty state while a refetch is running', () => { + const current = jest.mocked(useOffsetPaginatedQuery)(baseTestConfig); + jest.mocked(useOffsetPaginatedQuery).mockReturnValue({ + ...current, + data: { ...current.data!, data: [] }, + isPlaceholderData: true, + }); + + const { getByText } = renderWithMantine( + , + ); + expect(getByText('No data found within time range.')).toHaveClass( + 'effect-pulse', + ); + }); + + describe('row links', () => { + const getRowSearchLink = jest.fn(() => '/search'); + const rowAction = jest.fn(); + + afterEach(() => { + jest.mocked(useOnClickLinkBuilder).mockReturnValue(null); + }); + + const renderTable = (isPlaceholderData: boolean) => { + jest.mocked(useOffsetPaginatedQuery).mockReturnValue({ + ...jest.mocked(useOffsetPaginatedQuery)(baseTestConfig), + isPlaceholderData, + }); + renderWithMantine( + , + ); + return jest.mocked(Table).mock.calls.at(-1)![0]; + }; + + it('disables search links on rows kept from the previous query', () => { + expect(renderTable(true).getRowSearchLink).toBeUndefined(); + }); + + it('enables search links once fresh rows have loaded', () => { + expect(renderTable(false).getRowSearchLink).toBe(getRowSearchLink); + }); + + it('disables the configured row action on rows kept from the previous query', () => { + jest.mocked(useOnClickLinkBuilder).mockReturnValue(rowAction); + + expect(renderTable(true).getRowAction).toBeUndefined(); + expect(renderTable(false).getRowAction).toBe(rowAction); + }); + }); + }); + describe('groupByColumnsOnLeft', () => { // Emulates how the ClickHouse query returns rows for a builder table chart: // series columns are produced before groupBy columns. diff --git a/packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx b/packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx index edc70c9af4..60935680f2 100644 --- a/packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx +++ b/packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx @@ -753,6 +753,182 @@ describe('useOffsetPaginatedQuery', () => { }); }); + describe('Refetching for a new date range', () => { + // A single, non-windowed page so each query is one read of the stream + const rangeA = createMockChartConfig({ orderBy: 'ServiceName' }); + const rangeB = createMockChartConfig({ + orderBy: 'ServiceName', + dateRange: [ + new Date('2024-01-01T00:05:00Z'), + new Date('2024-01-02T00:05:00Z'), + ] as [Date, Date], + }); + + const page = (service: string) => ({ + done: false, + value: [ + { json: () => ['ServiceName'] }, + { json: () => ['String'] }, + { json: () => [service] }, + ], + }); + + // Loads rangeA, then switches to rangeB and holds its response until + // the returned `finish` is called, so the refetch can be inspected. + const loadThenRefetch = async (options: { keepPreviousData?: boolean }) => { + let releaseRangeB!: (value: unknown) => void; + mockReader.read + .mockResolvedValueOnce(page('from-range-a')) + .mockResolvedValueOnce({ done: true }) + .mockReturnValueOnce( + new Promise(resolve => { + releaseRangeB = resolve; + }), + ) + .mockResolvedValueOnce({ done: true }); + + const { result, rerender } = renderHook( + ({ config }) => useOffsetPaginatedQuery(config, options), + { wrapper, initialProps: { config: rangeA } }, + ); + await waitFor(() => + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-a'), + ); + + rerender({ config: rangeB }); + await waitFor(() => + expect(mockClickhouseClient.query).toHaveBeenCalledTimes(2), + ); + + return { + result, + finish: () => act(async () => releaseRangeB(page('from-range-b'))), + }; + }; + + it('keeps the previous rows as placeholder data when keepPreviousData is set', async () => { + const { result, finish } = await loadThenRefetch({ + keepPreviousData: true, + }); + + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-a'); + expect(result.current.isPlaceholderData).toBe(true); + expect(result.current.isFetching).toBe(true); + + await finish(); + await waitFor(() => + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-b'), + ); + expect(result.current.isPlaceholderData).toBe(false); + }); + + it('clears the previous rows by default', async () => { + const { result, finish } = await loadThenRefetch({}); + + expect(result.current.data).toBeNull(); + expect(result.current.isPlaceholderData).toBe(false); + expect(result.current.isLoading).toBe(true); + + await finish(); + await waitFor(() => + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-b'), + ); + }); + + it('clears the previous rows when more than the date range changes', async () => { + // e.g. a tile edit: rows from the old query would otherwise be rendered + // with the new config's columns, formats and colours + const edited = { ...rangeB, where: "ServiceName = 'api'" }; + + let releaseQuery!: () => void; + mockClickhouseClient.query + .mockImplementationOnce(() => + Promise.resolve({ stream: () => mockStream }), + ) + .mockImplementationOnce( + () => + new Promise(resolve => { + releaseQuery = () => resolve({ stream: () => mockStream }); + }), + ); + mockReader.read + .mockResolvedValueOnce(page('from-range-a')) + .mockResolvedValueOnce({ done: true }) + .mockResolvedValueOnce(page('edited')) + .mockResolvedValueOnce({ done: true }); + + const { result, rerender } = renderHook( + ({ config }) => + useOffsetPaginatedQuery(config, { keepPreviousData: true }), + { wrapper, initialProps: { config: rangeA } }, + ); + await waitFor(() => + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-a'), + ); + + rerender({ config: edited }); + await waitFor(() => + expect(mockClickhouseClient.query).toHaveBeenCalledTimes(2), + ); + + expect(result.current.data).toBeNull(); + expect(result.current.isPlaceholderData).toBe(false); + + await act(async () => releaseQuery()); + await waitFor(() => + expect(result.current.data?.data[0]?.ServiceName).toBe('edited'), + ); + }); + + it('does not offer more pages while showing placeholder rows', async () => { + // Ordered by timestamp, so each range is paged through time windows + const windowedA = createMockChartConfig(); + const windowedB = createMockChartConfig({ + dateRange: rangeB.dateRange, + }); + + let releaseRangeB!: (value: unknown) => void; + mockReader.read + .mockResolvedValueOnce(page('from-range-a')) + .mockResolvedValueOnce({ done: true }) + .mockReturnValueOnce( + new Promise(resolve => { + releaseRangeB = resolve; + }), + ) + .mockResolvedValueOnce({ done: true }); + + const { result, rerender } = renderHook( + ({ config }) => + useOffsetPaginatedQuery(config, { keepPreviousData: true }), + { wrapper, initialProps: { config: windowedA } }, + ); + await waitFor(() => + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-a'), + ); + // Range A has later time windows left to load + expect(result.current.hasNextPage).toBe(true); + + rerender({ config: windowedB }); + await waitFor(() => + expect(mockClickhouseClient.query).toHaveBeenCalledTimes(2), + ); + + // The old rows stay, but the "load more" trigger must not fire for them + expect(result.current.isPlaceholderData).toBe(true); + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-a'); + expect(result.current.hasNextPage).toBe(false); + + await act(async () => releaseRangeB(page('from-range-b'))); + await waitFor(() => + expect(result.current.data?.data[0]?.ServiceName).toBe('from-range-b'), + ); + expect(result.current.isPlaceholderData).toBe(false); + // Paging resumes against the new range + expect(result.current.hasNextPage).toBe(true); + }); + }); + describe('Query Key Management', () => { it('should generate unique query keys for different configurations', async () => { const config1 = createMockChartConfig({ diff --git a/packages/app/src/hooks/useOffsetPaginatedQuery.tsx b/packages/app/src/hooks/useOffsetPaginatedQuery.tsx index 5a27d39b96..32f635ee2c 100644 --- a/packages/app/src/hooks/useOffsetPaginatedQuery.tsx +++ b/packages/app/src/hooks/useOffsetPaginatedQuery.tsx @@ -1,5 +1,5 @@ import { useMemo } from 'react'; -import { omit } from 'lodash'; +import { isEqual, omit } from 'lodash'; import ms from 'ms'; import type { ClickHouseSettings, @@ -61,6 +61,16 @@ function queryKeyFn( return [prefix, config, queryTimeout]; } +// True when two keys describe the same query over a different time range, +// e.g. a dashboard refresh. +function differsOnlyInDateRange(a: TQueryKey, b: TQueryKey) { + return ( + a[0] === b[0] && + a[2] === b[2] && + isEqual(omit(a[1], ['dateRange']), omit(b[1], ['dateRange'])) + ); +} + type TPageParam = { windowIndex: number; offset: number; @@ -444,11 +454,14 @@ export default function useOffsetPaginatedQuery( config: ChartConfigWithOptTimestamp, { isLive, + keepPreviousData, enabled = true, queryKeyPrefix = '', enableSmallFirstWindow, }: { isLive?: boolean; + /** Keep showing the previous result while the same query loads a new date range */ + keepPreviousData?: boolean; enabled?: boolean; queryKeyPrefix?: string; enableSmallFirstWindow?: boolean; @@ -489,6 +502,7 @@ export default function useOffsetPaginatedQuery( isError, error, isLoading, + isPlaceholderData, } = useInfiniteQuery< TQueryFnData, Error | ClickHouseQueryError, @@ -497,9 +511,16 @@ export default function useOffsetPaginatedQuery( TPageParam >({ queryKey: key, - placeholderData: (prev: TData | undefined) => { - // Only preserve previous query in live mode - return isLive ? prev : undefined; + placeholderData: (prev: TData | undefined, prevQuery) => { + // Only preserve previous query in live mode, or when the caller asks + // and only the date range changed. Rows from a different config would + // be rendered with the new config's columns and formats. + if (isLive) return prev; + return keepPreviousData && + prevQuery != null && + differsOnlyInDateRange(prevQuery.queryKey, key) + ? prev + : undefined; }, enabled: enabled && !isLoadingMe && !isLoadingMVOptimization && !isSourceLoading, @@ -538,5 +559,6 @@ export default function useOffsetPaginatedQuery( hasNextPage, isFetching: isFetching || isLoadingMe || isLoadingMVOptimization, isLoading: isLoading || isLoadingMe || isLoadingMVOptimization, + isPlaceholderData, }; }