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
5 changes: 5 additions & 0 deletions .changeset/green-needles-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperdx/app": patch
---

feat: Add group-by to the search page histogram
280 changes: 173 additions & 107 deletions packages/app/src/DBSearchPage.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ jest.mock('@/components/SearchInput/SearchWhereInput', () => ({
default: () => <div />,
getStoredLanguage: () => 'lucene',
}));
jest.mock('@/components/SearchGroupBy/SearchGroupBySelect', () => ({
SearchGroupBySelect: () => <div />,
}));
jest.mock('@/components/SearchPageActionBar', () => () => <div />);
jest.mock('@/components/SearchTotalCountChart', () => () => <div />);
jest.mock('@/components/Sources/SourceForm', () => ({
Expand Down
17 changes: 6 additions & 11 deletions packages/app/src/components/DBSearchPageFilters/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import { escapeFilterStateKeys, usePinnedFilters } from '@/searchFilters';
import { useSource } from '@/source';
import { mergePath } from '@/utils';

import { listRequestedFacets, toQuotedClickHouseKeyExpression } from './utils';
import {
listRequestedFacets,
sortLowCardinalityFirst,
toQuotedClickHouseKeyExpression,
} from './utils';

const INITIAL_LOAD_LIMIT = 20;

Expand Down Expand Up @@ -120,16 +124,7 @@ function useFacets({
return [];
}

const strings = allFields
.sort((a, b) => {
// First show low cardinality fields
const isLowCardinality = (type: string) =>
type.includes('LowCardinality');
return (
(isLowCardinality(b.type) ? 1 : 0) -
(isLowCardinality(a.type) ? 1 : 0)
);
})
const strings = sortLowCardinalityFirst(allFields)
.filter(
field => field.jsType && ['string'].includes(field.jsType),
// todo: add number type with sliders :D
Expand Down
17 changes: 16 additions & 1 deletion packages/app/src/components/DBSearchPageFilters/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Utility functions for parsing and grouping map-like field names

import SqlString from 'sqlstring';
import { parseKeyPath } from '@hyperdx/common-utils/dist/core/metadata';
import {
type Field,
parseKeyPath,
} from '@hyperdx/common-utils/dist/core/metadata';
import type { FilterState } from '@hyperdx/common-utils/dist/filters';

import type { Facet } from '@/hooks/useMetadata';
Expand Down Expand Up @@ -73,6 +76,18 @@ export function cleanClickHouseExpression(key: string): string {
return cleanKey;
}

const isLowCardinality = (type: string) => type.includes('LowCardinality');

// Low-cardinality fields first: they make the most useful facets and groupings.
export function sortLowCardinalityFirst<T extends Pick<Field, 'type'>>(
fields: T[],
): T[] {
return [...fields].sort(
(a, b) =>
Number(isLowCardinality(b.type)) - Number(isLowCardinality(a.type)),
);
}

// Parse map-like field names and extract the base name and property path
export function parseMapFieldName(
key: string,
Expand Down
70 changes: 70 additions & 0 deletions packages/app/src/components/SearchGroupBy/GroupByFieldList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useRef } from 'react';
import { Checkbox, ScrollArea, Text } from '@mantine/core';
import { useVirtualizer } from '@tanstack/react-virtual';

const ROW_HEIGHT_ESTIMATE = 26;

export function GroupByFieldList({
fields,
selected,
onToggle,
}: {
fields: string[];
selected: string[];
onToggle: (field: string) => void;
}) {
const viewportRef = useRef<HTMLDivElement>(null);
const virtualizer = useVirtualizer({
count: fields.length,
getScrollElement: () => viewportRef.current,
estimateSize: () => ROW_HEIGHT_ESTIMATE,
overscan: 10,
getItemKey: index => fields[index] ?? index,
});

if (fields.length === 0) {
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
return (
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
<Text size="xs" c="dimmed" py="xs" ta="center">
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
No fields found
</Text>
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
);
}

return (
<ScrollArea.Autosize mah={260} type="auto" viewportRef={viewportRef}>
<div
style={{
height: virtualizer.getTotalSize(),
width: '100%',
position: 'relative',
}}
>
{virtualizer.getVirtualItems().map(item => {
const field = fields[item.index];
return (
<div
key={item.key}
data-index={item.index}
ref={virtualizer.measureElement}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: `translateY(${item.start}px)`,
}}
>
<Checkbox
size="xs"
py={4}
label={field}
checked={selected.includes(field)}
onChange={() => onToggle(field)}
/>
</div>
);
})}
</div>
</ScrollArea.Autosize>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// The label sits in a filled addon so it reads as the frame's name rather than
// something to press; only the value side is the button.
.control {
display: flex;
align-items: center;
flex-shrink: 0;
overflow: hidden;
background: var(--color-bg-surface);
border: 1px solid var(--color-border);
border-radius: var(--mantine-radius-default);
}

.label {
align-self: stretch;
display: flex;
align-items: center;
padding-inline: 8px;
border-right: 1px solid var(--color-border);
background: var(--color-bg-muted);
white-space: nowrap;
}

.target {
display: flex;
align-items: center;
gap: 6px;
height: 28px;
padding-inline: 8px;
white-space: nowrap;

&:hover {
background: var(--color-bg-hover);
}
}

// Long expressions truncate rather than pushing the row's other controls off.
.value {
overflow: hidden;
max-width: 220px;
text-overflow: ellipsis;
}
Loading
Loading