diff --git a/.changeset/adhoc-filter-click-table.md b/.changeset/adhoc-filter-click-table.md new file mode 100644 index 0000000000..090f5266cf --- /dev/null +++ b/.changeset/adhoc-filter-click-table.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +feat: Filter table tile group-by cells into adhoc dashboard filters diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index c2b72b654b..6355045685 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -547,7 +547,7 @@ function inferValueColumns( ); } -function inferGroupColumns(meta: Array<{ name: string; type: string }>) { +export function inferGroupColumns(meta: Array<{ name: string; type: string }>) { return filterColumnMetaByType(meta, [ JSDataType.String, JSDataType.Map, diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 2768782fff..11e581301c 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -1321,6 +1321,7 @@ const Tile = ({ }) : undefined } + onFilter={onFilter} /> )} @@ -1500,6 +1501,7 @@ const Tile = ({ tileVariables, variableNames, searchRowContext, + onFilter, ], ); diff --git a/packages/app/src/HDXMultiSeriesTableChart.tsx b/packages/app/src/HDXMultiSeriesTableChart.tsx index 8211f473b7..9392dd5fad 100644 --- a/packages/app/src/HDXMultiSeriesTableChart.tsx +++ b/packages/app/src/HDXMultiSeriesTableChart.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useRef, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import Link from 'next/link'; import cx from 'classnames'; import { @@ -28,6 +28,9 @@ import { ColumnDef } from '@tanstack/react-table'; import { useVirtualizer } from '@tanstack/react-virtual'; import { CsvExportButton } from './components/CsvExportButton'; +import DBRowTableFieldWithPopover, { + type CellFilterHandler, +} from './components/DBTable/DBRowTableFieldWithPopover'; import TableHeader from './components/DBTable/TableHeader'; import { useCsvExport } from './hooks/useCsvExport'; import type { RowAction } from './hooks/useOnClickLinkBuilder'; @@ -61,6 +64,7 @@ export const Table = ({ variant = 'default', alternateRowBackground = false, className, + onCellFilter, }: { data: any[]; columns: { @@ -78,6 +82,9 @@ export const Table = ({ // Ordered conditional color rules evaluated against each cell's value // (last match wins). Resolves to `color` when no rule matches. colorRules?: ColorCondition[]; + // Expression (or PromQL label) the column's values filter on. Cells of a + // column with a key offer filter / exclude actions via `onCellFilter`. + filterKey?: string; }[]; groupColumnName?: string; // Returns the row click destination + a hover-hint description. When @@ -105,11 +112,14 @@ export const Table = ({ // Extra classes for the scroll container, e.g. `effect-pulse` while the // tile is refreshing. className?: string; + onCellFilter?: CellFilterHandler; }) => { const brandName = useBrandDisplayName(); const MIN_COLUMN_WIDTH_PX = 100; //we need a reference to the scrolling element for logic down below - const tableContainerRef = useRef(null); + const [tableContainer, setTableContainer] = useState( + null, + ); const truncatedData = useMemo(() => { if (data.length > MAX_TABLE_ROWS) { @@ -119,7 +129,7 @@ export const Table = ({ }, [data]); const isTruncated = truncatedData.length === MAX_TABLE_ROWS; - const tableWidth = tableContainerRef.current?.clientWidth; + const tableWidth = tableContainer?.clientWidth; const numColumns = columns.filter(c => c.visible !== false).length + 1; const dataColumnsWidthPerc = columns .filter(c => c.visible !== false) @@ -163,6 +173,7 @@ export const Table = ({ sortingFn, color, colorRules, + filterKey, }, i, ) => @@ -189,6 +200,29 @@ export const Table = ({ formattedValue = formatNumber(value, numberFormat); } + // Map / Array values render as JSON, which their `toString()` + // in a filter condition wouldn't match. + const isAdhocFilterable = + filterKey && + onCellFilter && + value != null && + typeof value !== 'object'; + + const content: React.ReactNode = isAdhocFilterable ? ( + + {formattedValue} + + ) : ( + formattedValue + ); + // Resolve this cell's color from the column config: ordered // rules first (last match wins), then the column's static // color, else no override. ClickHouse serializes numeric @@ -257,7 +291,7 @@ export const Table = ({ data-testid="dashboard-table-row-action" data-shape="external-link" > - {formattedValue} + {content} ); } @@ -273,7 +307,7 @@ export const Table = ({ data-testid="dashboard-table-row-action" data-shape="link" > - {formattedValue} + {content} ); } @@ -295,7 +329,7 @@ export const Table = ({ data-testid="dashboard-table-row-action" data-shape="button" > - {formattedValue} + {content} ); } @@ -312,7 +346,7 @@ export const Table = ({ data-testid="dashboard-table-row-action" data-shape="link" > - {formattedValue} + {content} ); } @@ -320,7 +354,7 @@ export const Table = ({ return (
- {formattedValue} + {content}
); }, @@ -376,7 +410,7 @@ export const Table = ({ const rowVirtualizer = useVirtualizer({ count: rows.length, - getScrollElement: () => tableContainerRef.current, + getScrollElement: () => tableContainer, estimateSize: useCallback(() => 58, []), overscan: 10, paddingEnd: 20, @@ -409,7 +443,7 @@ export const Table = ({ return (
({ jest.mock('nuqs', () => ({ parseAsString: 'parseAsString', useQueryState: () => [null, jest.fn()], + createParser: (opts: object) => opts, })); jest.mock('../source', () => ({ useSources: () => ({ data: mockSources }), diff --git a/packages/app/src/__tests__/HDXMultiSeriesTableChart.test.tsx b/packages/app/src/__tests__/HDXMultiSeriesTableChart.test.tsx index 06ba35410e..ae71b5da29 100644 --- a/packages/app/src/__tests__/HDXMultiSeriesTableChart.test.tsx +++ b/packages/app/src/__tests__/HDXMultiSeriesTableChart.test.tsx @@ -566,4 +566,83 @@ describe('HDXMultiSeriesTableChart
', () => { }); }); }); + + describe('cell filter actions', () => { + const filterColumns = [ + { ...baseColumns[0], filterKey: 'ServiceName' }, + baseColumns[1], + { + id: 'attrs', + dataKey: 'Attrs', + displayName: 'Attrs', + filterKey: 'Attrs', + }, + ]; + const filterData = [{ ServiceName: 'web', Count: 10, Attrs: { a: '1' } }]; + const renderTable = (onCellFilter = jest.fn()) => + renderWithMantine( +
{}} + />, + ); + + it('excludes a value by its column filter key', async () => { + const onCellFilter = jest.fn(); + renderTable(onCellFilter); + + await userEvent.hover(screen.getByText('web')); + await userEvent.click( + await screen.findByRole('button', { name: 'Exclude this value' }), + ); + + expect(onCellFilter).toHaveBeenCalledWith( + 'ServiceName', + 'web', + 'exclude', + ); + }); + + it("keeps row-action links' accessible name when cells are filterable", () => { + renderWithMantine( +
({ + url: '/search?q=web', + description: 'Search HyperDX Logs', + })} + sorting={[]} + onSortingChange={() => {}} + />, + ); + + expect(screen.getByRole('link', { name: 'web' })).toBeInTheDocument(); + }); + + it('offers no actions on columns without a filter key', async () => { + renderTable(); + + await userEvent.hover(screen.getByText('10')); + + // The popover opens through a transition, so wait it out + await expect( + screen.findByRole('button', { name: 'Copy field value' }), + ).rejects.toThrow(); + }); + + it('offers no actions on Map values', async () => { + renderTable(); + + await userEvent.hover(screen.getByText('{"a":"1"}')); + + await expect( + screen.findByRole('button', { name: 'Copy field value' }), + ).rejects.toThrow(); + }); + }); }); diff --git a/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx b/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx index 5180754ebb..1e45269d42 100644 --- a/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx +++ b/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx @@ -15,6 +15,13 @@ import { DBRowTableIconButton } from './DBRowTableIconButton'; import styles from '@styles/LogTable.module.scss'; +/** Filter on (`include`) or out (`exclude`) a cell's value, by its column's filter key. */ +export type CellFilterHandler = ( + key: string, + value: string, + action: 'include' | 'exclude', +) => void; + interface DBRowTableFieldWithPopoverProps { children: React.ReactNode; cellValue: unknown; @@ -24,6 +31,10 @@ interface DBRowTableFieldWithPopoverProps { filterValue?: string; tableContainerRef: HTMLDivElement | null; isChart?: boolean; + /** Leaves out the cell padding, for tables that pad their own cells. */ + inline?: boolean; + /** Overrides the side panel context's filter handler. */ + onPropertyAddClick?: CellFilterHandler; } const DBRowTableFieldWithPopover = ({ @@ -34,6 +45,8 @@ const DBRowTableFieldWithPopover = ({ columnName, filterValue, isChart = false, + inline = false, + onPropertyAddClick: onPropertyAddClickProp, }: DBRowTableFieldWithPopoverProps) => { const [opened, { close, open }] = useDisclosure(false); const [isCopied, setIsCopied] = useState(false); @@ -54,7 +67,10 @@ const DBRowTableFieldWithPopover = ({ }, []); // Get filter functionality from context - const { onPropertyAddClick } = use(RowSidePanelContext); + const { onPropertyAddClick: onPropertyAddClickContext } = + use(RowSidePanelContext); + const onPropertyAddClick = + onPropertyAddClickProp ?? onPropertyAddClickContext; // Check if we have both the column name and filter function available const canFilter = columnName && onPropertyAddClick && cellValue != null; @@ -151,6 +167,7 @@ const DBRowTableFieldWithPopover = ({ [styles.truncated]: !wrapLinesEnabled && !isChart, [styles.wrapped]: wrapLinesEnabled, [styles.chart]: isChart, + [styles.inline]: inline, })} >
` before giving up. - const projection = extractOuterSelectProjection(sqlWithReplacements); + const projection = extractOuterSelectProjection(sql); if (projection == null) { throw fullParseError; } return selectColumnsToAliasMap( `SELECT ${projection} FROM ${ALIAS_FALLBACK_TABLE}`, - jsonReplacementsToExpressions, - identifierReplacementsToExpressions, + jsonReplacements, + identifierReplacements, ); } } catch (e) { @@ -1094,6 +1116,119 @@ export function chSqlToAliasMap( return {}; } +const isNumberLiteral = ( + expr: unknown, +): expr is { type: 'number'; value: number } => + typeof expr === 'object' && + expr != null && + 'type' in expr && + expr.type === 'number' && + 'value' in expr && + typeof expr.value === 'number'; + +/** An item of a query's outer SELECT list, in projection order. */ +export type SelectColumn = { + /** The item's expression, without its alias. */ + expression: string; + /** Whether the outer GROUP BY lists the item (by expression, alias, or position). */ + isGroupBy: boolean; +}; + +/** + * The outer SELECT list of `chSql`, in order, so the i-th item lines up with + * the i-th result column even where ClickHouse renames an unaliased + * expression's column. Undefined when the query doesn't parse, has no GROUP BY + * (or `GROUP BY ALL`, which doesn't parse), or selects `*`. + */ +export function chSqlToSelectColumns( + chSql: ChSql | undefined, +): SelectColumn[] | undefined { + if (chSql == null || !chSql.sql) return undefined; + + try { + const { sql, restore, identifierReplacements } = + prepareChSqlForParser(chSql); + const parser = new SQLParser.Parser(); + const options = { + database: 'Postgresql', + parseOptions: { includeLocations: true }, + }; + const ast = parser.astify(sql, options); + if (Array.isArray(ast) || ast.type !== 'select') return undefined; + + const columns = ast.columns; + const groupBy = ast.groupby?.columns; + if (!Array.isArray(columns) || !groupBy?.length) return undefined; + + // Each backtick-quoted occurrence gets its own token, so identifiers + // compare by the name the token stands for. + const bareName = (name: string) => + identifierReplacements.names.get(name) ?? name; + // Unquoted, so `"svc"` matches `svc`; printing would keep the quotes. + const columnRefName = ( + expr: SQLParser.ExpressionValue, + ): string | undefined => { + if (expr.type !== 'column_ref' || !('column' in expr) || expr.table) { + return undefined; + } + const name = + typeof expr.column === 'string' ? expr.column : expr.column.expr.value; + return typeof name === 'string' ? bareName(name) : undefined; + }; + // Case-, whitespace- and outer-parentheses-insensitive, so `lower(x)` + // matches `(LOWER( x ))`. + const normalize = (expr: SQLParser.ExpressionValue) => + restore(parser.exprToSQL({ ...expr, parentheses: false }, options)) + .replace(/\s+/g, '') + .toLowerCase(); + const groupByNames = new Set(); + const groupByPositions = new Set(); + const groupByExpressions = new Set(); + for (const item of groupBy) { + // The parser's types omit the number literal of `GROUP BY 1`. + const literal: unknown = item; + if (isNumberLiteral(literal)) { + groupByPositions.add(literal.value - 1); + } else { + const name = columnRefName(item); + if (name != null) groupByNames.add(name); + groupByExpressions.add(normalize(item)); + } + } + + const selectColumns: SelectColumn[] = []; + for (const [index, column] of columns.entries()) { + const { expr } = column; + if ( + expr.type === 'column_ref' && + parser.exprToSQL(expr, options) === '*' + ) { + return undefined; + } + // Column references carry no location, so they are printed instead. + // The location of a binary expression can take in trailing whitespace. + const text = + expr.loc != null + ? sql.slice(expr.loc.start.offset, expr.loc.end.offset).trim() + : parser.exprToSQL(expr, options); + const alias = + typeof column.as === 'string' ? column.as : column.as?.value; + const name = columnRefName(expr); + selectColumns.push({ + expression: restore(text), + isGroupBy: + groupByPositions.has(index) || + (alias != null && groupByNames.has(bareName(alias))) || + (name != null && groupByNames.has(name)) || + groupByExpressions.has(normalize(expr)), + }); + } + return selectColumns; + } catch { + return undefined; + } +} + export type ColumnMetaType = { name: string; type: string }; export function filterColumnMetaByType( meta: Array, diff --git a/packages/common-utils/src/core/renderChartConfig.ts b/packages/common-utils/src/core/renderChartConfig.ts index 28e6c6985a..8de7551337 100644 --- a/packages/common-utils/src/core/renderChartConfig.ts +++ b/packages/common-utils/src/core/renderChartConfig.ts @@ -182,7 +182,7 @@ const quotedColumnName = (name: string) => `"${name.replace(/"/g, '""')}"`; // column named GROUP_ALIAS instead of projecting them as individual columns // (see translateHistogram), so grouped histogram rows never share a merge key // with grouped gauge/sum rows. -const isHistogramClassSelect = (select: { +export const isHistogramClassSelect = (select: { metricType?: MetricsDataType; }): boolean => select.metricType === MetricsDataType.Histogram ||