From fa728fa1366c8f471ce2ae08b147dae56cfac2a2 Mon Sep 17 00:00:00 2001 From: Jordan Simonovski Date: Sat, 26 Sep 2026 08:35:54 +1000 Subject: [PATCH 1/2] fix(app): stop labelling values the field's own filter rules out as <1% --- .changeset/quiet-filters-hide.md | 5 ++ .../src/components/DBSearchPageFilters.tsx | 14 ++++- .../__tests__/DBSearchPageFilters.test.tsx | 58 +++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 .changeset/quiet-filters-hide.md diff --git a/.changeset/quiet-filters-hide.md b/.changeset/quiet-filters-hide.md new file mode 100644 index 0000000000..c94d77e558 --- /dev/null +++ b/.changeset/quiet-filters-hide.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +fix: Show distribution no longer labels values ruled out by the field's own filter as "<1%" diff --git a/packages/app/src/components/DBSearchPageFilters.tsx b/packages/app/src/components/DBSearchPageFilters.tsx index 67a154aae7..dc3477185a 100644 --- a/packages/app/src/components/DBSearchPageFilters.tsx +++ b/packages/app/src/components/DBSearchPageFilters.tsx @@ -629,6 +629,12 @@ const FilterGroupBody = ({ [onChange, selectedValues], ); + // The distribution query runs under this field's own filter, so a value the + // filter rules out has zero rows; showing it as "<1%" would read as rare. + const isExcludedByOwnFilter = (value: string | boolean) => + selectedValues.excluded.has(value) || + (selectedValues.included.size > 0 && !selectedValues.included.has(value)); + const isLimitingDisplayedItems = sortedMatchingOptions.length > displayedOptions.length; @@ -683,7 +689,9 @@ const FilterGroupBody = ({ } isPercentageLoading={isFetchingDistribution} percentage={ - showDistributions && distributionData + showDistributions && + distributionData && + !isExcludedByOwnFilter(option.value) ? (distributionData.get(option.value.toString()) ?? 0) : undefined } @@ -799,7 +807,9 @@ function FilterGroupActions({ <> { expect(labels[2]).toHaveTextContent('<1%'); // banana }); + it('should not show percentages for values the field filter rules out', async () => { + jest.mocked(useGetValuesDistribution).mockReturnValue({ + data: new Map([['apple', 100]]), + isFetching: false, + error: null, + } as UseQueryResult>); + + renderWithMantine( + , + ); + + await userEvent.click( + screen.getByTestId('toggle-distribution-button-Test Filter'), + ); + + const labels = screen.getAllByText(/%/); + expect(labels).toHaveLength(1); + expect(labels[0]).toHaveTextContent('>99%'); + }); + + it('should not show a percentage for an excluded value', async () => { + jest.mocked(useGetValuesDistribution).mockReturnValue({ + data: new Map([ + ['apple', 60], + ['banana', 40], + ]), + isFetching: false, + error: null, + } as UseQueryResult>); + + renderWithMantine( + , + ); + + await userEvent.click( + screen.getByTestId('toggle-distribution-button-Test Filter'), + ); + + expect( + screen.queryByTestId('filter-distribution-Test Filter-zebra'), + ).not.toBeInTheDocument(); + expect( + screen.getByTestId('filter-distribution-Test Filter-apple'), + ).toHaveTextContent('~60%'); + }); + it('should handle excluded items', () => { renderWithMantine( Date: Sat, 26 Sep 2026 08:54:26 +1000 Subject: [PATCH 2/2] fix(app): only show <1% and 100% when the data backs them --- .changeset/quiet-filters-hide.md | 3 +- .../src/components/DBSearchPageFilters.tsx | 36 ++++-- .../__tests__/DBSearchPageFilters.test.tsx | 116 ++++++++++++++---- .../src/__tests__/metadata.test.ts | 25 ++++ packages/common-utils/src/core/metadata.ts | 5 +- 5 files changed, 151 insertions(+), 34 deletions(-) diff --git a/.changeset/quiet-filters-hide.md b/.changeset/quiet-filters-hide.md index c94d77e558..c3bb2ea08d 100644 --- a/.changeset/quiet-filters-hide.md +++ b/.changeset/quiet-filters-hide.md @@ -1,5 +1,6 @@ --- '@hyperdx/app': patch +'@hyperdx/common-utils': patch --- -fix: Show distribution no longer labels values ruled out by the field's own filter as "<1%" +fix: Show distribution only labels a value "<1%" when the sample shows it is rare, not when it has no matching rows, and shows 100% when the field's filter allows only that value diff --git a/packages/app/src/components/DBSearchPageFilters.tsx b/packages/app/src/components/DBSearchPageFilters.tsx index dc3477185a..c26942b647 100644 --- a/packages/app/src/components/DBSearchPageFilters.tsx +++ b/packages/app/src/components/DBSearchPageFilters.tsx @@ -88,6 +88,9 @@ const INITIAL_MAX_VALUES_DISPLAYED = 10; /* The maximum number of values per filter to render at once after loading more */ const SHOW_MORE_MAX_VALUES_DISPLAYED = 50; +/* The 100 most common values are enough to find any value present in at least 1% of rows */ +const DISTRIBUTION_VALUE_LIMIT = 100; + // This function will clean json string attributes specifically. It will turn a string like // 'toString(ResourceAttributes.`hdx`.`sdk`.`version`)' into 'ResourceAttributes.hdx.sdk.version'. export function cleanedFacetName(key: string): string { @@ -133,6 +136,7 @@ type FilterCheckboxProps = { className?: string; percentage?: number; isPercentageLoading?: boolean; + isPercentageExact?: boolean; }; const TextButton = ({ @@ -165,19 +169,24 @@ const TextButton = ({ type FilterPercentageProps = { percentage: number; isLoading?: boolean; + // Share is guaranteed by the filter rather than estimated from the sample. + isExact?: boolean; 'data-testid'?: string; }; const FilterPercentage = ({ percentage, isLoading, + isExact, 'data-testid': dataTestId, }: FilterPercentageProps) => { const formattedPercentage = percentage < 1 ? `<1%` : percentage >= 99.5 - ? `>99%` + ? isExact + ? '100%' + : `>99%` : `~${Math.round(percentage)}%`; return ( @@ -205,6 +214,7 @@ const FilterCheckbox = ({ className, percentage, isPercentageLoading, + isPercentageExact, }: FilterCheckboxProps) => { const [pinMenuOpened, setPinMenuOpened] = useState(false); const testIdPrefix = `filter-checkbox-${columnName}-${label}`; @@ -268,6 +278,7 @@ const FilterCheckbox = ({ )} @@ -492,7 +503,8 @@ const FilterGroupBody = ({ { chartConfig: { ...chartConfig, dateRange }, key: distributionKey || name, - limit: 100, // The 100 most common values are enough to find any values that are present in at least 1% of rows + // One extra row tells a capped result apart from exactly 100 values + limit: DISTRIBUTION_VALUE_LIMIT + 1, }, { enabled: showDistributions, @@ -635,6 +647,16 @@ const FilterGroupBody = ({ selectedValues.excluded.has(value) || (selectedValues.included.size > 0 && !selectedValues.included.has(value)); + const getPercentage = (value: string | boolean) => { + if (!showDistributions || !distributionData || isExcludedByOwnFilter(value)) + return undefined; + const percentage = distributionData.get(value.toString()); + if (percentage != null) return percentage; + // Missing from a capped result means it ranks below the 100th value, so + // under 1%. Missing from an uncapped result means it wasn't seen at all. + return distributionData.size > DISTRIBUTION_VALUE_LIMIT ? 0 : undefined; + }; + const isLimitingDisplayedItems = sortedMatchingOptions.length > displayedOptions.length; @@ -688,13 +710,11 @@ const FilterGroupBody = ({ onSharedPinClick ? () => onSharedPinClick(option.value) : undefined } isPercentageLoading={isFetchingDistribution} - percentage={ - showDistributions && - distributionData && - !isExcludedByOwnFilter(option.value) - ? (distributionData.get(option.value.toString()) ?? 0) - : undefined + isPercentageExact={ + selectedValues.included.size === 1 && + selectedValues.included.has(option.value) } + percentage={getPercentage(option.value)} /> ))} {optionsLoading ? ( diff --git a/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx b/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx index 2ba924b37f..092b58141c 100644 --- a/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx +++ b/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx @@ -55,6 +55,13 @@ jest.mock('@/hooks/useMetadata', () => ({ .mockReturnValue({ data: undefined, isFetching: false, error: undefined }), })); +const mockDistribution = (data: Map) => + jest.mocked(useGetValuesDistribution).mockReturnValue({ + data, + isFetching: false, + error: null, + } as UseQueryResult>); + describe('cleanedFacetName', () => { describe('basic functionality', () => { it('should return non-toString strings unchanged', () => { @@ -441,15 +448,13 @@ describe('FilterGroup', () => { }); it('should show selected items first, then sort by counts, if percentages when they are enabled', () => { - jest.mocked(useGetValuesDistribution).mockReturnValue({ - data: new Map([ + mockDistribution( + new Map([ ['apple', 30], ['banana', 20], ['zebra', 50], ]), - isFetching: false, - error: null, - } as UseQueryResult>); + ); renderWithMantine( { }); it('should show percentages, if enabled', async () => { - jest.mocked(useGetValuesDistribution).mockReturnValue({ - data: new Map([ + mockDistribution( + new Map([ ['apple', 99.2], ['zebra', 0.6], ]), - isFetching: false, - error: null, - } as UseQueryResult>); + ); renderWithMantine( { const options = screen.getAllByTestId(/filter-checkbox-.+-input/); expect(options).toHaveLength(3); const labels = screen.getAllByText(/%/); + expect(labels).toHaveLength(2); expect(labels[0]).toHaveTextContent('~99%'); // apple expect(labels[1]).toHaveTextContent('<1%'); // zebra - expect(labels[2]).toHaveTextContent('<1%'); // banana + // banana is missing from an uncapped result, so it was not seen at all }); - it('should not show percentages for values the field filter rules out', async () => { - jest.mocked(useGetValuesDistribution).mockReturnValue({ - data: new Map([['apple', 100]]), - isFetching: false, - error: null, - } as UseQueryResult>); + it('should show <1% for a value missing from a capped result', async () => { + mockDistribution( + new Map( + Array.from({ length: 101 }, (_, i) => [`value-${i}`, 1] as const), + ), + ); + + renderWithMantine(); + + await userEvent.click( + screen.getByTestId('toggle-distribution-button-Test Filter'), + ); + + expect( + screen.getByTestId('filter-distribution-Test Filter-banana'), + ).toHaveTextContent('<1%'); + }); + + it("should run the distribution query under the field's own filter", async () => { + const filters = [ + { type: 'sql' as const, condition: "Test Filter IN ('apple')" }, + ]; + renderWithMantine( + , + ); + + await userEvent.click( + screen.getByTestId('toggle-distribution-button-Test Filter'), + ); + + expect(jest.mocked(useGetValuesDistribution)).toHaveBeenLastCalledWith( + expect.objectContaining({ + chartConfig: expect.objectContaining({ filters }), + }), + expect.objectContaining({ enabled: true }), + ); + }); + + it('should show 100% for the only value the field filter allows, and nothing for the rest', async () => { + mockDistribution(new Map([['apple', 100]])); renderWithMantine( { const labels = screen.getAllByText(/%/); expect(labels).toHaveLength(1); - expect(labels[0]).toHaveTextContent('>99%'); + expect(labels[0]).toHaveTextContent('100%'); + }); + + it('should keep estimated percentages when the field filter allows several values', async () => { + mockDistribution( + new Map([ + ['apple', 99.7], + ['banana', 0.3], + ]), + ); + + renderWithMantine( + , + ); + + await userEvent.click( + screen.getByTestId('toggle-distribution-button-Test Filter'), + ); + + expect( + screen.getByTestId('filter-distribution-Test Filter-apple'), + ).toHaveTextContent('>99%'); }); it('should not show a percentage for an excluded value', async () => { - jest.mocked(useGetValuesDistribution).mockReturnValue({ - data: new Map([ + mockDistribution( + new Map([ ['apple', 60], ['banana', 40], ]), - isFetching: false, - error: null, - } as UseQueryResult>); + ); renderWithMantine( { ); }); + it('should key numeric and boolean values by their string form', async () => { + (mockClickhouseClient.query as jest.Mock).mockResolvedValue({ + json: () => + Promise.resolve({ + data: [ + { __hdx_value: 200, __hdx_percentage: 90 }, + { __hdx_value: false, __hdx_percentage: 10 }, + ], + }), + }); + + const result = await metadata.getValuesDistribution({ + chartConfig: mockChartConfig, + key: 'status', + source, + }); + + expect(result).toEqual( + new Map([ + ['200', 90], + ['false', 10], + ]), + ); + }); + it('should include alias CTEs when provided in the config', async () => { const configWithAliases = { ...mockChartConfig, diff --git a/packages/common-utils/src/core/metadata.ts b/packages/common-utils/src/core/metadata.ts index 80f5c3ebcd..52511d20f1 100644 --- a/packages/common-utils/src/core/metadata.ts +++ b/packages/common-utils/src/core/metadata.ts @@ -2363,14 +2363,15 @@ export class Metadata { }) .then(res => res.json<{ - __hdx_value: string; + __hdx_value: string | number | boolean; __hdx_percentage: string | number; }>(), ); return new Map( + // ClickHouse JSON returns numbers and booleans unquoted json.data.map(({ __hdx_value, __hdx_percentage }) => [ - __hdx_value, + String(__hdx_value), Number(__hdx_percentage), ]), );