Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/table-tile-refresh-pulse.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 8 additions & 1 deletion packages/app/src/HDXMultiSeriesTableChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const Table = ({
onSortingChange,
variant = 'default',
alternateRowBackground = false,
className,
}: {
data: any[];
columns: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -403,7 +407,10 @@ export const Table = ({
);

return (
<div className="overflow-auto h-100 fs-8" ref={tableContainerRef}>
<div
className={cx('overflow-auto h-100 fs-8', className)}
ref={tableContainerRef}
>
<table
className="w-100"
style={{
Expand Down
38 changes: 30 additions & 8 deletions packages/app/src/components/DBTableChart.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useMemo, useState } from 'react';
import cx from 'classnames';
import { isRatioChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig';
import {
isBuilderChartConfig,
Expand Down Expand Up @@ -106,11 +107,20 @@ export default function DBTableChart({
isBuilderChartConfig(queriedConfig) ? queriedConfig : undefined,
);

const { data, fetchNextPage, hasNextPage, isLoading, isError, error } =
useOffsetPaginatedQuery(queriedConfig, {
enabled,
queryKeyPrefix,
});
const {
data,
fetchNextPage,
hasNextPage,
isLoading,
isError,
error,
isPlaceholderData,
} = useOffsetPaginatedQuery(queriedConfig, {
enabled,
queryKeyPrefix,
// Keep the current rows on screen while a refresh loads the new range
keepPreviousData: true,
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
});
const { observerRef: fetchMoreRef } = useIntersectionObserver(fetchNextPage);

// Returns an array of aliases, so we can check if something is using an alias
Expand Down Expand Up @@ -295,20 +305,32 @@ export default function DBTableChart({
) : isError && error ? (
<ChartErrorState error={error} variant={errorVariant} />
) : data?.data.length === 0 ? (
<div className="d-flex h-100 w-100 align-items-center justify-content-center text-muted">
<div
className={cx(
'd-flex h-100 w-100 align-items-center justify-content-center text-muted',
{ 'effect-pulse': isPlaceholderData },
)}
Comment thread
Harshul1484 marked this conversation as resolved.
Resolved
>
No data found within time range.
</div>
) : (
<Table
data={data?.data ?? []}
columns={columns}
getRowAction={getRowAction ?? undefined}
getRowSearchLink={getRowAction ? undefined : getRowSearchLink}
// Rows kept from the previous query would link with the new date
// range and config, so leave them inert until fresh rows arrive.
getRowAction={
isPlaceholderData ? undefined : (getRowAction ?? undefined)
}
getRowSearchLink={
isPlaceholderData || getRowAction ? undefined : getRowSearchLink
}
sorting={effectiveSort}
enableClientSideSorting={isRawSqlChartConfig(config)}
onSortingChange={handleSortingChange}
variant={variant}
alternateRowBackground={!!queriedConfig.alternateRowBackground}
className={isPlaceholderData ? 'effect-pulse' : undefined}
tableBottom={
hasNextPage && (
<Text ref={fetchMoreRef} ta="center">
Expand Down
83 changes: 83 additions & 0 deletions packages/app/src/components/__tests__/DBTableChart.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(<DBTableChart config={baseTestConfig} />);

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(<DBTableChart config={baseTestConfig} />);
expect(jest.mocked(Table).mock.calls.at(-1)![0].className).toBe(
'effect-pulse',
);
});

it('does not pulse once fresh rows have loaded', () => {
renderWithMantine(<DBTableChart config={baseTestConfig} />);
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(
<DBTableChart config={baseTestConfig} />,
);
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(
<DBTableChart
config={baseTestConfig}
getRowSearchLink={getRowSearchLink}
/>,
);
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.
Expand Down
176 changes: 176 additions & 0 deletions packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,182 @@ describe('useOffsetPaginatedQuery', () => {
});
});

describe('Refetching for a new date range', () => {
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
// 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({
Expand Down
Loading
Loading