From 05260bf354d37635f6203b690a4efeef61104a19 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Mon, 16 Nov 2026 14:02:37 -0500 Subject: [PATCH 1/4] feat: let tiles and filters use a data source variable as their source --- .changeset/source-vars-4.md | 5 + packages/app/src/DBDashboardImportPage.tsx | 60 ++++++-- packages/app/src/DBDashboardPage.tsx | 8 +- .../__tests__/DBDashboardImportPage.test.tsx | 55 ++++++++ .../ChartEditor/PromqlChartEditor.tsx | 4 +- .../ChartEditor/RawSqlChartEditor.tsx | 21 ++- .../resolveConnectionSourceSync.test.ts | 24 ++++ .../ChartEditor/__tests__/utils.test.ts | 84 +++++++++++ .../app/src/components/ChartEditor/utils.ts | 72 +++++++--- .../ChartEditorControls.tsx | 1 + .../DBEditTimeChartForm/ChartPreviewPanel.tsx | 2 +- .../DBEditTimeChartForm/EditTimeChartForm.tsx | 131 ++++++++++++++++-- .../__tests__/ChartPreviewPanel.test.tsx | 4 +- .../__tests__/utils.test.ts | 28 ++++ .../components/DBEditTimeChartForm/utils.ts | 17 ++- .../AdhocFilterEditForm.tsx | 37 ++++- .../DataSourceFilterEditForm.tsx | 23 ++- .../PromqlLabelFilterEditForm.tsx | 4 +- .../QueryExpressionFilterEditForm.tsx | 5 +- .../DataSourceFilterEditForm.test.tsx | 38 +++++ .../app/src/components/SourceMultiSelect.tsx | 14 +- packages/app/src/components/SourceSelect.tsx | 25 ++-- .../__tests__/SourceSelect.test.tsx | 77 +++++++++- .../app/src/components/sourceSelectUtils.tsx | 116 +++++++++++++++- .../dashboard-source-variables.spec.ts | 94 ++++++++++++- .../dashboard-tile-editor-filters.spec.ts | 2 +- .../tests/e2e/page-objects/DashboardPage.ts | 6 + 27 files changed, 868 insertions(+), 89 deletions(-) create mode 100644 .changeset/source-vars-4.md create mode 100644 packages/app/src/components/DashboardFiltersModal/__tests__/DataSourceFilterEditForm.test.tsx diff --git a/.changeset/source-vars-4.md b/.changeset/source-vars-4.md new file mode 100644 index 000000000..312539ba4 --- /dev/null +++ b/.changeset/source-vars-4.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +feat: let tiles and filters use a data source variable as their source diff --git a/packages/app/src/DBDashboardImportPage.tsx b/packages/app/src/DBDashboardImportPage.tsx index 21c5936fd..7a8859123 100644 --- a/packages/app/src/DBDashboardImportPage.tsx +++ b/packages/app/src/DBDashboardImportPage.tsx @@ -34,6 +34,7 @@ import { displayTypeRequiresSource, isRawSqlSavedChartConfig, } from '@hyperdx/common-utils/dist/guards'; +import { parseVariableReference } from '@hyperdx/common-utils/dist/referenceVariables'; import { type DashboardFilter, type DashboardTemplate, @@ -398,11 +399,25 @@ function resolveAppliesToSources( return { ids: slots.filter(Boolean), slots }; } +/** + * A data source variable reference (`$name`) is portable as written, so + * import neither maps it nor asks the user to. + */ +const isVariableRef = (name: string | undefined) => + parseVariableReference(name) != null; + +const sourceVariableRefs = (names: string[] | undefined) => + names?.filter(isVariableRef) ?? []; + +const mappableSourceNames = (names: string[] | undefined) => + names?.filter(name => !isVariableRef(name)); + /** Whether the filter names a single source that import must map. */ const isSourceBackedTemplateFilter = ( filter: DashboardFilter, ): filter is QueryExpressionDashboardFilter | PromqlLabelDashboardFilter => - isQueryExpressionFilter(filter) || isPrometheusLabelFilter(filter); + (isQueryExpressionFilter(filter) || isPrometheusLabelFilter(filter)) && + !isVariableRef(filter.source); /** Returns the filter if it names a source, or `undefined` if it doesn't. */ const sourceBackedTemplateFilter = (filter: DashboardFilter | undefined) => @@ -497,6 +512,7 @@ export function buildMappingFormSchema( // meaningful source even when the template names one. if ( config.source && + !isVariableRef(config.source) && displayTypeRequiresSource(config.displayType) && !data.tileSourceMappings[idx] ) { @@ -533,7 +549,10 @@ export function buildMappingFormSchema( const mappedSourceIds = data.filterAdhocSourceMappings?.[idx]?.filter( id => !!id, ); - if (!mappedSourceIds?.length) { + if ( + !mappedSourceIds?.length && + sourceVariableRefs(filter.sources).length === 0 + ) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ['filterAdhocSourceMappings', idx], @@ -646,7 +665,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { const tileSourceMappings = input.tiles.map(tile => { const config = tile.config as SavedChartConfig; - if (!config.source) return ''; + if (!config.source || isVariableRef(config.source)) return ''; const match = sources.find( source => source.name.toLowerCase() === config.source!.toLowerCase(), ); @@ -1118,15 +1137,24 @@ export function Mapping({ input }: { input: DashboardTemplate }) { // Zip the source mappings with the input filters const zippedFilters = input.filters?.map((filter, idx) => { - const appliesTo = data.filterAppliesToSourceMappings?.[idx]?.filter( - id => !!id?.length, - ); - const appliesToSourceIds = appliesTo?.length ? appliesTo : undefined; + // Data source variable references carry over unmapped + const appliesTo = [ + ...sourceVariableRefs( + appliesToTemplateFilter(filter)?.appliesToSourceIds, + ), + ...(data.filterAppliesToSourceMappings?.[idx]?.filter( + id => !!id?.length, + ) ?? []), + ]; + const appliesToSourceIds = appliesTo.length ? appliesTo : undefined; if (isAdhocFilter(filter)) { return { ...filter, - sources: - data.filterAdhocSourceMappings?.[idx]?.filter(id => !!id) ?? [], + sources: [ + ...sourceVariableRefs(filter.sources), + ...(data.filterAdhocSourceMappings?.[idx]?.filter(id => !!id) ?? + []), + ], appliesToSourceIds, }; } @@ -1227,7 +1255,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { return ( {/** Mapping for the tile's source, if one exists (they're optional for raw sql tiles) */} - {tile.config.source && ( + {tile.config.source && !isVariableRef(tile.config.source) && ( {tile.config.name} Data source @@ -1310,8 +1338,12 @@ export function Mapping({ input }: { input: DashboardTemplate }) { {/** Map filter sources */} {input.filters?.map((filter, i) => { - const appliesToSourceIds = - appliesToTemplateFilter(filter)?.appliesToSourceIds; + const appliesToSourceIds = mappableSourceNames( + appliesToTemplateFilter(filter)?.appliesToSourceIds, + ); + const adhocSourceNames = isAdhocFilter(filter) + ? mappableSourceNames(filter.sources) + : undefined; return ( {isSourceBackedTemplateFilter(filter) && ( @@ -1356,11 +1388,11 @@ export function Mapping({ input }: { input: DashboardTemplate }) { )} - {isAdhocFilter(filter) && ( + {isAdhocFilter(filter) && !!adhocSourceNames?.length && ( {filter.name} (Filter) Data sources - {filter.sources.join(', ')} + {adhocSourceNames.join(', ')} ` return shallow and prevents the tile-grid tree below // from being wrapped in an extra indentation level. const dashboardBody = ( - <> + {title} @@ -3601,7 +3605,7 @@ function DBDashboardPage({ variables={variables} /> )} - + ); return ( diff --git a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx index 2bfec3b19..9a5873603 100644 --- a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx +++ b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx @@ -724,3 +724,58 @@ describe('Dashboard import - ad hoc filter sources', () => { expect(issuesFor(['src-logs'])).toBeUndefined(); }); }); + +describe('Dashboard import - data source variables', () => { + const sourceVariableTemplate = DashboardTemplateSchema.parse({ + version: '0.1.0', + name: 'Source Variables', + tiles: [builderTile('tile-line', 'line', '$logs', 0)], + filters: [ + { + id: 'ds', + type: 'DATA_SOURCE', + name: 'Logs source', + variableName: 'logs', + allowedSourceKinds: [SourceKind.Log], + minSelections: 1, + maxSelections: 1, + isBroadcastEnabled: false, + isVariableEnabled: true, + }, + { + id: 'svc', + type: 'QUERY_EXPRESSION', + name: 'Service', + expression: 'ServiceName', + source: '$logs', + }, + { + id: 'adhoc', + type: 'ADHOC', + name: 'Conditions', + sourceType: 'sql', + sources: ['$logs', 'Traces'], + isBroadcastEnabled: true, + isVariableEnabled: true, + }, + ], + }); + + it('keeps references as written without asking for a mapping', async () => { + renderWithMantine(); + + const finish = await screen.findByRole('button', { + name: /finish import/i, + }); + expect(screen.queryByText('$logs')).not.toBeInTheDocument(); + await act(async () => { + fireEvent.click(finish); + }); + + await waitFor(() => expect(mockMutateAsync).toHaveBeenCalledTimes(1)); + const payload = mockMutateAsync.mock.calls[0][0]; + expect(payload.tiles[0].config.source).toBe('$logs'); + expect(payload.filters[1].source).toBe('$logs'); + expect(payload.filters[2].sources).toEqual(['$logs', 'src-traces']); + }); +}); diff --git a/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx b/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx index 838e3ec73..9a7942579 100644 --- a/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx +++ b/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx @@ -21,6 +21,7 @@ import { IconCirclePlus } from '@tabler/icons-react'; import { HeatmapModeControl } from '@/components/DBEditTimeChartForm/HeatmapModeControl'; import { SourceSelectControlled } from '@/components/SourceSelect'; +import { useResolvedReference } from '@/components/sourceSelectUtils'; import { usePromqlMetricNames } from '@/hooks/usePromqlMetadata'; import { useSource } from '@/source'; @@ -80,7 +81,7 @@ export default function PromqlChartEditor({ ); }, []); - const sourceId = useWatch({ control, name: 'source' }); + const sourceId = useResolvedReference(useWatch({ control, name: 'source' })); const displayType = useWatch({ control, name: 'displayType' }); const { data: source } = useSource({ id: sourceId }); // The form can still hold a non-PromQL source right after switching a tile @@ -107,6 +108,7 @@ export default function PromqlChartEditor({ name="source" data-testid="source-selector" allowedSourceKinds={allowedSourceKinds} + allowSourceVariables /> {displayType === DisplayType.Heatmap && ( diff --git a/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx b/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx index ea2304d03..bfc60685a 100644 --- a/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx +++ b/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx @@ -9,6 +9,7 @@ import { validateRawSqlChartConfig, validateRawSqlForAlert, } from '@hyperdx/common-utils/dist/core/utils'; +import { parseVariableReference } from '@hyperdx/common-utils/dist/referenceVariables'; import { ChartVariable, RawSqlChartConfig, @@ -41,6 +42,7 @@ import SourceSchemaPreview, { isSourceSchemaPreviewEnabled, } from '@/components/SourceSchemaPreview'; import { SourceSelectControlled } from '@/components/SourceSelect'; +import { useResolvedReference } from '@/components/sourceSelectUtils'; import { SQLEditorControlled } from '@/components/SQLEditor/SQLEditor'; import { type SQLCompletion } from '@/components/SQLEditor/utils'; import { IS_LOCAL_MODE } from '@/config'; @@ -80,9 +82,14 @@ export function resolveConnectionSourceSync({ sources: { id: string; connection: string }[] | undefined; }): ConnectionSourceSyncResult { if (!sources) return null; + // A data source variable reference isn't bound to a connection here: the + // filter bar decides what it selects, so it neither moves the connection + // nor is cleared by a connection change. + const isSourceReference = parseVariableReference(source) != null; // When the source changes, sync the connection to match. if (source !== prevSource) { + if (isSourceReference) return null; const sourceConnection = sources.find(s => s.id === source)?.connection; if (sourceConnection && sourceConnection !== connection) { return { field: 'connection', value: sourceConnection }; @@ -105,7 +112,7 @@ export function resolveConnectionSourceSync({ // connection change above was itself triggered by the source. if (connection !== prevConnection && prevConnection !== undefined) { const sourceConnection = sources.find(s => s.id === source)?.connection; - if (source && sourceConnection !== connection) { + if (source && !isSourceReference && sourceConnection !== connection) { return { field: 'source', value: '' }; } } @@ -148,7 +155,10 @@ export default function RawSqlChartEditor({ const displayType = useWatch({ control, name: 'displayType' }); const connection = useWatch({ control, name: 'connection' }); - const source = useWatch({ control, name: 'source' }); + const storedSource = useWatch({ control, name: 'source' }); + // A data source variable reference expands macros as the source it + // currently selects. + const source = useResolvedReference(storedSource); const sqlTemplate = useWatch({ control, name: 'sqlTemplate' }); const chartName = useWatch({ control, name: 'name' }); const sourceObject = sources?.find(s => s.id === source); @@ -196,12 +206,12 @@ export default function RawSqlChartEditor({ }; }, [debouncedRawSqlConfig, isDashboardForm]); - const prevSource = usePrevious(source); + const prevSource = usePrevious(storedSource); const prevConnection = usePrevious(connection); useEffect(() => { const update = resolveConnectionSourceSync({ - source, + source: storedSource, connection, prevSource, prevConnection, @@ -212,7 +222,7 @@ export default function RawSqlChartEditor({ } else if (update?.field === 'source') { setValue('source', update.value); } - }, [connection, prevConnection, prevSource, setValue, source, sources]); + }, [connection, prevConnection, prevSource, setValue, storedSource, sources]); const placeholderSQl = SQL_PLACEHOLDERS[displayType ?? DisplayType.Table]; @@ -293,6 +303,7 @@ export default function RawSqlChartEditor({ name="source" data-testid="source-selector" allowedSourceKinds={allowedSourceKinds} + allowSourceVariables connectionId={connection} size="xs" clearable diff --git a/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts b/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts index 7e979f0a1..57efcda34 100644 --- a/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts +++ b/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts @@ -107,4 +107,28 @@ describe('resolveConnectionSourceSync', () => { }), ).toBeNull(); }); + + it('leaves the connection alone when a data source variable is selected', () => { + expect( + resolveConnectionSourceSync({ + source: '$logs', + connection: 'conn-2', + prevSource: 'src-b', + prevConnection: 'conn-2', + sources, + }), + ).toBeNull(); + }); + + it('keeps a data source variable when the connection changes', () => { + expect( + resolveConnectionSourceSync({ + source: '$logs', + connection: 'conn-2', + prevSource: '$logs', + prevConnection: 'conn-1', + sources, + }), + ).toBeNull(); + }); }); diff --git a/packages/app/src/components/ChartEditor/__tests__/utils.test.ts b/packages/app/src/components/ChartEditor/__tests__/utils.test.ts index 63f4e2ded..24f910d61 100644 --- a/packages/app/src/components/ChartEditor/__tests__/utils.test.ts +++ b/packages/app/src/components/ChartEditor/__tests__/utils.test.ts @@ -19,6 +19,7 @@ import { convertFormStateToSavedChartConfig, convertSavedChartConfigToFormState, getAllowedSourceKinds, + getInterchangeableSourceKind, promqlExpressionPlaceholder, validateChartForm, } from '@/components/ChartEditor/utils'; @@ -2705,3 +2706,86 @@ describe('promqlExpressionPlaceholder', () => { ); }); }); + +describe('data source variable references', () => { + it('saves the reference rather than the source it resolves to', () => { + const form: ChartEditorFormState = { + displayType: DisplayType.Line, + source: '$logs', + series: [seriesItem], + }; + expect(convertFormStateToSavedChartConfig(form, logSource)).toMatchObject({ + source: '$logs', + }); + }); + + it('queries the resolved source for a raw SQL chart', () => { + const form: ChartEditorFormState = { + configType: 'sql', + displayType: DisplayType.Table, + source: '$logs', + connection: 'conn-1', + sqlTemplate: 'SELECT 1', + series: [], + }; + expect(convertFormStateToSavedChartConfig(form, logSource)).toMatchObject({ + source: '$logs', + }); + expect( + convertFormStateToChartConfig(form, dateRange, logSource), + ).toMatchObject({ source: logSource.id }); + }); + + it('saves a reference with no selected source when its kind is known', () => { + const form: ChartEditorFormState = { + displayType: DisplayType.Line, + source: '$logs', + series: [ + { ...seriesItem, metricName: 'cpu', metricType: MetricsDataType.Gauge }, + ], + }; + const saved = convertFormStateToSavedChartConfig( + form, + undefined, + SourceKind.Log, + ); + expect(saved).toMatchObject({ source: '$logs' }); + expect(saved).toMatchObject({ + select: [expect.not.objectContaining({ metricName: 'cpu' })], + }); + }); + + it('validates a reference with no selected source by its kind', () => { + const form: ChartEditorFormState = { + displayType: DisplayType.Line, + source: '$logs', + series: [seriesItem], + }; + expect( + validateChartForm(form, undefined, jest.fn(), { + sourceKind: SourceKind.Log, + }), + ).toEqual([]); + expect( + validateChartForm(form, undefined, jest.fn(), { + missingSourceMessage: 'Select a source for $logs', + }), + ).toEqual([{ path: 'source', message: 'Select a source for $logs' }]); + }); +}); + +describe('getInterchangeableSourceKind', () => { + it.each<[SourceKind[], SourceKind | undefined]>([ + [[SourceKind.Log], SourceKind.Log], + [[SourceKind.Log, SourceKind.Trace], SourceKind.Log], + [[SourceKind.Log, SourceKind.Metric], undefined], + [[SourceKind.Log, SourceKind.Session], undefined], + [[SourceKind.Session, SourceKind.Promql], undefined], + [[], undefined], + ])( + 'returns a stand-in for %p only when charts treat them alike', + (kinds, expected) => { + expect(getInterchangeableSourceKind(kinds)).toBe(expected); + }, + ); +}); diff --git a/packages/app/src/components/ChartEditor/utils.ts b/packages/app/src/components/ChartEditor/utils.ts index ba2239df5..ca2e8224a 100644 --- a/packages/app/src/components/ChartEditor/utils.ts +++ b/packages/app/src/components/ChartEditor/utils.ts @@ -85,6 +85,32 @@ export function buildRawSqlCompletions({ ]; } +/** + * Groups kinds that charts treat alike: validation and normalization only ask + * whether a source is a metric source and whether it supports formulas, and + * PromQL sources only serve PromQL charts. + */ +function chartSourceKindClass(kind: SourceKind): string { + if (kind === SourceKind.Promql) return 'promql'; + if (kind === SourceKind.Metric) return 'metric'; + return isFormulaSourceKind(kind) ? 'events' : 'session'; +} + +/** + * A kind that stands for all of `kinds` when charts treat them alike, so a + * chart built for one is valid on the others. Undefined when they differ. + */ +export function getInterchangeableSourceKind( + kinds: readonly SourceKind[], +): SourceKind | undefined { + const [first] = kinds; + if (first == null) return undefined; + const firstClass = chartSourceKindClass(first); + return kinds.every(kind => chartSourceKindClass(kind) === firstClass) + ? first + : undefined; +} + function normalizeChartConfig< C extends Pick< BuilderSavedChartConfig, @@ -100,15 +126,15 @@ function normalizeChartConfig< | 'heatmap' | 'where' >, ->(config: C, source: TSource): C { - const isMetricSource = source.kind === SourceKind.Metric; +>(config: C, sourceKind: SourceKind): C { + const isMetricSource = sourceKind === SourceKind.Metric; // Formulas (HDX-5080) render on metric and event (log/trace) sources, and // only on the display types the formula query paths support (time series / // table / number). Strip them elsewhere so a source or display-type switch // can't persist a config the renderer would reject. The form state keeps // them, so switching back restores the formula rows. const keepFormulas = - isFormulaSourceKind(source.kind) && + isFormulaSourceKind(sourceKind) && (config.formulas?.length ?? 0) > 0 && isFormulaDisplayType(config.displayType); return { @@ -200,9 +226,14 @@ export { toBuilderHeatmapMode, }; +/** + * `sourceKind` stands in for `source` when there is no source to read it from, + * as for a data source variable with no selection. + */ export function convertFormStateToSavedChartConfig( form: ChartEditorFormState, source: TSource | undefined, + sourceKind: SourceKind | undefined = source?.kind, ): SavedChartConfig | undefined { if (form.configType === 'promql' && isPromqlDisplayType(form.displayType)) { const promqlConfig: PromqlSavedChartConfig = { @@ -225,7 +256,7 @@ export function convertFormStateToSavedChartConfig( // 'alert', // TODO: Support alerts on PromQL (HDX-4636) ]), promqlExpression: formPromqlExpressions(form), - source: source?.id ?? form.source ?? '', + source: form.source || (source?.id ?? ''), legendTemplate: form.legendTemplate?.trim() || undefined, heatmap: formPromqlHeatmap(form), }; @@ -276,12 +307,12 @@ export function convertFormStateToSavedChartConfig( heatmap: formBuilderHeatmap(form), select: [], where: form.where ?? '', - source: source?.id ?? form.source ?? '', + source: form.source || (source?.id ?? ''), }; return config; } - if (source) { + if (sourceKind) { // Merge the series and select fields back together, and prevent the series field from being submitted const config: BuilderSavedChartConfig = { ...omit(form, [ @@ -299,10 +330,10 @@ export function convertFormStateToSavedChartConfig( : '' : form.series, where: form.where ?? '', - source: source.id, + source: form.source || (source?.id ?? ''), }; - return normalizeChartConfig(config, source); + return normalizeChartConfig(config, sourceKind); } } @@ -361,7 +392,7 @@ export function convertFormStateToChartConfig( ]), sqlTemplate: form.sqlTemplate ?? '', connection: form.connection ?? '', - source: form.source || undefined, + source: source?.id ?? (form.source || undefined), from: source?.from, implicitColumnExpression: source && (isLogSource(source) || isTraceSource(source)) @@ -434,7 +465,7 @@ export function convertFormStateToChartConfig( : mergedSelect, }; - return structuredClone(normalizeChartConfig(newConfig, source)); + return structuredClone(normalizeChartConfig(newConfig, source.kind)); } } @@ -550,6 +581,8 @@ export const validateChartForm = ( setError: UseFormSetError, { requireAlertDisplayName = false, + sourceKind = source?.kind, + missingSourceMessage = 'Source is required', }: { /** * Whether the alert must carry a name. Set for inline alerts, which have @@ -557,6 +590,13 @@ export const validateChartForm = ( * name the alert after whatever the chart happens to be called. */ requireAlertDisplayName?: boolean; + /** + * Stands in for `source` when there is no source to read the kind from, + * as for a data source variable with no selection. + */ + sourceKind?: SourceKind; + /** Replaces the generic error for a chart with no usable source. */ + missingSourceMessage?: string; } = {}, ) => { const errors: { path: Path; message: string }[] = []; @@ -580,9 +620,9 @@ export const validateChartForm = ( if ( !isRawSqlChart && form.displayType !== DisplayType.Markdown && - (!form.source || !source) + (!form.source || !sourceKind) ) { - errors.push({ path: `source`, message: 'Source is required' }); + errors.push({ path: `source`, message: missingSourceMessage }); } // Validate that valueExpressions are specified for each series. @@ -591,7 +631,7 @@ export const validateChartForm = ( if ( !isRawSqlChart && Array.isArray(form.series) && - source?.kind !== SourceKind.Metric && + sourceKind !== SourceKind.Metric && form.displayType !== DisplayType.Markdown && !isStringSelectDisplayType(form.displayType) ) { @@ -607,7 +647,7 @@ export const validateChartForm = ( // Validate metric names for metric sources if ( - source?.kind === SourceKind.Metric && + sourceKind === SourceKind.Metric && Array.isArray(form.series) && form.displayType !== DisplayType.Markdown && form.displayType !== DisplayType.Search && @@ -629,7 +669,7 @@ export const validateChartForm = ( // source kind + display type). if ( !isRawSqlChart && - isFormulaSourceKind(source?.kind) && + isFormulaSourceKind(sourceKind) && isFormulaDisplayType(form.displayType) && Array.isArray(form.formulas) ) { @@ -727,7 +767,7 @@ export const validateChartForm = ( !isRawSqlChart && Array.isArray(form.series) && form.displayType === DisplayType.Number && - !(form.formulas?.length && isFormulaSourceKind(source?.kind)) && + !(form.formulas?.length && isFormulaSourceKind(sourceKind)) && form.series.length > (form.seriesReturnType === 'ratio' ? 2 : 1) ) { errors.push({ diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx index 4524f411a..7c5d577cd 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx @@ -211,6 +211,7 @@ export function ChartEditorControls({ name="source" data-testid="source-selector" allowedSourceKinds={allowedSourceKinds} + allowSourceVariables onSchemaPreview={() => setIsSourceSchemaPreviewOpen(true)} isSchemaPreviewEnabled={isSourceSchemaPreviewEnabled(tableSource)} /> diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx index 61e1b47af..d08e213a8 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx @@ -262,7 +262,7 @@ export function ChartPreviewPanel({ <> {isBlockedByRequiredFilters ? ( convertSavedChartConfigToFormState(chartConfig), [chartConfig], @@ -346,13 +356,61 @@ export default function EditTimeChartForm({ [configType, displayType, heatmapMode], ); + // The form keeps a data source variable reference as written; everything + // that queries uses the source the variable currently selects. + const sourceVariableName = parseVariableReference(sourceId); + const resolvedSourceId = useResolvedReference(sourceId); + const resolvedConnection = useResolvedReference(connection); + const sourceVariableDeclarations = useSourceVariableDeclarations(); + + const referenceVariableValues = useReferenceVariableValues(); + // Only raw SQL tiles store a connection of their own + const referenceVariableNames = useMemo( + () => + getConfigReferenceVariables({ + source: sourceId, + connection: isRawSqlInput ? connection : undefined, + }), + [sourceId, connection, isRawSqlInput], + ); + const usesReferenceVariable = referenceVariableNames.length > 0; + + // An alert must query one fixed source and connection, not the viewer's + // current selection. + const alertsEnabled = + (enableAlerts ?? dashboardId != null) && !usesReferenceVariable; + // A source selection that current mode can't query counts as no source, // so that nothing downstream builds a query from it. const { data: tableSource } = useSource({ - id: sourceId, + id: resolvedSourceId, kinds: allowedSourceKinds, }); + // Saving reads only the source's kind. With no source selected, a variable + // whose kinds charts all treat alike still determines it. + const sourceVariable = sourceVariableDeclarations.find( + variable => variable.name === sourceVariableName, + ); + const sourceKind = useMemo(() => { + if (tableSource) return tableSource.kind; + if (resolvedSourceId != null) return; + const kinds = sourceVariable?.allowedSourceKinds.filter(kind => + allowedSourceKinds.includes(kind), + ); + return kinds && getInterchangeableSourceKind(kinds); + }, [tableSource, resolvedSourceId, sourceVariable, allowedSourceKinds]); + + // Why a `$name` source gave no usable source kind + const missingSourceMessage = + sourceVariableName == null + ? undefined + : sourceVariable == null + ? `This dashboard has no data source variable named $${sourceVariableName}` + : resolvedSourceId == null + ? `Select a source for $${sourceVariableName} in the filter bar to save this tile` + : `The source selected for $${sourceVariableName} can't be used with this chart type`; + const databaseName = tableSource?.from.databaseName; const tableName = tableSource?.from.tableName; @@ -380,6 +438,10 @@ export default function EditTimeChartForm({ } }, [configType, displayType, previousDisplayType, setValue]); + useEffect(() => { + if (usesReferenceVariable) setValue('alert', undefined); + }, [usesReferenceVariable, setValue]); + const showGeneratedSql = TABS_WITH_GENERATED_SQL.has(activeTab) && !isPromqlInput; @@ -534,6 +596,7 @@ export default function EditTimeChartForm({ variables, unsatisfiedRequiredFilters, applySelections: resolvedApplyFilters, + referenceVariableNames, }); }, [ queriedConfig, @@ -541,6 +604,7 @@ export default function EditTimeChartForm({ variables, unsatisfiedRequiredFilters, resolvedApplyFilters, + referenceVariableNames, ]); // Attach the dashboard's filters and variables so that the preview queries @@ -555,6 +619,28 @@ export default function EditTimeChartForm({ }; }, [queriedConfig, previewDashboardFilters]); + // With no selection for a variable it references the tile has nothing to + // query, so there may be no queried config to compute the block from. + const missingRequiredFilterNames = useMemo(() => { + if (previewDashboardFilters) { + return previewDashboardFilters.missingRequiredFilterNames; + } + const unresolved = referenceVariableNames.filter( + name => !referenceVariableValues.get(name), + ); + return unresolved.length > 0 + ? getBlockingRequiredFilters(unsatisfiedRequiredFilters ?? [], { + referenceVariableNames: unresolved, + consumesBroadcastFilters: false, + }).map(filter => filter.name) + : undefined; + }, [ + previewDashboardFilters, + referenceVariableNames, + referenceVariableValues, + unsatisfiedRequiredFilters, + ]); + const dbTimeChartConfig = useMemo( () => computeDbTimeChartConfig(previewConfig, alert), [previewConfig, alert], @@ -584,10 +670,16 @@ export default function EditTimeChartForm({ const errors = validateChartForm(form, tableSource, setError, { // An inline alert has no tile to inherit a name from. requireAlertDisplayName: alertsEnabled && dashboardId == null, + sourceKind, + missingSourceMessage, }); if (errors.length > 0) return { errors, config: null }; - const savedConfig = convertFormStateToSavedChartConfig(form, tableSource); + const savedConfig = convertFormStateToSavedChartConfig( + form, + tableSource, + sourceKind, + ); if (!savedConfig) { console.error( 'convertFormStateToSavedChartConfig returned undefined after validation passed. ' + @@ -621,6 +713,8 @@ export default function EditTimeChartForm({ }, [ tableSource, + sourceKind, + missingSourceMessage, setError, alertsEnabled, dashboardId, @@ -646,11 +740,19 @@ export default function EditTimeChartForm({ return; } - const queriedConfig = convertFormStateToChartConfig( + // A raw SQL tile's connection may be a variable reference; the + // source is already resolved through `tableSource`. + const convertedQueriedConfig = convertFormStateToChartConfig( form, dateRange, tableSource, ); + const queriedConfig = + convertedQueriedConfig && + resolveConfigReferences( + convertedQueriedConfig, + referenceVariableValues, + ); if (config && queriedConfig) { const isRawSqlChart = @@ -670,6 +772,7 @@ export default function EditTimeChartForm({ setQueriedConfigAndSource, tableSource, dateRange, + referenceVariableValues, ], ); @@ -784,7 +887,7 @@ export default function EditTimeChartForm({ const allowedSourceItems = useFilteredSortedSourceItems({ sources, allowedSourceKinds, - connectionId: configType === 'sql' ? connection : undefined, + connectionId: configType === 'sql' ? resolvedConnection : undefined, groupBySection: true, }); @@ -817,6 +920,17 @@ export default function EditTimeChartForm({ // should be filled in. Raw SQL source is optional, so leave it. if (!sourceId && configType === 'sql') return; + // Kept on the same terms the picker offers it + if ( + isSourceVariableReferenceAccepted( + sourceId, + sourceVariableDeclarations, + allowedSourceKinds, + ) + ) { + return; + } + // If the currently selected source is still allowed, keep it. const selected = sources.find(s => s.id === sourceId); if ( @@ -846,6 +960,7 @@ export default function EditTimeChartForm({ setValue, sourceId, sources, + sourceVariableDeclarations, ]); useEffect(() => { @@ -1388,16 +1503,14 @@ export default function EditTimeChartForm({ dateRange={dateRange} activeTab={activeTab} alert={alert} - sourceId={sourceId} + sourceId={resolvedSourceId} onTimeRangeSelect={onTimeRangeSelect} chartConfigForExplanations={chartConfigForExplanations} showGeneratedSql={showGeneratedSql} showSampleEvents={showSampleEvents} showGeneratedPromql={isPromqlInput} dbTimeChartConfig={dbTimeChartConfig} - missingRequiredFilterNames={ - previewDashboardFilters?.missingRequiredFilterNames - } + missingRequiredFilterNames={missingRequiredFilterNames} setValue={(name, value) => setValue(name, value)} setDisplayType={type => setValue('displayType', type, { shouldDirty: true }) diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx index 1b50d0218..92d73fca8 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx @@ -521,7 +521,7 @@ describe('ChartPreviewPanel', () => { expect( screen.getByText( - 'Missing required filters: Service, Environment. Select a value for each required filter, or turn off “Apply filters” to preview this tile without them.', + 'Missing required filters or variables: Service, Environment. Select a value for each required filter, or turn off “Apply filters” to preview this tile without them.', ), ).toBeInTheDocument(); expect(screen.queryByTestId('db-time-chart')).not.toBeInTheDocument(); @@ -544,7 +544,7 @@ describe('ChartPreviewPanel', () => { expect(screen.getByTestId('db-time-chart')).toBeInTheDocument(); expect( - screen.queryByText(/Missing required filters/), + screen.queryByText(/Missing required filters or variables/), ).not.toBeInTheDocument(); }); }); diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts index 2aa241098..d1864336f 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts @@ -796,6 +796,34 @@ describe('resolveTilePreviewFilters', () => { }).missingRequiredFilterNames, ).toEqual([]); }); + + it('still blocks on a referenced source variable while the filters are turned off', () => { + const sourceVariable: DashboardFilter = { + id: 'f3', + type: 'DATA_SOURCE', + name: 'Logs source', + allowedSourceKinds: [SourceKind.Log], + isBroadcastEnabled: false, + isVariableEnabled: true, + variableName: 'logs', + minSelections: 1, + maxSelections: 1, + }; + const unsatisfiedRequiredFilters = [requiredFilter, sourceVariable]; + + expect( + resolve({ + config: rawSqlWithFiltersMacro, + unsatisfiedRequiredFilters, + applySelections: false, + referenceVariableNames: ['logs'], + }).missingRequiredFilterNames, + ).toEqual(['Logs source']); + expect( + resolve({ unsatisfiedRequiredFilters, applySelections: false }) + .missingRequiredFilterNames, + ).toEqual([]); + }); }); describe('withBroadcastFilters', () => { diff --git a/packages/app/src/components/DBEditTimeChartForm/utils.ts b/packages/app/src/components/DBEditTimeChartForm/utils.ts index d49cc65bc..c54aeee2a 100644 --- a/packages/app/src/components/DBEditTimeChartForm/utils.ts +++ b/packages/app/src/components/DBEditTimeChartForm/utils.ts @@ -23,6 +23,7 @@ import { isRawSqlSavedChartConfig, } from '@hyperdx/common-utils/dist/guards'; import { substitutePromqlChartConfigTemplates } from '@hyperdx/common-utils/dist/macros'; +import { getReferenceVariableFilters } from '@hyperdx/common-utils/dist/referenceVariables'; import { BroadcastFilter, BuilderChartConfigWithDateRange, @@ -218,7 +219,8 @@ export type TilePreviewFilters = { * Applies the parent dashboard's filter selections to a tile preview. * * With `applySelections` off, the preview runs as an alert would: no broadcast - * filters, empty variable selections, and no required-filter block. + * filters, empty variable selections, and no required-filter block other than + * the reference variables the tile can't be queried without. */ export function resolveTilePreviewFilters({ config, @@ -227,9 +229,12 @@ export function resolveTilePreviewFilters({ variables, unsatisfiedRequiredFilters, applySelections, + referenceVariableNames, }: { config: ChartConfigWithDateRange; sourceId: string | undefined; + /** The data source and connection variables the tile's stored config references. */ + referenceVariableNames?: readonly string[]; filters: BroadcastFilter[] | undefined; variables: ChartVariable[] | undefined; unsatisfiedRequiredFilters: DashboardFilter[] | undefined; @@ -242,10 +247,17 @@ export function resolveTilePreviewFilters({ }); if (!applySelections) { + // Without a selection, a variable the tile's source or connection + // references leaves nothing to query, so it still blocks. + const referenced = new Set(referenceVariableNames); return { filters: undefined, variables: previewVariables, - missingRequiredFilterNames: [], + missingRequiredFilterNames: getReferenceVariableFilters( + unsatisfiedRequiredFilters, + ) + .filter(({ name }) => referenced.has(name)) + .map(({ filter }) => filter.name), }; } @@ -265,6 +277,7 @@ export function resolveTilePreviewFilters({ sourceId, unsatisfiedRequiredFilters, referencedVariables: previewVariables, + referenceVariableNames, }), }; } diff --git a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx index 6cf870d08..9b65f17a6 100644 --- a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx @@ -7,6 +7,11 @@ import { getAdhocFilterSourceKinds, isAdhocFilter, } from '@hyperdx/common-utils/dist/filters'; +import { + isSourceVariableReferenceAccepted, + parseVariableReference, + resolveVariableReference, +} from '@hyperdx/common-utils/dist/referenceVariables'; import { AdhocFilterSourceType, DashboardFilter, @@ -17,6 +22,10 @@ import { IconAlertTriangle } from '@tabler/icons-react'; import SearchWhereInput from '@/components/SearchInput/SearchWhereInput'; import { SourceMultiSelectControlled } from '@/components/SourceMultiSelect'; +import { + useReferenceVariableValues, + useSourceVariableDeclarations, +} from '@/components/sourceSelectUtils'; import { IS_PROMQL_ENABLED } from '@/config'; import { useSources } from '@/source'; @@ -44,6 +53,7 @@ export const AdhocFilterEditForm = ({ otherFilters, }: AdhocFilterEditFormProps) => { const { data: allSources } = useSources(); + const sourceVariables = useSourceVariableDeclarations(); const [sourceType, sourceIds, appliesToSourceIds] = useWatch({ control, name: ['sourceType', 'sources', 'appliesToSourceIds'], @@ -65,13 +75,17 @@ export const AdhocFilterEditForm = ({ ); }, [otherFilters, sourceType, sourceIds, appliesToSourceIds]); const allowedSourceKinds = getAdhocFilterSourceKinds(sourceType); - const tableConnections = useMemo( - () => - (allSources ?? []) - .filter(source => sourceIds?.includes(source.id)) - .map(tcFromSource), - [allSources, sourceIds], - ); + const referenceVariableValues = useReferenceVariableValues(); + const tableConnections = useMemo(() => { + const resolvedIds = new Set( + sourceIds?.map(id => + resolveVariableReference(id, referenceVariableValues), + ), + ); + return (allSources ?? []) + .filter(source => resolvedIds.has(source.id)) + .map(tcFromSource); + }, [allSources, sourceIds, referenceVariableValues]); // Kept visible for a saved PromQL filter so its source type can be changed const showSourceType = sourceType === 'promql' || @@ -82,6 +96,13 @@ export const AdhocFilterEditForm = ({ // hold after switching from another filter type. const validateSourceKinds = (ids: string[] | undefined) => { const hasOtherKind = ids?.some(id => { + if (parseVariableReference(id) != null) { + return !isSourceVariableReferenceAccepted( + id, + sourceVariables, + allowedSourceKinds, + ); + } const source = allSources?.find(s => s.id === id); return source && !allowedSourceKinds.includes(source.kind); }); @@ -131,6 +152,7 @@ export const AdhocFilterEditForm = ({ comboboxProps={{ withinPortal: true }} placeholder="Select sources" allowedSourceKinds={allowedSourceKinds} + allowSourceVariables rules={{ validate: (value: string[] | undefined) => !value?.length @@ -173,6 +195,7 @@ export const AdhocFilterEditForm = ({ comboboxProps={{ withinPortal: true }} placeholder="Same as data sources" allowedSourceKinds={allowedSourceKinds} + allowSourceVariables rules={{ validate: validateSourceKinds }} /> diff --git a/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx index fdb6e8bc4..b7348e491 100644 --- a/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx @@ -1,7 +1,9 @@ -import { Controller, useFormState } from 'react-hook-form'; +import { Controller, useFormState, useWatch } from 'react-hook-form'; import { DashboardFilter, SourceKind } from '@hyperdx/common-utils/dist/types'; -import { Group, MultiSelect } from '@mantine/core'; +import { Alert, Group, MultiSelect } from '@mantine/core'; +import { IconAlertTriangle } from '@tabler/icons-react'; +import { getInterchangeableSourceKind } from '@/components/ChartEditor/utils'; import { SOURCE_KIND_LABELS } from '@/components/Sources/SourceForm/constants'; import { SOURCE_KIND_ICONS } from '@/components/sourceSelectUtils'; import { IS_PROMQL_ENABLED } from '@/config'; @@ -26,6 +28,10 @@ export const DataSourceFilterEditForm = ({ otherFilters, }: DataSourceFilterEditFormProps) => { const { errors } = useFormState({ control }); + const allowedSourceKinds = useWatch({ control, name: 'allowedSourceKinds' }); + const hasMixedKinds = + (allowedSourceKinds?.length ?? 0) > 1 && + getInterchangeableSourceKind(allowedSourceKinds) == null; return ( <> @@ -60,6 +66,19 @@ export const DataSourceFilterEditForm = ({ /> + {hasMixedKinds && ( + } + data-testid="filter-mixed-source-kinds-warning" + > + These kinds support different chart options, such as metrics and + formulas. A tile built while one kind is selected may not load when + the variable is switched to another, and a new tile can't be + saved until a source is selected. + + )} + ); diff --git a/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx index bb3d90a8f..32092ff43 100644 --- a/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx @@ -8,6 +8,7 @@ import { import { AutocompleteControlled } from '@/components/InputControlled'; import PromQLEditor from '@/components/PromQLEditor/PromQLEditor'; import { SourceSelectControlled } from '@/components/SourceSelect'; +import { useResolvedReference } from '@/components/sourceSelectUtils'; import { usePromqlLabelNames, usePromqlMetricNames, @@ -42,7 +43,7 @@ export const PromqlLabelFilterEditForm = ({ // Only a PromQL source can answer the label lookup; the form may briefly hold // another kind while the user switches types. - const sourceId = useWatch({ control, name: 'source' }); + const sourceId = useResolvedReference(useWatch({ control, name: 'source' })); const source = sources?.find(s => s.id === sourceId); const promqlSource = source && isPromqlSource(source) ? source : undefined; const { data: labelNames } = usePromqlLabelNames(promqlSource?.id); @@ -64,6 +65,7 @@ export const PromqlLabelFilterEditForm = ({ }} comboboxProps={{ withinPortal: true }} allowedSourceKinds={[SourceKind.Promql]} + allowSourceVariables /> diff --git a/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx index 8d88a40b0..f45676a0e 100644 --- a/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx @@ -27,6 +27,7 @@ import SourceSchemaPreview, { isSourceSchemaPreviewEnabled, } from '@/components/SourceSchemaPreview'; import { SourceSelectControlled } from '@/components/SourceSelect'; +import { useResolvedReference } from '@/components/sourceSelectUtils'; import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; import { useSource } from '@/source'; import { getMetricTableName } from '@/utils'; @@ -60,7 +61,7 @@ export const QueryExpressionFilterEditForm = ({ showVariableOptions, }: QueryExpressionFilterEditFormProps) => { const sourceId = useWatch({ control, name: 'source' }); - const { data: source } = useSource({ id: sourceId }); + const { data: source } = useSource({ id: useResolvedReference(sourceId) }); const metricType = useWatch({ control, name: 'sourceMetricType' }); const tableName = source && getMetricTableName(source, metricType); @@ -137,6 +138,7 @@ export const QueryExpressionFilterEditForm = ({ isSchemaPreviewEnabled={isSourceSchemaPreviewEnabled(source)} disabled={!!presetSource} allowedSourceKinds={QUERY_EXPRESSION_FILTER_SOURCE_KINDS} + allowSourceVariables /> diff --git a/packages/app/src/components/DashboardFiltersModal/__tests__/DataSourceFilterEditForm.test.tsx b/packages/app/src/components/DashboardFiltersModal/__tests__/DataSourceFilterEditForm.test.tsx new file mode 100644 index 000000000..63dd72a6f --- /dev/null +++ b/packages/app/src/components/DashboardFiltersModal/__tests__/DataSourceFilterEditForm.test.tsx @@ -0,0 +1,38 @@ +import { useForm } from 'react-hook-form'; +import { SourceKind } from '@hyperdx/common-utils/dist/types'; +import { screen } from '@testing-library/react'; + +import { DataSourceFilterEditForm } from '@/components/DashboardFiltersModal/DataSourceFilterEditForm'; +import { + FilterFormValues, + toFormValues, +} from '@/components/DashboardFiltersModal/filterFormState'; + +function Harness({ allowedSourceKinds }: { allowedSourceKinds: SourceKind[] }) { + const { control } = useForm({ + defaultValues: { + ...toFormValues(), + type: 'DATA_SOURCE', + allowedSourceKinds, + }, + }); + return ; +} + +const warning = () => screen.queryByTestId('filter-mixed-source-kinds-warning'); + +describe('DataSourceFilterEditForm', () => { + it('warns when the kinds support different chart options', () => { + renderWithMantine( + , + ); + expect(warning()).toBeInTheDocument(); + }); + + it('does not warn for kinds that charts treat alike', () => { + renderWithMantine( + , + ); + expect(warning()).not.toBeInTheDocument(); + }); +}); diff --git a/packages/app/src/components/SourceMultiSelect.tsx b/packages/app/src/components/SourceMultiSelect.tsx index 665761057..2eccb1e30 100644 --- a/packages/app/src/components/SourceMultiSelect.tsx +++ b/packages/app/src/components/SourceMultiSelect.tsx @@ -10,21 +10,25 @@ import { import { IconCheck } from '@tabler/icons-react'; import { - SOURCE_KIND_ICONS, + getSourceOptionIcon, useFilteredSortedSourceItems, useSourceKindMap, + useWithSourceVariableItems, } from '@/components/sourceSelectUtils'; import { useSources } from '@/source'; function SourceMultiSelectControlledComponent({ allowedSourceKinds, connectionId, + allowSourceVariables = false, size, placeholder, ...props }: { allowedSourceKinds?: SourceKind[]; connectionId?: string; + /** Whether to offer the dashboard's data source variables. */ + allowSourceVariables?: boolean; size?: string; placeholder?: string; } & UseControllerProps & @@ -37,18 +41,22 @@ function SourceMultiSelectControlledComponent({ const sourceKindMap = useSourceKindMap(data); - const items = useFilteredSortedSourceItems({ + const sourceItems = useFilteredSortedSourceItems({ sources: data, allowedSourceKinds, connectionId, }); + const items = useWithSourceVariableItems(sourceItems, { + enabled: allowSourceVariables, + allowedSourceKinds, + }); // Mantine passes `checked` to renderOption for MultiSelect items; show a // check on selected entries so the dropdown reflects current selection // without forcing the user to scan pills above. const renderOption = useCallback( ({ option, checked }: { option: ComboboxItem; checked?: boolean }) => { - const icon = SOURCE_KIND_ICONS[sourceKindMap.get(option.value) ?? '']; + const icon = getSourceOptionIcon(option.value, sourceKindMap); return ( {icon} diff --git a/packages/app/src/components/SourceSelect.tsx b/packages/app/src/components/SourceSelect.tsx index c17bfae30..2dd72a808 100644 --- a/packages/app/src/components/SourceSelect.tsx +++ b/packages/app/src/components/SourceSelect.tsx @@ -1,4 +1,4 @@ -import { memo, useCallback, useMemo } from 'react'; +import { memo, useCallback } from 'react'; import { UseControllerProps, useWatch } from 'react-hook-form'; import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; import { @@ -22,10 +22,11 @@ import { import SelectControlled from '@/components/SelectControlled'; import { - SOURCE_KIND_ICONS, + getSourceOptionIcon, sourceSelectFilter, useFilteredSortedSourceItems, useSourceKindMap, + useWithSourceVariableItems, } from '@/components/sourceSelectUtils'; import { useSources } from '@/source'; @@ -161,6 +162,7 @@ function SourceSelectControlledComponent({ allowedSourceKinds, connectionId, isSourceAllowed, + allowSourceVariables = false, comboboxProps, ...props }: { @@ -174,6 +176,8 @@ function SourceSelectControlledComponent({ connectionId?: string; /** Extra per-source gate for restrictions a kind can't express. */ isSourceAllowed?: (source: TSource) => boolean; + /** Whether to offer the dashboard's data source variables. */ + allowSourceVariables?: boolean; } & UseControllerProps & SelectProps) { const { data } = useSources(); @@ -182,23 +186,18 @@ function SourceSelectControlledComponent({ name: props.name, }); - const selectedSourceKind = useMemo( - () => data?.find(s => s.id === selectedSourceId)?.kind, - [data, selectedSourceId], - ); + const sourceKindMap = useSourceKindMap(data); - const leftIcon = SOURCE_KIND_ICONS[selectedSourceKind ?? ''] ?? ( + const leftIcon = getSourceOptionIcon(selectedSourceId, sourceKindMap) ?? ( ); - const sourceKindMap = useSourceKindMap(data); - // Mantine passes `checked` to renderOption for the currently selected // option; render a trailing check so the active source is obvious in the // dropdown (the closed input only shows the kind icon + label). const renderOption = useCallback( ({ option, checked }: { option: ComboboxItem; checked?: boolean }) => { - const icon = SOURCE_KIND_ICONS[sourceKindMap.get(option.value) ?? '']; + const icon = getSourceOptionIcon(option.value, sourceKindMap); return ( {icon} @@ -219,6 +218,10 @@ function SourceSelectControlledComponent({ isSourceAllowed, groupBySection: true, }); + const items = useWithSourceVariableItems(sourceItems, { + enabled: allowSourceVariables, + allowedSourceKinds, + }); const hasSelection = !!selectedSourceId; const hasMenu = @@ -233,7 +236,7 @@ function SourceSelectControlledComponent({ > ({ @@ -265,3 +270,73 @@ describe('SourceSelectControlled (grouped rendering + tag-style search)', () => expect(screen.queryByText('Other')).not.toBeInTheDocument(); }); }); + +describe('SourceSelectControlled (data source variables)', () => { + const SOURCES = [makeSource('logs', 'Logs', SourceKind.Log)]; + const sourceVariable = ( + variableName: string, + allowedSourceKinds: SourceKind[], + connection?: string, + ): DataSourceDashboardFilter => ({ + id: variableName, + type: 'DATA_SOURCE', + name: variableName, + variableName, + allowedSourceKinds, + connection, + minSelections: 1, + maxSelections: 1, + isBroadcastEnabled: false, + isVariableEnabled: true, + }); + const VARIABLES = [ + sourceVariable('logs_source', [SourceKind.Log]), + sourceVariable('any_source', [SourceKind.Log, SourceKind.Promql]), + ]; + + function VariableHarness({ + allowSourceVariables, + }: { + allowSourceVariables?: boolean; + }) { + const { control } = useForm(); + return ( + + + + ); + } + + it('offers only variables whose every kind the picker allows', async () => { + asMock(useSources).mockReturnValue({ data: SOURCES }); + renderWithMantine(); + await userEvent.click(screen.getByPlaceholderText('Data Source')); + + expect(await screen.findByText('Variables')).toBeInTheDocument(); + expect(screen.getByText('$logs_source')).toBeInTheDocument(); + expect(screen.queryByText('$any_source')).not.toBeInTheDocument(); + }); + + it('puts unsectioned sources under their own header after the variables', async () => { + asMock(useSources).mockReturnValue({ data: SOURCES }); + renderWithMantine(); + await userEvent.click(screen.getByPlaceholderText('Data Source')); + + expect(await screen.findByText('Variables')).toBeInTheDocument(); + expect(screen.getByText('Sources')).toBeInTheDocument(); + }); + + it('offers no variables unless the picker opts in', async () => { + asMock(useSources).mockReturnValue({ data: SOURCES }); + renderWithMantine(); + await userEvent.click(screen.getByPlaceholderText('Data Source')); + + expect(await screen.findByText('Logs')).toBeInTheDocument(); + expect(screen.queryByText('$logs_source')).not.toBeInTheDocument(); + }); +}); diff --git a/packages/app/src/components/sourceSelectUtils.tsx b/packages/app/src/components/sourceSelectUtils.tsx index 1533d6e56..67b4bc65d 100644 --- a/packages/app/src/components/sourceSelectUtils.tsx +++ b/packages/app/src/components/sourceSelectUtils.tsx @@ -1,6 +1,18 @@ -import { useMemo } from 'react'; -import { parseVariableReference } from '@hyperdx/common-utils/dist/referenceVariables'; -import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; +import { createContext, use, useMemo } from 'react'; +import { + getSourceVariableDeclarations, + isSourceVariableAccepted, + parseVariableReference, + ReferenceVariableValues, + resolveVariableReference, + SourceVariableDeclaration, + toVariableReference, +} from '@hyperdx/common-utils/dist/referenceVariables'; +import { + DashboardFilter, + SourceKind, + TSource, +} from '@hyperdx/common-utils/dist/types'; import { ComboboxItem, OptionsFilter } from '@mantine/core'; import { IconChartLine, @@ -28,6 +40,104 @@ export function getSourceOptionIcon( return SOURCE_KIND_ICONS[(value && sourceKindMap.get(value)) ?? '']; } +type ReferenceVariablesContextValue = { + sourceVariables: SourceVariableDeclaration[]; + values: ReferenceVariableValues; +}; + +const ReferenceVariablesContext = createContext( + { sourceVariables: [], values: new Map() }, +); + +/** + * Offers the data source variables `filters` declare to the pickers below it + * that opt in, and their selections to `useResolvedReference`. + */ +export function ReferenceVariablesProvider({ + filters, + values, + children, +}: { + filters: DashboardFilter[]; + values: ReferenceVariableValues; + children: React.ReactNode; +}) { + const value = useMemo( + () => ({ + sourceVariables: getSourceVariableDeclarations(filters), + values, + }), + [filters, values], + ); + return ( + + {children} + + ); +} + +/** + * The ID a source field currently queries: an ID as is, or the selection of + * the variable it references. + */ +export function useResolvedReference( + value: string | undefined, +): string | undefined { + const { values } = use(ReferenceVariablesContext); + return resolveVariableReference(value, values); +} + +export function useReferenceVariableValues(): ReferenceVariableValues { + return use(ReferenceVariablesContext).values; +} + +export function useSourceVariableDeclarations(): SourceVariableDeclaration[] { + return use(ReferenceVariablesContext).sourceVariables; +} + +const toVariableItem = (name: string): ComboboxItem => { + const reference = toVariableReference(name); + return { value: reference, label: reference }; +}; + +/** + * A source picker's items, preceded by a "Variables" group of the data source + * variables it can offer. A variable is offered only if every kind it can + * resolve to is allowed, so no selection can produce an unusable source. + */ +export function useWithSourceVariableItems( + sourceItems: ComboboxItem[] | SourceSelectGroup[], + { + enabled, + allowedSourceKinds, + }: { + enabled: boolean; + allowedSourceKinds?: SourceKind[]; + }, +): (ComboboxItem | SourceSelectGroup)[] { + const { sourceVariables } = use(ReferenceVariablesContext); + return useMemo(() => { + if (!enabled) return sourceItems; + const items = sourceVariables + .filter( + variable => + !allowedSourceKinds || + isSourceVariableAccepted(variable, allowedSourceKinds), + ) + .map(variable => toVariableItem(variable.name)); + if (items.length === 0) return sourceItems; + // Mantine renders ungrouped items after a group as part of it, so a flat + // source list gets a header of its own + const isFlat = sourceItems.some(item => !('group' in item)); + return [ + { group: 'Variables', items }, + ...(isFlat + ? [{ group: 'Sources', items: sourceItems as ComboboxItem[] }] + : sourceItems), + ]; + }, [enabled, sourceVariables, allowedSourceKinds, sourceItems]); +} + /** Header for sources that have no section assigned. Internal to this module. */ const UNSECTIONED_SOURCE_GROUP = 'Other'; diff --git a/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts b/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts index 1929d8705..b1f38a3d5 100644 --- a/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts +++ b/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts @@ -1,8 +1,10 @@ /** * A `DATA_SOURCE` dashboard filter is a single-select variable whose value is - * a source, picked from the filter bar. + * a source. Tiles and other filters reference it as `$name` in place of a + * source, block until it has a selection, and cannot carry alerts. */ import { DashboardPage } from '../page-objects/DashboardPage'; +import { SERVICES } from '../seed-clickhouse'; import { expect, test } from '../utils/base-test'; import { DEFAULT_LOGS_SOURCE_NAME, @@ -20,9 +22,17 @@ test.describe( await dashboardPage.goto(); }); - test('picks a source of the allowed kinds from the filter bar', async ({ + test('drives tiles and filters from the selected source', async ({ page, }) => { + const editor = dashboardPage.chartEditor; + const reloadWithoutFilterState = async () => { + const url = new URL(page.url()); + url.search = ''; + await page.goto(url.toString()); + await dashboardPage.waitForLoaded(); + }; + await test.step('Create a data source variable limited to logs', async () => { await dashboardPage.createNewDashboard(); await dashboardPage.openEditFiltersModal(); @@ -34,7 +44,21 @@ test.describe( await expect( dashboardPage.getFilterItemByName('Logs source'), ).toContainText('($logs)'); + }); + + await test.step('A queried filter can read from the variable', async () => { + await dashboardPage.addFilterToDashboard( + 'Service', + '$logs', + 'ServiceName', + ); + await expect( + dashboardPage.getFilterItemByName('Service'), + ).toContainText('$logs'); await dashboardPage.closeFiltersModal(); + await expect( + dashboardPage.getFilterPendingVariableWarning('Service'), + ).toBeVisible(); }); await test.step('The variable offers only allowed kinds', async () => { @@ -48,20 +72,76 @@ test.describe( await page.keyboard.press('Escape'); }); - await test.step('A saved selection is the initial value', async () => { + await test.step('A tile can be saved on the variable before it has a value', async () => { + await dashboardPage.addTile(); + await expect(editor.nameInput).toBeVisible(); + await editor.waitForDataToLoad(); + await expect(editor.alertButton).toBeVisible(); + await editor.setChartName('Variable tile'); + await editor.selectSource('$logs'); + await expect(editor.alertButton).toHaveCount(0); + await editor.save(); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await expect( + dashboardPage.getTileMissingRequiredFilters(0), + ).toContainText('Logs source'); + }); + + await test.step('A selection loads the tile and the filter', async () => { await dashboardPage.selectReferenceFilterValue( 'Logs source', DEFAULT_LOGS_SOURCE_NAME, ); + await expect( + dashboardPage.getTileMissingRequiredFilters(0), + ).toHaveCount(0); + await expect(dashboardPage.getTileTimeChart(0)).toBeVisible(); + await expect( + dashboardPage.getFilterPendingVariableWarning('Service'), + ).toHaveCount(0); + await dashboardPage.openFilterDropdown('Service'); + await expect(dashboardPage.getFilterOption(SERVICES[0])).toBeVisible(); + await page.keyboard.press('Escape'); + }); + + await test.step('The editor keeps the reference', async () => { + await dashboardPage.editTile(0); + await expect(editor.source).toHaveValue('$logs'); + await expect(editor.alertButton).toHaveCount(0); + await editor.save(); + }); + + await test.step('A saved selection is the initial value', async () => { await dashboardPage.saveQueryAndFiltersAsDefault(); - const url = new URL(page.url()); - url.search = ''; - await page.goto(url.toString()); - await dashboardPage.waitForLoaded(); + await reloadWithoutFilterState(); await expect( dashboardPage.getFilterSelectByName('Logs source'), ).toHaveValue(DEFAULT_LOGS_SOURCE_NAME); + await expect( + dashboardPage.getTileMissingRequiredFilters(0), + ).toHaveCount(0); + }); + + await test.step('A selection the variable no longer offers stops applying', async () => { + await dashboardPage.openEditFiltersModal(); + await dashboardPage.openEditFilterForm('Logs source'); + const kindsInput = page.getByTestId( + 'filter-allowed-source-kinds-input', + ); + await kindsInput.click(); + await dashboardPage.getFilterOption('Traces').click(); + await dashboardPage.getFilterOption('Logs').click(); + await dashboardPage.getFilterNameInput().click(); + await page.getByTestId('save-filter-button').click(); + await dashboardPage.closeFiltersModal(); + + await expect( + dashboardPage.getFilterSelectByName('Logs source'), + ).toHaveValue(''); + await expect( + dashboardPage.getTileMissingRequiredFilters(0), + ).toContainText('Logs source'); }); }); diff --git a/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts b/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts index 314cf2ac1..cd25f4f0c 100644 --- a/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts +++ b/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts @@ -120,7 +120,7 @@ test.describe( dashboardPage.chartEditor.previewMissingRequiredFilters(); await expect(placeholder).toBeVisible({ timeout: 20000 }); await expect(placeholder).toContainText( - 'Missing required filters: Service', + 'Missing required filters or variables: Service', ); }); diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index 9b6fdd00d..86130cde3 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -632,6 +632,12 @@ export class DashboardPage { return new HeatmapComponent(this.page, this.getTile(tileIndex)); } + getTileTimeChart(tileIndex = 0): Locator { + return this.getTile(tileIndex) + .locator('.recharts-responsive-container') + .first(); + } + /** * Hover over a tile to reveal action buttons */ From e5613ee88553ead408d8f358e1ab3b89680caaef Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Mon, 16 Nov 2026 14:46:01 -0500 Subject: [PATCH 2/4] feat: offer data source variables with some usable kinds in pickers, and ignore unusable selections in filter forms --- .../AdhocFilterEditForm.tsx | 8 ++- .../QueryExpressionFilterEditForm.tsx | 6 +- .../__tests__/SourceSelect.test.tsx | 6 +- .../app/src/components/sourceSelectUtils.tsx | 5 +- .../dashboard-source-variables.spec.ts | 55 +++++++++++++++++++ 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx index 9b65f17a6..29b23c09d 100644 --- a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx @@ -82,10 +82,14 @@ export const AdhocFilterEditForm = ({ resolveVariableReference(id, referenceVariableValues), ), ); + // A data source variable can select a source of a kind this filter can't use + const kinds = getAdhocFilterSourceKinds(sourceType); return (allSources ?? []) - .filter(source => resolvedIds.has(source.id)) + .filter( + source => resolvedIds.has(source.id) && kinds.includes(source.kind), + ) .map(tcFromSource); - }, [allSources, sourceIds, referenceVariableValues]); + }, [allSources, sourceIds, referenceVariableValues, sourceType]); // Kept visible for a saved PromQL filter so its source type can be changed const showSourceType = sourceType === 'promql' || diff --git a/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx index f45676a0e..a806135d4 100644 --- a/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx @@ -61,7 +61,11 @@ export const QueryExpressionFilterEditForm = ({ showVariableOptions, }: QueryExpressionFilterEditFormProps) => { const sourceId = useWatch({ control, name: 'source' }); - const { data: source } = useSource({ id: useResolvedReference(sourceId) }); + // A data source variable can select a source of a kind this filter can't use + const { data: source } = useSource({ + id: useResolvedReference(sourceId), + kinds: QUERY_EXPRESSION_FILTER_SOURCE_KINDS, + }); const metricType = useWatch({ control, name: 'sourceMetricType' }); const tableName = source && getMetricTableName(source, metricType); diff --git a/packages/app/src/components/__tests__/SourceSelect.test.tsx b/packages/app/src/components/__tests__/SourceSelect.test.tsx index 24d3b5bdd..b4dbdbbf0 100644 --- a/packages/app/src/components/__tests__/SourceSelect.test.tsx +++ b/packages/app/src/components/__tests__/SourceSelect.test.tsx @@ -292,6 +292,7 @@ describe('SourceSelectControlled (data source variables)', () => { const VARIABLES = [ sourceVariable('logs_source', [SourceKind.Log]), sourceVariable('any_source', [SourceKind.Log, SourceKind.Promql]), + sourceVariable('prom_source', [SourceKind.Promql]), ]; function VariableHarness({ @@ -312,14 +313,15 @@ describe('SourceSelectControlled (data source variables)', () => { ); } - it('offers only variables whose every kind the picker allows', async () => { + it('offers only variables with at least one kind the picker allows', async () => { asMock(useSources).mockReturnValue({ data: SOURCES }); renderWithMantine(); await userEvent.click(screen.getByPlaceholderText('Data Source')); expect(await screen.findByText('Variables')).toBeInTheDocument(); expect(screen.getByText('$logs_source')).toBeInTheDocument(); - expect(screen.queryByText('$any_source')).not.toBeInTheDocument(); + expect(screen.getByText('$any_source')).toBeInTheDocument(); + expect(screen.queryByText('$prom_source')).not.toBeInTheDocument(); }); it('puts unsectioned sources under their own header after the variables', async () => { diff --git a/packages/app/src/components/sourceSelectUtils.tsx b/packages/app/src/components/sourceSelectUtils.tsx index 67b4bc65d..ede2f0e45 100644 --- a/packages/app/src/components/sourceSelectUtils.tsx +++ b/packages/app/src/components/sourceSelectUtils.tsx @@ -102,8 +102,9 @@ const toVariableItem = (name: string): ComboboxItem => { /** * A source picker's items, preceded by a "Variables" group of the data source - * variables it can offer. A variable is offered only if every kind it can - * resolve to is allowed, so no selection can produce an unusable source. + * variables it can offer. A variable is offered if at least one kind it can + * resolve to is allowed; a selection of another kind shows as an error where + * the reference is used. */ export function useWithSourceVariableItems( sourceItems: ComboboxItem[] | SourceSelectGroup[], diff --git a/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts b/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts index b1f38a3d5..8c5d73610 100644 --- a/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts +++ b/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts @@ -8,6 +8,7 @@ import { SERVICES } from '../seed-clickhouse'; import { expect, test } from '../utils/base-test'; import { DEFAULT_LOGS_SOURCE_NAME, + DEFAULT_METRICS_SOURCE_NAME, DEFAULT_TRACES_SOURCE_NAME, } from '../utils/constants'; @@ -185,5 +186,59 @@ test.describe( }); }); }); + + // A filter edit can leave other filters with a variable they can't always + // use. It still saves, and the filter shows an error while it can't. + test("saves a kind other filters can't use, and flags them while it's selected", async ({ + page, + }) => { + await test.step('An ad hoc filter reads from a logs variable', async () => { + await dashboardPage.createNewDashboard(); + await dashboardPage.openEditFiltersModal(); + await dashboardPage.addDataSourceFilterToDashboard( + 'Logs source', + ['Logs'], + 'logs', + ); + await dashboardPage.addAdhocFilterToDashboard('Conditions', ['$logs']); + }); + + await test.step('Metrics can be added to the variable', async () => { + await dashboardPage.openEditFilterForm('Logs source'); + await page.getByTestId('filter-allowed-source-kinds-input').click(); + await dashboardPage.getFilterOption('OTel metrics').click(); + await dashboardPage.getFilterNameInput().click(); + await page.getByTestId('save-filter-button').click(); + await dashboardPage.closeFiltersModal(); + await expect(page.getByText('Unable to save dashboard')).toHaveCount(0); + }); + + await test.step('Selecting a metric source flags the ad hoc filter', async () => { + await dashboardPage.selectReferenceFilterValue( + 'Logs source', + DEFAULT_METRICS_SOURCE_NAME, + ); + await expect( + dashboardPage.getFilterErrorIcon('Conditions'), + ).toBeVisible(); + + await dashboardPage.selectReferenceFilterValue( + 'Logs source', + DEFAULT_LOGS_SOURCE_NAME, + ); + await expect( + dashboardPage.getFilterErrorIcon('Conditions'), + ).toHaveCount(0); + }); + + await test.step('The added kind was saved', async () => { + await page.reload(); + await dashboardPage.waitForLoaded(); + await dashboardPage.getFilterSelectByName('Logs source').click(); + await expect( + dashboardPage.getFilterOption(DEFAULT_METRICS_SOURCE_NAME), + ).toBeVisible(); + }); + }); }, ); From 64ab3f368e92512195e3d7cf25c6713681222181 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Tue, 17 Nov 2026 12:04:42 -0500 Subject: [PATCH 3/4] review: Address review feedback --- .../DBEditTimeChartForm/ChartPreviewPanel.tsx | 25 +++- .../DBEditTimeChartForm/EditTimeChartForm.tsx | 61 ++++++--- .../__tests__/ChartPreviewPanel.test.tsx | 43 +++++++ .../__tests__/DBEditTimeChartForm.test.tsx | 116 +++++++++++++++++ .../__tests__/utils.test.ts | 76 ++++++++++++ .../useDebouncedFormValues.ts | 25 ++++ .../components/DBEditTimeChartForm/utils.ts | 117 ++++++++++++++---- .../AdhocFilterEditForm.tsx | 12 +- .../app/src/components/sourceSelectUtils.tsx | 8 +- 9 files changed, 427 insertions(+), 56 deletions(-) create mode 100644 packages/app/src/components/DBEditTimeChartForm/useDebouncedFormValues.ts diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx index d08e213a8..a061b8811 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx @@ -178,6 +178,10 @@ type ChartPreviewPanelProps = { dbTimeChartConfig?: ChartConfigWithDateRange; /** Required dashboard filters with nothing selected that block this tile. */ missingRequiredFilterNames?: string[]; + /** Whether turning off "Apply filters" would clear the required-filter block. */ + canPreviewWithoutFilters?: boolean; + /** Why the current selections can't be queried, shown instead of the chart. */ + blockedReason?: string; setValue: (name: 'orderBy', value: string) => void; setDisplayType: (displayType: DisplayType) => void; onSubmit: () => void; @@ -197,6 +201,8 @@ export function ChartPreviewPanel({ showGeneratedPromql, dbTimeChartConfig, missingRequiredFilterNames, + canPreviewWithoutFilters = false, + blockedReason, setValue, setDisplayType, onSubmit, @@ -213,8 +219,12 @@ export function ChartPreviewPanel({ const blockingFilterNames = missingRequiredFilterNames ?? []; const isBlockedByRequiredFilters = blockingFilterNames.length > 0 && tabQueriesData(activeTab); + const isBlocked = + blockedReason != null && + !isBlockedByRequiredFilters && + tabQueriesData(activeTab); const queryReady = - !isBlockedByRequiredFilters && !!isQueryReady(queriedConfig); + !isBlockedByRequiredFilters && !isBlocked && !!isQueryReady(queriedConfig); const onTableSortingChange = useCallback( (sortState: SortingState | null) => { @@ -264,11 +274,22 @@ export function ChartPreviewPanel({ + ) : isBlocked ? ( + ) : !queryReady && tabQueriesData(activeTab) ? ( { - if (previewDashboardFilters) { - return previewDashboardFilters.missingRequiredFilterNames; - } - const unresolved = referenceVariableNames.filter( - name => !referenceVariableValues.get(name), - ); - return unresolved.length > 0 - ? getBlockingRequiredFilters(unsatisfiedRequiredFilters ?? [], { - referenceVariableNames: unresolved, - consumesBroadcastFilters: false, - }).map(filter => filter.name) - : undefined; + const previewBlockingFilters = useMemo(() => { + if (previewDashboardFilters) return previewDashboardFilters; + if ( + !referenceVariableNames.some(name => !referenceVariableValues.get(name)) + ) + return undefined; + return getPreviewBlockingFilters({ + config: convertFormStateToSavedChartConfig(debouncedForm, tableSource), + sourceId: undefined, + variables, + unsatisfiedRequiredFilters, + applySelections: resolvedApplyFilters, + referenceVariableNames, + }); }, [ previewDashboardFilters, referenceVariableNames, referenceVariableValues, + debouncedForm, + tableSource, + variables, unsatisfiedRequiredFilters, + resolvedApplyFilters, ]); const dbTimeChartConfig = useMemo( @@ -646,10 +665,6 @@ export default function EditTimeChartForm({ [previewConfig, alert], ); - // Casting because `useWatch` returns a deep partial type, but we know that the - // form state is complete due to default values set above. - const watchedForm = useWatch({ control }) as ChartEditorFormState; - const [debouncedForm] = useDebouncedValue(watchedForm, 300); const additionalAlertWarnings = useMemo(() => { if (alert == null) return []; const config = convertFormStateToSavedChartConfig( @@ -1510,7 +1525,13 @@ export default function EditTimeChartForm({ showSampleEvents={showSampleEvents} showGeneratedPromql={isPromqlInput} dbTimeChartConfig={dbTimeChartConfig} - missingRequiredFilterNames={missingRequiredFilterNames} + missingRequiredFilterNames={ + previewBlockingFilters?.missingRequiredFilterNames + } + canPreviewWithoutFilters={ + previewBlockingFilters?.canPreviewWithoutFilters + } + blockedReason={previewBlockedReason} setValue={(name, value) => setValue(name, value)} setDisplayType={type => setValue('displayType', type, { shouldDirty: true }) diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx index 92d73fca8..878ccf494 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx @@ -514,6 +514,7 @@ describe('ChartPreviewPanel', () => { queriedConfig: baseBuilderConfig, dbTimeChartConfig: baseBuilderConfig, missingRequiredFilterNames: ['Service', 'Environment'], + canPreviewWithoutFilters: true, }; it('names them instead of querying', () => { @@ -531,6 +532,16 @@ describe('ChartPreviewPanel', () => { ).not.toBeInTheDocument(); }); + it('leaves out the "Apply filters" hint when turning it off would not help', () => { + renderPanel({ ...blockedProps, canPreviewWithoutFilters: false }); + + expect( + screen.getByTestId('preview-missing-required-filters'), + ).toHaveTextContent( + /^Missing required filters or variables: Service, Environment\. Select a value for each one to preview this tile\.$/, + ); + }); + it('does not block a markdown tile, which queries nothing', () => { renderPanel({ ...blockedProps, activeTab: 'markdown' }); @@ -548,4 +559,36 @@ describe('ChartPreviewPanel', () => { ).not.toBeInTheDocument(); }); }); + + describe('when the selections are blocked', () => { + const blockedProps = { + queriedConfig: baseBuilderConfig, + dbTimeChartConfig: baseBuilderConfig, + blockedReason: 'The selected data source is on another connection.', + }; + + it('shows the reason instead of querying', () => { + renderPanel(blockedProps); + + expect(screen.getByTestId('preview-blocked')).toHaveTextContent( + 'The selected data source is on another connection.', + ); + expect(screen.queryByTestId('db-time-chart')).not.toBeInTheDocument(); + }); + + it('shows missing required filters first', () => { + renderPanel({ ...blockedProps, missingRequiredFilterNames: ['Service'] }); + + expect( + screen.getByTestId('preview-missing-required-filters'), + ).toBeInTheDocument(); + expect(screen.queryByTestId('preview-blocked')).not.toBeInTheDocument(); + }); + + it('does not block a markdown tile, which queries nothing', () => { + renderPanel({ ...blockedProps, activeTab: 'markdown' }); + + expect(screen.queryByTestId('preview-blocked')).not.toBeInTheDocument(); + }); + }); }); diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx b/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx index bd9dd337b..d0be20577 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx @@ -3,6 +3,8 @@ import React from 'react'; import { useController as mockUseController } from 'react-hook-form'; import { AlertThresholdType, + DashboardFilter, + DataSourceDashboardFilter, DisplayType, MetricsDataType, SavedChartConfig, @@ -13,6 +15,7 @@ import { fireEvent, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import DBEditTimeChartForm from '@/components/DBEditTimeChartForm'; +import { ReferenceVariablesProvider } from '@/components/sourceSelectUtils'; import { useSource } from '@/source'; import { DEFAULT_TILE_ALERT } from '@/utils/alerts'; @@ -40,6 +43,8 @@ jest.mock('@/hooks/useFetchMetricMetadata', () => ({ jest.mock('@/hooks/useMetadata', () => ({ useMetadataWithSettings: jest.fn().mockReturnValue({}), + // The raw SQL editor's completions + useMultipleAllFields: jest.fn().mockReturnValue({ data: [] }), useGetKeyValues: jest.fn().mockReturnValue({ data: [ { @@ -1585,3 +1590,114 @@ describe('DBEditTimeChartForm - Inline alerts', () => { expect(screen.queryByTestId('remove-alert-button')).not.toBeInTheDocument(); }); }); + +describe('DBEditTimeChartForm - Raw SQL data source variables', () => { + const logsVariable: DataSourceDashboardFilter = { + id: 'logs', + type: 'DATA_SOURCE', + name: 'logs', + variableName: 'logs', + allowedSourceKinds: [SourceKind.Log], + minSelections: 1, + maxSelections: 1, + isBroadcastEnabled: false, + isVariableEnabled: true, + }; + + const rawSqlConfig: SavedChartConfig = { + configType: 'sql', + displayType: DisplayType.Table, + name: 'Raw SQL tile', + source: '$logs', + connection: 'conn-2', + sqlTemplate: 'SELECT count() FROM $__sourceTable', + }; + + const renderWithSelection = (sourceId: string) => + renderWithMantine( + + + + + , + ); + + const logSource = (connection: string) => ({ + id: 'logs-source', + kind: SourceKind.Log, + name: 'Logs', + from: { databaseName: 'default', tableName: 'otel_logs' }, + connection, + timestampValueExpression: 'Timestamp', + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("blocks the preview when the variable's source is on another connection", async () => { + mockUseSourceData(logSource('conn-1')); + renderWithSelection('logs-source'); + + expect(await screen.findByTestId('preview-blocked')).toHaveTextContent( + "The selected data source is on a different connection than this tile's query.", + ); + }); + + it('blocks only on the unselected variable once filters are not applied', async () => { + const globalFilter: DashboardFilter = { + id: 'service', + type: 'QUERY_EXPRESSION', + name: 'Service', + expression: 'ServiceName', + source: 'logs-source', + minSelections: 1, + isGlobalRequirement: true, + }; + mockUseSourceData(undefined); + renderWithMantine( + + + []} + unsatisfiedRequiredFilters={[logsVariable, globalFilter]} + /> + + , + ); + + expect( + await screen.findByTestId('preview-missing-required-filters'), + ).toHaveTextContent( + /^Missing required filters or variables: logs, Service\. Select a value for each required filter, or turn off “Apply filters” to preview this tile without them\.$/, + ); + + await userEvent.click( + screen.getByRole('switch', { name: 'Apply filters' }), + ); + + expect( + screen.getByTestId('preview-missing-required-filters'), + ).toHaveTextContent( + /^Missing required filters or variables: logs\. Select a value for each one to preview this tile\.$/, + ); + }); + + it("previews when the variable's source is on the tile's connection", async () => { + mockUseSourceData(logSource('conn-2')); + renderWithSelection('logs-source'); + + await screen.findByTestId('chart-run-query-button'); + expect(screen.queryByTestId('preview-blocked')).not.toBeInTheDocument(); + }); +}); diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts index d1864336f..6635957e5 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts @@ -19,6 +19,7 @@ import { buildSampleEventsConfig, computeDbTimeChartConfig, displayTypeToActiveTab, + getPreviewBlockingFilters, isQueryReady, resolvePreviewVariables, resolveTilePreviewFilters, @@ -696,6 +697,7 @@ describe('resolveTilePreviewFilters', () => { filters: dashboardFilters, variables: [], missingRequiredFilterNames: [], + canPreviewWithoutFilters: false, }); }); @@ -708,6 +710,7 @@ describe('resolveTilePreviewFilters', () => { filters: undefined, variables: [{ name: 'service', values: [] }], missingRequiredFilterNames: [], + canPreviewWithoutFilters: false, }); }); @@ -826,6 +829,79 @@ describe('resolveTilePreviewFilters', () => { }); }); +describe('getPreviewBlockingFilters', () => { + const sourceVariable: DashboardFilter = { + id: 'logs', + type: 'DATA_SOURCE', + name: 'Logs source', + allowedSourceKinds: [SourceKind.Log], + isBroadcastEnabled: false, + isVariableEnabled: true, + variableName: 'logs', + minSelections: 1, + maxSelections: 1, + }; + const requiredVariable: DashboardFilter = { + id: 'env', + type: 'STATIC_LIST', + name: 'Environment', + options: ['prod'], + isBroadcastEnabled: false, + isVariableEnabled: true, + variableName: 'env', + minSelections: 1, + }; + const rawSqlWithVariable: ChartConfigWithDateRange = { + configType: 'sql', + sqlTemplate: "SELECT count() FROM logs WHERE env = '$env'", + connection: 'clickhouse', + dateRange, + }; + + const getBlocking = ( + overrides: Partial[0]> = {}, + ) => + getPreviewBlockingFilters({ + config: rawSqlWithVariable, + sourceId: undefined, + variables: [{ name: 'env', values: [] }], + unsatisfiedRequiredFilters: [sourceVariable, requiredVariable], + applySelections: true, + referenceVariableNames: ['logs'], + ...overrides, + }); + + it('offers turning off the filters when that clears part of the block', () => { + expect(getBlocking()).toEqual({ + missingRequiredFilterNames: ['Logs source', 'Environment'], + canPreviewWithoutFilters: true, + }); + }); + + it('does not offer turning off the filters when only a reference variable blocks', () => { + expect( + getBlocking({ unsatisfiedRequiredFilters: [sourceVariable] }), + ).toEqual({ + missingRequiredFilterNames: ['Logs source'], + canPreviewWithoutFilters: false, + }); + }); + + it('does not offer turning off the filters when they are already off', () => { + expect(getBlocking({ applySelections: false })).toEqual({ + missingRequiredFilterNames: ['Logs source'], + canPreviewWithoutFilters: false, + }); + }); + + it('blocks only on reference variables when no query could be built', () => { + expect(getBlocking({ config: undefined })).toEqual({ + missingRequiredFilterNames: ['Logs source'], + canPreviewWithoutFilters: false, + }); + }); +}); + describe('withBroadcastFilters', () => { const sqlFilter: Filter = { type: 'sql', condition: "ServiceName = 'api'" }; const promqlFilter: PromqlConditionFilter = { diff --git a/packages/app/src/components/DBEditTimeChartForm/useDebouncedFormValues.ts b/packages/app/src/components/DBEditTimeChartForm/useDebouncedFormValues.ts new file mode 100644 index 000000000..80c504a1c --- /dev/null +++ b/packages/app/src/components/DBEditTimeChartForm/useDebouncedFormValues.ts @@ -0,0 +1,25 @@ +import { useEffect, useState } from 'react'; +import { FieldValues, UseFormGetValues, UseFormWatch } from 'react-hook-form'; +import { useDebouncedCallback } from '@mantine/hooks'; + +/** + * The form's values, updated `delay` ms after the last change. Unlike + * `useWatch({ control })`, which types them as a deep partial, they're typed + * as complete. + * + * Kept out of the form component because the React Compiler skips any + * component that calls `watch`. + */ +export function useDebouncedFormValues( + watch: UseFormWatch, + getValues: UseFormGetValues, + delay: number, +): T { + const [values, setValues] = useState(() => getValues()); + const update = useDebouncedCallback(() => setValues(getValues()), delay); + useEffect(() => { + const subscription = watch(() => update()); + return () => subscription.unsubscribe(); + }, [watch, update]); + return values; +} diff --git a/packages/app/src/components/DBEditTimeChartForm/utils.ts b/packages/app/src/components/DBEditTimeChartForm/utils.ts index c54aeee2a..4ee56423e 100644 --- a/packages/app/src/components/DBEditTimeChartForm/utils.ts +++ b/packages/app/src/components/DBEditTimeChartForm/utils.ts @@ -10,7 +10,7 @@ import { import { isTimeSeriesDisplayType } from '@hyperdx/common-utils/dist/core/utils'; import { configConsumesBroadcastFilters, - getBlockingRequiredFilterNames, + getBlockingRequiredFilters, } from '@hyperdx/common-utils/dist/dashboardFilterValues'; import { getPromqlFilters, @@ -29,6 +29,7 @@ import { BuilderChartConfigWithDateRange, ChartAlertBaseSchema, ChartConfigWithDateRange, + ChartConfigWithOptDateRange, ChartConfigWithOptTimestamp, ChartVariable, DashboardFilter, @@ -205,14 +206,89 @@ export function resolvePreviewVariables({ : referenced.map(variable => ({ ...variable, values: [] })); } +/** + * The required filters that block a preview with `applySelections` off: only + * the reference variables the tile can't be queried without. + */ +function getUnappliedPreviewBlockingFilterNames( + unsatisfiedRequiredFilters: DashboardFilter[] | undefined, + referenceVariableNames: readonly string[] | undefined, +): string[] { + const referenced = new Set(referenceVariableNames); + return getReferenceVariableFilters(unsatisfiedRequiredFilters) + .filter(({ name }) => referenced.has(name)) + .map(({ filter }) => filter.name); +} + +export type PreviewBlockingFilters = { + /** Names of the required filters that have nothing selected and block the preview. */ + missingRequiredFilterNames: string[]; + /** Whether turning off "Apply filters" would clear the block. */ + canPreviewWithoutFilters: boolean; +}; + +/** + * The required filters with nothing selected that block a tile preview. + * + * With `applySelections` off, only the reference variables the tile can't be + * queried without still block. `config` is undefined when no query could be + * built, e.g. because a source variable has no selection; the tile then + * consumes no broadcast filters and references no template variables. + */ +export function getPreviewBlockingFilters({ + config, + sourceId, + variables, + unsatisfiedRequiredFilters, + applySelections, + referenceVariableNames, +}: { + config: ChartConfigWithOptDateRange | SavedChartConfig | undefined; + sourceId: string | undefined; + variables: ChartVariable[] | undefined; + unsatisfiedRequiredFilters: DashboardFilter[] | undefined; + applySelections: boolean; + /** The data source and connection variables the tile's stored config references. */ + referenceVariableNames: readonly string[] | undefined; +}): PreviewBlockingFilters { + const unappliedNames = getUnappliedPreviewBlockingFilterNames( + unsatisfiedRequiredFilters, + referenceVariableNames, + ); + if (!applySelections) { + return { + missingRequiredFilterNames: unappliedNames, + canPreviewWithoutFilters: false, + }; + } + + const missingRequiredFilterNames = getBlockingRequiredFilters( + unsatisfiedRequiredFilters ?? [], + { + sourceId, + referenceVariableNames, + referencedVariableNames: + config && variables + ? filterReferencedVariables(config, variables).map(v => v.name) + : undefined, + consumesBroadcastFilters: + config != null && configConsumesBroadcastFilters(config, sourceId), + }, + ).map(filter => filter.name); + return { + missingRequiredFilterNames, + canPreviewWithoutFilters: missingRequiredFilterNames.some( + name => !unappliedNames.includes(name), + ), + }; +} + /** What the dashboard's filter state contributes to a tile preview. */ -export type TilePreviewFilters = { +export type TilePreviewFilters = PreviewBlockingFilters & { /** Broadcast filter conditions to query the preview with, in its query language. */ filters: BroadcastFilter[] | undefined; /** The referenced variables, with or without their selections. */ variables: ChartVariable[] | undefined; - /** Names of the required filters that have nothing selected. */ - missingRequiredFilterNames: string[]; }; /** @@ -240,6 +316,14 @@ export function resolveTilePreviewFilters({ unsatisfiedRequiredFilters: DashboardFilter[] | undefined; applySelections: boolean; }): TilePreviewFilters { + const blocking = getPreviewBlockingFilters({ + config, + sourceId, + variables, + unsatisfiedRequiredFilters, + applySelections, + referenceVariableNames, + }); const previewVariables = resolvePreviewVariables({ config, variables, @@ -247,18 +331,7 @@ export function resolveTilePreviewFilters({ }); if (!applySelections) { - // Without a selection, a variable the tile's source or connection - // references leaves nothing to query, so it still blocks. - const referenced = new Set(referenceVariableNames); - return { - filters: undefined, - variables: previewVariables, - missingRequiredFilterNames: getReferenceVariableFilters( - unsatisfiedRequiredFilters, - ) - .filter(({ name }) => referenced.has(name)) - .map(({ filter }) => filter.name), - }; + return { ...blocking, filters: undefined, variables: previewVariables }; } // PromQL tiles take only PromQL conditions; `configConsumesBroadcastFilters` @@ -269,17 +342,7 @@ export function resolveTilePreviewFilters({ ? getSqlFilters(filters) : undefined; - return { - filters: previewFilters, - variables: previewVariables, - missingRequiredFilterNames: getBlockingRequiredFilterNames({ - config, - sourceId, - unsatisfiedRequiredFilters, - referencedVariables: previewVariables, - referenceVariableNames, - }), - }; + return { ...blocking, filters: previewFilters, variables: previewVariables }; } /** The config with the broadcast filters in its own query language attached. */ diff --git a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx index 29b23c09d..8eb2c5293 100644 --- a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx @@ -74,7 +74,10 @@ export const AdhocFilterEditForm = ({ ), ); }, [otherFilters, sourceType, sourceIds, appliesToSourceIds]); - const allowedSourceKinds = getAdhocFilterSourceKinds(sourceType); + const allowedSourceKinds = useMemo( + () => getAdhocFilterSourceKinds(sourceType), + [sourceType], + ); const referenceVariableValues = useReferenceVariableValues(); const tableConnections = useMemo(() => { const resolvedIds = new Set( @@ -83,13 +86,14 @@ export const AdhocFilterEditForm = ({ ), ); // A data source variable can select a source of a kind this filter can't use - const kinds = getAdhocFilterSourceKinds(sourceType); return (allSources ?? []) .filter( - source => resolvedIds.has(source.id) && kinds.includes(source.kind), + source => + resolvedIds.has(source.id) && + allowedSourceKinds.includes(source.kind), ) .map(tcFromSource); - }, [allSources, sourceIds, referenceVariableValues, sourceType]); + }, [allSources, sourceIds, referenceVariableValues, allowedSourceKinds]); // Kept visible for a saved PromQL filter so its source type can be changed const showSourceType = sourceType === 'promql' || diff --git a/packages/app/src/components/sourceSelectUtils.tsx b/packages/app/src/components/sourceSelectUtils.tsx index ede2f0e45..4a06f779b 100644 --- a/packages/app/src/components/sourceSelectUtils.tsx +++ b/packages/app/src/components/sourceSelectUtils.tsx @@ -129,11 +129,13 @@ export function useWithSourceVariableItems( if (items.length === 0) return sourceItems; // Mantine renders ungrouped items after a group as part of it, so a flat // source list gets a header of its own - const isFlat = sourceItems.some(item => !('group' in item)); + const ungrouped = sourceItems.filter( + (item): item is ComboboxItem => !('group' in item), + ); return [ { group: 'Variables', items }, - ...(isFlat - ? [{ group: 'Sources', items: sourceItems as ComboboxItem[] }] + ...(ungrouped.length > 0 + ? [{ group: 'Sources', items: ungrouped }] : sourceItems), ]; }, [enabled, sourceVariables, allowedSourceKinds, sourceItems]); From a808f3c10c690d88ebf183e90e3a216c51ebac1f Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Tue, 17 Nov 2026 19:48:55 -0500 Subject: [PATCH 4/4] fix: Improve alert removal and import validation --- packages/app/src/DBDashboardImportPage.tsx | 48 ++++++++ .../__tests__/DBDashboardImportPage.test.tsx | 34 +++++- .../app/src/components/ChartEditor/utils.ts | 17 +++ .../DBEditTimeChartForm/EditTimeChartForm.tsx | 72 ++++++++---- .../__tests__/DBEditTimeChartForm.test.tsx | 108 ++++++++++++++++++ 5 files changed, 258 insertions(+), 21 deletions(-) diff --git a/packages/app/src/DBDashboardImportPage.tsx b/packages/app/src/DBDashboardImportPage.tsx index 7a8859123..9f7eda309 100644 --- a/packages/app/src/DBDashboardImportPage.tsx +++ b/packages/app/src/DBDashboardImportPage.tsx @@ -16,6 +16,10 @@ import { Controller, useForm, useWatch } from 'react-hook-form'; import { z } from 'zod'; import { zodResolver } from '@hookform/resolvers/zod'; import { convertToDashboardDocument } from '@hyperdx/common-utils/dist/core/utils'; +import { + collectRefinementIssues, + validateDashboardVariableReferences, +} from '@hyperdx/common-utils/dist/dashboardValidation'; import { type DashboardFilterQueryIssue, getAdhocFilterSourceKinds, @@ -131,6 +135,32 @@ function CollapsibleMessage({ ); } +/** + * A `$name` that names no variable of the right type in the template. The API + * rejects these too, but only once the mappings are filled in, and local mode + * doesn't check at all. + */ +export function getTemplateVariableReferenceIssues( + template: DashboardTemplate, +): string[] { + return collectRefinementIssues(ctx => + validateDashboardVariableReferences( + template.tiles, + template.filters ?? [], + ctx, + ), + ).map(({ path, message }) => { + const [collection, index] = path; + const owner = + collection === 'tiles' && typeof index === 'number' + ? `Tile "${template.tiles[index]?.config.name ?? index}"` + : collection === 'filters' && typeof index === 'number' + ? `Filter "${template.filters?.[index]?.name ?? index}"` + : path.join('.'); + return `${owner}: ${message}`; + }); +} + function FileSelection({ onComplete, }: { @@ -204,6 +234,24 @@ function FileSelection({ return; } + const referenceIssues = getTemplateVariableReferenceIssues(result.data); + if (referenceIssues.length > 0) { + onComplete(null); + setError({ + message: 'Failed to Import Dashboard', + details: ( + + {referenceIssues.map(issue => ( + + {issue} + + ))} + + ), + }); + return; + } + // The schema only guarantees saved filter values are well-shaped, not that // their condition strings parse. Surface a non-blocking warning so users // who hand-edited or generated the file know a value won't actually filter, diff --git a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx index 9a5873603..1083ba8ca 100644 --- a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx +++ b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx @@ -6,7 +6,11 @@ import { } from '@hyperdx/common-utils/dist/types'; import { act, fireEvent, screen, waitFor } from '@testing-library/react'; -import { buildMappingFormSchema, Mapping } from '@/DBDashboardImportPage'; +import { + buildMappingFormSchema, + getTemplateVariableReferenceIssues, + Mapping, +} from '@/DBDashboardImportPage'; // Sources/connections the import maps the template's source *names* onto. The // auto-mapping effect in `Mapping` matches by name (case-insensitive), so a @@ -778,4 +782,32 @@ describe('Dashboard import - data source variables', () => { expect(payload.filters[1].source).toBe('$logs'); expect(payload.filters[2].sources).toEqual(['$logs', 'src-traces']); }); + + it('accepts references to the variables the template declares', () => { + expect(getTemplateVariableReferenceIssues(sourceVariableTemplate)).toEqual( + [], + ); + }); + + it('reports references to variables the template does not declare', () => { + const template = DashboardTemplateSchema.parse({ + version: '0.1.0', + name: 'Undeclared', + tiles: [builderTile('tile-line', 'line', '$traces', 0)], + filters: [ + { + id: 'svc', + type: 'QUERY_EXPRESSION', + name: 'Service', + expression: 'ServiceName', + source: '$traces', + }, + ], + }); + + expect(getTemplateVariableReferenceIssues(template)).toEqual([ + 'Tile "tile-line": "$traces" does not name a data source variable on this dashboard', + 'Filter "Service": "$traces" does not name a data source variable on this dashboard', + ]); + }); }); diff --git a/packages/app/src/components/ChartEditor/utils.ts b/packages/app/src/components/ChartEditor/utils.ts index ca2e8224a..3d6babc24 100644 --- a/packages/app/src/components/ChartEditor/utils.ts +++ b/packages/app/src/components/ChartEditor/utils.ts @@ -23,6 +23,7 @@ import { } from '@hyperdx/common-utils/dist/guards'; import { MACRO_SUGGESTIONS } from '@hyperdx/common-utils/dist/macros'; import { QUERY_PARAMS_BY_DISPLAY_TYPE } from '@hyperdx/common-utils/dist/rawSqlParams'; +import { getConfigReferenceVariables } from '@hyperdx/common-utils/dist/referenceVariables'; import { BuilderSavedChartConfig, ChartConfigWithDateRange, @@ -205,6 +206,22 @@ export const isRawSqlDisplayType = ( displayType === DisplayType.Bar || displayType === DisplayType.Number; +/** The variables a chart form's source, or a raw SQL tile's connection, references. */ +export function getFormReferenceVariables( + form: Pick< + ChartEditorFormState, + 'source' | 'connection' | 'configType' | 'displayType' + >, +): string[] { + // Only raw SQL tiles store a connection of their own + const isRawSql = + form.configType === 'sql' && isRawSqlDisplayType(form.displayType); + return getConfigReferenceVariables({ + source: form.source, + connection: isRawSql ? form.connection : undefined, + }); +} + const isCustomOrderByDisplayType = ( displayType: DisplayType | undefined, ): displayType is DisplayType.Table | DisplayType.Bar | DisplayType.Pie => diff --git a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx index d021ca393..0bf68cfe8 100644 --- a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx @@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { isEqual } from 'lodash'; import { Controller, + type Resolver, useFieldArray, useForm, type UseFormGetValues, @@ -22,7 +23,6 @@ import { isRawSqlSavedChartConfig, } from '@hyperdx/common-utils/dist/guards'; import { - getConfigReferenceVariables, isSourceVariableReferenceAccepted, parseVariableReference, resolveConfigReferences, @@ -41,6 +41,7 @@ import { } from '@hyperdx/common-utils/dist/types'; import { getAlertVariableWarning } from '@hyperdx/common-utils/dist/variables'; import { + Alert, Box, Flex, SegmentedControl, @@ -51,6 +52,7 @@ import { import { useDisclosure, usePrevious } from '@mantine/hooks'; import { notifications } from '@mantine/notifications'; import { + IconAlertTriangle, IconBracketsContain, IconChartBar, IconChartLine, @@ -78,6 +80,7 @@ import { convertFormStateToSavedChartConfig, convertSavedChartConfigToFormState, getAllowedSourceKinds, + getFormReferenceVariables, getInterchangeableSourceKind, isPromqlDisplayType, isRawSqlDisplayType, @@ -174,6 +177,24 @@ type EditTimeChartFormProps = { showSaveToDashboard?: boolean; }; +const zodChartFormResolver = zodResolver(zSavedChartConfig); + +// A tile on a variable can't keep an alert. The form holds on to it, so that +// switching back to a fixed source restores it, but validates and submits +// without it. +const chartFormResolver: Resolver = ( + values, + context, + options, +) => + zodChartFormResolver( + getFormReferenceVariables(values).length > 0 + ? { ...values, alert: undefined } + : values, + context, + options, + ); + const ALERT_IGNORES_DASHBOARD_FILTERS = 'Dashboard-level filter and variable selections cannot be applied when an alert is configured.'; @@ -277,7 +298,7 @@ export default function EditTimeChartForm({ } = useForm({ defaultValues: formValue, values: formValue, - resolver: zodResolver(zSavedChartConfig), + resolver: chartFormResolver, }); const { @@ -365,14 +386,15 @@ export default function EditTimeChartForm({ const sourceVariableDeclarations = useSourceVariableDeclarations(); const referenceVariableValues = useReferenceVariableValues(); - // Only raw SQL tiles store a connection of their own const referenceVariableNames = useMemo( () => - getConfigReferenceVariables({ + getFormReferenceVariables({ source: sourceId, - connection: isRawSqlInput ? connection : undefined, + connection, + configType, + displayType, }), - [sourceId, connection, isRawSqlInput], + [sourceId, connection, configType, displayType], ); const usesReferenceVariable = referenceVariableNames.length > 0; @@ -380,6 +402,8 @@ export default function EditTimeChartForm({ // current selection. const alertsEnabled = (enableAlerts ?? dashboardId != null) && !usesReferenceVariable; + // The alert as it will save + const activeAlert = usesReferenceVariable ? undefined : alert; // A source selection that current mode can't query counts as no source, // so that nothing downstream builds a query from it. @@ -450,10 +474,6 @@ export default function EditTimeChartForm({ } }, [configType, displayType, previousDisplayType, setValue]); - useEffect(() => { - if (usesReferenceVariable) setValue('alert', undefined); - }, [usesReferenceVariable, setValue]); - const showGeneratedSql = TABS_WITH_GENERATED_SQL.has(activeTab) && !isPromqlInput; @@ -575,7 +595,7 @@ export default function EditTimeChartForm({ // Alerts ignore dashboard-level filters and variables, // so disable the toggle when an alert is configured. const [applyFilters, setApplyFilters] = useState(true); - const resolvedApplyFilters = applyFilters && alert == null; + const resolvedApplyFilters = applyFilters && activeAlert == null; const dashboardFiltersToggleProps = useMemo< DashboardFiltersToggleProps | undefined @@ -586,10 +606,10 @@ export default function EditTimeChartForm({ : { checked: resolvedApplyFilters, disabledReason: - alert != null ? ALERT_IGNORES_DASHBOARD_FILTERS : undefined, + activeAlert != null ? ALERT_IGNORES_DASHBOARD_FILTERS : undefined, onChange: setApplyFilters, }, - [getDashboardFilters, resolvedApplyFilters, alert], + [getDashboardFilters, resolvedApplyFilters, activeAlert], ); const previewDashboardFilters = useMemo(() => { @@ -661,12 +681,12 @@ export default function EditTimeChartForm({ ]); const dbTimeChartConfig = useMemo( - () => computeDbTimeChartConfig(previewConfig, alert), - [previewConfig, alert], + () => computeDbTimeChartConfig(previewConfig, activeAlert), + [previewConfig, activeAlert], ); const additionalAlertWarnings = useMemo(() => { - if (alert == null) return []; + if (activeAlert == null) return []; const config = convertFormStateToSavedChartConfig( debouncedForm, tableSource, @@ -675,7 +695,7 @@ export default function EditTimeChartForm({ ? getAlertVariableWarning(config, variables) : undefined; return variableWarning ? [variableWarning] : []; - }, [alert, debouncedForm, tableSource, variables]); + }, [activeAlert, debouncedForm, tableSource, variables]); const [saveToDashboardModalOpen, setSaveToDashboardModalOpen] = useState(false); @@ -1450,7 +1470,7 @@ export default function EditTimeChartForm({ onOpenDisplaySettings={openDisplaySettings} onSubmit={onSubmit} isDashboardForm={isDashboardForm} - alert={alert} + alert={activeAlert} additionalWarnings={additionalAlertWarnings} alertsEnabled={alertsEnabled} isAlertRequired={isAlertRequired} @@ -1479,7 +1499,7 @@ export default function EditTimeChartForm({ activeTab={activeTab} seriesReturnType={seriesReturnType} ratioMode={ratioMode} - alert={alert} + alert={activeAlert} additionalWarnings={additionalAlertWarnings} alertsEnabled={alertsEnabled} isAlertRequired={isAlertRequired} @@ -1493,6 +1513,18 @@ export default function EditTimeChartForm({ onHeatmapModeChange={onHeatmapModeChange} /> )} + {alert != null && usesReferenceVariable && ( + } + p="xs" + mt="sm" + > + Alerts aren't available on tiles that use a data source or + connection variable. This tile's alert will be removed when + you save. + + )} ({ + ); }, @@ -1701,3 +1702,110 @@ describe('DBEditTimeChartForm - Raw SQL data source variables', () => { expect(screen.queryByTestId('preview-blocked')).not.toBeInTheDocument(); }); }); + +describe('DBEditTimeChartForm - Alerts on data source variables', () => { + const metricsVariable: DataSourceDashboardFilter = { + id: 'metrics', + type: 'DATA_SOURCE', + name: 'metrics', + variableName: 'metrics', + allowedSourceKinds: [SourceKind.Metric], + minSelections: 1, + maxSelections: 1, + isBroadcastEnabled: false, + isVariableEnabled: true, + }; + + const series = { + aggFn: 'avg' as const, + aggCondition: '', + aggConditionLanguage: 'lucene' as const, + valueExpression: 'Value', + metricType: MetricsDataType.Gauge, + metricName: 'test.metric.gauge', + }; + + const completeAlert = { + ...DEFAULT_TILE_ALERT, + channels: [{ type: 'webhook' as const, webhookId: 'webhook-1' }], + }; + + const renderWithAlert = ( + alert: NonNullable, + onSave: jest.Mock, + ) => + renderWithMantine( + + + + + , + ); + + const removalNotice = /This tile's alert will be removed when you save\./; + + beforeEach(() => { + jest.clearAllMocks(); + mockUseSource.mockImplementation(({ id }: { id?: string }) => ({ + data: + id === 'metric-source' + ? { + id: 'metric-source', + kind: SourceKind.Metric, + name: 'Test Metric Source', + from: { databaseName: 'default', tableName: '' }, + connection: 'default', + timestampValueExpression: 'Timestamp', + metricTables: { gauge: 'metrics.gauge' }, + } + : undefined, + })); + }); + + it('keeps the alert when the source goes to a variable and back', async () => { + const onSave = jest.fn(); + renderWithAlert(completeAlert, onSave); + const sourceSelector = screen.getByTestId('source-selector'); + + await userEvent.selectOptions(sourceSelector, '$metrics'); + expect(screen.getByText(removalNotice)).toBeInTheDocument(); + expect(screen.queryByTestId('alert-button')).not.toBeInTheDocument(); + + await userEvent.selectOptions(sourceSelector, 'metric-source'); + expect(screen.queryByText(removalNotice)).not.toBeInTheDocument(); + await userEvent.click(screen.getByTestId('chart-save-button')); + + await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1)); + expect(onSave.mock.calls[0][0].alert).toMatchObject(completeAlert); + }); + + it('saves without the alert while the source is a variable, even an invalid one', async () => { + const onSave = jest.fn(); + // No webhook picked, so the alert alone would block the save + renderWithAlert(DEFAULT_TILE_ALERT, onSave); + + await userEvent.selectOptions( + screen.getByTestId('source-selector'), + '$metrics', + ); + await userEvent.click(screen.getByTestId('chart-save-button')); + + await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1)); + const saved = onSave.mock.calls[0][0]; + expect(saved.source).toBe('$metrics'); + expect(saved.alert).toBeUndefined(); + }); +});