From 157d5330fd860d87afe8d12e65c4cf70aa5e5491 Mon Sep 17 00:00:00 2001 From: Mike Shi Date: Sat, 27 Sep 2025 00:08:04 -0700 Subject: [PATCH 1/3] feat: Add support for filter by parsed JSON string --- .changeset/young-tools-sneeze.md | 5 ++ .../app/src/components/DBRowJsonViewer.tsx | 86 ++++++++++++++++--- packages/app/src/components/HyperJson.tsx | 55 +++++++++++- 3 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 .changeset/young-tools-sneeze.md diff --git a/.changeset/young-tools-sneeze.md b/.changeset/young-tools-sneeze.md new file mode 100644 index 0000000000..c520588be8 --- /dev/null +++ b/.changeset/young-tools-sneeze.md @@ -0,0 +1,5 @@ +--- +"@hyperdx/app": minor +--- + +feat: Add support for filter by parsed JSON string diff --git a/packages/app/src/components/DBRowJsonViewer.tsx b/packages/app/src/components/DBRowJsonViewer.tsx index 825b8b71db..93c50b8919 100644 --- a/packages/app/src/components/DBRowJsonViewer.tsx +++ b/packages/app/src/components/DBRowJsonViewer.tsx @@ -152,7 +152,7 @@ export function DBRowJsonViewer({ }, [data, debouncedFilter]); const getLineActions = useCallback( - ({ keyPath, value }) => { + ({ keyPath, value, isInParsedJson, parsedJsonRootPath }) => { const actions: LineAction[] = []; const fieldPath = mergePath(keyPath, jsonColumns); const isJsonColumn = @@ -177,10 +177,32 @@ export function DBRowJsonViewer({ ), title: 'Add to Filters', onClick: () => { - onPropertyAddClick( - isJsonColumn ? `toString(${fieldPath})` : fieldPath, - value, - ); + let filterFieldPath = fieldPath; + + // Handle parsed JSON from string columns using JSONExtractString + if (isInParsedJson && parsedJsonRootPath) { + // Extract the nested path within the parsed JSON + const nestedPath = keyPath.slice(parsedJsonRootPath.length); + if (nestedPath.length > 0) { + // Build JSONExtractString query for the nested path + const baseColumn = + parsedJsonRootPath[parsedJsonRootPath.length - 1]; + const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); + filterFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + } else { + // We're at the root of the parsed JSON, treat as string + filterFieldPath = isJsonColumn + ? `toString(${fieldPath})` + : fieldPath; + } + } else { + // Regular JSON column or non-JSON field + filterFieldPath = isJsonColumn + ? `toString(${fieldPath})` + : fieldPath; + } + + onPropertyAddClick(filterFieldPath, value); notifications.show({ color: 'green', message: `Added "${fieldPath} = ${value}" to filters`, @@ -200,13 +222,29 @@ export function DBRowJsonViewer({ ), title: 'Search for this value only', onClick: () => { - let defaultWhere = `${fieldPath} = ${ + let searchFieldPath = fieldPath; + + // Handle parsed JSON from string columns using JSONExtractString + if (isInParsedJson && parsedJsonRootPath) { + const nestedPath = keyPath.slice(parsedJsonRootPath.length); + if (nestedPath.length > 0) { + const baseColumn = + parsedJsonRootPath[parsedJsonRootPath.length - 1]; + const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); + searchFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + } + } + + let defaultWhere = `${searchFieldPath} = ${ typeof value === 'string' ? `'${value}'` : value }`; // FIXME: TOTAL HACK - if (fieldPath == 'Timestamp' || fieldPath == 'TimestampTime') { - defaultWhere = `${fieldPath} = parseDateTime64BestEffort('${value}', 9)`; + if ( + searchFieldPath == 'Timestamp' || + searchFieldPath == 'TimestampTime' + ) { + defaultWhere = `${searchFieldPath} = parseDateTime64BestEffort('${value}', 9)`; } router.push( generateSearchUrl({ @@ -225,10 +263,23 @@ export function DBRowJsonViewer({ label: , title: 'Chart', onClick: () => { + let chartFieldPath = fieldPath; + + // Handle parsed JSON from string columns using JSONExtractString + if (isInParsedJson && parsedJsonRootPath) { + const nestedPath = keyPath.slice(parsedJsonRootPath.length); + if (nestedPath.length > 0) { + const baseColumn = + parsedJsonRootPath[parsedJsonRootPath.length - 1]; + const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); + chartFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + } + } + router.push( generateChartUrl({ aggFn: 'avg', - field: fieldPath, + field: chartFieldPath, groupBy: [], }), ); @@ -238,7 +289,20 @@ export function DBRowJsonViewer({ // Toggle column action (non-object values) if (toggleColumn && typeof value !== 'object') { - const isIncluded = displayedColumns?.includes(fieldPath); + let columnFieldPath = fieldPath; + + // Handle parsed JSON from string columns using JSONExtractString + if (isInParsedJson && parsedJsonRootPath) { + const nestedPath = keyPath.slice(parsedJsonRootPath.length); + if (nestedPath.length > 0) { + const baseColumn = + parsedJsonRootPath[parsedJsonRootPath.length - 1]; + const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); + columnFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + } + } + + const isIncluded = displayedColumns?.includes(columnFieldPath); actions.push({ key: 'toggle-column', label: isIncluded ? ( @@ -256,7 +320,7 @@ export function DBRowJsonViewer({ ? `Remove ${fieldPath} column from results table` : `Add ${fieldPath} column to results table`, onClick: () => { - toggleColumn(fieldPath); + toggleColumn(columnFieldPath); notifications.show({ color: 'green', message: `Column "${fieldPath}" ${ diff --git a/packages/app/src/components/HyperJson.tsx b/packages/app/src/components/HyperJson.tsx index 2d70e9556e..91de551bb6 100644 --- a/packages/app/src/components/HyperJson.tsx +++ b/packages/app/src/components/HyperJson.tsx @@ -25,6 +25,8 @@ export type GetLineActions = (arg0: { key: string; keyPath: string[]; value: any; + isInParsedJson?: boolean; + parsedJsonRootPath?: string[]; }) => LineAction[]; // Store common state in an atom so that it can be shared between components @@ -90,19 +92,36 @@ const LineMenu = React.memo( keyName, keyPath, value, + isInParsedJson, + parsedJsonRootPath, }: { keyName: string; keyPath: string[]; value: any; + isInParsedJson?: boolean; + parsedJsonRootPath?: string[]; }) => { const { getLineActions } = useAtomValue(hyperJsonAtom); const lineActions = React.useMemo(() => { if (getLineActions) { - return getLineActions({ key: keyName, keyPath, value }); + return getLineActions({ + key: keyName, + keyPath, + value, + isInParsedJson, + parsedJsonRootPath, + }); } return []; - }, [getLineActions, keyName, keyPath, value]); + }, [ + getLineActions, + keyName, + keyPath, + value, + isInParsedJson, + parsedJsonRootPath, + ]); return (
@@ -130,11 +149,15 @@ const Line = React.memo( keyPath: parentKeyPath, value, disableMenu, + isInParsedJson = false, + parsedJsonRootPath = [], }: { keyName: string; keyPath: string[]; value: any; disableMenu: boolean; + isInParsedJson?: boolean; + parsedJsonRootPath?: string[]; }) => { const { normallyExpanded } = useAtomValue(hyperJsonAtom); @@ -195,6 +218,16 @@ const Line = React.memo( [keyName, parentKeyPath], ); + // Determine the context for nested parsed JSON + const childIsInParsedJson = isInParsedJson || isStringValueValidJson; + const childParsedJsonRootPath = React.useMemo(() => { + if (isStringValueValidJson) { + // This is the start of a new parsed JSON context + return keyPath; + } + return parsedJsonRootPath; + }, [isStringValueValidJson, keyPath, parsedJsonRootPath]); + // Hide LineMenu when selecting text in the value const valueRef = React.useRef(null); const [isSelectingValue, setIsSelectingValue] = React.useState(false); @@ -256,14 +289,22 @@ const Line = React.memo( )}
{hovered && !disableMenu && !isSelectingValue && ( - + )} {isExpanded && isExpandable && ( )} @@ -276,10 +317,14 @@ function TreeNode({ data, keyPath = [], disableMenu = false, + isInParsedJson = false, + parsedJsonRootPath = [], }: { data: object; keyPath?: string[]; disableMenu?: boolean; + isInParsedJson?: boolean; + parsedJsonRootPath?: string[]; }) { const [isExpanded, setIsExpanded] = React.useState(false); @@ -300,6 +345,8 @@ function TreeNode({ value={value} keyPath={keyPath} disableMenu={disableMenu} + isInParsedJson={isInParsedJson} + parsedJsonRootPath={parsedJsonRootPath} /> ))} {originalLength > MAX_TREE_NODE_ITEMS && !isExpanded && ( From be18f36a116d717487e536f683f3a5b4c9b61c29 Mon Sep 17 00:00:00 2001 From: Mike Shi Date: Sat, 27 Sep 2025 00:20:12 -0700 Subject: [PATCH 2/3] DRY code --- .../app/src/components/DBRowJsonViewer.tsx | 64 +++++++++++-------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/packages/app/src/components/DBRowJsonViewer.tsx b/packages/app/src/components/DBRowJsonViewer.tsx index 93c50b8919..ed71b9a704 100644 --- a/packages/app/src/components/DBRowJsonViewer.tsx +++ b/packages/app/src/components/DBRowJsonViewer.tsx @@ -20,6 +20,20 @@ import { notifications } from '@mantine/notifications'; import HyperJson, { GetLineActions, LineAction } from '@/components/HyperJson'; import { mergePath } from '@/utils'; +function buildJSONExtractStringQuery( + keyPath: string[], + parsedJsonRootPath: string[], +): string | null { + const nestedPath = keyPath.slice(parsedJsonRootPath.length); + if (nestedPath.length === 0) { + return null; // No nested path to extract + } + + const baseColumn = parsedJsonRootPath[parsedJsonRootPath.length - 1]; + const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); + return `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; +} + import { RowSidePanelContext } from './DBRowSidePanel'; function filterObjectRecursively(obj: any, filter: string): any { @@ -181,14 +195,12 @@ export function DBRowJsonViewer({ // Handle parsed JSON from string columns using JSONExtractString if (isInParsedJson && parsedJsonRootPath) { - // Extract the nested path within the parsed JSON - const nestedPath = keyPath.slice(parsedJsonRootPath.length); - if (nestedPath.length > 0) { - // Build JSONExtractString query for the nested path - const baseColumn = - parsedJsonRootPath[parsedJsonRootPath.length - 1]; - const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); - filterFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + const jsonQuery = buildJSONExtractStringQuery( + keyPath, + parsedJsonRootPath, + ); + if (jsonQuery) { + filterFieldPath = jsonQuery; } else { // We're at the root of the parsed JSON, treat as string filterFieldPath = isJsonColumn @@ -226,12 +238,12 @@ export function DBRowJsonViewer({ // Handle parsed JSON from string columns using JSONExtractString if (isInParsedJson && parsedJsonRootPath) { - const nestedPath = keyPath.slice(parsedJsonRootPath.length); - if (nestedPath.length > 0) { - const baseColumn = - parsedJsonRootPath[parsedJsonRootPath.length - 1]; - const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); - searchFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + const jsonQuery = buildJSONExtractStringQuery( + keyPath, + parsedJsonRootPath, + ); + if (jsonQuery) { + searchFieldPath = jsonQuery; } } @@ -267,12 +279,12 @@ export function DBRowJsonViewer({ // Handle parsed JSON from string columns using JSONExtractString if (isInParsedJson && parsedJsonRootPath) { - const nestedPath = keyPath.slice(parsedJsonRootPath.length); - if (nestedPath.length > 0) { - const baseColumn = - parsedJsonRootPath[parsedJsonRootPath.length - 1]; - const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); - chartFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + const jsonQuery = buildJSONExtractStringQuery( + keyPath, + parsedJsonRootPath, + ); + if (jsonQuery) { + chartFieldPath = jsonQuery; } } @@ -293,12 +305,12 @@ export function DBRowJsonViewer({ // Handle parsed JSON from string columns using JSONExtractString if (isInParsedJson && parsedJsonRootPath) { - const nestedPath = keyPath.slice(parsedJsonRootPath.length); - if (nestedPath.length > 0) { - const baseColumn = - parsedJsonRootPath[parsedJsonRootPath.length - 1]; - const jsonPathArgs = nestedPath.map(p => `'${p}'`).join(', '); - columnFieldPath = `JSONExtractString(${baseColumn}, ${jsonPathArgs})`; + const jsonQuery = buildJSONExtractStringQuery( + keyPath, + parsedJsonRootPath, + ); + if (jsonQuery) { + columnFieldPath = jsonQuery; } } From 9cfc42e484bb92e4d4af827e16abf67de38040f6 Mon Sep 17 00:00:00 2001 From: Mike Shi Date: Sat, 27 Sep 2025 00:36:51 -0700 Subject: [PATCH 3/3] fix flaky e2e test --- packages/app/tests/e2e/features/traces-workflow.spec.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/app/tests/e2e/features/traces-workflow.spec.ts b/packages/app/tests/e2e/features/traces-workflow.spec.ts index c0a5c6ce97..9e87e9eb84 100644 --- a/packages/app/tests/e2e/features/traces-workflow.spec.ts +++ b/packages/app/tests/e2e/features/traces-workflow.spec.ts @@ -25,6 +25,9 @@ test.describe('Advanced Search Workflow - Traces', { tag: '@traces' }, () => { await expect(searchInput).toBeVisible(); await searchInput.fill('Order'); + await page.locator('[data-testid="time-picker-input"]').click(); + await page.locator('text=Last 1 days').click(); + const searchSubmitButton = page.locator( '[data-testid="search-submit-button"]', );