Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tricky-brooms-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hyperdx/common-utils": patch
"@hyperdx/app": patch
---

feat: Add percentages to filter values
7 changes: 5 additions & 2 deletions packages/app/src/DBSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
useDocumentVisibility,
} from '@mantine/hooks';
import { notifications } from '@mantine/notifications';
import { useIsFetching } from '@tanstack/react-query';
import { keepPreviousData, useIsFetching } from '@tanstack/react-query';
import { SortingState } from '@tanstack/react-table';
import CodeMirror from '@uiw/react-codemirror';

Expand Down Expand Up @@ -1099,7 +1099,10 @@ function DBSearchPage() {
}
}, [isReady, queryReady, isChartConfigLoading, onSearch]);

const { data: aliasMap } = useAliasMapFromChartConfig(dbSqlRowTableConfig);
const { data: aliasMap } = useAliasMapFromChartConfig(dbSqlRowTableConfig, {
placeholderData: keepPreviousData,

@pulpdrew pulpdrew Oct 8, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly fixes an existing bug where the aliasMap had not yet loaded, but configs that reference the empty aliasMap were queried, resulting in query errors due to missing aliases in the queries when an alias was included in the where filter. This would happen repeatedly during live mode, as aliasMap flipped back and forth between valid and invalid. With placeholder data, the invalid queries may happen once per change in the SELECT fields, but should work for all queries after that.

queryKey: ['aliasMap', dbSqlRowTableConfig, 'withPlaceholder'],
});

