Skip to content

Commit fd9f290

Browse files
authored
feat: Add query params, sorting, and placeholders to Raw-SQL tables (#1857)
Closes HDX-3580 Closes HDX-3034 ## Summary This PR enhances the support for SQL-driven tables 1. The selected date range can now be referenced in the query with query params. The available query params are outlined in a new section above the SQL input. 2. The table no longer OOMs on large result sets (it is now truncated to the first 10k results), or crashes when selecting columns that are Map or JSON type 3. The table can now be sorted client-side for sql-driven tables 4. There is now placeholder / example SQL in the input ### Screenshots or video https://github.com/user-attachments/assets/4f39fd0a-d33e-4f8c-9e91-84143d23e293 ### How to test locally or on Vercel This feature can be tested locally or on the preview environment without any special toggles. ### References - Linear Issue: HDX-3580 - Related PRs:
1 parent 6e4660f commit fd9f290

14 files changed

Lines changed: 735 additions & 97 deletions

‎.changeset/blue-seals-double.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@hyperdx/common-utils": patch
3+
"@hyperdx/api": patch
4+
"@hyperdx/app": patch
5+
---
6+
7+
feat: Add query params, sorting, and placeholders to Raw-SQL tables

‎packages/app/src/HDXMultiSeriesTableChart.tsx‎

Lines changed: 119 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { IconDownload, IconTextWrap } from '@tabler/icons-react';
66
import {
77
flexRender,
88
getCoreRowModel,
9+
getSortedRowModel,
910
Getter,
1011
Row,
1112
Row as TableRow,
13+
SortingFnOption,
1214
SortingState,
15+
TableOptions,
1316
useReactTable,
1417
} from '@tanstack/react-table';
1518
import { ColumnDef } from '@tanstack/react-table';
@@ -25,12 +28,16 @@ import { formatNumber } from './utils';
2528

2629
export type TableVariant = 'default' | 'muted';
2730

31+
// Arbitrary limit to prevent OOM crashes for very large result sets. Most result sets should be paginated anyway.
32+
export const MAX_TABLE_ROWS = 10_000;
33+
2834
export const Table = ({
2935
data,
3036
groupColumnName,
3137
columns,
3238
getRowSearchLink,
3339
tableBottom,
40+
enableClientSideSorting = false,
3441
sorting,
3542
onSortingChange,
3643
variant = 'default',
@@ -44,10 +51,12 @@ export const Table = ({
4451
numberFormat?: NumberFormat;
4552
columnWidthPercent?: number;
4653
visible?: boolean;
54+
sortingFn?: SortingFnOption<any>;
4755
}[];
4856
groupColumnName?: string;
4957
getRowSearchLink?: (row: any) => string | null;
5058
tableBottom?: React.ReactNode;
59+
enableClientSideSorting?: boolean;
5160
sorting: SortingState;
5261
onSortingChange: (sorting: SortingState) => void;
5362
variant?: TableVariant;
@@ -57,6 +66,14 @@ export const Table = ({
5766
//we need a reference to the scrolling element for logic down below
5867
const tableContainerRef = useRef<HTMLDivElement>(null);
5968

69+
const truncatedData = useMemo(() => {
70+
if (data.length > MAX_TABLE_ROWS) {
71+
return data.slice(0, MAX_TABLE_ROWS);
72+
}
73+
return data;
74+
}, [data]);
75+
const isTruncated = truncatedData.length === MAX_TABLE_ROWS;
76+
6077
const tableWidth = tableContainerRef.current?.clientWidth;
6178
const numColumns = columns.filter(c => c.visible !== false).length + 1;
6279
const dataColumnsWidthPerc = columns
@@ -92,95 +109,120 @@ export const Table = ({
92109
.filter(c => c.visible !== false)
93110
.map(
94111
(
95-
{ id, dataKey, displayName, numberFormat, columnWidthPercent },
112+
{
113+
id,
114+
dataKey,
115+
displayName,
116+
numberFormat,
117+
columnWidthPercent,
118+
sortingFn,
119+
},
96120
i,
97-
) => ({
98-
id: id,
99-
accessorKey: dataKey,
100-
header: displayName,
101-
accessorFn: (row: any) => row[dataKey],
102-
cell: ({
103-
getValue,
104-
row,
105-
}: {
106-
getValue: Getter<number>;
107-
row: Row<any>;
108-
}) => {
109-
const value = getValue();
110-
let formattedValue: string | number | null = value ?? null;
111-
if (numberFormat) {
112-
formattedValue = formatNumber(value, numberFormat);
113-
}
114-
if (getRowSearchLink == null) {
115-
return formattedValue;
116-
}
117-
const link = getRowSearchLink(row.original);
121+
) =>
122+
({
123+
id: id,
124+
accessorKey: dataKey,
125+
header: displayName,
126+
accessorFn: (row: any) => row[dataKey],
127+
sortingFn,
128+
cell: ({
129+
getValue,
130+
row,
131+
}: {
132+
getValue: Getter<number>;
133+
row: Row<any>;
134+
}) => {
135+
const value = getValue();
136+
let formattedValue: string | number | null = value ?? null;
137+
138+
// Table cannot accept values which are objects or arrays, so we need to stringify them
139+
if (typeof value !== 'string' && typeof value !== 'number') {
140+
formattedValue = JSON.stringify(value);
141+
} else if (numberFormat) {
142+
formattedValue = formatNumber(value, numberFormat);
143+
}
144+
145+
if (getRowSearchLink == null) {
146+
return formattedValue;
147+
}
148+
const link = getRowSearchLink(row.original);
149+
150+
if (!link) {
151+
return (
152+
<div
153+
className={cx('align-top overflow-hidden py-1 pe-3', {
154+
'text-break': wrapLinesEnabled,
155+
'text-truncate': !wrapLinesEnabled,
156+
})}
157+
>
158+
{formattedValue}
159+
</div>
160+
);
161+
}
118162

119-
if (!link) {
120163
return (
121-
<div
164+
<Link
165+
href={link}
122166
className={cx('align-top overflow-hidden py-1 pe-3', {
123167
'text-break': wrapLinesEnabled,
124168
'text-truncate': !wrapLinesEnabled,
125169
})}
170+
style={{
171+
display: 'block',
172+
color: 'inherit',
173+
textDecoration: 'none',
174+
}}
126175
>
127176
{formattedValue}
128-
</div>
177+
</Link>
129178
);
130-
}
131-
132-
return (
133-
<Link
134-
href={link}
135-
className={cx('align-top overflow-hidden py-1 pe-3', {
136-
'text-break': wrapLinesEnabled,
137-
'text-truncate': !wrapLinesEnabled,
138-
})}
139-
style={{
140-
display: 'block',
141-
color: 'inherit',
142-
textDecoration: 'none',
143-
}}
144-
>
145-
{formattedValue}
146-
</Link>
147-
);
148-
},
149-
size:
150-
i === numColumns - 2
151-
? UNDEFINED_WIDTH
152-
: tableWidth != null && columnWidthPercent != null
153-
? Math.max(
154-
tableWidth * (columnWidthPercent / 100),
155-
MIN_COLUMN_WIDTH_PX,
156-
)
157-
: tableWidth != null
158-
? tableWidth / numColumns
159-
: 200,
160-
enableResizing: i !== numColumns - 2,
161-
}),
179+
},
180+
size:
181+
i === numColumns - 2
182+
? UNDEFINED_WIDTH
183+
: tableWidth != null && columnWidthPercent != null
184+
? Math.max(
185+
tableWidth * (columnWidthPercent / 100),
186+
MIN_COLUMN_WIDTH_PX,
187+
)
188+
: tableWidth != null
189+
? tableWidth / numColumns
190+
: 200,
191+
enableResizing: i !== numColumns - 2,
192+
}) satisfies ColumnDef<any>,
162193
),
163194
];
164195

196+
const sortingParams: Partial<TableOptions<any>> = useMemo(() => {
197+
return enableClientSideSorting
198+
? {
199+
enableSorting: true,
200+
getSortedRowModel: getSortedRowModel(),
201+
}
202+
: {
203+
enableSorting: true,
204+
manualSorting: true,
205+
onSortingChange: v => {
206+
if (typeof v === 'function') {
207+
const newSortVal = v(sorting);
208+
onSortingChange?.(newSortVal ?? null);
209+
} else {
210+
onSortingChange?.(v ?? null);
211+
}
212+
},
213+
state: {
214+
sorting,
215+
},
216+
};
217+
}, [enableClientSideSorting, onSortingChange, sorting]);
218+
165219
const table = useReactTable({
166-
data,
220+
data: truncatedData,
167221
columns: reactTableColumns,
168222
getCoreRowModel: getCoreRowModel(),
169223
enableColumnResizing: true,
170224
columnResizeMode: 'onChange',
171-
enableSorting: true,
172-
manualSorting: true,
173-
onSortingChange: v => {
174-
if (typeof v === 'function') {
175-
const newSortVal = v(sorting);
176-
onSortingChange?.(newSortVal ?? null);
177-
} else {
178-
onSortingChange?.(v ?? null);
179-
}
180-
},
181-
state: {
182-
sorting,
183-
},
225+
...sortingParams,
184226
});
185227

186228
const { rows } = table.getRowModel();
@@ -209,7 +251,7 @@ export const Table = ({
209251
const [wrapLinesEnabled, setWrapLinesEnabled] = useState(false);
210252

211253
const { csvData } = useCsvExport(
212-
data,
254+
truncatedData,
213255
columns.map(col => ({
214256
dataKey: col.dataKey,
215257
displayName: col.displayName,
@@ -307,6 +349,11 @@ export const Table = ({
307349
)}
308350
</tbody>
309351
</table>
352+
{isTruncated && (
353+
<div className="p-2 text-center">
354+
Showing the first {MAX_TABLE_ROWS} rows.
355+
</div>
356+
)}
310357
{tableBottom}
311358
</div>
312359
);

0 commit comments

Comments
 (0)