diff --git a/.changeset/import-adhoc-filter-sources.md b/.changeset/import-adhoc-filter-sources.md new file mode 100644 index 0000000000..d7828bcc0c --- /dev/null +++ b/.changeset/import-adhoc-filter-sources.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +feat: Support source remapping during ad hoc filter import diff --git a/packages/app/src/DBDashboardImportPage.tsx b/packages/app/src/DBDashboardImportPage.tsx index 9e63561373..b8cbe0da5c 100644 --- a/packages/app/src/DBDashboardImportPage.tsx +++ b/packages/app/src/DBDashboardImportPage.tsx @@ -10,6 +10,7 @@ import dynamic from 'next/dynamic'; import Head from 'next/head'; import Link from 'next/link'; import { useRouter } from 'next/router'; +import { uniq } from 'lodash'; import { parseAsString, useQueryState } from 'nuqs'; import { Controller, useForm, useWatch } from 'react-hook-form'; import { z } from 'zod'; @@ -17,8 +18,8 @@ import { zodResolver } from '@hookform/resolvers/zod'; import { convertToDashboardDocument } from '@hyperdx/common-utils/dist/core/utils'; import { type DashboardFilterQueryIssue, + getAdhocFilterSourceKinds, isAdhocFilter, - isAdhocFilterValue, isPrometheusLabelFilter, isQueryExpressionFilter, QUERY_EXPRESSION_FILTER_SOURCE_KINDS, @@ -367,32 +368,26 @@ function FileSelection({ /** * Resolve a filter's templated `appliesToSourceIds` (an ordered list of * source names from the exported file) against the current workspace's - * sources. Names that don't match any current source are dropped. + * sources. * - * `resolvedIndexOf(name)` returns the position of `name` in the surviving - * `ids` array (case-insensitive), or -1 if the name wasn't present in the - * template or didn't resolve. + * `slots` is aligned one-to-one with `templateNames`, with '' for names that + * don't match any current source. `ids` is the matched ids alone. */ function resolveAppliesToSources( templateNames: string[] | undefined, sources: TSource[] | undefined, -): { ids: string[]; resolvedIndexOf: (name: string) => number } { - const ids: string[] = []; - const resolvedIndexByLowerName = new Map(); - templateNames?.forEach(name => { - const match = sources?.find( - source => source.name.toLowerCase() === name.toLowerCase(), - ); - if (match) { - resolvedIndexByLowerName.set(name.toLowerCase(), ids.length); - ids.push(match.id); - } - }); - return { - ids, - resolvedIndexOf: name => - resolvedIndexByLowerName.get(name.toLowerCase()) ?? -1, - }; + allowedKinds?: SourceKind[], +): { ids: string[]; slots: string[] } { + const slots = + templateNames?.map( + name => + sources?.find( + source => + source.name.toLowerCase() === name.toLowerCase() && + (!allowedKinds || allowedKinds.includes(source.kind)), + )?.id ?? '', + ) ?? []; + return { ids: slots.filter(Boolean), slots }; } /** Whether the filter names a single source that import must map. */ @@ -406,11 +401,21 @@ const sourceBackedTemplateFilter = (filter: DashboardFilter | undefined) => filter && isSourceBackedTemplateFilter(filter) ? filter : undefined; /** - * The query-expression half of a template filter, or `undefined` for any other - * type. Only a queried filter broadcasts, so only it carries an applies-to list. + * The template filter if it can carry an applies-to list (query-expression and + * ad hoc filters), or `undefined` for any other type. */ -const queriedTemplateFilter = (filter: DashboardFilter | undefined) => - filter && isQueryExpressionFilter(filter) ? filter : undefined; +const appliesToTemplateFilter = (filter: DashboardFilter | undefined) => + filter && (isQueryExpressionFilter(filter) || isAdhocFilter(filter)) + ? filter + : undefined; + +/** The source kinds an applies-to list may name, or `undefined` for any kind. */ +const appliesToSourceKinds = ( + filter: DashboardFilter, +): SourceKind[] | undefined => + isAdhocFilter(filter) + ? getAdhocFilterSourceKinds(filter.sourceType) + : undefined; /** * The source kinds a filter of this type can read its dropdown values from, or @@ -461,6 +466,14 @@ export function buildMappingFormSchema( filterAppliesToSourceMappings: z .array(z.array(MappingValueSchema)) .optional(), + /** + * Per-filter ad hoc source mappings. Each entry is an array of mapped + * source IDs in the same order as the ad hoc filter's `sources` names + * from the template. Empty for every other filter type. + */ + filterAdhocSourceMappings: z + .array(z.array(MappingValueSchema)) + .optional(), /** A list of onClick source mappings, ordered by input tile index */ onClickSourceMappings: z.array(MappingValueSchema).optional(), /** A list of onClick dashboard mappings, ordered by input tile index */ @@ -494,7 +507,45 @@ export function buildMappingFormSchema( } }); + const hasWrongKind = (ids: string[] | undefined, kinds: SourceKind[]) => + ids?.some(id => { + const source = sources?.find(s => s.id === id); + return source != null && !kinds.includes(source.kind); + }); + input.filters?.forEach((filter, idx) => { + if (isAdhocFilter(filter)) { + const kinds = getAdhocFilterSourceKinds(filter.sourceType); + const kindMessage = + filter.sourceType === 'promql' + ? 'Select only PromQL sources for this filter' + : 'Select only log, trace, or session sources for this filter'; + const mappedSourceIds = data.filterAdhocSourceMappings?.[idx]?.filter( + id => !!id, + ); + if (!mappedSourceIds?.length) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ['filterAdhocSourceMappings', idx], + message: 'Select at least one source for this filter', + }); + } else if (hasWrongKind(mappedSourceIds, kinds)) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ['filterAdhocSourceMappings', idx], + message: kindMessage, + }); + } + if (hasWrongKind(data.filterAppliesToSourceMappings?.[idx], kinds)) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ['filterAppliesToSourceMappings', idx], + message: kindMessage, + }); + } + return; + } + if (!isSourceBackedTemplateFilter(filter)) return; const mappedSourceId = data.filterSourceMappings?.[idx]; if (!mappedSourceId) { @@ -540,7 +591,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { [input, sources], ); - const { handleSubmit, getFieldState, control, setValue, getValues } = + const { handleSubmit, getFieldState, control, setValue } = useForm({ resolver: zodResolver(formSchema), mode: 'onChange', @@ -551,6 +602,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { connectionMappings: input.tiles.map(() => ''), filterSourceMappings: input.filters?.map(() => '') ?? [], filterAppliesToSourceMappings: input.filters?.map(() => []) ?? [], + filterAdhocSourceMappings: input.filters?.map(() => []) ?? [], onClickSourceMappings: input.tiles.map(() => ''), onClickDashboardMappings: input.tiles.map(() => ''), }, @@ -559,6 +611,11 @@ export function Mapping({ input }: { input: DashboardTemplate }) { // The template whose names have already been auto-mapped const autoMappedInputRef = useRef(null); + // Per multiselect field, the mapped ids aligned one-to-one with the + // template's source names ('' where unmapped). The form value drops empties + // and duplicates, so it can't be spliced by template position itself. + const multiMappingSlotsRef = useRef(new Map()); + // When the input changes, reset the form useEffect(() => { if (!input || !sources || !connections || !dashboards) return; @@ -599,8 +656,18 @@ export function Mapping({ input }: { input: DashboardTemplate }) { const filterAppliesToSourceMappings = input.filters?.map( filter => resolveAppliesToSources( - queriedTemplateFilter(filter)?.appliesToSourceIds, + appliesToTemplateFilter(filter)?.appliesToSourceIds, sources, + appliesToSourceKinds(filter), + ).ids, + ); + + const filterAdhocSourceMappings = input.filters?.map( + filter => + resolveAppliesToSources( + isAdhocFilter(filter) ? filter.sources : undefined, + sources, + appliesToSourceKinds(filter), ).ids, ); @@ -631,9 +698,13 @@ export function Mapping({ input }: { input: DashboardTemplate }) { setValue('tileSourceMappings', tileSourceMappings); setValue('connectionMappings', connectionMappings); setValue('filterSourceMappings', filterSourceMappings); + multiMappingSlotsRef.current.clear(); filterAppliesToSourceMappings?.forEach((mapping, idx) => { setValue(`filterAppliesToSourceMappings.${idx}`, mapping); }); + filterAdhocSourceMappings?.forEach((mapping, idx) => { + setValue(`filterAdhocSourceMappings.${idx}`, mapping); + }); setValue('onClickSourceMappings', onClickSourceMappings); setValue('onClickDashboardMappings', onClickDashboardMappings); }, [setValue, sources, connections, dashboards, input]); @@ -757,30 +828,47 @@ export function Mapping({ input }: { input: DashboardTemplate }) { } } - // Propagate changes to applies-to multiselects. Anchor the splice on - // the resolver's stripped-list index so it lines up with the form-state - // array (which has unresolved template names dropped). - input.filters?.forEach((filter, filterIdx) => { - if (!isQueryExpressionFilter(filter)) return; - if (!filter.appliesToSourceIds?.includes(inputSourceName)) return; - const key = `filterAppliesToSourceMappings.${filterIdx}` as const; + // Propagate changes to multiselects (applies-to lists and ad hoc source + // lists). + const propagateToMultiMapping = ( + templateNames: string[] | undefined, + key: + | `filterAppliesToSourceMappings.${number}` + | `filterAdhocSourceMappings.${number}`, + allowedKinds: SourceKind[] | undefined, + ) => { + if (!templateNames?.includes(inputSourceName)) return; if (getFieldState(key).isDirty) return; - const currentIdMappings = getValues(key)?.slice() ?? []; - if (selectedSourceId && currentIdMappings.includes(selectedSourceId)) + const selectedSource = sources?.find(s => s.id === selectedSourceId); + if ( + allowedKinds && + selectedSource && + !allowedKinds.includes(selectedSource.kind) + ) return; - - const { resolvedIndexOf } = resolveAppliesToSources( - filter.appliesToSourceIds, - sources, + // Slot by template position, not resolved position, so a name that + // didn't auto-resolve can still be filled in by a remap. + const slots = ( + multiMappingSlotsRef.current.get(key) ?? + resolveAppliesToSources(templateNames, sources, allowedKinds).slots + ).map((id, i) => + templateNames[i] === inputSourceName ? selectedSourceId : id, ); - const indexToUpdate = resolvedIndexOf(inputSourceName); - if (indexToUpdate >= 0) { - const next = [ - ...currentIdMappings.slice(0, indexToUpdate), - selectedSourceId, - ...currentIdMappings.slice(indexToUpdate + 1), - ]; - setValue(key, next, { shouldValidate: true }); + multiMappingSlotsRef.current.set(key, slots); + setValue(key, uniq(slots.filter(Boolean)), { shouldValidate: true }); + }; + input.filters?.forEach((filter, filterIdx) => { + propagateToMultiMapping( + appliesToTemplateFilter(filter)?.appliesToSourceIds, + `filterAppliesToSourceMappings.${filterIdx}`, + appliesToSourceKinds(filter), + ); + if (isAdhocFilter(filter)) { + propagateToMultiMapping( + filter.sources, + `filterAdhocSourceMappings.${filterIdx}`, + appliesToSourceKinds(filter), + ); } }); isUpdatingRef.current = false; @@ -791,7 +879,6 @@ export function Mapping({ input }: { input: DashboardTemplate }) { input.tiles, input.filters, getFieldState, - getValues, setValue, sources, ]); @@ -954,31 +1041,34 @@ export function Mapping({ input }: { input: DashboardTemplate }) { }); // Zip the source mappings with the input filters - const zippedFilters = input.filters - ?.map((filter, idx) => { - const source = findSource(data.filterSourceMappings?.[idx]); - const appliesTo = data.filterAppliesToSourceMappings?.[idx]?.filter( - id => !!id?.length, - ); + const zippedFilters = input.filters?.map((filter, idx) => { + const appliesTo = data.filterAppliesToSourceMappings?.[idx]?.filter( + id => !!id?.length, + ); + const appliesToSourceIds = appliesTo?.length ? appliesTo : undefined; + if (isAdhocFilter(filter)) { return { ...filter, - // A sourceless filter (`STATIC_LIST`) gets no mapping row, so - // `source` is undefined here - ...(source ? { source: source.id } : {}), - appliesToSourceIds: appliesTo?.length ? appliesTo : undefined, + sources: + data.filterAdhocSourceMappings?.[idx]?.filter(id => !!id) ?? [], + appliesToSourceIds, }; - }) - // Ad hoc filters aren't importable yet. - .filter(filter => !isAdhocFilter(filter)); + } + const source = findSource(data.filterSourceMappings?.[idx]); + return { + ...filter, + // A sourceless filter (`STATIC_LIST`) gets no mapping row, so + // `source` is undefined here + ...(source ? { source: source.id } : {}), + appliesToSourceIds, + }; + }); // Format for server const output = convertToDashboardDocument({ ...input, tiles: zippedTiles, filters: zippedFilters, - savedFilterValues: input.savedFilterValues?.filter( - value => !isAdhocFilterValue(value), - ), name: data.dashboardName, tags: data.tags, }); @@ -1020,7 +1110,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { control={control} render={({ field, formState }) => ( @@ -1042,7 +1132,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { Tile / Filter - Mapping Type + Mapping type From To @@ -1058,7 +1148,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { {tile.config.source && ( {tile.config.name} - Data Source + Data source {config.source ?? ''} {tile.config.name} - Data Connection + Data connection {config.connection} {tile.config.name} - On Click - Search Source + On click - search source {tile.config.onClick.target.id} {tile.config.name} - On Click - Dashboard + On click - dashboard {tile.config.onClick.target.id} ( - - {isSourceBackedTemplateFilter(filter) && ( - - {filter.name} (Filter) - Data Source - {filter.source} - - - filterSourceKinds(filter)?.includes(source.kind), - ) - .map(source => ({ - value: source.id, - label: source.name, - }))} - placeholder="Select a source" - /> - - - - - )} - {isQueryExpressionFilter(filter) && - !!filter.appliesToSourceIds?.length && ( + {input.filters?.map((filter, i) => { + const appliesToSourceIds = + appliesToTemplateFilter(filter)?.appliesToSourceIds; + return ( + + {isSourceBackedTemplateFilter(filter) && ( {filter.name} (Filter) + Data source + {filter.source} - - Applies to Sources - + + filterSourceKinds(filter)?.includes(source.kind), + ) + .map(source => ({ + value: source.id, + label: source.name, + }))} + placeholder="Select a source" + /> + + + + )} + {isAdhocFilter(filter) && ( + + {filter.name} (Filter) + Data sources + {filter.sources.join(', ')} - {filter.appliesToSourceIds.join(', ')} + + + + + )} + {!!appliesToSourceIds?.length && ( + + {filter.name} (Filter) + + + Applies to sources + + + {appliesToSourceIds.join(', ')} @@ -1187,8 +1304,9 @@ export function Mapping({ input }: { input: DashboardTemplate }) { )} - - ))} + + ); + })} {createDashboard.isError && ( diff --git a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx index 111eae0850..7346642034 100644 --- a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx +++ b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx @@ -552,3 +552,116 @@ describe('Dashboard import - filter source kind', () => { expect(issuesFor(promqlFilterTemplate, 'src-logs')).toBeUndefined(); }); }); + +const adhocFilterTemplate = DashboardTemplateSchema.parse({ + version: '0.1.0', + name: 'Ad Hoc Filter', + tiles: [builderTile('tile-search', 'search', 'Logs', 0)], + filters: [ + { + id: 'filter-adhoc', + type: 'ADHOC', + name: 'Conditions', + sourceType: 'sql', + sources: ['Logs', 'Traces', MISSING], + appliesToSourceIds: ['Traces'], + isVariableEnabled: true, + variableName: 'conditions', + }, + ], + savedFilterValues: [ + { + type: 'adhoc', + name: 'conditions', + conditions: [{ key: 'ServiceName', operator: '=', value: 'api' }], + }, + ], +}); + +describe('Dashboard import - ad hoc filter sources', () => { + it('auto-maps sources and applies-to by name and imports them as ids', async () => { + renderWithMantine(); + + await act(async () => { + fireEvent.click( + await screen.findByRole('button', { name: /finish import/i }), + ); + }); + + await waitFor(() => expect(mockMutateAsync).toHaveBeenCalledTimes(1)); + const payload = mockMutateAsync.mock.calls[0][0]; + expect(payload.filters[0]).toMatchObject({ + type: 'ADHOC', + sources: ['src-logs', 'src-traces'], + appliesToSourceIds: ['src-traces'], + }); + expect(payload.savedFilterValues).toEqual( + adhocFilterTemplate.savedFilterValues, + ); + }); + + it('does not auto-map a source of a kind the filter cannot read', async () => { + const template = DashboardTemplateSchema.parse({ + ...adhocFilterTemplate, + filters: [ + { + ...adhocFilterTemplate.filters![0], + sources: ['Metrics'], + appliesToSourceIds: undefined, + }, + ], + }); + renderWithMantine(); + + await act(async () => { + fireEvent.click( + await screen.findByRole('button', { name: /finish import/i }), + ); + }); + + await waitFor(() => + expect( + screen.getByText('Select at least one source for this filter'), + ).toBeInTheDocument(), + ); + expect(mockMutateAsync).not.toHaveBeenCalled(); + }); + + const issuesFor = (sources: string[], appliesTo: string[] = []) => + buildMappingFormSchema(adhocFilterTemplate, mockSources) + .safeParse({ + dashboardName: adhocFilterTemplate.name, + tags: [], + tileSourceMappings: ['src-logs'], + connectionMappings: [''], + filterAdhocSourceMappings: [sources], + filterAppliesToSourceMappings: [appliesTo], + }) + .error?.issues.map(issue => [issue.path.join('.'), issue.message]); + + it('requires at least one source', () => { + expect(issuesFor([])).toEqual([ + [ + 'filterAdhocSourceMappings.0', + 'Select at least one source for this filter', + ], + ]); + }); + + it('rejects sources and applies-to entries of the wrong kind', () => { + expect(issuesFor(['src-promql'], ['src-metrics'])).toEqual([ + [ + 'filterAdhocSourceMappings.0', + 'Select only log, trace, or session sources for this filter', + ], + [ + 'filterAppliesToSourceMappings.0', + 'Select only log, trace, or session sources for this filter', + ], + ]); + }); + + it('accepts sources of the right kind with an empty applies-to', () => { + expect(issuesFor(['src-logs'])).toBeUndefined(); + }); +}); diff --git a/packages/app/tests/e2e/features/dashboard-template-import.spec.ts b/packages/app/tests/e2e/features/dashboard-template-import.spec.ts index 34e9b5ae85..6abeb69e86 100644 --- a/packages/app/tests/e2e/features/dashboard-template-import.spec.ts +++ b/packages/app/tests/e2e/features/dashboard-template-import.spec.ts @@ -12,6 +12,7 @@ import { DEFAULT_CONNECTION_NAME, DEFAULT_LOGS_SOURCE_NAME, DEFAULT_METRICS_SOURCE_NAME, + DEFAULT_SESSIONS_SOURCE_NAME, DEFAULT_TRACES_SOURCE_NAME, } from '../utils/constants'; @@ -90,6 +91,78 @@ function makeTemplateWithOnClick({ }; } +const ADHOC_TILE_NAME = 'Events'; + +/** + * Build a single-tile dashboard template with one SQL ad hoc filter and a saved + * condition for it. Source names that don't match a workspace source are left + * for the test to map. + */ +function makeAdhocFilterTemplate({ + dashboardName, + tileSourceName, + sources, + appliesToSourceIds, +}: { + dashboardName: string; + tileSourceName: string; + sources: string[]; + appliesToSourceIds?: string[]; +}) { + return { + version: '0.1.0', + name: dashboardName, + tiles: [ + { + id: 'tile-1', + x: 0, + y: 0, + w: 6, + h: 4, + config: { + name: ADHOC_TILE_NAME, + source: tileSourceName, + displayType: 'number', + granularity: 'auto', + select: [{ aggFn: 'count', valueExpression: '' }], + where: '', + whereLanguage: 'sql', + }, + }, + ], + filters: [ + { + id: 'filter-1', + type: 'ADHOC', + name: 'Conditions', + sourceType: 'sql', + sources, + appliesToSourceIds, + isVariableEnabled: true, + variableName: 'conditions', + }, + ], + savedFilterValues: [ + { + type: 'adhoc', + name: 'conditions', + conditions: [{ key: 'ServiceName', operator: '=', value: 'api' }], + }, + ], + }; +} + +async function getSourceId( + page: Page, + kind: 'log' | 'trace' | 'session', + name: string, +): Promise { + const sources = await getSources(page, kind); + const match = sources.find((s: { name: string }) => s.name === name); + expect(match, `seeded ${kind} source "${name}"`).toBeDefined(); + return match.id; +} + test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { let dashboardsListPage: DashboardsListPage; let dashboardImportPage: DashboardImportPage; @@ -292,12 +365,12 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Verify both tile source and onClick source rows are rendered', async () => { await expect( - dashboardImportPage.getMappingRow(tileName, 'Data Source'), + dashboardImportPage.getMappingRow(tileName, 'Data source'), ).toBeVisible(); await expect( dashboardImportPage.getMappingRow( tileName, - 'On Click - Search Source', + 'On click - search source', ), ).toBeVisible(); }); @@ -305,7 +378,7 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Verify the onClick source dropdown only lists log and trace sources', async () => { const onClickRow = dashboardImportPage.getMappingRow( tileName, - 'On Click - Search Source', + 'On click - search source', ); await onClickRow.getByPlaceholder('Select a source').click(); await expect( @@ -333,12 +406,12 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Map the tile source and onClick search source', async () => { await dashboardImportPage.selectMapping( tileName, - 'Data Source', + 'Data source', DEFAULT_METRICS_SOURCE_NAME, ); await dashboardImportPage.selectMapping( tileName, - 'On Click - Search Source', + 'On click - search source', DEFAULT_LOGS_SOURCE_NAME, ); }); @@ -410,19 +483,19 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Verify the onClick dashboard row is rendered', async () => { await expect( - dashboardImportPage.getMappingRow(tileName, 'On Click - Dashboard'), + dashboardImportPage.getMappingRow(tileName, 'On click - dashboard'), ).toBeVisible(); }); await test.step('Map the tile source and onClick target dashboard', async () => { await dashboardImportPage.selectMapping( tileName, - 'Data Source', + 'Data source', DEFAULT_METRICS_SOURCE_NAME, ); await dashboardImportPage.selectMapping( tileName, - 'On Click - Dashboard', + 'On click - dashboard', targetDashboardName, ); }); @@ -481,10 +554,10 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await expect(dashboardImportPage.mappingStepHeading).toBeVisible(); }); - await test.step('Select only the tile Data Source mapping', async () => { + await test.step('Select only the tile Data source mapping', async () => { await dashboardImportPage.selectMapping( tileName, - 'Data Source', + 'Data source', DEFAULT_LOGS_SOURCE_NAME, ); }); @@ -492,7 +565,7 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Verify the onClick source select auto-filled with the same source', async () => { const onClickRow = dashboardImportPage.getMappingRow( tileName, - 'On Click - Search Source', + 'On click - search source', ); await expect( onClickRow.getByPlaceholder('Select a source'), @@ -578,12 +651,12 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Verify the applies-to row is rendered alongside the filter source row', async () => { await expect( - dashboardImportPage.getMappingRow('Service (Filter)', 'Data Source'), + dashboardImportPage.getMappingRow('Service (Filter)', 'Data source'), ).toBeVisible(); await expect( dashboardImportPage.getMappingRow( 'Service (Filter)', - 'Applies to Sources', + 'Applies to sources', ), ).toBeVisible(); }); @@ -611,6 +684,376 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { }, ); + test( + 'should remap ad hoc filter sources and applies-to sources on import', + { tag: '@full-stack' }, + async ({ page }) => { + const ts = Date.now(); + const dashboardName = `E2E Import Ad Hoc Filter ${ts}`; + // `E2E Logs` auto-resolves; the other names exist only in the exporting + // workspace, so the test maps them by hand. + const missingTracesName = `Exported Traces ${ts}`; + const template = makeAdhocFilterTemplate({ + dashboardName, + tileSourceName: DEFAULT_LOGS_SOURCE_NAME, + sources: [DEFAULT_LOGS_SOURCE_NAME, missingTracesName], + appliesToSourceIds: [missingTracesName], + }); + + const templatePath = writeTempTemplate(template); + + await test.step('Upload the template and wait for mapping step', async () => { + await dashboardImportPage.gotoImport(); + await dashboardImportPage.uploadTemplateFile(templatePath); + await expect(dashboardImportPage.mappingStepHeading).toBeVisible(); + }); + + await test.step('Map the unresolved source and applies-to names', async () => { + await dashboardImportPage.selectMapping( + 'Conditions (Filter)', + 'Data sources', + DEFAULT_TRACES_SOURCE_NAME, + ); + await dashboardImportPage.selectMapping( + 'Conditions (Filter)', + 'Applies to sources', + DEFAULT_TRACES_SOURCE_NAME, + ); + }); + + await test.step('Finish import', async () => { + await dashboardImportPage.finishImportButton.click(); + await expect( + dashboardImportPage.getImportSuccessNotification(), + ).toBeVisible(); + await page.waitForURL(/\/dashboards\/[a-f0-9]{24}/); + }); + + await test.step('Verify the saved filter carries the mapped source ids and its saved conditions', async () => { + const logsSourceId = await getSourceId( + page, + 'log', + DEFAULT_LOGS_SOURCE_NAME, + ); + const tracesSourceId = await getSourceId( + page, + 'trace', + DEFAULT_TRACES_SOURCE_NAME, + ); + + const dashboard = await fetchDashboardById( + page, + dashboardPage.getCurrentDashboardId(), + ); + expect(dashboard.filters).toHaveLength(1); + expect(dashboard.filters[0]).toMatchObject({ + type: 'ADHOC', + sources: [logsSourceId, tracesSourceId], + appliesToSourceIds: [tracesSourceId], + }); + expect(dashboard.savedFilterValues).toEqual(template.savedFilterValues); + }); + }, + ); + + test( + 'should block the import until an ad hoc filter has a source, and propagate tile source remaps to it', + { tag: '@full-stack' }, + async ({ page }) => { + const ts = Date.now(); + const dashboardName = `E2E Import Ad Hoc Filter Required ${ts}`; + const missingSourceName = `Exported Logs ${ts}`; + const template = makeAdhocFilterTemplate({ + dashboardName, + tileSourceName: DEFAULT_LOGS_SOURCE_NAME, + sources: [missingSourceName], + appliesToSourceIds: [DEFAULT_LOGS_SOURCE_NAME], + }); + + const templatePath = writeTempTemplate(template); + + await test.step('Upload the template and wait for mapping step', async () => { + await dashboardImportPage.gotoImport(); + await dashboardImportPage.uploadTemplateFile(templatePath); + await expect(dashboardImportPage.mappingStepHeading).toBeVisible(); + }); + + await test.step('Finish import is blocked and the ad hoc sources row shows the error', async () => { + await dashboardImportPage.finishImportButton.click(); + await expect( + dashboardImportPage.getMappingRowError( + 'Conditions (Filter)', + 'Data sources', + ), + ).toBeVisible(); + await expect( + dashboardImportPage.getImportSuccessNotification(), + ).toBeHidden(); + await expect(page).toHaveURL(/\/dashboards\/import/); + // Submitting focuses the invalid multiselect, which opens its dropdown + // over the rows below. + await page.keyboard.press('Escape'); + }); + + await test.step('Remapping the tile source carries over to the applies-to list', async () => { + await dashboardImportPage.selectMapping( + ADHOC_TILE_NAME, + 'Data source', + DEFAULT_TRACES_SOURCE_NAME, + ); + }); + + await test.step('Picking a source clears the error and lets the import finish', async () => { + await dashboardImportPage.selectMapping( + 'Conditions (Filter)', + 'Data sources', + DEFAULT_LOGS_SOURCE_NAME, + ); + await expect( + dashboardImportPage.getMappingRowError( + 'Conditions (Filter)', + 'Data sources', + ), + ).toBeHidden(); + + await dashboardImportPage.finishImportButton.click(); + await expect( + dashboardImportPage.getImportSuccessNotification(), + ).toBeVisible(); + await page.waitForURL(/\/dashboards\/[a-f0-9]{24}/); + }); + + await test.step('Verify the saved filter carries the picked and propagated source ids', async () => { + const logsSourceId = await getSourceId( + page, + 'log', + DEFAULT_LOGS_SOURCE_NAME, + ); + const tracesSourceId = await getSourceId( + page, + 'trace', + DEFAULT_TRACES_SOURCE_NAME, + ); + + const dashboard = await fetchDashboardById( + page, + dashboardPage.getCurrentDashboardId(), + ); + expect(dashboard.filters[0]).toMatchObject({ + type: 'ADHOC', + sources: [logsSourceId], + appliesToSourceIds: [tracesSourceId], + }); + }); + }, + ); + + test( + 'should replace an ad hoc filter source when a tile is remapped to a source already in the list', + { tag: '@full-stack' }, + async ({ page }) => { + const ts = Date.now(); + const dashboardName = `E2E Import Ad Hoc Filter Remap Duplicate ${ts}`; + const template = makeAdhocFilterTemplate({ + dashboardName, + tileSourceName: DEFAULT_LOGS_SOURCE_NAME, + sources: [DEFAULT_LOGS_SOURCE_NAME, DEFAULT_TRACES_SOURCE_NAME], + }); + + const templatePath = writeTempTemplate(template); + + await test.step('Upload the template and wait for mapping step', async () => { + await dashboardImportPage.gotoImport(); + await dashboardImportPage.uploadTemplateFile(templatePath); + await expect(dashboardImportPage.mappingStepHeading).toBeVisible(); + }); + + await test.step('Remap the tile source from logs to traces', async () => { + await dashboardImportPage.selectMapping( + ADHOC_TILE_NAME, + 'Data source', + DEFAULT_TRACES_SOURCE_NAME, + ); + }); + + await test.step('Finish import', async () => { + await dashboardImportPage.finishImportButton.click(); + await expect( + dashboardImportPage.getImportSuccessNotification(), + ).toBeVisible(); + await page.waitForURL(/\/dashboards\/[a-f0-9]{24}/); + }); + + await test.step('Verify logs was replaced rather than kept alongside traces', async () => { + const tracesSourceId = await getSourceId( + page, + 'trace', + DEFAULT_TRACES_SOURCE_NAME, + ); + + const dashboard = await fetchDashboardById( + page, + dashboardPage.getCurrentDashboardId(), + ); + expect(dashboard.filters[0]).toMatchObject({ + type: 'ADHOC', + sources: [tracesSourceId], + }); + }); + }, + ); + + test( + 'should keep ad hoc filter sources in place across successive tile remaps', + { tag: '@full-stack' }, + async ({ page }) => { + const ts = Date.now(); + const dashboardName = `E2E Import Ad Hoc Filter Successive Remaps ${ts}`; + const tracesTileName = 'Spans'; + const baseTemplate = makeAdhocFilterTemplate({ + dashboardName, + tileSourceName: DEFAULT_LOGS_SOURCE_NAME, + sources: [ + DEFAULT_LOGS_SOURCE_NAME, + DEFAULT_TRACES_SOURCE_NAME, + DEFAULT_SESSIONS_SOURCE_NAME, + ], + }); + const template = { + ...baseTemplate, + tiles: [ + ...baseTemplate.tiles, + { + ...baseTemplate.tiles[0], + id: 'tile-2', + y: 4, + config: { + ...baseTemplate.tiles[0].config, + name: tracesTileName, + source: DEFAULT_TRACES_SOURCE_NAME, + }, + }, + ], + }; + + const templatePath = writeTempTemplate(template); + + await test.step('Upload the template and wait for mapping step', async () => { + await dashboardImportPage.gotoImport(); + await dashboardImportPage.uploadTemplateFile(templatePath); + await expect(dashboardImportPage.mappingStepHeading).toBeVisible(); + }); + + // The first remap collapses the list to two entries; the second must + // still replace the traces entry rather than the sessions one. + await test.step('Remap the logs tile to traces, then the traces tile to logs', async () => { + await dashboardImportPage.selectMapping( + ADHOC_TILE_NAME, + 'Data source', + DEFAULT_TRACES_SOURCE_NAME, + ); + await dashboardImportPage.selectMapping( + tracesTileName, + 'Data source', + DEFAULT_LOGS_SOURCE_NAME, + ); + }); + + await test.step('Finish import', async () => { + await dashboardImportPage.finishImportButton.click(); + await expect( + dashboardImportPage.getImportSuccessNotification(), + ).toBeVisible(); + await page.waitForURL(/\/dashboards\/[a-f0-9]{24}/); + }); + + await test.step('Verify every template source maps to its latest remap', async () => { + const logsSourceId = await getSourceId( + page, + 'log', + DEFAULT_LOGS_SOURCE_NAME, + ); + const tracesSourceId = await getSourceId( + page, + 'trace', + DEFAULT_TRACES_SOURCE_NAME, + ); + const sessionsSourceId = await getSourceId( + page, + 'session', + DEFAULT_SESSIONS_SOURCE_NAME, + ); + + const dashboard = await fetchDashboardById( + page, + dashboardPage.getCurrentDashboardId(), + ); + expect(dashboard.filters[0]).toMatchObject({ + type: 'ADHOC', + sources: [tracesSourceId, logsSourceId, sessionsSourceId], + }); + }); + }, + ); + + test( + 'should fill unresolved ad hoc filter sources and applies-to sources from a tile remap', + { tag: '@full-stack' }, + async ({ page }) => { + const ts = Date.now(); + const dashboardName = `E2E Import Ad Hoc Filter Unresolved Remap ${ts}`; + const missingLogsName = `Exported Logs ${ts}`; + const template = makeAdhocFilterTemplate({ + dashboardName, + tileSourceName: missingLogsName, + sources: [missingLogsName], + appliesToSourceIds: [missingLogsName], + }); + + const templatePath = writeTempTemplate(template); + + await test.step('Upload the template and wait for mapping step', async () => { + await dashboardImportPage.gotoImport(); + await dashboardImportPage.uploadTemplateFile(templatePath); + await expect(dashboardImportPage.mappingStepHeading).toBeVisible(); + }); + + await test.step('Map the tile source only', async () => { + await dashboardImportPage.selectMapping( + ADHOC_TILE_NAME, + 'Data source', + DEFAULT_LOGS_SOURCE_NAME, + ); + }); + + await test.step('Finish import', async () => { + await dashboardImportPage.finishImportButton.click(); + await expect( + dashboardImportPage.getImportSuccessNotification(), + ).toBeVisible(); + await page.waitForURL(/\/dashboards\/[a-f0-9]{24}/); + }); + + await test.step('Verify the filter picked up the tile mapping', async () => { + const logsSourceId = await getSourceId( + page, + 'log', + DEFAULT_LOGS_SOURCE_NAME, + ); + + const dashboard = await fetchDashboardById( + page, + dashboardPage.getCurrentDashboardId(), + ); + expect(dashboard.filters[0]).toMatchObject({ + type: 'ADHOC', + sources: [logsSourceId], + appliesToSourceIds: [logsSourceId], + }); + }); + }, + ); + test( 'should block the import until a queried filter has a source picked', { tag: '@full-stack' }, @@ -678,7 +1121,7 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Picking a source clears the error and lets the import finish', async () => { await dashboardImportPage.selectMapping( 'Machine (Filter)', - 'Data Source', + 'Data source', DEFAULT_LOGS_SOURCE_NAME, ); await expect( @@ -790,7 +1233,7 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await expect( dashboardImportPage.getMappingRowError( sqlTileName, - 'Data Connection', + 'Data connection', ), ).toBeVisible(); // Markdown needs no source, so its row is not flagged. @@ -806,12 +1249,12 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Picking the tile source and connection lets the import finish', async () => { await dashboardImportPage.selectMapping( builderTileName, - 'Data Source', + 'Data source', DEFAULT_LOGS_SOURCE_NAME, ); await dashboardImportPage.selectMapping( sqlTileName, - 'Data Connection', + 'Data connection', DEFAULT_CONNECTION_NAME, ); @@ -891,12 +1334,12 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Pick the first tile source and verify it propagates to the second', async () => { await dashboardImportPage.selectMapping( firstTileName, - 'Data Source', + 'Data source', DEFAULT_LOGS_SOURCE_NAME, ); await expect( dashboardImportPage - .getMappingRow(secondTileName, 'Data Source') + .getMappingRow(secondTileName, 'Data source') .getByPlaceholder('Select a source'), ).toHaveValue(DEFAULT_LOGS_SOURCE_NAME); }); @@ -904,7 +1347,7 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Clearing it flags both rows, including the one that was cleared', async () => { await dashboardImportPage.clearMapping( firstTileName, - 'Data Source', + 'Data source', DEFAULT_LOGS_SOURCE_NAME, ); await expect( @@ -918,7 +1361,7 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await test.step('Re-picking the source clears both errors and lets the import finish', async () => { await dashboardImportPage.selectMapping( firstTileName, - 'Data Source', + 'Data source', DEFAULT_LOGS_SOURCE_NAME, ); await expect( @@ -1152,11 +1595,11 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await expect( dashboardImportPage.getMappingRow( tileName, - 'On Click - Search Source', + 'On click - search source', ), ).toBeHidden(); await expect( - dashboardImportPage.getMappingRow(tileName, 'On Click - Dashboard'), + dashboardImportPage.getMappingRow(tileName, 'On click - dashboard'), ).toBeHidden(); }); @@ -1210,10 +1653,10 @@ test.describe('Dashboard Template Import', { tag: ['@dashboard'] }, () => { await expect(dashboardImportPage.mappingStepHeading).toBeVisible(); }); - await test.step('Only map the tile Data Source, leave onClick source empty', async () => { + await test.step('Only map the tile Data source, leave onClick source empty', async () => { await dashboardImportPage.selectMapping( tileName, - 'Data Source', + 'Data source', DEFAULT_METRICS_SOURCE_NAME, ); }); diff --git a/packages/app/tests/e2e/page-objects/DashboardImportPage.ts b/packages/app/tests/e2e/page-objects/DashboardImportPage.ts index 5a4d100013..4889c1526b 100644 --- a/packages/app/tests/e2e/page-objects/DashboardImportPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardImportPage.ts @@ -5,15 +5,22 @@ import { Locator, Page } from '@playwright/test'; /** - * Mapping type labels rendered in the Step 2 table's "Mapping Type" column. + * Mapping type labels rendered in the Step 2 table's "Mapping type" column. * Each row is keyed by (tile/filter name, mapping type). */ export type MappingType = - | 'Data Source' - | 'Data Connection' - | 'On Click - Search Source' - | 'On Click - Dashboard' - | 'Applies to Sources'; + | 'Data source' + | 'Data connection' + | 'On click - search source' + | 'On click - dashboard' + | 'Applies to sources' + | 'Data sources'; + +/** Test ID prefixes of the multiselect mapping types, keyed by mapping type. */ +const MULTI_SELECT_TEST_ID_PREFIXES: Partial> = { + 'Applies to sources': 'filter-applies-to-mapping-', + 'Data sources': 'filter-adhoc-sources-mapping-', +}; export class DashboardImportPage { readonly page: Page; @@ -32,7 +39,7 @@ export class DashboardImportPage { this.page = page; this.templatesPageContainer = page.getByTestId('dashboard-templates-page'); this.mappingStepHeading = page.getByText('Step 2: Map Data'); - this.dashboardNameInput = page.getByLabel('Dashboard Name'); + this.dashboardNameInput = page.getByLabel('Dashboard name'); this.finishImportButton = page.getByRole('button', { name: 'Finish Import', }); @@ -119,22 +126,26 @@ export class DashboardImportPage { optionName: string, ) { const row = this.getMappingRow(inputName, mappingType); + const testIdPrefix = MULTI_SELECT_TEST_ID_PREFIXES[mappingType]; + if (testIdPrefix) { + await row.getByTestId(new RegExp(`^${testIdPrefix}`)).click(); + await this.page + .getByRole('option', { name: optionName, exact: true }) + .click(); + // Close the dropdown so it doesn't intercept future clicks. + await this.page.keyboard.press('Escape'); + return; + } const placeholder = - mappingType === 'Data Connection' + mappingType === 'Data connection' ? 'Select a connection' - : mappingType === 'On Click - Dashboard' + : mappingType === 'On click - dashboard' ? 'Select a dashboard' - : mappingType === 'Applies to Sources' - ? 'Select sources' - : 'Select a source'; + : 'Select a source'; await row.getByPlaceholder(placeholder).click(); await this.page .getByRole('option', { name: optionName, exact: true }) .click(); - if (mappingType === 'Applies to Sources') { - // Multiselect — close the dropdown so it doesn't intercept future clicks. - await this.page.keyboard.press('Escape'); - } } /** @@ -161,10 +172,10 @@ export class DashboardImportPage { */ getMappingRowError( inputName: string, - mappingType: MappingType = 'Data Source', + mappingType: MappingType = 'Data source', ) { return this.getMappingRow(inputName, mappingType).getByText( - /^Select a (?:source|connection) for this (?:tile|filter)$/, + /^Select (?:a|at least one) (?:source|connection) for this (?:tile|filter)$/, ); }