const aliasWith = useMemo(
() =>
Expand Down
138 changes: 131 additions & 7 deletions packages/app/src/components/DBSearchPageFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { useExplainQuery } from '@/hooks/useExplainQuery';
import {
useAllFields,
useGetKeyValues,
useGetValuesDistribution,
useJsonColumns,
useTableMetadata,
} from '@/hooks/useMetadata';
Expand Down Expand Up @@ -76,6 +77,8 @@ type FilterCheckboxProps = {
onClickExclude?: VoidFunction;
onClickPin: VoidFunction;
className?: string;
percentage?: number;
isPercentageLoading?: boolean;
};

export const TextButton = ({
Expand Down Expand Up @@ -105,6 +108,26 @@ export const TextButton = ({
);
};

type FilterPercentageProps = {
percentage: number;
isLoading?: boolean;
};

const FilterPercentage = ({ percentage, isLoading }: FilterPercentageProps) => {
const formattedPercentage =
percentage < 1
? `<1%`
: percentage >= 99.5
? `>99%`
: `~${Math.round(percentage)}%`;

return (
<Text size="xs" c="gray.3" className={isLoading ? 'effect-pulse' : ''}>
{formattedPercentage}
</Text>
);
};

const emptyFn = () => {};
export const FilterCheckbox = ({
value,
Expand All @@ -115,6 +138,8 @@ export const FilterCheckbox = ({
onClickExclude,
onClickPin,
className,
percentage,
isPercentageLoading,
}: FilterCheckboxProps) => {
return (
<div
Expand Down Expand Up @@ -146,15 +171,30 @@ export const FilterCheckbox = ({
fz="xxs"
color="gray"
>
<Text
size="xs"
c={value === 'excluded' ? 'red.4' : 'gray.3'}
truncate="end"
<Group
w="100%"
title={label}
gap="xs"
wrap="nowrap"
justify="space-between"
pe={'11px'}
miw={0}
>
{label}
</Text>
<Text
size="xs"
c={value === 'excluded' ? 'red.4' : 'gray.3'}
truncate="end"
flex={1}
title={label}
>
{label}
</Text>
{percentage != null && (
<FilterPercentage
percentage={percentage}
isLoading={isPercentageLoading}
/>
)}
</Group>
</Tooltip>
</Group>
<div className={classes.filterActions}>
Expand Down Expand Up @@ -208,6 +248,8 @@ export type FilterGroupProps = {
hasLoadedMore: boolean;
isDefaultExpanded?: boolean;
'data-testid'?: string;
chartConfig: ChartConfigWithDateRange;
isLive?: boolean;
};

const MAX_FILTER_GROUP_ITEMS = 10;
Expand All @@ -230,6 +272,8 @@ export const FilterGroup = ({
hasLoadedMore,
isDefaultExpanded,
'data-testid': dataTestId,
chartConfig,
isLive,
}: FilterGroupProps) => {
const [search, setSearch] = useState('');
// "Show More" button when there's lots of options
Expand All @@ -238,13 +282,60 @@ export const FilterGroup = ({
const [isExpanded, setExpanded] = useState(isDefaultExpanded ?? false);
// Track recently moved items for highlight animation
const [recentlyMoved, setRecentlyMoved] = useState<Set<string>>(new Set());
// Show what percentage of the data has each value
const [showDistributions, setShowDistributions] = useState(false);
// For live searches, don't refresh percentages when date range changes
const [dateRange, setDateRange] = useState<[Date, Date]>(
chartConfig.dateRange,
);

const toggleShowDistributions = () => {
if (!showDistributions) {
setExpanded(true);
setDateRange(chartConfig.dateRange);
}
setShowDistributions(prev => !prev);
};

useEffect(() => {
if (!isLive) {
setDateRange(chartConfig.dateRange);
}
}, [chartConfig.dateRange, isLive]);

useEffect(() => {
if (isDefaultExpanded) {
setExpanded(true);
}
}, [isDefaultExpanded]);

const {
data: distributionData,
isFetching: isFetchingDistribution,
error: distributionError,
} = useGetValuesDistribution(
{
chartConfig: { ...chartConfig, dateRange },
key: name,
limit: 100, // The 100 most common values are enough to find any values that are present in at least 1% of rows
},
{
enabled: showDistributions,
},
);

useEffect(() => {
if (distributionError) {
notifications.show({
color: 'red',
title: 'Error loading filter distribution',
message: distributionError?.message,
autoClose: 5000,
});
setShowDistributions(false);
}
}, [distributionError]);

const totalFiltersSize =
selectedValues.included.size + selectedValues.excluded.size;

Expand Down Expand Up @@ -292,6 +383,13 @@ export const FilterGroup = ({
if (aExcluded && !bExcluded) return -1;
if (!aExcluded && bExcluded) return 1;

// Then sort by estimated percentage of rows with this value, if available
const aPercentage = distributionData?.get(a.value) ?? 0;
const bPercentage = distributionData?.get(b.value) ?? 0;
if (aPercentage !== bPercentage) {
return bPercentage - aPercentage;
}

// Finally sort alphabetically/numerically
return a.value.localeCompare(b.value, undefined, { numeric: true });
});
Expand All @@ -310,6 +408,7 @@ export const FilterGroup = ({
augmentedOptions,
selectedValues,
totalFiltersSize,
distributionData,
]);

// Simple highlight animation when checkbox is checked
Expand Down Expand Up @@ -402,13 +501,30 @@ export const FilterGroup = ({
</Tooltip>
</Accordion.Control>
<Group gap="xxxs" wrap="nowrap">
<ActionIcon
size="xs"
variant="subtle"
color="gray"
onClick={toggleShowDistributions}
title={
showDistributions ? 'Hide distribution' : 'Show distribution'
}
data-testid={`toggle-distribution-button-${name}`}
aria-checked={showDistributions}
role="checkbox"
>
<i
className={`bi ${isFetchingDistribution ? 'spinner-border spinner-border-sm' : showDistributions ? 'bi-bar-chart-line-fill' : 'bi-bar-chart-line'}`}
/>
</ActionIcon>
{onFieldPinClick && (
<ActionIcon
size="xs"
variant="subtle"
color="gray"
onClick={onFieldPinClick}
title={isFieldPinned ? 'Unpin field' : 'Pin field'}
me={'4px'}
>
<i
className={`bi bi-pin-angle${isFieldPinned ? '-fill' : ''}`}
Expand Down Expand Up @@ -452,6 +568,12 @@ export const FilterGroup = ({
onClickOnly={() => onOnlyClick(option.value)}
onClickExclude={() => onExcludeClick(option.value)}
onClickPin={() => onPinClick(option.value)}
isPercentageLoading={isFetchingDistribution}
percentage={
showDistributions && distributionData
? (distributionData.get(option.value) ?? 0)
: undefined
}
/>
))}
{optionsLoading ? (
Expand Down Expand Up @@ -900,6 +1022,8 @@ const DBSearchPageFiltersComponent = ({
(filterState[facet.key].included.size > 0 ||
filterState[facet.key].excluded.size > 0))
}
chartConfig={chartConfig}
isLive={isLive}
/>
))}

Expand Down
Loading