diff --git a/.changeset/quiet-filters-hide.md b/.changeset/quiet-filters-hide.md
new file mode 100644
index 0000000000..c3bb2ea08d
--- /dev/null
+++ b/.changeset/quiet-filters-hide.md
@@ -0,0 +1,6 @@
+---
+'@hyperdx/app': patch
+'@hyperdx/common-utils': patch
+---
+
+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 20d1d9edb0..08c35d32da 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,
@@ -629,6 +641,22 @@ 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 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;
@@ -682,11 +710,11 @@ const FilterGroupBody = ({
onSharedPinClick ? () => onSharedPinClick(option.value) : undefined
}
isPercentageLoading={isFetchingDistribution}
- percentage={
- showDistributions && distributionData
- ? (distributionData.get(option.value.toString()) ?? 0)
- : undefined
+ isPercentageExact={
+ selectedValues.included.size === 1 &&
+ selectedValues.included.has(option.value)
}
+ percentage={getPercentage(option.value)}
/>
))}
{optionsLoading ? (
@@ -799,7 +827,9 @@ function FilterGroupActions({
<>
({
.mockReturnValue({ data: undefined, isFetching: false, error: undefined }),
}));
+const mockDistribution = (data: Map) =>
+ jest.mocked(useGetValuesDistribution).mockReturnValue({
+ data,
+ isFetching: false,
+ error: null,
+ } as UseQueryResult