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
5 changes: 5 additions & 0 deletions .changeset/per-connection-all-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

refactor: useMultipleAllFields returns each table connection's fields and columns.
27 changes: 27 additions & 0 deletions packages/app/src/components/DBSearchPageFilters/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const makeLogSource = (opts: { withMVs: boolean }) => ({
});

type SourceQueryResult = ReturnType<typeof sourceModule.useSource>;
type AllFieldsResult = ReturnType<typeof useMetadataModule.useAllFields>;
type MetadataWithSettings = ReturnType<
typeof useMetadataModule.useMetadataWithSettings
>;
Expand Down Expand Up @@ -452,6 +453,32 @@ describe('useFetchFacets', () => {
});
});

describe('errors', () => {
it('surfaces a field lookup error so the filter panel can report it', () => {
setupDefaultMocks({ withMVs: false });
const fieldsError = new Error('DESCRIBE failed');
useAllFields.mockReturnValue({
data: [],
error: fieldsError,
isLoading: false,
} as unknown as AllFieldsResult);
const { wrapper } = makeWrapper();

const { result } = renderHook(
() =>
useFetchFacets({
chartConfig: CHART_CONFIG,
sourceId: 'source1',
dateRange: DATE_RANGE,
mode: 'exact',
}),
{ wrapper },
);

expect(result.current.error).toBe(fieldsError);
});
});

describe('loadMoreFacetsForKey (raw-tables pipeline)', () => {
it('reports the key as loading while the fetch is in flight, then clears it', async () => {
setupDefaultMocks({ withMVs: false });
Expand Down
136 changes: 128 additions & 8 deletions packages/app/src/hooks/__tests__/useMetadata.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import * as metadataModule from '@hyperdx/app/src/metadata';
import { JSDataType } from '@hyperdx/common-utils/dist/clickhouse';
import { ColumnMeta, JSDataType } from '@hyperdx/common-utils/dist/clickhouse';
import { ClickhouseClient } from '@hyperdx/common-utils/dist/clickhouse/browser';
import {
Field,
Expand Down Expand Up @@ -303,14 +303,41 @@ describe('useMultipleAllFields', () => {
connectionId: 'conn1',
};

const column = (name: string, type: string): ColumnMeta => ({
name,
type,
codec_expression: '',
comment: '',
default_expression: '',
default_type: '',
ttl_expression: '',
});
const columnsA = [
column('col_a', 'String'),
column('attrs', 'Map(String, String)'),
column('json', 'JSON'),
];
const columnsB = [column('col_b', 'String')];

let team: { fieldMetadataDisabled?: boolean };

beforeEach(() => {
jest.clearAllMocks();
team = {};
mockMetadata = new Metadata({} as ClickhouseClient, {} as MetadataCache);
jest.spyOn(metadataModule, 'getMetadata').mockReturnValue(mockMetadata);
jest.spyOn(api, 'useMe').mockReturnValue({
data: { team: {} },
isFetched: true,
} as any);
jest.spyOn(api, 'useMe').mockImplementation(
() =>
({
data: { team },
isFetched: true,
}) as any,
);
jest
.spyOn(mockMetadata, 'getColumns')
.mockImplementation(async ({ tableName }) =>
tableName === 'table_a' ? columnsA : columnsB,
);

queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
Expand All @@ -320,7 +347,8 @@ describe('useMultipleAllFields', () => {
);
});

it('should return fields from successful connections and empty array for failed ones', async () => {
it('returns the other table connections and no error when only some fail', async () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest
.spyOn(mockMetadata, 'getAllFields')
.mockResolvedValueOnce(fieldsA)
Expand All @@ -332,8 +360,14 @@ describe('useMultipleAllFields', () => {

await waitFor(() => expect(result.current.isSuccess).toBe(true));

// Should contain only fieldsA since fieldsB failed
expect(result.current.data).toEqual(fieldsA);
expect(result.current.errorsByTableConnection).toEqual(
new Map([[1, new Error('connection refused')]]),
);
expect(result.current.isError).toBe(false);
expect(result.current.error).toBeNull();
// Columns loaded before the field lookup failed, so they're kept.
expect(result.current.columnsByTableConnection.get(1)).toEqual(columnsB);
});

it('falls back to each connection timestampValueExpression when none is passed', async () => {
Expand Down Expand Up @@ -392,9 +426,12 @@ describe('useMultipleAllFields', () => {
wrapper,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));
await waitFor(() => expect(result.current.isError).toBe(true));

expect(result.current.data).toEqual([]);
expect([...result.current.errorsByTableConnection.keys()]).toEqual([0, 1]);
expect(result.current.isSuccess).toBe(false);
expect(result.current.error).toEqual(new Error('fail 1'));
});

it('should log a warning for each failed connection', async () => {
Expand Down Expand Up @@ -479,6 +516,89 @@ describe('useMultipleAllFields', () => {
expect(getAllFields).not.toHaveBeenCalled();
});

it('returns the fields and columns of each table connection in input order', async () => {
jest
.spyOn(mockMetadata, 'getAllFields')
.mockImplementation(async ({ tableName }) =>
tableName === 'table_a' ? fieldsA : fieldsB,
);

const { result } = renderHook(() => useMultipleAllFields([tcA, tcB]), {
wrapper,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.fieldsByTableConnection).toEqual(
new Map([
[0, fieldsA],
[1, fieldsB],
]),
);
expect(result.current.columnsByTableConnection).toEqual(
new Map([
[0, columnsA],
[1, columnsB],
]),
);
expect(result.current.errorsByTableConnection.size).toBe(0);
expect(result.current.isError).toBe(false);
});

it('fails closed when intersecting and one table connection fails', async () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest
.spyOn(mockMetadata, 'getAllFields')
.mockImplementation(async ({ tableName }) => {
if (tableName === 'table_b') throw new Error('timeout');
return fieldsA;
});

const { result } = renderHook(
() => useMultipleAllFields([tcA, tcB], { intersect: true }),
{ wrapper },
);

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data).toEqual([]);
});

it('returns only columns when the team disables field metadata', async () => {
team = { fieldMetadataDisabled: true };
const getAllFields = jest.spyOn(mockMetadata, 'getAllFields');

const { result } = renderHook(() => useMultipleAllFields([tcA]), {
wrapper,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data).toEqual([]);
expect(result.current.isFieldMetadataDisabled).toBe(true);
expect(result.current.columnsByTableConnection.get(0)).toEqual(columnsA);
expect(getAllFields).not.toHaveBeenCalled();
});

it('keeps the same data reference across re-renders', async () => {
jest
.spyOn(mockMetadata, 'getAllFields')
.mockImplementation(async ({ tableName }) =>
tableName === 'table_a' ? fieldsA : fieldsB,
);
const tableConnections = [tcA, tcB];

const { result, rerender } = renderHook(
() => useMultipleAllFields(tableConnections),
{ wrapper },
);

await waitFor(() => expect(result.current.isSuccess).toBe(true));
const first = result.current;
rerender();
expect(result.current.data).toBe(first.data);
expect(result.current.fieldsByTableConnection).toBe(
first.fieldsByTableConnection,
);
});

it('should fetch a populated table connection when the caller passes enabled: true', async () => {
jest.spyOn(mockMetadata, 'getAllFields').mockResolvedValueOnce(fieldsA);

Expand Down
Loading
Loading