From d41179203a3477ee164e0a560d8dc735db703376 Mon Sep 17 00:00:00 2001 From: Karl Power Date: Mon, 16 Nov 2026 16:48:38 +0100 Subject: [PATCH 1/2] feat: create SQL query dashboard filters and fill their dropdowns Add a "SQL query values" type to the dashboard filter editor and populate the filter's dropdown from its query. - Render the filter's query in common-utils: expand time macros and other filters' variables, select the first column as a string, and cap the result at 10,000 rows - Run each SQL query filter's query on its connection and merge the values and errors into the dashboard's filter values - Keep the query's row order in the dropdown and show a caution while a referenced variable has no selection - Add the editor form: connection picker, SQL editor with macro and variable autocomplete, variable name, and an on-demand preview of the values over the dashboard's time range Refs HDX-5524 --- .changeset/sql-query-dashboard-filter-app.md | 8 + packages/app/src/DBDashboardPage.tsx | 1 + packages/app/src/DashboardFilters.tsx | 13 +- .../DashboardFilterEditForm.tsx | 13 + .../SqlQueryFilterEditForm.tsx | 232 ++++++++++++++++++ .../__tests__/SqlQueryFilterEditForm.test.tsx | 166 +++++++++++++ .../__tests__/filterFormState.test.ts | 51 +++- .../DashboardFiltersModal/filterFormState.ts | 20 +- .../DashboardFiltersModal/index.tsx | 4 + .../useDashboardFilterValues.test.tsx | 43 ++++ .../useSqlQueryFilterValues.test.tsx | 167 +++++++++++++ .../src/hooks/useDashboardFilterValues.tsx | 59 +++-- .../app/src/hooks/useSqlQueryFilterValues.tsx | 135 ++++++++++ .../src/__tests__/filters.test.ts | 86 +++++++ packages/common-utils/src/filters.ts | 63 ++++- 15 files changed, 1035 insertions(+), 26 deletions(-) create mode 100644 .changeset/sql-query-dashboard-filter-app.md create mode 100644 packages/app/src/components/DashboardFiltersModal/SqlQueryFilterEditForm.tsx create mode 100644 packages/app/src/components/DashboardFiltersModal/__tests__/SqlQueryFilterEditForm.test.tsx create mode 100644 packages/app/src/hooks/__tests__/useSqlQueryFilterValues.test.tsx create mode 100644 packages/app/src/hooks/useSqlQueryFilterValues.tsx diff --git a/.changeset/sql-query-dashboard-filter-app.md b/.changeset/sql-query-dashboard-filter-app.md new file mode 100644 index 0000000000..9c9ec29a04 --- /dev/null +++ b/.changeset/sql-query-dashboard-filter-app.md @@ -0,0 +1,8 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +feat: create SQL query dashboard filters and fill their dropdowns + +Dashboard filters can now take their values from a ClickHouse SQL query. The filter editor offers a "SQL query values" type with a connection, a SQL editor, and a preview of the values. The dropdown lists the query's first column in the order it returns, up to 10,000 values, and the query can use the time macros and other filters' variables. diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index f5000d1a75..c3172241ed 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -3510,6 +3510,7 @@ function DBDashboardPage({ showVariableOptions showRequiredFilterOptions variables={variables} + dateRange={searchedTimeRange} /> )} diff --git a/packages/app/src/DashboardFilters.tsx b/packages/app/src/DashboardFilters.tsx index 5c15cdf9a5..6d131d951e 100644 --- a/packages/app/src/DashboardFilters.tsx +++ b/packages/app/src/DashboardFilters.tsx @@ -9,6 +9,7 @@ import { isFilterRequired, isFilterVariableEnabled, isQueryExpressionFilter, + isSqlQueryFilter, isStaticListFilter, } from '@hyperdx/common-utils/dist/filters'; import { @@ -141,8 +142,9 @@ const DashboardFilterSelect = ({ // so a completed/empty/failed query stays interactive and the user can // still clear or adjust the selection. loading={isLoading} - // A static list renders in the order its author wrote it. - sort={!isStaticListFilter(filter)} + // A static list renders in the order its author wrote it, and a SQL + // query filter in the order its query returns. + sort={!isStaticListFilter(filter) && !isSqlQueryFilter(filter)} isMultiSelect={!isFilterSingleSelect(filter)} onChange={onChange} data-testid={`dashboard-filter-select-${filter.name}`} @@ -245,7 +247,12 @@ const DashboardFilters = ({ // variable's selection. const pendingVariables = isQueryExpressionFilter(filter) ? getPendingFilterValuesVariables(filter, variables) - : undefined; + : isSqlQueryFilter(filter) + ? getPendingFilterValuesVariables( + { where: filter.sqlTemplate, whereLanguage: 'sql' }, + variables, + ) + : undefined; return ( void; onClose: () => void; onCancel: () => void; @@ -97,6 +101,7 @@ export const DashboardFilterEditForm = ({ showVariableOptions, showRequiredFilterOptions, variables, + dateRange, onSave, onClose, onCancel, @@ -249,6 +254,14 @@ export const DashboardFilterEditForm = ({ otherFilters={otherFilters} /> + ) : formFilterType === 'SQL_QUERY' ? ( + + + ) : formFilterType === 'PROMETHEUS_LABEL' ? ( { + const { errors } = useFormState({ control }); + const variables = useChartVariables(); + const completions = useMemo( + () => [ + ...MACRO_SUGGESTIONS.filter( + // `sourceTable(gauge)` etc. are per-metric variants of `sourceTable`. + macro => !SOURCE_MACROS.has(macro.name.split('(')[0]), + ).map(toMacroCompletion), + ...buildSqlVariableCompletions(variables), + ], + [variables], + ); + + const [connection, sqlTemplate] = useWatch({ + control, + name: ['connection', 'sqlTemplate'], + }); + // What the preview last ran, so editing doesn't re-query on every keystroke. + const [previewed, setPreviewed] = useState<{ + connection: string; + sqlTemplate: string; + }>(); + const canPreview = !!connection && !!sqlTemplate?.trim(); + const runPreview = () => { + if (canPreview) setPreviewed({ connection, sqlTemplate }); + }; + + return ( + <> + +
+ +
+
+ + + + + (typeof value === 'string' && value.trim().length > 0) || + 'This field is required', + }} + height="150px" + enableLineWrapping + placeholder={PLACEHOLDER_SQL} + additionalCompletions={completions} + onSubmit={runPreview} + /> + + + + {previewed && ( + + )} + + + + + + ); +}; + +const SqlQueryFilterPreview = ({ + connection, + sqlTemplate, + dateRange, + variables, +}: { + connection: string; + sqlTemplate: string; + dateRange?: [Date, Date]; + variables: ReturnType; +}) => { + const lastHour = useMemo( + (): [Date, Date] => [new Date(NOW - 60 * 60 * 1000), new Date(NOW)], + [], + ); + const filters = useMemo( + (): SqlQueryDashboardFilter[] => [ + { + id: 'preview', + type: 'SQL_QUERY', + name: 'Preview', + connection, + sqlTemplate, + isBroadcastEnabled: false, + isVariableEnabled: true, + }, + ], + [connection, sqlTemplate], + ); + const { data, filterErrorMessages, isLoading, isError } = + useSqlQueryFilterValues({ + filters, + dateRange: dateRange ?? lastHour, + variables, + }); + const values = data.get('preview')?.values ?? []; + + if (isLoading) { + return ( + + + Running query + + ); + } + + if (isError) { + return ( + } + data-testid="filter-sql-preview" + > + {filterErrorMessages.get('preview') ?? 'The query failed.'} + + ); + } + + if (values.length === 0) { + return ( +
+ +
+ ); + } + + const notShown = values.length - PREVIEW_VALUES_SHOWN; + return ( + + + {values.length} {values.length === 1 ? 'value' : 'values'} + + + {values.slice(0, PREVIEW_VALUES_SHOWN).map((value, index) => ( + + {index > 0 && ', '} + + {truncateText(value, PREVIEW_VALUE_MAX_LENGTH, '…')} + + + ))} + {notShown > 0 && `, and ${notShown} more`} + + + ); +}; diff --git a/packages/app/src/components/DashboardFiltersModal/__tests__/SqlQueryFilterEditForm.test.tsx b/packages/app/src/components/DashboardFiltersModal/__tests__/SqlQueryFilterEditForm.test.tsx new file mode 100644 index 0000000000..226224a9f8 --- /dev/null +++ b/packages/app/src/components/DashboardFiltersModal/__tests__/SqlQueryFilterEditForm.test.tsx @@ -0,0 +1,166 @@ +import { useForm } from 'react-hook-form'; +import { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import { + FilterFormControl, + FilterFormValues, + toFormValues, +} from '@/components/DashboardFiltersModal/filterFormState'; +import { SqlQueryFilterEditForm } from '@/components/DashboardFiltersModal/SqlQueryFilterEditForm'; + +const mockUseSqlQueryFilterValues = jest.fn(); +const mockCompletionLabels: string[] = []; + +jest.mock('@/hooks/useSqlQueryFilterValues', () => ({ + useSqlQueryFilterValues: (...args: unknown[]) => + mockUseSqlQueryFilterValues(...args), +})); +// The real selector fetches connections, and the real editor is CodeMirror; +// plain inputs bound to the same fields are enough to drive the preview. +jest.mock('@/components/ConnectionSelect', () => ({ + ConnectionSelectControlled: ({ + control, + name, + }: { + control: FilterFormControl; + name: 'connection'; + }) => , +})); +jest.mock('@/components/SQLEditor/SQLEditor', () => ({ + SQLEditorControlled: ({ + control, + name, + additionalCompletions, + }: { + control: FilterFormControl; + name: 'sqlTemplate'; + additionalCompletions?: { label: string }[]; + }) => { + mockCompletionLabels.splice( + 0, + mockCompletionLabels.length, + ...(additionalCompletions ?? []).map(c => c.label), + ); + return