Repository navigation
fix: Only auto-detect metric tables when the database selection changes #2817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fa6c7a6
fix: Only auto-detect metric tables when the database selection changes
pulpdrew 7bce2aa
Merge branch 'main' into drew/metric-table-inference
kodiakhq[bot] bdfc945
Merge branch 'main' into drew/metric-table-inference
pulpdrew b6cc244
chore: Fix lint warnings
pulpdrew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@hyperdx/app': patch | ||
| --- | ||
|
|
||
| fix: Only auto-detect metric tables when the database selection changes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
261 changes: 261 additions & 0 deletions
261
packages/app/src/components/__tests__/MetricTableModelForm.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| import React, { useEffect } from 'react'; | ||
| import { useForm } from 'react-hook-form'; | ||
| import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; | ||
| import { MantineProvider } from '@mantine/core'; | ||
| import { render, waitFor } from '@testing-library/react'; | ||
|
|
||
| import { MetricTableModelForm } from '@/components/Sources/SourceForm/MetricTableModelForm'; | ||
|
|
||
| import '@testing-library/jest-dom'; | ||
|
|
||
| // Table lists per database, used by the mocked `useTablesDirect`. | ||
| const TABLES_BY_DB: Record<string, string[]> = { | ||
| default: ['otel_metrics_gauge', 'otel_metrics_sum'], | ||
| otel_v2: ['otel_metrics_gauge', 'otel_metrics_sum', 'otel_metrics_histogram'], | ||
| other_db: [ | ||
| 'other_metrics_gauge', | ||
| 'other_metrics_sum', | ||
| 'other_metrics_histogram', | ||
| ], | ||
| }; | ||
|
|
||
| let savedSource: TSource | undefined; | ||
|
|
||
| jest.mock('@/api', () => ({ | ||
| useTeam: () => ({ data: { isMetricsSeriesTableEnabled: false } }), | ||
| })); | ||
|
|
||
| jest.mock('@/clickhouse', () => ({ | ||
| useTablesDirect: ({ database }: { database: string }) => ({ | ||
| data: { | ||
| data: (TABLES_BY_DB[database] ?? []).map(name => ({ name })), | ||
| }, | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('@/hooks/useMetadata', () => ({ | ||
| useMetadataWithSettings: () => ({}), | ||
| })); | ||
|
|
||
| jest.mock('@/hooks/useMetricsSeriesTableAvailability', () => ({ | ||
| useMetricsSeriesTableAvailability: () => ({ | ||
| status: 'disabled', | ||
| missingSeriesHashTables: [], | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('@/source', () => ({ | ||
| isValidMetricTable: jest.fn(() => Promise.resolve(true)), | ||
| useSource: ({ id }: { id?: string }) => ({ | ||
| data: id ? savedSource : undefined, | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('@/theme/ThemeProvider', () => ({ | ||
| useBrandDisplayName: () => 'HyperDX', | ||
| })); | ||
|
|
||
| jest.mock('@mantine/notifications', () => ({ | ||
| notifications: { show: jest.fn() }, | ||
| })); | ||
|
|
||
| jest.mock('@/components/DBTableSelect', () => ({ | ||
| DBTableSelectControlled: ({ name }: { name: string }) => <div>{name}</div>, | ||
| })); | ||
|
|
||
| jest.mock('@/components/SourceSelect', () => ({ | ||
| SourceSelectControlled: () => <div />, | ||
| })); | ||
|
|
||
| const mockSetValue = jest.fn(); | ||
|
|
||
| // Rendered with a wrapper rather than the global `renderWithMantine` helper so | ||
| // that `rerender` keeps the provider (and the component instance) in place. | ||
| function renderHarness(ui: React.ReactElement) { | ||
| return render(ui, { | ||
| wrapper: ({ children }) => <MantineProvider>{children}</MantineProvider>, | ||
| }); | ||
| } | ||
|
|
||
| function Harness({ | ||
| databaseName, | ||
| metricTables, | ||
| sourceId, | ||
| }: { | ||
| databaseName: string; | ||
| metricTables?: Record<string, string>; | ||
| sourceId?: string; | ||
| }) { | ||
| const { control, setValue } = useForm<TSource>({ | ||
| defaultValues: { | ||
| kind: SourceKind.Metric, | ||
| connection: 'conn-1', | ||
| from: { databaseName, tableName: '' }, | ||
| metricTables, | ||
| }, | ||
| }); | ||
|
|
||
| // Mirrors how SourceForm's watched database changes — either from the user | ||
| // picking one, or from the saved source landing in the form. | ||
| useEffect(() => { | ||
| setValue('from.databaseName', databaseName); | ||
| }, [databaseName, setValue]); | ||
|
|
||
| return ( | ||
| <MetricTableModelForm | ||
| control={control} | ||
| setValue={mockSetValue} | ||
| sourceId={sourceId} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| // The component also pushes the fixed OTEL expressions through setValue; only | ||
| // the table fields are autofill. | ||
| function autofilledTables() { | ||
| return mockSetValue.mock.calls | ||
| .filter( | ||
| ([path]) => | ||
| typeof path === 'string' && | ||
| (path.startsWith('metricTables.') || path === 'seriesTable'), | ||
| ) | ||
| .map(([path, value]) => [path, value]); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion | ||
| const SAVED_SOURCE: TSource = { | ||
| id: 'metric-source-1', | ||
| kind: SourceKind.Metric, | ||
| name: 'Metrics', | ||
| connection: 'conn-1', | ||
| from: { databaseName: 'otel_v2', tableName: '' }, | ||
| timestampValueExpression: 'TimeUnix', | ||
| metricTables: { | ||
| gauge: 'otel_metrics_gauge', | ||
| sum: 'otel_metrics_sum', | ||
| }, | ||
| } as any; | ||
|
|
||
| describe('MetricTableModelForm metric table autofill', () => { | ||
| beforeEach(() => { | ||
| mockSetValue.mockClear(); | ||
| savedSource = undefined; | ||
| }); | ||
|
|
||
| it('autofills tables for a new source', async () => { | ||
| renderHarness(<Harness databaseName="default" />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(autofilledTables()).toEqual( | ||
| expect.arrayContaining([ | ||
| ['metricTables.gauge', 'otel_metrics_gauge'], | ||
| ['metricTables.sum', 'otel_metrics_sum'], | ||
| ]), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('autofills when a new source switches database', async () => { | ||
| const { rerender } = renderHarness(<Harness databaseName="default" />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(autofilledTables().length).toBeGreaterThan(0); | ||
| }); | ||
| mockSetValue.mockClear(); | ||
|
|
||
| rerender(<Harness databaseName="otel_v2" />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(autofilledTables()).toEqual( | ||
| expect.arrayContaining([ | ||
| ['metricTables.histogram', 'otel_metrics_histogram'], | ||
| ]), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('does not autofill when opening an existing configured source', async () => { | ||
| // Saved source hasn't loaded yet, so the form still shows the new-source | ||
| // default database — autofill must not fire against it. | ||
| const { rerender } = renderHarness( | ||
| <Harness databaseName="default" sourceId="metric-source-1" />, | ||
| ); | ||
| await waitFor(() => { | ||
| expect(mockSetValue).toHaveBeenCalled(); // OTEL expressions applied | ||
| }); | ||
| expect(autofilledTables()).toEqual([]); | ||
|
|
||
| // Saved source arrives and the form resets to its saved values. | ||
| savedSource = SAVED_SOURCE; | ||
| rerender( | ||
| <Harness | ||
| databaseName="otel_v2" | ||
| metricTables={{ gauge: 'otel_metrics_gauge', sum: 'otel_metrics_sum' }} | ||
| sourceId="metric-source-1" | ||
| />, | ||
| ); | ||
|
|
||
| // `otel_v2` has an unconfigured histogram table, but opening the source | ||
| // must leave the form untouched. | ||
| await waitFor(() => { | ||
| expect(autofilledTables()).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it('autofills when an existing configured source switches database', async () => { | ||
| savedSource = SAVED_SOURCE; | ||
| const { rerender } = renderHarness( | ||
| <Harness | ||
| databaseName="otel_v2" | ||
| metricTables={{ gauge: 'otel_metrics_gauge', sum: 'otel_metrics_sum' }} | ||
| sourceId="metric-source-1" | ||
| />, | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockSetValue).toHaveBeenCalled(); | ||
| }); | ||
| expect(autofilledTables()).toEqual([]); | ||
|
|
||
| // Switching database fills the metric types that are still unset. Types | ||
| // the user already configured are never overwritten. | ||
| rerender( | ||
| <Harness | ||
| databaseName="other_db" | ||
| metricTables={{ gauge: 'otel_metrics_gauge', sum: 'otel_metrics_sum' }} | ||
| sourceId="metric-source-1" | ||
| />, | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(autofilledTables()).toEqual([ | ||
| ['metricTables.histogram', 'other_metrics_histogram'], | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| // A saved source of another kind switched over to OTEL Metrics has no metric | ||
| // tables to preserve, so it autofills like a new source. | ||
| it('autofills for an existing source switched to the metrics kind', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion | ||
| savedSource = { | ||
| ...SAVED_SOURCE, | ||
| kind: SourceKind.Log, | ||
| metricTables: undefined, | ||
| } as any; | ||
|
|
||
| renderHarness( | ||
| <Harness databaseName="otel_v2" sourceId="metric-source-1" />, | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(autofilledTables()).toEqual( | ||
| expect.arrayContaining([ | ||
| ['metricTables.gauge', 'otel_metrics_gauge'], | ||
| ['metricTables.histogram', 'otel_metrics_histogram'], | ||
| ['metricTables.sum', 'otel_metrics_sum'], | ||
| ]), | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.