Skip to content

Commit 337be9a

Browse files
Deeplink to Search From Charts and Tables (#1410)
This PR adds the ability to click on tables or graphs across the app, and be able to deep link into the search page to view the correlated search events. https://github.com/user-attachments/assets/e5c7c2f2-9b59-42de-bb73-2fa4cb5d09a2 Things to try: 1. **Attributable Functions** (min, max, average, p95, etc) can all reliably give us a y value back to enable filtering directly to the y-values value 2. **Non-attributable Functions** (count, sum, etc) can’t rely on the y value since it depends on the group by logic. As such, these will simply allow you to search by the service without passing the y value for deeper analysis. 3. **Multiple Group Bys** (ex `ServiceName, SpanName`) 4. **Where Clause** (SQL or Lucene) 5. Do a **general test** across the app to ensure everywhere that uses charting behaves the same or better than before. Some things that don’t work: 1. **Aliases in the group by** - we are hitting limitations with `node-sql-parser` that causes the aliasMap to not generate correctly when using aggregate functions in most cases. We may need to either fix these issues or find an alternative solution. 2. **Expressions in Group By** - If you have expressions in the group by, the filtering will not work based on the current implementation, it does not factor in the result of the expression in the filter value. 3. **Filters are not reflected in the UI on Search** - When you deeplink, the filters in the sidebar do not reflect what is actually being queried. This is because the filter UI logic uses `IN` but the deeplinking uses `=` to achieve the best level of precision (and avoid false positives). As such, this is something that could be improved in the future. Fixes HDX-2844, HDX-2922
1 parent 586bcce commit 337be9a

11 files changed

Lines changed: 920 additions & 254 deletions

‎.changeset/cold-ducks-sniff.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/app": patch
3+
---
4+
5+
Add support for deeplinking to search page from most charts and tables

‎packages/app/src/ChartUtils.tsx‎

Lines changed: 220 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useMemo } from 'react';
22
import { add } from 'date-fns';
3+
import SqlString from 'sqlstring';
34
import { z } from 'zod';
45
import {
56
ColumnMetaType,
@@ -8,17 +9,20 @@ import {
89
JSDataType,
910
ResponseJSON,
1011
} from '@hyperdx/common-utils/dist/clickhouse';
12+
import { isMetricChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig';
1113
import {
1214
AggregateFunction as AggFnV2,
1315
ChartConfigWithDateRange,
1416
DisplayType,
17+
Filter,
1518
MetricsDataType as MetricsDataTypeV2,
1619
SavedChartConfig,
1720
SourceKind,
1821
SQLInterval,
1922
TSource,
2023
} from '@hyperdx/common-utils/dist/types';
2124
import { SegmentedControl } from '@mantine/core';
25+
import { notifications } from '@mantine/notifications';
2226

2327
import { getMetricNameSql } from './otelSemanticConventions';
2428
import {
@@ -45,16 +49,20 @@ export const TABLES = [
4549
];
4650

4751
export const AGG_FNS = [
48-
{ value: 'count' as const, label: 'Count of Events' },
49-
{ value: 'sum' as const, label: 'Sum' },
52+
{ value: 'count' as const, label: 'Count of Events', isAttributable: false },
53+
{ value: 'sum' as const, label: 'Sum', isAttributable: false },
5054
{ value: 'p99' as const, label: '99th Percentile' },
5155
{ value: 'p95' as const, label: '95th Percentile' },
5256
{ value: 'p90' as const, label: '90th Percentile' },
5357
{ value: 'p50' as const, label: 'Median' },
5458
{ value: 'avg' as const, label: 'Average' },
5559
{ value: 'max' as const, label: 'Maximum' },
5660
{ value: 'min' as const, label: 'Minimum' },
57-
{ value: 'count_distinct' as const, label: 'Count Distinct' },
61+
{
62+
value: 'count_distinct' as const,
63+
label: 'Count Distinct',
64+
isAttributable: false,
65+
},
5866
{ value: 'any' as const, label: 'Any' },
5967
{ value: 'none' as const, label: 'None' },
6068
];
@@ -269,6 +277,9 @@ export function convertDateRangeToGranularityString(
269277
return Granularity.ThirtyDay;
270278
}
271279

280+
export const ChartKeyJoiner = ' · ';
281+
export const PreviousPeriodSuffix = ' (previous)';
282+
272283
export function convertGranularityToSeconds(granularity: SQLInterval): number {
273284
const [num, unit] = granularity.split(' ');
274285
const numInt = Number.parseInt(num);
@@ -462,7 +473,9 @@ export const isAggregateFunction = (value: string) => {
462473
'kurtSamp',
463474
];
464475

465-
return fns.some(fn => value.includes(fn + '('));
476+
// Make case-insensitive since ClickHouse function names are case-insensitive
477+
const lowerValue = value.toLowerCase();
478+
return fns.some(fn => lowerValue.includes(fn.toLowerCase() + '('));
466479
};
467480

468481
export const INTEGER_NUMBER_FORMAT: NumberFormat = {
@@ -636,8 +649,8 @@ function addResponseToFormattedData({
636649
// Simplify the display name if there's only one series and a group by
637650
...(isSingleValueColumn && hasGroupColumns ? [] : [valueColumn.name]),
638651
...groupColumns.map(g => row[g.name]),
639-
].join(' · ');
640-
const previousPeriodKey = `${currentPeriodKey} (previous)`;
652+
].join(ChartKeyJoiner);
653+
const previousPeriodKey = `${currentPeriodKey}${PreviousPeriodSuffix}`;
641654
const keyName = isPreviousPeriod ? previousPeriodKey : currentPeriodKey;
642655

643656
// UInt64 are returned as strings, we'll convert to number
@@ -691,6 +704,9 @@ export function formatResponseForTimeChart({
691704
}
692705

693706
const timestampColumn = inferTimestampColumn(meta);
707+
const valueColumns = inferValueColumns(meta) ?? [];
708+
const groupColumns = inferGroupColumns(meta) ?? [];
709+
const isSingleValueColumn = valueColumns.length === 1;
694710

695711
if (timestampColumn == null) {
696712
throw new Error(
@@ -776,6 +792,9 @@ export function formatResponseForTimeChart({
776792
graphResults,
777793
timestampColumn,
778794
lineData: sortedLineDataWithColors,
795+
groupColumns: groupColumns.map(g => g.name),
796+
valueColumns: valueColumns.map(v => v.name),
797+
isSingleValueColumn,
779798
};
780799
}
781800

@@ -919,3 +938,198 @@ export const convertV1ChartConfigToV2 = (
919938
}
920939
throw new Error(`unsupported table in v2: ${firstSeries.table}`);
921940
};
941+
942+
/**
943+
* Build search URL for viewing events based on group-by values
944+
* Used by both chart clicks and table row clicks
945+
*/
946+
export function buildEventsSearchUrl({
947+
source,
948+
config,
949+
dateRange,
950+
groupFilters,
951+
valueRangeFilter,
952+
}: {
953+
source: TSource;
954+
config: ChartConfigWithDateRange;
955+
dateRange: [Date, Date];
956+
groupFilters?: Array<{ column: string; value: any }>;
957+
valueRangeFilter?: { expression: string; value: number; threshold?: number };
958+
}): string | null {
959+
if (!source?.id) {
960+
return null;
961+
}
962+
963+
const isMetricChart = isMetricChartConfig(config);
964+
if (isMetricChart && source?.logSourceId == null) {
965+
notifications.show({
966+
color: 'yellow',
967+
message: 'No log source is associated with the selected metric source.',
968+
});
969+
return null;
970+
}
971+
972+
let where = config.where;
973+
let whereLanguage = config.whereLanguage || 'lucene';
974+
if (
975+
where.length === 0 &&
976+
Array.isArray(config.select) &&
977+
config.select.length === 1
978+
) {
979+
where = config.select[0].aggCondition ?? '';
980+
whereLanguage = config.select[0].aggConditionLanguage ?? 'lucene';
981+
}
982+
983+
const additionalFilters: Filter[] = [];
984+
985+
// Add group-by column filters
986+
if (groupFilters && groupFilters.length > 0) {
987+
groupFilters.forEach(({ column, value }) => {
988+
if (column && value != null) {
989+
// Can't use SQLString.escape here because the search endpoint relies on exist match for UI
990+
const condition = `${column} IN (${SqlString.escape(value)})`;
991+
additionalFilters.push({ type: 'sql', condition });
992+
}
993+
});
994+
}
995+
996+
// Add Y-axis value range filter (±threshold) for charts
997+
if (valueRangeFilter) {
998+
const { expression, value, threshold = 0.05 } = valueRangeFilter;
999+
const hasAggregateFunction = isAggregateFunction(expression);
1000+
1001+
if (!hasAggregateFunction) {
1002+
const lowerBound = value * (1 - threshold);
1003+
const upperBound = value * (1 + threshold);
1004+
// Can't use SQLString.escape here because the search endpoint relies on exist match for UI
1005+
const condition = `${expression} BETWEEN ${SqlString.escape(lowerBound)} AND ${SqlString.escape(upperBound)}`;
1006+
1007+
additionalFilters.push({
1008+
type: 'sql',
1009+
condition,
1010+
});
1011+
}
1012+
}
1013+
1014+
// Get the time range
1015+
const from = dateRange[0].getTime();
1016+
const to = dateRange[1].getTime();
1017+
1018+
const params: Record<string, string> = {
1019+
source: source?.id ?? '',
1020+
where: where,
1021+
whereLanguage: whereLanguage,
1022+
filters: JSON.stringify([...(config.filters ?? []), ...additionalFilters]),
1023+
isLive: 'false',
1024+
from: from.toString(),
1025+
to: to.toString(),
1026+
};
1027+
1028+
// If its a metric chart, we don't pass the where and filters
1029+
if (isMetricChart) {
1030+
params.where = '';
1031+
params.whereLanguage = 'lucene';
1032+
params.filters = JSON.stringify([]);
1033+
params.source = source?.logSourceId ?? '';
1034+
}
1035+
1036+
// Include the select parameter if provided to preserve custom columns
1037+
// eventTableSelect is used for charts that override select (like histograms with count)
1038+
// to preserve the original table's select expression
1039+
if (config.eventTableSelect) {
1040+
params.select = config.eventTableSelect;
1041+
}
1042+
1043+
return `/search?${new URLSearchParams(params).toString()}`;
1044+
}
1045+
1046+
/**
1047+
* Extract group column names from chart config's groupBy field
1048+
* Handles both string format ("col1, col2") and array format ([{ valueExpression: "col1" }, ...])
1049+
*/
1050+
function extractGroupColumns(
1051+
groupBy: ChartConfigWithDateRange['groupBy'],
1052+
): string[] {
1053+
if (!groupBy) return [];
1054+
1055+
if (typeof groupBy === 'string') {
1056+
// String GROUP BY: "col1, col2"
1057+
return groupBy.split(',').map(v => v.trim());
1058+
}
1059+
1060+
// Array GROUP BY: [{ valueExpression: "col1" }, ...] or ["col1", ...]
1061+
return groupBy.map(g => (typeof g === 'string' ? g : g.valueExpression));
1062+
}
1063+
1064+
/**
1065+
* Build search URL from a table row click
1066+
* Extracts group filters and value range filter from the row data
1067+
*/
1068+
export function buildTableRowSearchUrl({
1069+
row,
1070+
source,
1071+
config,
1072+
dateRange,
1073+
}: {
1074+
row: Record<string, any>;
1075+
source: TSource | undefined;
1076+
config: ChartConfigWithDateRange;
1077+
dateRange: [Date, Date];
1078+
}): string | null {
1079+
if (!source?.id) {
1080+
return null;
1081+
}
1082+
1083+
// Extract group-by column names and build filters from row values
1084+
const groupFilters: Array<{ column: string; value: any }> = [];
1085+
const groupColumns = extractGroupColumns(config.groupBy);
1086+
1087+
groupColumns.forEach(col => {
1088+
if (row[col] != null) {
1089+
groupFilters.push({ column: col, value: row[col] });
1090+
}
1091+
});
1092+
1093+
// Build value range filter from the first select column
1094+
let valueRangeFilter: { expression: string; value: number } | undefined;
1095+
1096+
const firstSelect = config.select?.[0];
1097+
if (firstSelect) {
1098+
const aggFn =
1099+
typeof firstSelect === 'string' ? undefined : firstSelect.aggFn;
1100+
const isAttributable =
1101+
AGG_FNS.find(fn => fn.value === aggFn)?.isAttributable !== false;
1102+
1103+
if (isAttributable) {
1104+
const valueExpression =
1105+
typeof firstSelect === 'string'
1106+
? firstSelect
1107+
: firstSelect.valueExpression;
1108+
1109+
// Extract group column names to exclude them from value columns
1110+
const groupColumnSet = new Set(extractGroupColumns(config.groupBy));
1111+
1112+
// Find the first value column (non-group column)
1113+
const valueColumn = Object.keys(row).find(
1114+
key => !groupColumnSet.has(key),
1115+
);
1116+
1117+
const rowValue = valueColumn ? row[valueColumn] : undefined;
1118+
1119+
if (rowValue != null && typeof rowValue === 'number') {
1120+
valueRangeFilter = {
1121+
expression: valueExpression,
1122+
value: rowValue,
1123+
};
1124+
}
1125+
}
1126+
}
1127+
1128+
return buildEventsSearchUrl({
1129+
source,
1130+
config,
1131+
dateRange,
1132+
groupFilters,
1133+
valueRangeFilter,
1134+
});
1135+
}

‎packages/app/src/DBDashboardPage.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import useDashboardFilters from './hooks/useDashboardFilters';
6868
import { useDashboardRefresh } from './hooks/useDashboardRefresh';
6969
import { parseAsStringWithNewLines } from './utils/queryParsers';
7070
import api from './api';
71-
import { DEFAULT_CHART_CONFIG } from './ChartUtils';
71+
import { buildTableRowSearchUrl, DEFAULT_CHART_CONFIG } from './ChartUtils';
7272
import { IS_LOCAL_MODE } from './config';
7373
import { useDashboard } from './dashboard';
7474
import DashboardFilters from './DashboardFilters';
@@ -321,7 +321,17 @@ const Tile = forwardRef(
321321
)}
322322
{queriedConfig?.displayType === DisplayType.Table && (
323323
<Box p="xs" h="100%">
324-
<DBTableChart config={queriedConfig} />
324+
<DBTableChart
325+
config={queriedConfig}
326+
getRowSearchLink={row =>
327+
buildTableRowSearchUrl({
328+
row,
329+
source,
330+
config: queriedConfig,
331+
dateRange: dateRange,
332+
})
333+
}
334+
/>
325335
</Box>
326336
)}
327337
{queriedConfig?.displayType === DisplayType.Number && (

‎packages/app/src/HDXMultiSeriesTableChart.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const Table = ({
4242
visible?: boolean;
4343
}[];
4444
groupColumnName?: string;
45-
getRowSearchLink?: (row: any) => string;
45+
getRowSearchLink?: (row: any) => string | null;
4646
tableBottom?: React.ReactNode;
4747
sorting: SortingState;
4848
onSortingChange: (sorting: SortingState) => void;
@@ -108,10 +108,15 @@ export const Table = ({
108108
if (getRowSearchLink == null) {
109109
return formattedValue;
110110
}
111+
const link = getRowSearchLink(row.original);
112+
113+
if (!link) {
114+
return formattedValue;
115+
}
111116

112117
return (
113118
<Link
114-
href={getRowSearchLink(row.original)}
119+
href={link}
115120
passHref
116121
className={'align-top overflow-hidden py-1 pe-3'}
117122
style={{
@@ -251,6 +256,7 @@ export const Table = ({
251256
)}
252257
{items.map(virtualRow => {
253258
const row = rows[virtualRow.index] as TableRow<any>;
259+
const link = getRowSearchLink?.(row.original);
254260
return (
255261
<tr
256262
key={virtualRow.key}
@@ -261,7 +267,7 @@ export const Table = ({
261267
{row.getVisibleCells().map(cell => {
262268
return (
263269
<td key={cell.id} title={`${cell.getValue()}`}>
264-
{getRowSearchLink == null ? (
270+
{!link ? (
265271
<div
266272
className={cx('align-top overflow-hidden py-1 pe-3', {
267273
'text-break': wrapLinesEnabled,
@@ -275,7 +281,7 @@ export const Table = ({
275281
</div>
276282
) : (
277283
<Link
278-
href={getRowSearchLink(row.original)}
284+
href={link}
279285
passHref
280286
className="align-top overflow-hidden py-1 pe-3"
281287
style={{

0 commit comments

Comments
 (0)