diff --git a/datahub-web-react/src/app/searchV2/filters/FilterOption.tsx b/datahub-web-react/src/app/searchV2/filters/FilterOption.tsx index 08ab65d6ccb9f..c920a15d29942 100644 --- a/datahub-web-react/src/app/searchV2/filters/FilterOption.tsx +++ b/datahub-web-react/src/app/searchV2/filters/FilterOption.tsx @@ -1,10 +1,16 @@ -import { Checkbox, Pill } from '@components'; +import { Checkbox, Pill, Tooltip } from '@components'; import { CaretUp } from '@phosphor-icons/react/dist/csr/CaretUp'; import React, { useState } from 'react'; import styled from 'styled-components'; import { generateColor } from '@app/entityV2/shared/components/styled/StyledTag'; +import { SingleEntityIcon } from '@app/searchV2/autoCompleteV2/components/icon/SingleEntityIcon'; import ParentEntities from '@app/searchV2/filters/ParentEntities'; +import { + DisambiguatingParent, + getContainerPathSegments, + getFullContainerPath, +} from '@app/searchV2/filters/containerDisambiguation'; import { Label } from '@app/searchV2/filters/styledComponents'; import { FilterOptionType } from '@app/searchV2/filters/types'; import { @@ -89,6 +95,26 @@ const LabelWrapper = styled.div` line-height: normal; `; +// getContainerPathSegments keeps paths short; the overflow rules are only a backstop for wide glyphs. +const ContainerPath = styled.span` + display: block; + max-width: 240px; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +`; + +const ContainerOption = styled.div` + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +`; + +const ElidedSegment = styled.span` + cursor: help; +`; + interface Props { filterOption: FilterOptionType; selectedFilterOptions: FilterOptionType[]; @@ -96,6 +122,8 @@ interface Props { nestedOptions?: FilterOptionType[]; addPadding?: boolean; includeCount?: boolean; + // Parent containers (nearest first) to prefix to the label so it differs from same-named options. + containerParents?: DisambiguatingParent[]; } export default function FilterOption({ @@ -105,10 +133,12 @@ export default function FilterOption({ nestedOptions, addPadding, includeCount = true, + containerParents, }: Props) { const [areChildrenVisible, setAreChildrenVisible] = useState(true); const { field, value, count, entity } = filterOption; const entityRegistry = useEntityRegistry(); + const getEntityLabel = (e: Entity) => entityRegistry.getDisplayName(e.type, e); const { label } = getFilterIconAndLabel(field, value, entityRegistry, entity || null, 14, filterOption.displayName); const showParentEntityPath = field === DOMAINS_FILTER_NAME && entity?.type === EntityType.Domain; const isSubTypeFilter = field === TYPE_NAMES_FILTER_NAME; @@ -134,6 +164,12 @@ export default function FilterOption({ } } + const defaultLabel = filterOption.entity ? ( + + ) : ( + + ); + return ( <> @@ -145,12 +181,37 @@ export default function FilterOption({ )} - {filterOption.entity ? ( - + {entity && containerParents?.length ? ( + + + + {getContainerPathSegments( + getEntityLabel(entity), + containerParents, + getEntityLabel, + ).map((segment, index) => { + const text = index ? `.${segment.text}` : segment.text; + return ( + + {segment.hidden ? ( + + {text} + + ) : ( + {text} + )} + + ); + })} + + ) : ( - + defaultLabel )} {nestedOptions && nestedOptions.length > 0 && ( { + it('shows sub-type labels capitalized', () => { + render( + + + + + , + ); + + expect(screen.getByText(/^External table/)).toBeInTheDocument(); + }); +}); diff --git a/datahub-web-react/src/app/searchV2/filters/__tests__/containerDisambiguation.test.ts b/datahub-web-react/src/app/searchV2/filters/__tests__/containerDisambiguation.test.ts new file mode 100644 index 0000000000000..0d6fcffa864e3 --- /dev/null +++ b/datahub-web-react/src/app/searchV2/filters/__tests__/containerDisambiguation.test.ts @@ -0,0 +1,157 @@ +import { + DisambiguatingParent, + formatContainerPath, + getContainerDisambiguationParents, + getContainerPathSegments, + getFullContainerPath, +} from '@app/searchV2/filters/containerDisambiguation'; + +import { Container, Entity, EntityType } from '@types'; + +const container = (urn: string, name: string, parents: Entity[] = []): Container => + ({ + urn, + type: EntityType.Container, + properties: { name }, + parentContainers: { count: parents.length, containers: parents }, + }) as unknown as Container; + +const getLabel = (entity: Entity) => (entity as Container).properties?.name || entity.urn; + +const option = (entity: Entity) => ({ value: entity.urn, entity }); + +const names = (parents: DisambiguatingParent[] | undefined) => parents?.map((parent) => getLabel(parent.entity)); + +const lakehouseA = container('urn:li:container:lakehouse_a', 'lakehouse_a'); +const lakehouseB = container('urn:li:container:lakehouse_b', 'lakehouse_b'); +const petshopEast = container('urn:li:container:petshop_east', 'petshop_east', [lakehouseA]); +const petshopWest = container('urn:li:container:petshop_west', 'petshop_west', [lakehouseA]); +const centralA = container('urn:li:container:central_a', 'petshop_central', [lakehouseA]); +const centralB = container('urn:li:container:central_b', 'petshop_central', [lakehouseB]); + +describe('getContainerDisambiguationParents', () => { + it('adds only the nearest parent when it tells same-named containers apart', () => { + const eastStore = container('urn:li:container:east_store', 'store', [petshopEast, lakehouseA]); + const westStore = container('urn:li:container:west_store', 'store', [petshopWest, lakehouseA]); + + const result = getContainerDisambiguationParents([option(eastStore), option(westStore)], getLabel); + + expect(names(result.get(eastStore.urn))).toEqual(['petshop_east']); + expect(names(result.get(westStore.urn))).toEqual(['petshop_west']); + }); + + it('adds as many levels as it takes when the nearest parents share a name too', () => { + const supplyA = container('urn:li:container:supply_a', 'supply', [centralA, lakehouseA]); + const supplyB = container('urn:li:container:supply_b', 'Supply ', [centralB, lakehouseB]); + + const result = getContainerDisambiguationParents([option(supplyA), option(supplyB)], getLabel); + + expect(names(result.get(supplyA.urn))).toEqual(['petshop_central', 'lakehouse_a']); + expect(names(result.get(supplyB.urn))).toEqual(['petshop_central', 'lakehouse_b']); + expect(result.get(supplyA.urn)?.map((parent) => parent.shared)).toEqual([true, false]); + }); + + it('leaves unique names and non-container options alone', () => { + const inventory = container('urn:li:container:inventory', 'inventory', [petshopEast]); + const tag = { urn: 'urn:li:tag:store', type: EntityType.Tag } as Entity; + const store = container('urn:li:container:east_store', 'store', [petshopEast]); + + const result = getContainerDisambiguationParents( + [option(inventory), option(store), { value: tag.urn, entity: tag }, { value: 'no-entity' }], + getLabel, + ); + + expect(result.size).toBe(0); + }); + + it('shows the whole chain when no level tells the options apart', () => { + const storeOne = container('urn:li:container:store_1', 'store', [petshopEast, lakehouseA]); + const storeTwo = container('urn:li:container:store_2', 'store', [petshopEast, lakehouseA]); + const rootStore = container('urn:li:container:store_root', 'store'); + + const result = getContainerDisambiguationParents( + [option(storeOne), option(storeTwo), option(rootStore)], + getLabel, + ); + + expect(names(result.get(storeOne.urn))).toEqual(['petshop_east', 'lakehouse_a']); + expect(names(result.get(storeTwo.urn))).toEqual(['petshop_east', 'lakehouse_a']); + expect(result.has(rootStore.urn)).toBe(false); + }); +}); + +describe('container paths', () => { + it('formats parents outermost first, ending with the label', () => { + expect(formatContainerPath('supply', [centralA, lakehouseA], getLabel)).toBe( + 'lakehouse_a.petshop_central.supply', + ); + }); + + it('builds the full path from all parent containers, or just the name without parents', () => { + const supply = container('urn:li:container:supply_a', 'supply', [centralA, lakehouseA]); + + expect(getFullContainerPath(supply, getLabel)).toBe('lakehouse_a.petshop_central.supply'); + expect( + getFullContainerPath({ urn: 'urn:li:container:bare', type: EntityType.Container } as Entity, getLabel), + ).toBe('urn:li:container:bare'); + }); +}); + +describe('getContainerPathSegments', () => { + const chain = (...labels: string[]) => labels.map((label) => container(`urn:li:container:${label}`, label)); + const parentsOf = (shared: string[], ...labels: string[]): DisambiguatingParent[] => + chain(...labels).map((entity) => ({ entity, shared: shared.includes(getLabel(entity)), keepWhole: false })); + const text = (segments: { text: string }[]) => segments.map((segment) => segment.text).join('.'); + + it('shows a short path whole', () => { + expect(getContainerPathSegments('store', parentsOf([], 'petshop_east', 'lakehouse_a'), getLabel)).toEqual([ + { key: 'urn:li:container:lakehouse_a', text: 'lakehouse_a' }, + { key: 'urn:li:container:petshop_east', text: 'petshop_east' }, + { key: 'self', text: 'store' }, + ]); + }); + + it('collapses the levels the whole group shares in a long path', () => { + const middle = ['department', 'petshop_central', 'region', 'warehouse']; + const parents = parentsOf(middle, ...middle, 'lakehouse_alpha'); + + expect(getContainerPathSegments('supply', parents, getLabel)).toEqual([ + { key: 'urn:li:container:lakehouse_alpha', text: 'lakeho\u2026e_alpha', hidden: 'lakehouse_alpha' }, + { + key: 'collapsed:urn:li:container:warehouse', + text: '\u2026', + hidden: 'warehouse.region.petshop_central.department', + }, + { key: 'self', text: 'supply' }, + ]); + }); + + it('shortens long names in the middle so names that differ at either end stay apart', () => { + const archive = 'petshop_loyalty_programme_archive_2019_to_2024'; + + expect(getContainerPathSegments('store', parentsOf([], archive), getLabel)).toEqual([ + { key: `urn:li:container:${archive}`, text: 'petsho\u2026to_2024', hidden: archive }, + { key: 'self', text: 'store' }, + ]); + }); + + it('keeps every level that differs, so long paths that need several levels stay apart', () => { + const lakeA = container('urn:li:container:lake_a', 'petshop_lakehouse_a'); + const lakeB = container('urn:li:container:lake_b', 'petshop_lakehouse_b'); + const central = (urn: string, lake: Entity) => container(urn, 'petshop_central_region', [lake]); + const east = container('urn:li:container:east_a', 'petshop_east_region', [lakeA]); + const supplies = [ + container('urn:li:container:supply_1', 'supply', [central('urn:li:container:central_a', lakeA), lakeA]), + container('urn:li:container:supply_2', 'supply', [central('urn:li:container:central_b', lakeB), lakeB]), + container('urn:li:container:supply_3', 'supply', [east, lakeA]), + ]; + + const parents = getContainerDisambiguationParents(supplies.map(option), getLabel); + const labels = supplies.map((supply) => + text(getContainerPathSegments('supply', parents.get(supply.urn) || [], getLabel)), + ); + + expect(new Set(labels).size).toBe(3); + expect(labels).toContain('petsho\u2026house_a.petshop_east_region.supply'); + }); +}); diff --git a/datahub-web-react/src/app/searchV2/filters/containerDisambiguation.ts b/datahub-web-react/src/app/searchV2/filters/containerDisambiguation.ts new file mode 100644 index 0000000000000..481ca7e4809d5 --- /dev/null +++ b/datahub-web-react/src/app/searchV2/filters/containerDisambiguation.ts @@ -0,0 +1,163 @@ +import { Container, Entity, EntityType } from '@types'; + +interface OptionWithEntity { + value: string; + entity?: Entity | null; +} + +type GetLabel = (entity: Entity) => string; + +const getParentChain = (entity: Entity): Entity[] => (entity as Container).parentContainers?.containers || []; + +const toKey = (label: string): string => label.trim().toLowerCase(); + +// Roughly what fits in the option row next to the checkbox and count. +export const MAX_PATH_CHARS = 32; +export const MAX_SEGMENT_CHARS = 14; + +const ELLIPSIS = '\u2026'; + +// Cut from the middle: same-named containers usually differ at the start or the end of a name +// ("..._2019_to_2024" vs "..._2014_to_2018"), so both ends stay visible. +const shortenName = (name: string): string => { + if (name.length <= MAX_SEGMENT_CHARS) return name; + const head = Math.floor((MAX_SEGMENT_CHARS - 1) / 2); + const tail = Math.ceil((MAX_SEGMENT_CHARS - 1) / 2); + return `${name.slice(0, head)}${ELLIPSIS}${name.slice(name.length - tail)}`; +}; + +/** + * A parent shown to tell same-named options apart. `shared`: every option in the group has this name at + * this level. `keepWhole`: shortening names at this level would make different names look the same. + */ +export interface DisambiguatingParent { + entity: Entity; + shared: boolean; + keepWhole: boolean; +} + +/** + * Container filter options often share a name: a "store" schema can exist in several databases, so + * the option list would show several identical "store" entries. For each group of container options + * whose labels collide, find the fewest parent levels (nearest parent first, so the database before + * any higher level) that make the group's labels unique, and return those parents per option value. + * Options with a unique label are left out, so the list only grows where it has to. If no depth tells + * a group apart (the same names all the way up), every option in it gets its whole parent chain. + * + * Parents are returned nearest-first, the order `parentContainers` uses. Each is marked `shared` when + * the whole group has the same name at that level, since such a level tells nothing apart and can be + * collapsed when the path is too long to show whole, and `keepWhole` when shortening the level's names + * would make different names look the same. + */ +export function getContainerDisambiguationParents( + options: OptionWithEntity[], + getLabel: GetLabel, +): Map { + const groups = new Map(); + options.forEach((option) => { + if (option.entity?.type !== EntityType.Container) return; + const key = toKey(getLabel(option.entity)); + groups.set(key, [...(groups.get(key) || []), option]); + }); + + const result = new Map(); + groups.forEach((group) => { + if (group.length < 2) return; + const chains = group.map((option) => getParentChain(option.entity as Entity)); + const maxDepth = Math.max(...chains.map((chain) => chain.length)); + const pathKey = (chain: Entity[], depth: number) => + chain + .slice(0, depth) + .map((parent) => toKey(getLabel(parent))) + .join('/'); + + let depth = maxDepth; + for (let candidate = 1; candidate < maxDepth; candidate++) { + const keys = new Set(chains.map((chain) => pathKey(chain, candidate))); + if (keys.size === chains.length) { + depth = candidate; + break; + } + } + + // A chain shorter than `level` has no name there, which counts as different from every name. + const levelNames = (level: number): (string | null)[] => + chains.map((chain) => (chain[level] ? getLabel(chain[level]) : null)); + const levels = Array.from({ length: depth }, (_, level) => { + const names = levelNames(level); + const distinct = new Set(names.map((name) => name && toKey(name))); + const distinctShortened = new Set(names.map((name) => name && toKey(shortenName(name)))); + return { + shared: distinct.size === 1 && !distinct.has(null), + keepWhole: distinctShortened.size < distinct.size, + }; + }); + + group.forEach((option, index) => { + const parents = chains[index].slice(0, depth).map((entity, level) => ({ entity, ...levels[level] })); + if (parents.length) result.set(option.value, parents); + }); + }); + return result; +} + +/** A dotted path from the outermost of `parents` (nearest-first) down to `label`: "petshop_east.store". */ +export function formatContainerPath(label: string, parents: Entity[], getLabel: GetLabel): string { + return [...parents].reverse().map(getLabel).concat(label).join('.'); +} + +/** The container's full path, from its outermost parent container down to the container itself. */ +export function getFullContainerPath(entity: Entity, getLabel: GetLabel): string { + return formatContainerPath(getLabel(entity), getParentChain(entity), getLabel); +} + +/** One piece of a displayed path. `hidden` is the text an ellipsis in `text` stands for, if any. */ +export interface ContainerPathSegment { + key: string; + text: string; + hidden?: string; +} + +const shorten = (key: string, name: string, keepWhole = false): ContainerPathSegment => { + const text = keepWhole ? name : shortenName(name); + return text === name ? { key, text } : { key, text, hidden: name }; +}; + +/** + * The path to display for `label` with its disambiguating `parents` (nearest-first). Short paths are + * shown whole. In a long one, each run of levels the whole group shares collapses to one ellipsis (those + * levels tell nothing apart), the levels that differ stay, and over-long names are shortened in the + * middle unless that would make them look alike. Each ellipsis records what it hides. + */ +export function getContainerPathSegments( + label: string, + parents: DisambiguatingParent[], + getLabel: GetLabel, +): ContainerPathSegment[] { + const outerFirst = [...parents].reverse(); + const names = outerFirst.map((parent) => getLabel(parent.entity)); + if (names.concat(label).join('.').length <= MAX_PATH_CHARS) { + return outerFirst + .map((parent, index) => ({ key: parent.entity.urn, text: names[index] })) + .concat({ key: 'self', text: label }); + } + + const segments: ContainerPathSegment[] = []; + let sharedRun: string[] = []; + let sharedRunKey = ''; + const closeSharedRun = () => { + if (sharedRun.length) segments.push({ key: sharedRunKey, text: ELLIPSIS, hidden: sharedRun.join('.') }); + sharedRun = []; + }; + outerFirst.forEach((parent, index) => { + if (parent.shared) { + if (!sharedRun.length) sharedRunKey = `collapsed:${parent.entity.urn}`; + sharedRun.push(names[index]); + } else { + closeSharedRun(); + segments.push(shorten(parent.entity.urn, names[index], parent.keepWhole)); + } + }); + closeSharedRun(); + return segments.concat(shorten('self', label)); +} diff --git a/datahub-web-react/src/app/searchV2/filters/mapFilterOption.tsx b/datahub-web-react/src/app/searchV2/filters/mapFilterOption.tsx index 571ca9364a8fc..91451ef64e52e 100644 --- a/datahub-web-react/src/app/searchV2/filters/mapFilterOption.tsx +++ b/datahub-web-react/src/app/searchV2/filters/mapFilterOption.tsx @@ -1,6 +1,7 @@ import React from 'react'; import FilterOption from '@app/searchV2/filters/FilterOption'; +import { DisambiguatingParent } from '@app/searchV2/filters/containerDisambiguation'; import { FilterOptionType } from '@app/searchV2/filters/types'; import { getFilterIconAndLabel } from '@app/searchV2/filters/utils'; import { EntityRegistry } from '@src/entityRegistryContext'; @@ -22,6 +23,7 @@ interface CreateFilterOptionProps { setSelectedFilterOptions: (values: FilterOptionType[]) => void; nestedOptions?: FilterOptionType[]; includeCount?: boolean; + containerParents?: DisambiguatingParent[]; } export function mapFilterOption({ @@ -31,6 +33,7 @@ export function mapFilterOption({ setSelectedFilterOptions, nestedOptions, includeCount = true, + containerParents, }: CreateFilterOptionProps): DisplayedFilterOption { const { label: displayName } = getFilterIconAndLabel( filterOption.field, @@ -50,6 +53,7 @@ export function mapFilterOption({ setSelectedFilterOptions={setSelectedFilterOptions} nestedOptions={nestedOptions} includeCount={includeCount} + containerParents={containerParents} /> ), entity: filterOption.entity, diff --git a/datahub-web-react/src/app/searchV2/filters/value/EntityValueMenu.tsx b/datahub-web-react/src/app/searchV2/filters/value/EntityValueMenu.tsx index f25e86d16e2cc..cb0544dd3df8c 100644 --- a/datahub-web-react/src/app/searchV2/filters/value/EntityValueMenu.tsx +++ b/datahub-web-react/src/app/searchV2/filters/value/EntityValueMenu.tsx @@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next'; import { useDebounce } from 'react-use'; import OptionsDropdownMenu from '@app/searchV2/filters/OptionsDropdownMenu'; +import { getContainerDisambiguationParents } from '@app/searchV2/filters/containerDisambiguation'; import { mapFilterOption } from '@app/searchV2/filters/mapFilterOption'; import { EntityFilterField, FilterValue, FilterValueOption } from '@app/searchV2/filters/types'; import { OptionList } from '@app/searchV2/filters/value/styledComponents'; @@ -67,8 +68,14 @@ export default function EntityValueMenu({ const finalDefaultOptions = defaultOptions.length ? defaultOptions : searchOptions; const finalOptions = searchQuery ? finalSearchOptions : finalDefaultOptions; + // Same-named containers (a "store" schema in two databases) get the parents that tell them apart. + const containerParents = getContainerDisambiguationParents(finalOptions, (entity) => + entityRegistry.getDisplayName(entity.type, entity), + ); + const filterMenuOptions = finalOptions.map((option) => mapFilterOption({ + containerParents: containerParents.get(option.value), filterOption: { field: field.field, value: option.value, diff --git a/datahub-web-react/src/app/searchV2/filters/value/__tests__/EntityValueMenu.test.tsx b/datahub-web-react/src/app/searchV2/filters/value/__tests__/EntityValueMenu.test.tsx new file mode 100644 index 0000000000000..35a00c9fbd5c1 --- /dev/null +++ b/datahub-web-react/src/app/searchV2/filters/value/__tests__/EntityValueMenu.test.tsx @@ -0,0 +1,68 @@ +import { MockedProvider } from '@apollo/client/testing'; +import { render, screen } from '@testing-library/react'; +import React from 'react'; + +import { FieldType, FilterValueOption } from '@app/searchV2/filters/types'; +import EntityValueMenu from '@app/searchV2/filters/value/EntityValueMenu'; +import { CONTAINER_FILTER_NAME } from '@app/searchV2/utils/constants'; +import TestPageContainer from '@utils/test-utils/TestPageContainer'; + +import { Container, Entity, EntityType } from '@types'; + +const container = (urn: string, name: string, parents: Entity[] = []): Container => + ({ + urn, + type: EntityType.Container, + properties: { name }, + parentContainers: { count: parents.length, containers: parents }, + }) as unknown as Container; + +const petshopEast = container('urn:li:container:petshop_east', 'petshop_east'); +const petshopWest = container('urn:li:container:petshop_west', 'petshop_west'); + +const LONG_ARCHIVE = 'petshop_loyalty_programme_archive'; +const archive2019 = container('urn:li:container:archive_2019', `${LONG_ARCHIVE}_2019_to_2024`); +const archive2014 = container('urn:li:container:archive_2014', `${LONG_ARCHIVE}_2014_to_2018`); + +const options: FilterValueOption[] = [ + container('urn:li:container:east_store', 'store', [petshopEast]), + container('urn:li:container:west_store', 'store', [petshopWest]), + container('urn:li:container:inventory', 'inventory', [petshopEast]), + container('urn:li:container:orders_2019', 'orders', [archive2019]), + container('urn:li:container:orders_2014', 'orders', [archive2014]), +].map((entity) => ({ value: entity.urn, entity, count: 1 })); + +describe('EntityValueMenu', () => { + it('labels same-named container options with the parent that tells them apart', () => { + render( + + + + + , + ); + + const paths = screen.getAllByTestId('filter-option-container-path').map((path) => path.textContent); + expect(paths).toEqual( + expect.arrayContaining([ + 'petshop_east.store', + 'petshop_west.store', + 'petsho\u2026to_2024.orders', + 'petsho\u2026to_2018.orders', + ]), + ); + expect(paths).toHaveLength(4); + expect(screen.getByText('inventory')).toBeInTheDocument(); + expect(screen.getAllByTestId('filter-option-container-path-elided')).toHaveLength(2); + }); +}); diff --git a/datahub-web-react/src/app/searchV2/filters/value/__tests__/utils.test.tsx b/datahub-web-react/src/app/searchV2/filters/value/__tests__/utils.test.tsx index 8654dcc1fe4b7..0c93f92f75ec7 100644 --- a/datahub-web-react/src/app/searchV2/filters/value/__tests__/utils.test.tsx +++ b/datahub-web-react/src/app/searchV2/filters/value/__tests__/utils.test.tsx @@ -7,6 +7,7 @@ import { getEntityTypeFilterValueDisplayName, mapFilterCountsToZero, mergeFilterOptionsInAllowedValuesOrder, + useFilterOptionsBySearchQuery, useLoadAggregationOptions, } from '@app/searchV2/filters/value/utils'; import { FILTER_DELIMITER } from '@app/searchV2/utils/constants'; @@ -14,7 +15,7 @@ import useGetSearchQueryInputs from '@src/app/search/useGetSearchQueryInputs'; import { renderHook } from '@utils/test-utils/renderHook'; import { useAggregateAcrossEntitiesQuery } from '@graphql/search.generated'; -import { AllowedValue, EntityType } from '@types'; +import { AllowedValue, Entity, EntityType } from '@types'; // Mock the GraphQL queries vi.mock('@graphql/search.generated', async (importOriginal) => { @@ -562,3 +563,21 @@ describe('getDefaultFieldOperatorType', () => { expect(getDefaultFieldOperatorType(parentDocumentField)).toBe(FilterOperatorType.WITHIN); }); }); + +describe('useFilterOptionsBySearchQuery', () => { + const container = (urn: string, name: string, parents: Entity[] = []) => + ({ urn, type: EntityType.Container, name, parentContainers: { containers: parents } }) as unknown as Entity; + const petshopEast = container('urn:li:container:petshop_east', 'petshop_east'); + const petshopWest = container('urn:li:container:petshop_west', 'petshop_west'); + const options: FilterValueOption[] = [ + container('urn:li:container:east_store', 'store', [petshopEast]), + container('urn:li:container:west_store', 'store', [petshopWest]), + { urn: 'urn:li:tag:petshop_east', type: EntityType.Tag, name: 'petshop_tag' } as unknown as Entity, + ].map((entity) => ({ value: entity.urn, entity })); + + it('matches containers on their whole path and other entities on their name', () => { + const { result } = renderHook(() => useFilterOptionsBySearchQuery(options, 'petshop_east')); + + expect(result.current.map((option) => option.value)).toEqual(['urn:li:container:east_store']); + }); +}); diff --git a/datahub-web-react/src/app/searchV2/filters/value/utils.tsx b/datahub-web-react/src/app/searchV2/filters/value/utils.tsx index 9f2706b4bb9d6..4b339cf7302aa 100644 --- a/datahub-web-react/src/app/searchV2/filters/value/utils.tsx +++ b/datahub-web-react/src/app/searchV2/filters/value/utils.tsx @@ -1,6 +1,7 @@ import { useMemo } from 'react'; import { getStructuredPropertyValue } from '@app/entityV2/shared/utils'; +import { getFullContainerPath } from '@app/searchV2/filters/containerDisambiguation'; import { EntityFilterField, FieldType, @@ -27,7 +28,7 @@ import { useGetAutoCompleteMultipleResultsQuery, useGetSearchResultsForMultipleQuery, } from '@graphql/search.generated'; -import { AllowedValue, AndFilterInput, EntityType, StructuredPropertyEntity } from '@types'; +import { AllowedValue, AndFilterInput, Entity, EntityType, StructuredPropertyEntity } from '@types'; const MAX_AGGREGATION_COUNT = 40; @@ -235,9 +236,12 @@ export const useFilterOptionsBySearchQuery = ( return options; } return options.filter((option) => { - const optionName = option.entity - ? entityRegistry.getDisplayName(option.entity.type, option.entity) - : option.displayName || option.value; + const getLabel = (entity: Entity) => entityRegistry.getDisplayName(entity.type, entity); + // Containers are matched on their whole path, so typing a parent shown in a disambiguated label + // ("petshop_east" in "petshop_east.store") keeps that option. + let optionName = option.displayName || option.value; + if (option.entity?.type === EntityType.Container) optionName = getFullContainerPath(option.entity, getLabel); + else if (option.entity) optionName = getLabel(option.entity); return filterOptionsWithSearch(searchQuery, optionName, []); }); }; diff --git a/e2e-test/ui/playwright/tests/search-container-filter/container-filter-disambiguation.spec.ts b/e2e-test/ui/playwright/tests/search-container-filter/container-filter-disambiguation.spec.ts new file mode 100644 index 0000000000000..b727a4086c48c --- /dev/null +++ b/e2e-test/ui/playwright/tests/search-container-filter/container-filter-disambiguation.spec.ts @@ -0,0 +1,64 @@ +/** + * Container filter options whose names repeat (a "store" schema in two databases) must be told apart + * in the value menu by the fewest parent containers that separate them; unique names stay plain. Long + * paths collapse their middle levels to an ellipsis that shows the hidden levels on hover. + */ + +import { expect, Page } from '@playwright/test'; + +import { test } from '../../fixtures/base-test'; +import { SearchPage } from '../../pages/search.page'; +import { GLOBAL_FEATURE_FLAGS } from '../../utils/test-feature-flags'; +import { DomainTag } from '../../utils/test-tags'; + +test.use({ featureName: 'search-container-filter' }); + +const INVENTORY_CONTAINER = 'urn:li:container:pw_cfd_east_inventory'; + +const option = (page: Page, name: RegExp) => page.getByTestId('filter-dropdown').getByRole('checkbox', { name }); + +test.describe('Container filter disambiguation', () => { + test.beforeEach(async ({ page, logger, logDir, apiMock }) => { + await apiMock.setFeatureFlags(GLOBAL_FEATURE_FLAGS); + const searchPage = new SearchPage(page, logger, logDir); + await searchPage.navigateToHome(); + await page.goto( + `/search?query=kibblecart&filter_container___false___EQUAL___0=${encodeURIComponent(INVENTORY_CONTAINER)}`, + ); + await page.getByTestId(`active-filter-value-container-${INVENTORY_CONTAINER}`).click(); + await expect(option(page, /inventory/)).toBeVisible(); + }); + + test('same-named containers show the parent that separates them', { tag: [DomainTag.Catalog] }, async ({ page }) => { + await expect(option(page, /petshop_east\.store/)).toHaveCount(1); + await expect(option(page, /petshop_west\.store/)).toHaveCount(1); + }); + + test( + 'containers that share a parent name show the level that separates them', + { tag: [DomainTag.Catalog] }, + async ({ page }) => { + await expect(option(page, /lakehouse_a\.\u2026\.supply/)).toHaveCount(1); + await expect(option(page, /lakehouse_b\.\u2026\.supply/)).toHaveCount(1); + }, + ); + + test('a container with a unique name keeps its plain name', { tag: [DomainTag.Catalog] }, async ({ page }) => { + await expect(option(page, /inventory/)).toHaveCount(1); + await expect(option(page, /petshop_east\.inventory/)).toHaveCount(0); + }); + + test( + 'hovering a collapsed path shows the levels it hides, and the full path elsewhere', + { tag: [DomainTag.Catalog] }, + async ({ page }) => { + const path = page.getByTestId('filter-option-container-path').filter({ hasText: 'lakehouse_a' }); + + await path.getByTestId('filter-option-container-path-elided').hover(); + await expect(page.getByRole('tooltip')).toHaveText('petshop_central'); + + await path.getByText('supply').hover(); + await expect(page.getByRole('tooltip')).toHaveText('lakehouse_a.petshop_central.supply'); + }, + ); +}); diff --git a/e2e-test/ui/playwright/tests/search-container-filter/fixtures/data.json b/e2e-test/ui/playwright/tests/search-container-filter/fixtures/data.json new file mode 100644 index 0000000000000..dd896035e0593 --- /dev/null +++ b/e2e-test/ui/playwright/tests/search-container-filter/fixtures/data.json @@ -0,0 +1,579 @@ +[ + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_petshop_east", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "petshop_east" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_petshop_east", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Database" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_petshop_east", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_petshop_west", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "petshop_west" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_petshop_west", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Database" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_petshop_west", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_store", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "store" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_store", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Schema" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_store", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_store", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_petshop_east" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_west_store", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "store" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_west_store", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Schema" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_west_store", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_west_store", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_petshop_west" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_inventory", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "inventory" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_inventory", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Schema" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_inventory", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_east_inventory", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_petshop_east" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_lakehouse_a", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "lakehouse_a" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_lakehouse_a", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Catalog" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_lakehouse_a", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_lakehouse_b", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "lakehouse_b" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_lakehouse_b", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Catalog" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_lakehouse_b", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_central", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "petshop_central" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_central", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Database" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_central", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_central", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_lakehouse_a" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_central", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "petshop_central" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_central", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Database" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_central", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_central", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_lakehouse_b" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_supply", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "supply" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_supply", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Schema" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_supply", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_a_supply", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_a_central" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_supply", + "changeType": "UPSERT", + "aspectName": "containerProperties", + "aspect": { + "json": { + "name": "supply" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_supply", + "changeType": "UPSERT", + "aspectName": "subTypes", + "aspect": { + "json": { + "typeNames": [ + "Schema" + ] + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_supply", + "changeType": "UPSERT", + "aspectName": "dataPlatformInstance", + "aspect": { + "json": { + "platform": "urn:li:dataPlatform:hive" + } + } + }, + { + "entityType": "container", + "entityUrn": "urn:li:container:pw_cfd_b_supply", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_b_central" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,petshop_east.store.kibblecart_orders,PROD)", + "changeType": "UPSERT", + "aspectName": "datasetProperties", + "aspect": { + "json": { + "name": "kibblecart_orders", + "description": "Kibblecart sample table for the container filter tests" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,petshop_east.store.kibblecart_orders,PROD)", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_east_store" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,petshop_west.store.kibblecart_orders,PROD)", + "changeType": "UPSERT", + "aspectName": "datasetProperties", + "aspect": { + "json": { + "name": "kibblecart_orders", + "description": "Kibblecart sample table for the container filter tests" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,petshop_west.store.kibblecart_orders,PROD)", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_west_store" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,petshop_east.inventory.kibblecart_stock,PROD)", + "changeType": "UPSERT", + "aspectName": "datasetProperties", + "aspect": { + "json": { + "name": "kibblecart_stock", + "description": "Kibblecart sample table for the container filter tests" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,petshop_east.inventory.kibblecart_stock,PROD)", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_east_inventory" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,lakehouse_a.petshop_central.supply.kibblecart_vendors,PROD)", + "changeType": "UPSERT", + "aspectName": "datasetProperties", + "aspect": { + "json": { + "name": "kibblecart_vendors", + "description": "Kibblecart sample table for the container filter tests" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,lakehouse_a.petshop_central.supply.kibblecart_vendors,PROD)", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_a_supply" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,lakehouse_b.petshop_central.supply.kibblecart_vendors,PROD)", + "changeType": "UPSERT", + "aspectName": "datasetProperties", + "aspect": { + "json": { + "name": "kibblecart_vendors", + "description": "Kibblecart sample table for the container filter tests" + } + } + }, + { + "entityType": "dataset", + "entityUrn": "urn:li:dataset:(urn:li:dataPlatform:hive,lakehouse_b.petshop_central.supply.kibblecart_vendors,PROD)", + "changeType": "UPSERT", + "aspectName": "container", + "aspect": { + "json": { + "container": "urn:li:container:pw_cfd_b_supply" + } + } + } +] \ No newline at end of file