diff --git a/.changeset/search-columns-sort-popovers.md b/.changeset/search-columns-sort-popovers.md new file mode 100644 index 0000000000..6bd0e8ed0e --- /dev/null +++ b/.changeset/search-columns-sort-popovers.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +feat: Edit search SELECT and ORDER BY from Columns and Sort popovers on the results table, and tidy the table controls row diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index 9f5f62522c..88d515a8a3 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -112,10 +112,12 @@ import SearchWhereInput, { getStoredLanguage, } from '@/components/SearchInput/SearchWhereInput'; import SearchPageActionBar from '@/components/SearchPageActionBar'; +import { ColumnsPopover } from '@/components/SearchResultsToolbar/ColumnsPopover'; +import { SortPopover } from '@/components/SearchResultsToolbar/SortPopover'; +import { useClauseFieldOptions } from '@/components/SearchResultsToolbar/useClauseFieldOptions'; import SearchTotalCountChart from '@/components/SearchTotalCountChart'; import { TableSourceForm } from '@/components/Sources/SourceForm'; import { SourceSelectControlled } from '@/components/SourceSelect'; -import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; import { Tags } from '@/components/Tags'; import { TimePicker } from '@/components/TimePicker'; import { IS_LOCAL_MODE } from '@/config'; @@ -1146,9 +1148,6 @@ function DBSearchPageContent() { // key from a phone would collapse the sidebar the next time the same // browser opens the page on a desktop. const [isCompactFiltersOpen, setIsCompactFiltersOpen] = useState(false); - // SELECT grows with its text. On a phone that pushes the logs off the - // screen, so the column editors stay closed until the user asks. - const [columnsOpen, setColumnsOpen] = useState(false); const isCompact = useCompactViewport(); const filtersCollapsed = isCompact ? !isCompactFiltersOpen @@ -2171,6 +2170,15 @@ function DBSearchPageContent() { [inputSourceObj], ); + const inputSelect = useWatch({ name: 'select', control }); + const inputOrderBy = useWatch({ name: 'orderBy', control }); + const clauseFieldOptions = useClauseFieldOptions({ + tableConnection: inputSourceTableConnection, + columns: inputSourceColumns, + dateRange: searchedTimeRange, + timestampValueExpression: inputSourceObj?.timestampValueExpression, + }); + const [isSourceSchemaPreviewOpen, setIsSourceSchemaPreviewOpen] = useState(false); @@ -2293,51 +2301,57 @@ function DBSearchPageContent() { [setValue, setNewSourceModalClosed], ); - const columnFields = (allowMultiline: boolean, stacked: boolean) => { - const select = ( - - ); - const orderBy = ( - - ); - if (stacked) { - return ( - <> - {select} - {orderBy} - - ); - } - return ( - <> - {select} - {orderBy} - - ); - }; + const applySelect = useCallback( + (select: string, removedItems: string[]) => { + setValue('select', select); + let orderBy = getValues('orderBy') ?? ''; + for (const item of removedItems) { + orderBy = orderByAfterRemovingSelectItem( + item, + orderBy, + defaultSearchConfig.orderBy ?? '', + ); + } + setValue('orderBy', orderBy); + onSubmit(); + }, + [setValue, getValues, defaultSearchConfig.orderBy, onSubmit], + ); + + const applyOrderBy = useCallback( + (orderBy: string) => { + setValue('orderBy', orderBy); + onSubmit(); + }, + [setValue, onSubmit], + ); + + // SELECT and ORDER BY shape the results table, so they sit on that table + // and stay hidden on Event Deltas and Event Patterns. + const resultsClauseEditors = + analysisMode === 'results' ? ( + + + + + ) : null; return ( - setIsSourceSchemaPreviewOpen(false)} - /> - {!isCompact && columnFields(true, false)} - <> + {!savedSearchId ? ( @@ -2525,7 +2533,6 @@ function DBSearchPageContent() { onClick={() => { setSaveSearchModalState('update'); }} - style={{ flexShrink: 0 }} > Update @@ -2536,7 +2543,6 @@ function DBSearchPageContent() { variant="secondary" size="xs" onClick={openAlertModal} - style={{ flexShrink: 0 }} > Alerts @@ -2544,74 +2550,53 @@ function DBSearchPageContent() { )} - {isCompact && ( - - )} - + - {isCompact && columnsOpen && ( - - {columnFields(false, true)} - - )} - - - + + + - + + + {isLive && ( @@ -2635,6 +2620,22 @@ function DBSearchPageContent() { + setIsSourceSchemaPreviewOpen(false)} + /> + + {!queryReady ? ( - } - title="No data to display" - description="Select a source and click the play button to query data." - /> + + {resultsClauseEditors != null && ( + + {resultsClauseEditors} + + )} + } + title="No data to display" + description="Select a source and click the play button to query data." + /> + ) : ( <> + {resultsClauseEditors != null && ( + + {resultsClauseEditors} + + )}
{whereSuggestions && whereSuggestions.length > 0 && ( @@ -3031,6 +3044,8 @@ function DBSearchPageContent() { selectionResetKey={selectionResetKey} onSelectedRowsChange={onSelectedRowsChange} onResolvedColumnsChange={onResolvedColumnsChange} + toolbarLeading={resultsClauseEditors} + controlsAboveTable /> )} diff --git a/packages/app/src/__tests__/DBSearchPage.directTrace.test.tsx b/packages/app/src/__tests__/DBSearchPage.directTrace.test.tsx index 5b335588ff..4e7cf0e478 100644 --- a/packages/app/src/__tests__/DBSearchPage.directTrace.test.tsx +++ b/packages/app/src/__tests__/DBSearchPage.directTrace.test.tsx @@ -196,6 +196,15 @@ jest.mock('@/components/SearchGroupBy/SearchGroupBySelect', () => ({ SearchGroupBySelect: () =>
, })); jest.mock('@/components/SearchPageActionBar', () => () =>
); +jest.mock('@/components/SearchResultsToolbar/ColumnsPopover', () => ({ + ColumnsPopover: () =>
, +})); +jest.mock('@/components/SearchResultsToolbar/SortPopover', () => ({ + SortPopover: () =>
, +})); +jest.mock('@/components/SearchResultsToolbar/useClauseFieldOptions', () => ({ + useClauseFieldOptions: () => [], +})); jest.mock('@/components/SearchTotalCountChart', () => () =>
); jest.mock('@/components/Sources/SourceForm', () => ({ TableSourceForm: () =>
, diff --git a/packages/app/src/__tests__/utils.test.ts b/packages/app/src/__tests__/utils.test.ts index 864d5ba1b2..92774a7183 100644 --- a/packages/app/src/__tests__/utils.test.ts +++ b/packages/app/src/__tests__/utils.test.ts @@ -30,6 +30,7 @@ import { parseTimestampToMs, resolveConditionalColor, sortingStateToOrderByString, + splitClauseList, stripTrailingSlash, useQueryHistory, withMapKeyAlias, @@ -1506,6 +1507,16 @@ describe('isColumnInSelect', () => { }); }); +describe('splitClauseList', () => { + it('keeps a comma inside a backtick-quoted name', () => { + expect(splitClauseList('Timestamp, `a,b`, Body')).toEqual([ + 'Timestamp', + '`a,b`', + 'Body', + ]); + }); +}); + describe('orderByAfterRemovingSelectItem', () => { const item = `ResourceAttributes['service.name'] AS "service.name"`; @@ -1534,6 +1545,33 @@ describe('orderByAfterRemovingSelectItem', () => { orderByAfterRemovingSelectItem('Body', 'Body DESC', 'Timestamp DESC'), ).toBe('Body DESC'); }); + + it('drops only the removed alias from a multi-field sort', () => { + expect( + orderByAfterRemovingSelectItem( + item, + '"service.name" ASC, Timestamp DESC', + 'Timestamp DESC', + ), + ).toBe('Timestamp DESC'); + expect( + orderByAfterRemovingSelectItem( + item, + '"http.method" DESC, "service.name" ASC', + 'Timestamp DESC', + ), + ).toBe('"http.method" DESC'); + }); + + it('keeps a comma inside a backtick-quoted sort name', () => { + expect( + orderByAfterRemovingSelectItem( + 'upper(Body) AS "message"', + '"message" ASC, `a,b` DESC', + 'Timestamp DESC', + ), + ).toBe('`a,b` DESC'); + }); }); describe('getLogSeverityLevel', () => { diff --git a/packages/app/src/components/DBRowTable.tsx b/packages/app/src/components/DBRowTable.tsx index 6e5885b4a8..0fb7b7d0ca 100644 --- a/packages/app/src/components/DBRowTable.tsx +++ b/packages/app/src/components/DBRowTable.tsx @@ -40,13 +40,13 @@ import { TSource, } from '@hyperdx/common-utils/dist/types'; import { + ActionIcon, Box, Flex, Group, Modal, Text, Tooltip as MantineTooltip, - UnstyledButton, } from '@mantine/core'; import { IconCode, @@ -389,6 +389,8 @@ export const RawLogTable = memo( getRowWhere, variant = 'default', onRemoveColumn, + toolbarLeading, + controlsAboveTable = false, }: { wrapLines?: boolean; displayedColumns: string[]; @@ -432,6 +434,10 @@ export const RawLogTable = memo( getRowWhere?: (row: Record) => RowWhereResult; variant?: DBRowTableVariant; onRemoveColumn?: (column: string) => void; + /** Rendered on the left of the bar above the table. */ + toolbarLeading?: React.ReactNode; + /** Keep the header actions above the table instead of in the last column. */ + controlsAboveTable?: boolean; }) => { const dedupedRows = useMemo(() => { const lIds = new Set(); @@ -1031,8 +1037,107 @@ export const RawLogTable = memo( [], ); + // Above the scroll area, so a wide table can't scroll these out of view. + const wrapLinesLabel = wrapLinesEnabled ? 'Unwrap lines' : 'Wrap lines'; + const csvLabel = `Download as CSV (max ${maxRows.toLocaleString()} rows)${isLimited ? ', data truncated' : ''}`; + const rowSelectionMenu = enableRowSelection ? ( + + ) : null; + + const tableControls = ( + + {tableId && Object.keys(columnSizeStorage).length > 0 && ( + + setColumnSizeStorage({})} + aria-label="Reset column widths" + > + + + + )} + + setWrapLinesEnabled(prev => !prev)} + aria-label={wrapLinesLabel} + > + {wrapLinesEnabled ? ( + + ) : ( + + )} + + + {config && ( + + handleSqlModalOpen(true)} + aria-label="Show generated SQL" + > + + + + )} + + + + + + + + {onSettingsClick != null && ( + + onSettingsClick()} + aria-label="Settings" + > + + + + )} + {!controlsAboveTable && rowSelectionMenu} + + ); + return ( + {(controlsAboveTable || toolbarLeading != null) && ( + + + {toolbarLeading} + {controlsAboveTable && rowSelectionMenu} + + {controlsAboveTable && ( + + {tableControls} + + )} + + )} {/* Find within page search bar - floating on top right */} - {tableId && - Object.keys(columnSizeStorage).length > 0 && ( - setColumnSizeStorage({})} - title="Reset Column Widths" - display="flex" - > - - - - - )} - {config && ( - handleSqlModalOpen(true)} - title="Show Generated SQL" - tabIndex={0} - display="flex" - > - - - - - )} - - setWrapLinesEnabled(prev => !prev) - } - title={`${wrapLinesEnabled ? 'Disable' : 'Enable'} Wrap Lines`} - display="flex" - > - - {wrapLinesEnabled ? ( - - ) : ( - - )} - - - - - - - - - {onSettingsClick != null && ( - onSettingsClick()} - title="Settings" - display="flex" - > - - - - - )} - {enableRowSelection && ( - - )} - + controlsAboveTable ? undefined : tableControls } /> ); @@ -1677,6 +1702,8 @@ function DBSqlRowTableComponent({ tableId, errorVariant, onResolvedColumnsChange, + toolbarLeading, + controlsAboveTable, }: { config: BuilderChartConfigWithDateRange; sourceId?: string; @@ -1710,6 +1737,8 @@ function DBSqlRowTableComponent({ tableId?: string; errorVariant?: ChartErrorStateVariant; onResolvedColumnsChange?: (meta: ColumnMetaType[]) => void; + toolbarLeading?: React.ReactNode; + controlsAboveTable?: boolean; }) { const { data: me } = api.useMe(); const { toggleColumn, displayedColumns: contextDisplayedColumns } = @@ -2033,6 +2062,8 @@ function DBSqlRowTableComponent({ variant={variant} onRemoveColumn={toggleColumn ? onRemoveColumnFromTable : undefined} tableId={tableId} + toolbarLeading={toolbarLeading} + controlsAboveTable={controlsAboveTable} /> ); diff --git a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx index c683786711..bdb6b34820 100644 --- a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx +++ b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx @@ -1,4 +1,4 @@ -import { useCallback, useState } from 'react'; +import { type ReactNode, useCallback, useState } from 'react'; import { useQueryState } from 'nuqs'; import { ClickHouseQueryError, @@ -50,6 +50,8 @@ interface Props { selectionResetKey?: string; onSelectedRowsChange?: (hasSelectedRows: boolean) => void; onResolvedColumnsChange?: (meta: ColumnMetaType[]) => void; + toolbarLeading?: ReactNode; + controlsAboveTable?: boolean; // Clicking outside the row side panel (and outside `keepOpenSelector`) closes // it. Enabled by default; pass `false` to opt out. closeOnClickOutside?: boolean; @@ -82,6 +84,8 @@ export default function DBSqlRowTableWithSideBar({ selectionResetKey, onSelectedRowsChange, onResolvedColumnsChange, + toolbarLeading, + controlsAboveTable, closeOnClickOutside = true, keepOpenSelector = DEFAULT_KEEP_OPEN_SELECTOR, }: Props) { @@ -163,6 +167,8 @@ export default function DBSqlRowTableWithSideBar({ selectionResetKey={selectionResetKey} onSelectedRowsChange={onSelectedRowsChange} onResolvedColumnsChange={onResolvedColumnsChange} + toolbarLeading={toolbarLeading} + controlsAboveTable={controlsAboveTable} /> ); diff --git a/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx b/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx index 6794ad77fb..d90cf8d5d6 100644 --- a/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx +++ b/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx @@ -6,12 +6,13 @@ import { Group, Popover, SegmentedControl, - Text, TextInput, - UnstyledButton, } from '@mantine/core'; -import { IconChevronDown } from '@tabler/icons-react'; +import { + fieldListSummary, + SplitValueButton, +} from '@/components/SplitValueButton/SplitValueButton'; import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; import { GroupByFieldList } from './GroupByFieldList'; @@ -23,16 +24,8 @@ import { } from './searchGroupBy'; import { useGroupByKeyOptions } from './useGroupByKeyOptions'; -import classes from './SearchGroupBySelect.module.scss'; - type Mode = 'fields' | 'sql'; -function triggerLabel(fields: string[]) { - if (fields.length === 0) return 'none'; - if (fields.length === 1) return fields[0]; - return `${fields.length} fields`; -} - export function SearchGroupBySelect({ source, value, @@ -112,8 +105,6 @@ export function SearchGroupBySelect({ prev.includes(field) ? prev.filter(f => f !== field) : [...prev, field], ); - const label = triggerLabel(appliedFields); - return ( -
- - Group by - - - handleOpenChange(!opened)} - data-testid="search-group-by-button" - aria-label={`Group by ${label}`} - > - - {label} - - - - -
+ + handleOpenChange(!opened)} + data-testid="search-group-by-button" + /> + void; + onApply: () => void; + /** Restore the source default in the draft. Apply still has to confirm it. */ + onReset: () => void; + resetDisabled?: boolean; + renderPick: (apply: () => void) => ReactNode; + renderSql: (apply: () => void) => ReactNode; + 'data-testid': string; +}) { + const [opened, setOpened] = useState(false); + const [mode, setMode] = useState(initialMode); + + const open = () => { + onOpen(); + setMode(initialMode); + setOpened(true); + }; + + const apply = () => { + onApply(); + setOpened(false); + }; + + return ( + + + (opened ? setOpened(false) : open())} + data-testid={testId} + /> + + + + setMode(value === 'sql' ? 'sql' : 'pick')} + data={MODE_OPTIONS} + data-testid={`${testId}-mode`} + /> + {mode === 'pick' ? renderPick(apply) : renderSql(apply)} + + + + + + + + + + + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx new file mode 100644 index 0000000000..375a0e5dcf --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/ColumnsPopover.tsx @@ -0,0 +1,226 @@ +import { useMemo, useRef, useState } from 'react'; +import { + DndContext, + type DragEndEvent, + MouseSensor, + TouchSensor, + useSensor, + useSensors, +} from '@dnd-kit/core'; +import { + arrayMove, + SortableContext, + verticalListSortingStrategy, +} from '@dnd-kit/sortable'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; +import { + Autocomplete, + Box, + Button, + Code, + Group, + Stack, + Text, +} from '@mantine/core'; + +import { fieldListSummary } from '@/components/SplitValueButton/SplitValueButton'; +import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; +import { splitClauseList, withMapKeyAlias } from '@/utils'; + +import { ClausePopover } from './ClausePopover'; +import { SortableColumn } from './SortableColumn'; + +const columnId = (item: string, index: number) => `${index}:${item}`; + +export function ColumnsPopover({ + value, + defaultValue, + fieldOptions, + knownColumns, + onApply, + tableConnection, + sourceId, + dateRange, +}: { + /** The SELECT expression in the search form. */ + value: string; + /** The source's SELECT, used while `value` is empty. */ + defaultValue: string; + fieldOptions: string[]; + knownColumns: Set; + /** Called with the new SELECT and the items it no longer contains. */ + onApply: (select: string, removedItems: string[]) => void; + tableConnection: TableConnection | undefined; + sourceId?: string; + dateRange?: [Date, Date]; +}) { + const [draft, setDraft] = useState(value); + const [search, setSearch] = useState(''); + // Autocomplete writes the chosen option back into the input after + // onOptionSubmit. Ignore that write so a picked map key keeps its alias. + const ignoreNextChangeRef = useRef(false); + + // An empty stored SELECT means "use the source default". An empty draft does + // not: the user may have removed every column before adding a replacement. + const storedColumns = (select: string) => + splitClauseList(select || defaultValue); + + const shown = storedColumns(value); + const items = splitClauseList(draft); + + const options = useMemo( + () => fieldOptions.filter(option => !items.includes(option)), + [fieldOptions, items], + ); + + const addColumn = (column: string) => { + const trimmed = column.trim(); + if (trimmed && !items.includes(trimmed)) { + setDraft( + [...items, withMapKeyAlias(trimmed, items, knownColumns)].join(', '), + ); + } + setSearch(''); + }; + + const removeColumn = (item: string) => { + setDraft(items.filter(i => i !== item).join(', ')); + }; + + const columnIds = items.map(columnId); + const sensors = useSensors( + useSensor(MouseSensor, { activationConstraint: { distance: 8 } }), + useSensor(TouchSensor, { + activationConstraint: { delay: 200, tolerance: 5 }, + }), + ); + + const reorderColumns = (event: DragEndEvent) => { + const { active, over } = event; + if (!over || active.id === over.id) return; + const from = columnIds.indexOf(String(active.id)); + const to = columnIds.indexOf(String(over.id)); + if (from === -1 || to === -1) return; + setDraft(arrayMove(items, from, to).join(', ')); + }; + + return ( + { + // The form stores "" while the source default is in use. Show that + // default as the editor value; Apply keeps "" unless it was edited. + setDraft(value || defaultValue); + setSearch(''); + }} + onReset={() => { + setDraft(defaultValue); + setSearch(''); + }} + resetDisabled={draft.trim() === defaultValue.trim()} + onApply={() => { + const baseline = value || defaultValue; + // An empty form value means "use the source default", so resetting + // stores "" instead of a copy of that default. + const submitted = + draft.trim() === defaultValue.trim() + ? '' + : draft === baseline + ? value + : draft; + const next = storedColumns(submitted); + onApply( + submitted, + storedColumns(value).filter(item => !next.includes(item)), + ); + }} + data-testid="search-columns-button" + renderPick={() => ( + + + {items.length === 0 ? ( + + No columns selected. + + ) : ( + + + + {items.map((item, index) => ( + removeColumn(item)} + /> + ))} + + + + )} + + + { + if (ignoreNextChangeRef.current) { + ignoreNextChangeRef.current = false; + return; + } + setSearch(next); + }} + onOptionSubmit={option => { + ignoreNextChangeRef.current = true; + setSearch(withMapKeyAlias(option, items, knownColumns)); + }} + data={options} + limit={50} + comboboxProps={{ withinPortal: false }} + style={{ flex: 1 }} + data-testid="search-columns-add-input" + /> + + + + Select a column, or type an expression such as{' '} + upper(Body) as message, then click Add. + + + )} + renderSql={apply => ( +
+ +
+ )} + /> + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx new file mode 100644 index 0000000000..0a58fa7acc --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/SortPopover.tsx @@ -0,0 +1,223 @@ +import { useRef, useState } from 'react'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; +import { + ActionIcon, + Autocomplete, + Group, + ScrollArea, + SegmentedControl, + Stack, + Text, +} from '@mantine/core'; +import { IconX } from '@tabler/icons-react'; + +import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; + +import { ClausePopover } from './ClausePopover'; +import { + newSortRow, + parseSortKeys, + sameKeys, + serializeSortKeys, + SortRow, + sortSummary, + toRows, +} from './sortClause'; + +const DIRECTION_OPTIONS = [ + { label: 'Asc', value: 'asc' }, + { label: 'Desc', value: 'desc' }, +]; + +export function SortPopover({ + value, + defaultValue, + fieldOptions, + onApply, + tableConnection, + sourceId, + dateRange, +}: { + /** The ORDER BY expression in the search form. */ + value: string; + /** The source's ORDER BY, used while `value` is empty. */ + defaultValue: string; + fieldOptions: string[]; + onApply: (orderBy: string) => void; + tableConnection: TableConnection | undefined; + sourceId?: string; + dateRange?: [Date, Date]; +}) { + const shown = value || defaultValue; + const [draft, setDraft] = useState(shown); + const [keys, setKeys] = useState(() => + toRows(parseSortKeys(shown)), + ); + const [search, setSearch] = useState(''); + // Autocomplete writes the chosen option back into the input after + // onOptionSubmit. Ignore that write so the field clears. + const ignoreNextChangeRef = useRef(false); + + const baseline = value || defaultValue; + const shownSort = sortSummary(baseline); + const used = new Set((keys ?? []).map(key => key.id)); + const addOptions = fieldOptions.filter(option => !used.has(option)); + + const reset = () => { + const next = value || defaultValue; + setDraft(next); + setKeys(toRows(parseSortKeys(next))); + setSearch(''); + }; + + const commitKeys = (next: SortRow[]) => { + setKeys(next); + const originalKeys = parseSortKeys(baseline); + // Keep `(a, b) DESC` until a field actually changes, so Apply doesn't + // rewrite the source default into separate keys. + setDraft( + originalKeys != null && sameKeys(next, originalKeys) + ? baseline + : serializeSortKeys(next), + ); + }; + + const addKey = (id: string) => { + const trimmed = id.trim(); + // A clause the field list cannot split, such as `Timestamp DESC NULLS LAST`, + // stays intact. Adding a field here would replace the whole clause. + if (keys == null || !trimmed || used.has(trimmed)) return; + commitKeys([...keys, newSortRow(trimmed, true)]); + setSearch(''); + }; + + return ( + { + setDraft(defaultValue); + setKeys(toRows(parseSortKeys(defaultValue))); + setSearch(''); + }} + resetDisabled={draft.trim() === defaultValue.trim()} + onApply={() => + onApply( + draft.trim() === defaultValue.trim() + ? '' + : draft === baseline + ? value + : draft, + ) + } + data-testid="search-sort-button" + renderPick={() => ( + + {keys == null && ( + + Custom sort. Edit it in SQL. + + )} + {keys != null && keys.length > 0 && ( + + + {keys.map((key, index) => ( + + { + const next = keys.slice(); + next[index] = { ...key, id }; + commitKeys(next); + }} + data={fieldOptions.filter( + option => option === key.id || !used.has(option), + )} + limit={50} + comboboxProps={{ withinPortal: false }} + style={{ flex: 1 }} + /> + { + const next = keys.slice(); + next[index] = { ...key, desc: direction === 'desc' }; + commitKeys(next); + }} + data={DIRECTION_OPTIONS} + data-testid="search-sort-direction" + /> + + commitKeys(keys.filter((_, i) => i !== index)) + } + aria-label={`Remove ${key.id}`} + > + + + + ))} + + + )} + {keys != null && ( + { + if (ignoreNextChangeRef.current) { + ignoreNextChangeRef.current = false; + return; + } + setSearch(next); + }} + data={addOptions} + limit={50} + onOptionSubmit={id => { + ignoreNextChangeRef.current = true; + addKey(id); + }} + comboboxProps={{ withinPortal: false }} + data-testid="search-sort-add-input" + /> + )} + + )} + renderSql={apply => ( + + { + setDraft(sql); + setKeys(toRows(parseSortKeys(sql))); + }} + placeholder="ORDER BY" + onSubmit={apply} + label="ORDER BY" + size="xs" + dateRange={dateRange} + sourceId={sourceId} + /> + + )} + /> + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/SortableColumn.tsx b/packages/app/src/components/SearchResultsToolbar/SortableColumn.tsx new file mode 100644 index 0000000000..6df50d85e5 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/SortableColumn.tsx @@ -0,0 +1,75 @@ +import { useSortable } from '@dnd-kit/sortable'; +import { CSS } from '@dnd-kit/utilities'; +import { ActionIcon, Box, Group, Text } from '@mantine/core'; +import { IconGripVertical, IconX } from '@tabler/icons-react'; + +export function SortableColumn({ + id, + label, + onRemove, +}: { + id: string; + label: string; + onRemove: () => void; +}) { + const { + attributes, + listeners, + setNodeRef, + transform, + transition, + isDragging, + } = useSortable({ id }); + + return ( + + + + + + + {label} + + + + + + + ); +} diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx new file mode 100644 index 0000000000..1be855de79 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/ColumnsPopover.test.tsx @@ -0,0 +1,227 @@ +import React from 'react'; +import { MantineProvider } from '@mantine/core'; +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; + +import { ColumnsPopover } from '@/components/SearchResultsToolbar/ColumnsPopover'; + +jest.mock('@/components/SQLEditor/SQLInlineEditor', () => ({ + __esModule: true, + default: ({ + value, + onChange, + label, + }: { + value: string; + onChange: (value: string) => void; + label: string; + }) => ( + onChange(e.target.value)} + /> + ), +})); + +function renderPopover( + props: Partial> = {}, +) { + const onApply = jest.fn(); + render( + + + , + ); + return { onApply }; +} + +const openPopover = () => + fireEvent.click(screen.getByTestId('search-columns-button')); + +describe('ColumnsPopover', () => { + it('shows the column count and falls back to the default SELECT', () => { + renderPopover({ value: '' }); + expect(screen.getByTestId('search-columns-button')).toHaveAccessibleName( + 'Columns 3 fields', + ); + }); + + it('offers a drag handle for each column', async () => { + renderPopover(); + openPopover(); + expect( + await screen.findByLabelText('Reorder Timestamp'), + ).toBeInTheDocument(); + expect(screen.getByLabelText('Reorder Body')).toBeInTheDocument(); + }); + + it('stays empty after the last column is removed', async () => { + renderPopover({ + value: 'Timestamp', + defaultValue: 'Timestamp, ServiceName, Body', + }); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Timestamp')); + expect(screen.getByText('No columns selected.')).toBeInTheDocument(); + expect( + screen.queryByLabelText('Remove ServiceName'), + ).not.toBeInTheDocument(); + + const input = screen.getByPlaceholderText('Search columns'); + fireEvent.change(input, { target: { value: 'Body' } }); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(screen.getByLabelText('Remove Body')).toBeInTheDocument(); + expect( + screen.queryByLabelText('Remove ServiceName'), + ).not.toBeInTheDocument(); + }); + + it('keeps a backtick-quoted name when another column is removed', async () => { + const { onApply } = renderPopover({ + value: 'Timestamp, `a,b`, Body', + defaultValue: 'Timestamp', + }); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + expect(screen.getByLabelText('Remove `a,b`')).toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp, `a,b`', ['Body']); + }); + + it('removes a column and reports it on apply', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp', ['Body']); + }); + + it('leaves a typed map subscript unchanged', async () => { + renderPopover({ + fieldOptions: ["LogAttributes['region']"], + }); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { + target: { value: "LogAttributes['region']" }, + }); + expect(input).toHaveValue("LogAttributes['region']"); + }); + + it('shows a map key alias in the input before adding it', async () => { + renderPopover({ + fieldOptions: ["LogAttributes['ads.consent_given']"], + }); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { + target: { value: "LogAttributes['ads" }, + }); + fireEvent.click( + await screen.findByText("LogAttributes['ads.consent_given']"), + ); + expect(input).toHaveValue( + `LogAttributes['ads.consent_given'] AS "ads.consent_given"`, + ); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(input).toHaveValue(''); + expect( + screen.getByText( + `LogAttributes['ads.consent_given'] AS "ads.consent_given"`, + ), + ).toBeInTheDocument(); + }); + + it('fills the input from a suggestion and adds only on Add', async () => { + const { onApply } = renderPopover(); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { target: { value: 'Severity' } }); + fireEvent.click(await screen.findByText('SeverityText')); + expect(input).toHaveValue('SeverityText'); + expect( + screen.queryByLabelText('Remove SeverityText'), + ).not.toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(input).toHaveValue(''); + expect(screen.getByLabelText('Remove SeverityText')).toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp, Body, SeverityText', []); + }); + + it('adds a typed expression such as an alias', async () => { + const { onApply } = renderPopover(); + openPopover(); + const input = await screen.findByPlaceholderText('Search columns'); + fireEvent.change(input, { + target: { value: 'upper(Body) as message' }, + }); + fireEvent.click(screen.getByTestId('search-columns-add-button')); + expect(input).toHaveValue(''); + expect( + screen.getByLabelText('Remove upper(Body) as message'), + ).toBeInTheDocument(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith( + 'Timestamp, Body, upper(Body) as message', + [], + ); + }); + + it('resets a changed column list to the source default', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + fireEvent.click(screen.getByTestId('search-columns-button-reset')); + expect(screen.getByLabelText('Remove ServiceName')).toBeInTheDocument(); + expect(screen.getByTestId('search-columns-button-reset')).toBeDisabled(); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('', []); + }); + + it('shows the default SELECT as the SQL value', async () => { + renderPopover({ value: '' }); + openPopover(); + expect( + await screen.findByTestId('search-columns-button-reset'), + ).toBeDisabled(); + fireEvent.click(await screen.findByText('SQL')); + expect(screen.getByLabelText('SELECT')).toHaveValue( + 'Timestamp, ServiceName, Body', + ); + }); + + it('edits the SELECT on the SQL tab', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByText('SQL')); + fireEvent.change(screen.getByLabelText('SELECT'), { + target: { value: 'count()' }, + }); + fireEvent.click(screen.getByTestId('search-columns-button-apply')); + expect(onApply).toHaveBeenCalledWith('count()', ['Timestamp', 'Body']); + }); + + it('discards the draft on cancel', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByLabelText('Remove Body')); + fireEvent.click(screen.getByText('Cancel')); + await waitFor(() => + expect( + screen.queryByTestId('search-columns-button-popover'), + ).not.toBeInTheDocument(), + ); + openPopover(); + expect(await screen.findByLabelText('Remove Body')).toBeInTheDocument(); + expect(onApply).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx new file mode 100644 index 0000000000..d1ec5d1409 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/SortPopover.test.tsx @@ -0,0 +1,207 @@ +import React from 'react'; +import { MantineProvider } from '@mantine/core'; +import { fireEvent, render, screen } from '@testing-library/react'; + +import { SortPopover } from '@/components/SearchResultsToolbar/SortPopover'; + +jest.mock('@/components/SQLEditor/SQLInlineEditor', () => ({ + __esModule: true, + default: ({ + value, + onChange, + label, + }: { + value: string; + onChange: (value: string) => void; + label: string; + }) => ( + onChange(e.target.value)} + /> + ), +})); + +function renderPopover( + props: Partial> = {}, +) { + const onApply = jest.fn(); + render( + + + , + ); + return { onApply }; +} + +const openPopover = () => + fireEvent.click(screen.getByTestId('search-sort-button')); + +describe('SortPopover', () => { + it('labels the button with the parsed field and direction', () => { + renderPopover({ value: 'Body ASC' }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort Body asc', + ); + }); + + it('flips the direction from the picker', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByText('Asc')); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith('Timestamp ASC'); + }); + + it('lists a tuple sort that spans more than one line', async () => { + const orderBy = '(toDateTime(Timestamp),\n Timestamp) DESC'; + renderPopover({ value: orderBy, defaultValue: orderBy }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect( + await screen.findByDisplayValue('toDateTime(Timestamp)'), + ).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.queryByTestId('search-sort-custom')).not.toBeInTheDocument(); + }); + + it('lists function calls inside a tuple sort', async () => { + const orderBy = '(toDateTime(Timestamp), Timestamp) DESC'; + renderPopover({ value: orderBy, defaultValue: orderBy }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect( + await screen.findByDisplayValue('toDateTime(Timestamp)'), + ).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.queryByTestId('search-sort-custom')).not.toBeInTheDocument(); + }); + + it('keeps a backtick-quoted field that contains a comma on SQL', async () => { + const orderBy = '(`a,b`, Timestamp) DESC'; + renderPopover({ value: orderBy, defaultValue: orderBy }); + openPopover(); + expect(await screen.findByLabelText('ORDER BY')).toHaveValue(orderBy); + expect(screen.queryByDisplayValue('Timestamp')).not.toBeInTheDocument(); + }); + + it('opens on the SQL tab for a custom ORDER BY', async () => { + const custom = 'Timestamp DESC NULLS LAST'; + const { onApply } = renderPopover({ value: custom, defaultValue: custom }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + `Sort ${custom}`, + ); + openPopover(); + const editor = await screen.findByLabelText('ORDER BY'); + expect(editor).toHaveValue(custom); + fireEvent.click(screen.getByText('Fields')); + expect(screen.getByTestId('search-sort-custom')).toBeInTheDocument(); + expect( + screen.queryByTestId('search-sort-add-input'), + ).not.toBeInTheDocument(); + fireEvent.click(screen.getByText('SQL')); + fireEvent.change(screen.getByLabelText('ORDER BY'), { + target: { value: 'Body ASC' }, + }); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith('Body ASC'); + }); + + it('shows the source default as the SQL value, not a placeholder', async () => { + const orderBy = '(TimestampTime, Timestamp) DESC'; + const { onApply } = renderPopover({ value: '', defaultValue: orderBy }); + openPopover(); + fireEvent.click(await screen.findByText('SQL')); + expect(screen.getByLabelText('ORDER BY')).toHaveValue(orderBy); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith(''); + }); + + it('lists every field of a tuple sort and can add another', async () => { + const { onApply } = renderPopover({ + value: '', + defaultValue: '(TimestampTime, Timestamp) DESC', + fieldOptions: ['Body', 'Timestamp', 'TimestampTime'], + }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect( + await screen.findByDisplayValue('TimestampTime'), + ).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + + const addSort = screen.getByPlaceholderText('Add sort'); + fireEvent.change(addSort, { + target: { value: 'Bo' }, + }); + fireEvent.click(await screen.findByText('Body')); + expect(addSort).toHaveValue(''); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith( + 'TimestampTime DESC, Timestamp DESC, Body DESC', + ); + }); + + it('keeps separate tuple keys instead of merging them', async () => { + const orderBy = '(a, b) ASC, (c, d) DESC'; + const { onApply } = renderPopover({ + value: orderBy, + defaultValue: orderBy, + }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect(await screen.findByDisplayValue('(a, b)')).toBeInTheDocument(); + expect(screen.getByDisplayValue('(c, d)')).toBeInTheDocument(); + fireEvent.click(screen.getAllByText('Desc')[0]); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith('(a, b) DESC, (c, d) DESC'); + }); + + it('lists every key of a multi-field sort', async () => { + renderPopover({ value: 'ServiceName ASC, Timestamp DESC' }); + expect(screen.getByTestId('search-sort-button')).toHaveAccessibleName( + 'Sort 2 fields', + ); + openPopover(); + expect(await screen.findByDisplayValue('ServiceName')).toBeInTheDocument(); + expect(screen.getByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.getByLabelText('Remove ServiceName')).toBeInTheDocument(); + }); + + it('resets a changed sort to the source default', async () => { + const { onApply } = renderPopover({ + value: 'Body ASC', + defaultValue: 'Timestamp DESC', + }); + openPopover(); + fireEvent.click(await screen.findByTestId('search-sort-button-reset')); + expect(await screen.findByDisplayValue('Timestamp')).toBeInTheDocument(); + expect(screen.getByTestId('search-sort-button-reset')).toBeDisabled(); + fireEvent.click(screen.getByTestId('search-sort-button-apply')); + expect(onApply).toHaveBeenCalledWith(''); + }); + + it('does not apply on cancel', async () => { + const { onApply } = renderPopover(); + openPopover(); + fireEvent.click(await screen.findByText('Asc')); + fireEvent.click(screen.getByText('Cancel')); + expect(onApply).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx b/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx new file mode 100644 index 0000000000..c044494281 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/__tests__/useClauseFieldOptions.test.tsx @@ -0,0 +1,80 @@ +import { ColumnMeta, JSDataType } from '@hyperdx/common-utils/dist/clickhouse'; +import { Field } from '@hyperdx/common-utils/dist/core/metadata'; +import { renderHook } from '@testing-library/react'; + +import { useClauseFieldOptions } from '@/components/SearchResultsToolbar/useClauseFieldOptions'; +import { useAllFields } from '@/hooks/useMetadata'; + +jest.mock('@/hooks/useMetadata', () => ({ + useAllFields: jest.fn(), +})); + +const column = (name: string, type: string): ColumnMeta => ({ + name, + type, + codec_expression: '', + comment: '', + default_expression: '', + default_type: '', + ttl_expression: '', +}); + +const columns = [ + column('ServiceName', 'LowCardinality(String)'), + column('service-name', 'String'), + column('my-map', 'Map(String, String)'), +]; + +function mockAllFields( + data: Field[] | undefined, + isFieldMetadataDisabled: boolean, +) { + const result: ReturnType = { + data, + error: null, + isError: false, + isPending: false, + isSuccess: true, + isLoading: false, + isFetching: false, + isFieldMetadataDisabled, + fieldsByTableConnection: new Map(), + columnsByTableConnection: new Map(), + errorsByTableConnection: new Map(), + refetch: () => Promise.reject(new Error('unused')), + }; + jest.mocked(useAllFields).mockReturnValue(result); +} + +describe('useClauseFieldOptions', () => { + it('quotes column names that are not bare identifiers', () => { + mockAllFields( + [ + { path: ['ServiceName'], type: 'String', jsType: JSDataType.String }, + { path: ['service-name'], type: 'String', jsType: JSDataType.String }, + { path: ['my-map', 'k'], type: 'String', jsType: null }, + ], + false, + ); + + const { result } = renderHook(() => + useClauseFieldOptions({ tableConnection: undefined, columns }), + ); + + expect(result.current).toEqual([ + 'ServiceName', + "`my-map`['k']", + '`service-name`', + ]); + }); + + it('offers scalar columns when field metadata is disabled', () => { + mockAllFields([], true); + + const { result } = renderHook(() => + useClauseFieldOptions({ tableConnection: undefined, columns }), + ); + + expect(result.current).toEqual(['ServiceName', '`service-name`']); + }); +}); diff --git a/packages/app/src/components/SearchResultsToolbar/sortClause.ts b/packages/app/src/components/SearchResultsToolbar/sortClause.ts new file mode 100644 index 0000000000..ba586497f0 --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/sortClause.ts @@ -0,0 +1,112 @@ +import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; +import { findBalancedParens } from '@hyperdx/common-utils/dist/variables'; + +import { parseAsSortingStateString } from '@/utils/queryParsers'; + +export type SortKey = { id: string; desc: boolean }; +export type SortRow = SortKey & { rowId: string }; + +let rowSeq = 0; + +export function newSortRow(id: string, desc: boolean): SortRow { + return { id, desc, rowId: `sort-${rowSeq++}` }; +} + +export const toRows = (keys: SortKey[] | null): SortRow[] | null => + keys?.map(key => newSortRow(key.id, key.desc)) ?? null; + +/** A comma inside backticks cannot be split without breaking the identifier. */ +function hasCommaInsideBackticks(input: string): boolean { + let quoted = false; + for (let i = 0; i < input.length; i++) { + if (input[i] !== '`') { + if (quoted && input[i] === ',') return true; + continue; + } + // ClickHouse escapes a backtick by doubling it. + if (quoted && input[i + 1] === '`') { + i += 1; + continue; + } + quoted = !quoted; + } + return false; +} + +/** + * Sort keys the picker can edit. A parenthesized list with one direction, + * `(toDateTime(Timestamp), Timestamp) DESC`, is several fields sharing that + * direction, including function calls. The parentheses have to wrap that whole + * expression: `(a, b) ASC, (c, d) DESC` is two keys, not one list. Anything + * without a plain ASC/DESC on each field stays in SQL. + */ +export function parseSortKeys(orderBy: string): SortKey[] | null { + const trimmed = orderBy.trim(); + if (!trimmed) return []; + // A comma inside a backtick-quoted name would be split into extra fields. + if (hasCommaInsideBackticks(trimmed)) return null; + + // [\s\S] so a newline inside `(toDateTime(Timestamp),\n Timestamp) DESC` + // still counts as one direction. `.` would miss it and hide the Fields controls. + const direction = trimmed.match(/^([\s\S]*)\s+(asc|desc)$/i); + const expr = direction?.[1].trim() ?? ''; + if ( + direction && + expr.startsWith('(') && + findBalancedParens(expr, 0) === expr.length - 1 + ) { + const ids = splitAndTrimWithBracket(expr.slice(1, -1)); + if (ids.length > 1 && ids.every(id => id.length > 0)) { + const desc = direction[2].toUpperCase() === 'DESC'; + return ids.map(id => ({ id, desc })); + } + } + + const keys: SortKey[] = []; + for (const part of splitAndTrimWithBracket(trimmed)) { + const parsed = parseAsSortingStateString.parse(part); + if (parsed == null || parsed.id.length === 0) return null; + keys.push({ id: parsed.id, desc: parsed.desc }); + } + return keys; +} + +export function serializeSortKeys(keys: SortKey[]) { + return keys + .filter(key => key.id.trim()) + .map(key => + parseAsSortingStateString.serialize({ + id: key.id.trim(), + desc: key.desc, + }), + ) + .join(', '); +} + +export function sameKeys(a: SortKey[], b: SortKey[]) { + return ( + a.length === b.length && + a.every((key, i) => key.id === b[i].id && key.desc === b[i].desc) + ); +} + +export function sortSummary(orderBy: string): { + summary: string; + count: number; +} { + const keys = parseSortKeys(orderBy); + if (keys == null) { + const summary = orderBy.trim() || 'none'; + // An expression the field list cannot split stays one value. A number + // would invent a field count the user cannot see in the list. + return { summary, count: summary === 'none' ? 0 : 1 }; + } + if (keys.length === 0) return { summary: 'none', count: 0 }; + if (keys.length === 1) { + return { + summary: `${keys[0].id} ${keys[0].desc ? 'desc' : 'asc'}`, + count: 1, + }; + } + return { summary: `${keys.length} fields`, count: keys.length }; +} diff --git a/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts new file mode 100644 index 0000000000..292844ba7f --- /dev/null +++ b/packages/app/src/components/SearchResultsToolbar/useClauseFieldOptions.ts @@ -0,0 +1,47 @@ +import { useMemo } from 'react'; +import { ColumnMeta } from '@hyperdx/common-utils/dist/clickhouse'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; + +import { + getSqlSourceColumnKeys, + getSqlSourceKeys, +} from '@/components/AdhocDashboardFilter/useAdhocFilterOptions'; +import { toQuotedClickHouseKeyExpression } from '@/components/DBSearchPageFilters/utils'; +import { useAllFields } from '@/hooks/useMetadata'; + +/** The source's fields as SQL expressions, for the Pick tabs. */ +export function useClauseFieldOptions({ + tableConnection, + columns, + dateRange, + timestampValueExpression, +}: { + tableConnection: TableConnection | undefined; + columns: ColumnMeta[] | undefined; + dateRange?: [Date, Date]; + timestampValueExpression?: string; +}): string[] { + const { data: fields, isFieldMetadataDisabled } = useAllFields( + tableConnection, + { + dateRange, + timestampValueExpression, + }, + ); + + return useMemo(() => { + if (columns == null) return []; + const knownColumns = new Set(columns.map(column => column.name)); + // Field discovery is off for some teams, and `useAllFields` then returns + // [] rather than "still loading". Scalar columns are still known. + const keys = + isFieldMetadataDisabled || fields == null + ? getSqlSourceColumnKeys(columns) + : getSqlSourceKeys(fields, columns); + return Array.from( + new Set( + keys.map(key => toQuotedClickHouseKeyExpression(key, knownColumns)), + ), + ).sort(); + }, [fields, columns, isFieldMetadataDisabled]); +} diff --git a/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.module.scss b/packages/app/src/components/SplitValueButton/SplitValueButton.module.scss similarity index 100% rename from packages/app/src/components/SearchGroupBy/SearchGroupBySelect.module.scss rename to packages/app/src/components/SplitValueButton/SplitValueButton.module.scss diff --git a/packages/app/src/components/SplitValueButton/SplitValueButton.stories.tsx b/packages/app/src/components/SplitValueButton/SplitValueButton.stories.tsx new file mode 100644 index 0000000000..1ea0ab32fb --- /dev/null +++ b/packages/app/src/components/SplitValueButton/SplitValueButton.stories.tsx @@ -0,0 +1,108 @@ +import { Group, Stack, Text } from '@mantine/core'; +import type { Meta, StoryObj } from '@storybook/nextjs'; + +import { SplitValueButton } from './SplitValueButton'; + +const meta: Meta = { + title: 'Components/SplitValueButton', + component: SplitValueButton, + parameters: { + layout: 'padded', + docs: { + description: { + component: + 'The shared trigger for Group by, Columns, and Sort. Use it when a search control shows the current value of a clause and opens an editor. Do not use it for an action such as Save, Run, or Apply.', + }, + }, + }, +}; + +export default meta; +type Story = StoryObj; + +const noop = () => {}; + +export const WhenToUse: Story = { + render: () => ( + + + Use this button for a search clause whose value is one field, or a count + of fields. The name stays fixed. The value is what you press. + + + + Group by splits the histogram. One series for each value of the field. + + + + + + Columns chooses which columns the results table shows, and their + order. + + + + + + Sort sets the order of the result rows. One field includes its + direction. + + + + + + On a narrow screen a count is only the number. One field keeps its + name, and an empty clause stays none. + + + + + + + + + ), +}; diff --git a/packages/app/src/components/SplitValueButton/SplitValueButton.tsx b/packages/app/src/components/SplitValueButton/SplitValueButton.tsx new file mode 100644 index 0000000000..5815600b58 --- /dev/null +++ b/packages/app/src/components/SplitValueButton/SplitValueButton.tsx @@ -0,0 +1,91 @@ +import { ComponentPropsWithoutRef, Ref } from 'react'; +import cx from 'classnames'; +import { Text, UnstyledButton } from '@mantine/core'; +import { IconChevronDown } from '@tabler/icons-react'; + +import { useCompactViewport } from '@/components/CompactViewport/useCompactViewport'; + +import classes from './SplitValueButton.module.scss'; + +/** One field by name, several as a count, or none. */ +export function fieldListSummary(fields: string[]): string { + if (fields.length === 0) return 'none'; + if (fields.length === 1) return fields[0]; + return `${fields.length} fields`; +} + +type SplitValueButtonProps = { + /** Fixed name, such as Group by, Columns, or Sort. */ + name: string; + /** Desktop value: a field name, "3 fields", or "none". */ + summary: string; + /** How many fields the summary counts. Drives the narrow-screen number. */ + count: number; + /** The clause is still the source default, so the value is quiet. */ + dimmed?: boolean; + /** Full clause, shown on hover when the value is only a summary. */ + title?: string; + onClick: () => void; + /** Story and test override. Otherwise follows the compact viewport. */ + compact?: boolean; + 'data-testid': string; + ref?: Ref; +} & Omit, 'onClick' | 'title'>; + +/** + * Split button for a search clause: a fixed name, then the current value. + * + * Use it for Group by, Columns, and Sort. The name says which clause it is. + * The value is one field, or "3 fields" when there are several. On a narrow + * screen that count is only the number, so the row still fits. + * + * Group by splits the histogram, one series per value. Columns chooses and + * orders the results table. Sort sets the row order, and a single field + * includes its direction. + * + * Do not use it for an action such as Save, Run, or Apply. + */ +export function SplitValueButton({ + name, + summary, + count, + dimmed, + title, + onClick, + compact, + 'data-testid': testId, + className, + ref, + ...others +}: SplitValueButtonProps) { + const viewportCompact = useCompactViewport(); + const isCompact = compact ?? viewportCompact; + const shown = isCompact && count > 1 ? String(count) : summary; + + return ( +
+ + {name} + + + + {shown} + + + +
+ ); +} diff --git a/packages/app/src/components/SplitValueButton/__tests__/SplitValueButton.test.tsx b/packages/app/src/components/SplitValueButton/__tests__/SplitValueButton.test.tsx new file mode 100644 index 0000000000..e43815f63e --- /dev/null +++ b/packages/app/src/components/SplitValueButton/__tests__/SplitValueButton.test.tsx @@ -0,0 +1,53 @@ +import { screen } from '@testing-library/react'; + +import { SplitValueButton } from '@/components/SplitValueButton/SplitValueButton'; + +describe('SplitValueButton', () => { + it('shows a field count as words on a wide screen', () => { + renderWithMantine( + {}} + data-testid="columns" + />, + ); + expect(screen.getByTestId('columns')).toHaveTextContent('3 fields'); + expect(screen.getByTestId('columns')).toHaveAccessibleName( + 'Columns 3 fields', + ); + }); + + it('shows only the number on a narrow screen', () => { + renderWithMantine( + {}} + data-testid="columns" + />, + ); + expect(screen.getByTestId('columns')).toHaveTextContent('3'); + expect(screen.getByTestId('columns')).toHaveAccessibleName( + 'Columns 3 fields', + ); + }); + + it('keeps a single field name on a narrow screen', () => { + renderWithMantine( + {}} + data-testid="group-by" + />, + ); + expect(screen.getByTestId('group-by')).toHaveTextContent('SeverityText'); + }); +}); diff --git a/packages/app/src/utils.ts b/packages/app/src/utils.ts index b8e0e91ea3..1eb769dbf3 100644 --- a/packages/app/src/utils.ts +++ b/packages/app/src/utils.ts @@ -8,6 +8,11 @@ import { TableConnection, unquoteIdentifier, } from '@hyperdx/common-utils/dist/core/metadata'; +import { + replaceBacktickedIdentifiers, + restoreReplacements, + splitAndTrimWithBracket, +} from '@hyperdx/common-utils/dist/core/utils'; import { CATEGORICAL_PALETTE_TOKENS, ChartPaletteToken, @@ -1289,11 +1294,24 @@ export const withMapKeyAlias = ( return `${column} AS "${key}"`; }; +/** + * Split a SELECT or ORDER BY list. A comma inside a backtick-quoted name is + * not a separator. Those names are tokenized first so a later join cannot + * insert a space inside one. The shared splitter stays unaware of backticks. + */ +export function splitClauseList(input: string): string[] { + const { sqlWithReplacements, replacements } = + replaceBacktickedIdentifiers(input); + return splitAndTrimWithBracket(sqlWithReplacements).map(part => + restoreReplacements(part, replacements.quotedText), + ); +} + /** * The sort to keep once a SELECT item is removed. Sorting on an aliased column * from the table header orders by the alias itself (`"service.name" DESC`), - * which stops resolving once the item is gone, so that falls back to the - * default sort. + * which stops resolving once the item is gone. Each top-level key that sorts + * by that alias is dropped. The default sort is used only when none remain. */ export const orderByAfterRemovingSelectItem = ( item: string, @@ -1304,8 +1322,15 @@ export const orderByAfterRemovingSelectItem = ( if (alias == null) { return orderBy; } - const sortedBy = orderBy.trim().replace(/\s+(ASC|DESC)$/i, ''); - return sortedBy === `"${alias}"` ? defaultOrderBy : orderBy; + const quoted = `"${alias}"`; + const keys = splitClauseList(orderBy); + const kept = keys.filter( + key => key.replace(/\s+(ASC|DESC)$/i, '') !== quoted, + ); + if (kept.length === keys.length) { + return orderBy; + } + return kept.length === 0 ? defaultOrderBy : kept.join(', '); }; const _useTry = (fn: () => T): [null | Error | unknown, null | T] => { diff --git a/packages/app/styles/SearchPage.module.scss b/packages/app/styles/SearchPage.module.scss index 713ee35f05..e291a44a94 100644 --- a/packages/app/styles/SearchPage.module.scss +++ b/packages/app/styles/SearchPage.module.scss @@ -171,4 +171,46 @@ z-index: 4; padding-bottom: var(--mantine-spacing-xs); border-bottom: 1px solid var(--color-border); + + // The where row uses this width. 900px is the compact search cutoff. + container-type: inline-size; +} + +.whereRow { + flex-wrap: wrap; +} + +.whereField { + flex: 1 1 0%; + max-width: 100%; + min-width: 0; +} + +.timeControls { + flex: 0 0 auto; + max-width: 100%; +} + +.timePicker { + width: 350px; + max-width: 100%; + min-width: 0; +} + +// Stack the where clause once the form itself is under 900px. Wider than +// that, it stays beside the time controls. Matches the compact layout cutoff. +@container (min-width: 900px) { + .whereRow { + flex-wrap: nowrap; + } +} + +@container (max-width: 899px) { + .whereField { + flex-basis: 100%; + } + + .timeControls { + flex-basis: 100%; + } } diff --git a/packages/app/tests/e2e/features/search/saved-search.spec.ts b/packages/app/tests/e2e/features/search/saved-search.spec.ts index 9201f730d3..610c189a2c 100644 --- a/packages/app/tests/e2e/features/search/saved-search.spec.ts +++ b/packages/app/tests/e2e/features/search/saved-search.spec.ts @@ -98,10 +98,10 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify custom SELECT is preserved', async () => { - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toContainText( - 'upper(ServiceName) as service_name', - ); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .toContain('upper(ServiceName) as service_name'); }); }, ); @@ -140,11 +140,11 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify different source has its own default SELECT', async () => { - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).not.toContainText( - 'lower(Body) as body_lower', - ); - await expect(selectEditor).toContainText('Timestamp'); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .not.toContain('lower(Body) as body_lower'); + await expect.poll(selectValue).toContain('Timestamp'); }); await test.step('Navigate back to saved search', async () => { @@ -153,12 +153,12 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify saved search SELECT is restored', async () => { - const selectEditor = searchPage.getSELECTEditor(); + const selectValue = () => searchPage.getSelectValue(); // Verifies the fix: SELECT restores to saved search's custom value - await expect(selectEditor).toContainText('lower(Body) as body_lower'); - await expect(selectEditor).toContainText( - 'Timestamp, Body, lower(Body)', - ); + await expect.poll(selectValue).toContain('lower(Body) as body_lower'); + await expect + .poll(selectValue) + .toContain('Timestamp, Body, lower(Body)'); }); }, ); @@ -186,11 +186,11 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify SELECT changed to the new source default', async () => { - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).not.toContainText( - 'lower(ServiceName) as service_name', - ); - await expect(selectEditor).toContainText('Timestamp'); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .not.toContain('lower(ServiceName) as service_name'); + await expect.poll(selectValue).toContain('Timestamp'); }); await test.step('Switch back to original source via dropdown', async () => { @@ -201,10 +201,10 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify SELECT is search custom SELECT', async () => { - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toContainText( - 'lower(ServiceName) as service_name', - ); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .toContain('lower(ServiceName) as service_name'); }); }, ); @@ -255,9 +255,9 @@ test.describe('Saved Search Functionality', () => { expect(savedSearchParams.has('to')).toBe(false); await expect(searchPage.input).toHaveValue('SeverityText:info'); - await expect(searchPage.getOrderByEditor()).toContainText( - customOrderBy, - ); + await expect + .poll(() => searchPage.getOrderByValue()) + .toContain(customOrderBy); // Capture the saved search URL (without query params) savedSearchUrl = page.url().split('?')[0]; @@ -289,8 +289,8 @@ test.describe('Saved Search Functionality', () => { await expect(whereInput).toHaveValue('SeverityText:info'); // Verify ORDER BY is restored - const orderByEditor = searchPage.getOrderByEditor(); - await expect(orderByEditor).toContainText('ServiceName ASC'); + const orderByValue = () => searchPage.getOrderByValue(); + await expect.poll(orderByValue).toContain('ServiceName ASC'); // Verify search results are visible (search executed automatically) await searchPage.table.waitForRowsToPopulate(); @@ -343,11 +343,11 @@ test.describe('Saved Search Functionality', () => { await searchPage.table.waitForRowsToPopulate(true); // Verify SELECT content - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toContainText( - 'upper(ServiceName) as service_name', - ); - await expect(selectEditor).toContainText('Timestamp, Body'); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .toContain('upper(ServiceName) as service_name'); + await expect.poll(selectValue).toContain('Timestamp, Body'); }); }, ); @@ -401,9 +401,9 @@ test.describe('Saved Search Functionality', () => { }); await test.step('The tweak survives, in the form and in the URL', async () => { - await expect(searchPage.getSELECTEditor()).toContainText( - 'lower(Body) as body_lower', - ); + await expect + .poll(() => searchPage.getSelectValue()) + .toContain('lower(Body) as body_lower'); expect(new URL(page.url()).searchParams.get('select')).toContain( 'lower(Body)', ); @@ -427,10 +427,10 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify default SELECT is loaded', async () => { - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toContainText( - 'Timestamp, ServiceName, SeverityText, Body', - ); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .toContain('Timestamp, ServiceName, SeverityText, Body'); }); await test.step('Navigate to dashboards page', async () => { @@ -444,10 +444,10 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify default SELECT is loaded', async () => { - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toContainText( - 'Timestamp, ServiceName, SeverityText, Body', - ); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .toContain('Timestamp, ServiceName, SeverityText, Body'); }); }, ); @@ -531,16 +531,16 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify ORDER BY changed to second source default', async () => { - const orderByEditor = searchPage.getOrderByEditor(); + const orderByValue = () => searchPage.getOrderByValue(); // Should not contain the custom ORDER BY from the saved search - await expect(orderByEditor).not.toHaveText('Body DESC', { - timeout: 5000, - }); - await expect(orderByEditor).toHaveText(/(Timestamp|timestamp)/i, { - timeout: 5000, - }); + await expect + .poll(orderByValue, { timeout: 5000 }) + .not.toBe('Body DESC'); + await expect + .poll(orderByValue, { timeout: 5000 }) + .toMatch(/(Timestamp|timestamp)/i); }); await test.step('Switch back to original source', async () => { @@ -556,8 +556,8 @@ test.describe('Saved Search Functionality', () => { }); await test.step('Verify ORDER BY restored to saved search custom value', async () => { - const orderByEditor = searchPage.getOrderByEditor(); - await expect(orderByEditor).toHaveText('Body DESC', { timeout: 5000 }); + const orderByValue = () => searchPage.getOrderByValue(); + await expect.poll(orderByValue, { timeout: 5000 }).toBe('Body DESC'); }); }, ); @@ -780,10 +780,10 @@ test.describe('Saved Search Functionality', () => { await test.step('Verify the new search preserved the SELECT', async () => { await searchPage.table.waitForRowsToPopulate(); - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toContainText( - 'upper(ServiceName) as service_upper', - ); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .toContain('upper(ServiceName) as service_upper'); }); await test.step('Verify the new search preserved the WHERE', async () => { @@ -796,10 +796,10 @@ test.describe('Saved Search Functionality', () => { await searchPage.table.waitForRowsToPopulate(); await expect(searchPage.input).toHaveValue('SeverityText:info'); - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toContainText( - 'upper(ServiceName) as service_upper', - ); + const selectValue = () => searchPage.getSelectValue(); + await expect + .poll(selectValue) + .toContain('upper(ServiceName) as service_upper'); }); }, ); diff --git a/packages/app/tests/e2e/features/search/search.spec.ts b/packages/app/tests/e2e/features/search/search.spec.ts index 3f72a5cba8..7ed7f20497 100644 --- a/packages/app/tests/e2e/features/search/search.spec.ts +++ b/packages/app/tests/e2e/features/search/search.spec.ts @@ -314,10 +314,7 @@ test.describe('Search', { tag: '@search' }, () => { expect(url, 'URL should contain select parameter').toContain('select='); expect(url, 'URL should contain alias "message"').toContain('message'); - // Verify SELECT editor content using page object - const selectEditor = searchPage.getSELECTEditor(); - await expect(selectEditor).toBeVisible(); - const selectValue = await selectEditor.textContent(); + const selectValue = await searchPage.getSelectValue(); expect(selectValue, 'SELECT should contain alias').toContain( 'Body as message', diff --git a/packages/app/tests/e2e/features/shared/input-focus.spec.ts b/packages/app/tests/e2e/features/shared/input-focus.spec.ts index 984d80f7ea..1812e7ce83 100644 --- a/packages/app/tests/e2e/features/shared/input-focus.spec.ts +++ b/packages/app/tests/e2e/features/shared/input-focus.spec.ts @@ -41,12 +41,14 @@ test.describe('Query input focus states', { tag: '@search' }, () => { const searchPage = new SearchPage(page); await searchPage.goto(); + await searchPage.openSelectEditor(); const selectBlurred = await searchPage.getSelectBorderColor(); await searchPage.getSELECTEditor().focus(); await expect .poll(() => searchPage.getSelectBorderColor()) .not.toBe(selectBlurred); + await searchPage.openOrderByEditor(); const orderByBlurred = await searchPage.getOrderByBorderColor(); await searchPage.getOrderByEditor().focus(); await expect diff --git a/packages/app/tests/e2e/features/shared/multiline.spec.ts b/packages/app/tests/e2e/features/shared/multiline.spec.ts index 0696d75cef..df0df202bb 100644 --- a/packages/app/tests/e2e/features/shared/multiline.spec.ts +++ b/packages/app/tests/e2e/features/shared/multiline.spec.ts @@ -60,10 +60,11 @@ test.describe('Multiline Input', { tag: '@search' }, () => { const searchPage = new SearchPage(page); await searchPage.goto(); - for (const field of [ - searchPage.selectClauseField(), - searchPage.orderByClauseField(), - ]) { + for (const [open, field] of [ + [() => searchPage.openSelectEditor(), searchPage.selectClauseField()], + [() => searchPage.openOrderByEditor(), searchPage.orderByClauseField()], + ] as const) { + await open(); await expect(field.focusTarget).toBeVisible(); await searchPage.clearClause(field); await expectExpandsAndStaysExpanded(page, field); diff --git a/packages/app/tests/e2e/page-objects/SearchPage.ts b/packages/app/tests/e2e/page-objects/SearchPage.ts index 5806833fdc..215a24c5e6 100644 --- a/packages/app/tests/e2e/page-objects/SearchPage.ts +++ b/packages/app/tests/e2e/page-objects/SearchPage.ts @@ -459,11 +459,53 @@ export class SearchPage { return this.whereInput.borderColors(); } + get columnsButton() { + return this.page.getByTestId('search-columns-button'); + } + + get sortButton() { + return this.page.getByTestId('search-sort-button'); + } + + /** The SELECT the table runs with; the Columns button carries it as its title. */ + async getSelectValue(): Promise { + return (await this.columnsButton.getAttribute('title')) ?? ''; + } + + async getOrderByValue(): Promise { + return (await this.sortButton.getAttribute('title')) ?? ''; + } + /** - * Get SELECT editor (CodeMirror) + * Get SELECT editor (CodeMirror). It lives on the SQL tab of the Columns + * popover, so call `openSelectEditor` first. */ getSELECTEditor() { - return this.page.locator('.cm-content').first(); + return this.page.getByTestId('search-select-input').locator('.cm-content'); + } + + async openSelectEditor() { + return this.openClauseSqlTab( + 'search-columns-button', + this.getSELECTEditor(), + ); + } + + async openOrderByEditor() { + return this.openClauseSqlTab('search-sort-button', this.getOrderByEditor()); + } + + private async openClauseSqlTab(testId: string, editor: Locator) { + const popover = this.page.getByTestId(`${testId}-popover`); + if (!(await popover.isVisible())) { + await this.page.getByTestId(testId).click(); + } + await popover + .getByTestId(`${testId}-mode`) + .getByText('SQL', { exact: true }) + .click(); + await editor.waitFor({ state: 'visible' }); + return editor; } async getSelectBorderColor(): Promise { @@ -505,33 +547,42 @@ export class SearchPage { * Get ORDER BY editor (CodeMirror) */ getOrderByEditor() { - return this.page.locator('.cm-content').nth(1); + return this.page + .getByTestId('search-order-by-input') + .locator('.cm-content'); } /** - * Set custom SELECT columns + * Set custom SELECT columns through the Columns popover's SQL tab and apply + * it, which also runs the search. */ async setCustomSELECT(selectStatement: string) { - const selectEditor = this.getSELECTEditor(); + const selectEditor = await this.openSelectEditor(); await selectEditor.click({ clickCount: 3 }); // Select all // Insert atomically rather than per-keystroke: under load CodeMirror can // drop individual keys from keyboard.type (e.g. "Timestamp" -> "Timstamp"), // which then gets faithfully saved and fails later assertions. await this.page.keyboard.insertText(selectStatement); - // Dismiss the autocomplete popup so it can't linger and overlay the next - // control (e.g. the Save Search button). - await dismissSqlAutocomplete(this.page); + await this.applyClause('search-columns-button'); } /** - * Set custom ORDER BY clause + * Set custom ORDER BY through the Sort popover's SQL tab and apply it. */ async setCustomOrderBy(orderByStatement: string) { - const orderByEditor = this.getOrderByEditor(); + const orderByEditor = await this.openOrderByEditor(); await orderByEditor.click({ clickCount: 3 }); // Select all - await this.page.keyboard.type(orderByStatement); - // CLoses Autocomplete Modal if open - await this.page.keyboard.press('Escape'); + await this.page.keyboard.insertText(orderByStatement); + await this.applyClause('search-sort-button'); + } + + private async applyClause(testId: string) { + // Blur rather than Escape: Escape also closes the popover, dropping the draft. + await dismissSqlAutocomplete(this.page); + await this.page.getByTestId(`${testId}-apply`).click(); + await this.page + .getByTestId(`${testId}-popover`) + .waitFor({ state: 'hidden' }); } get groupByButton() {