Repository navigation
feat: add an "All" option to SQL query dashboard filters #3861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karl-power
wants to merge
1
commit into
claude/sql-filter-all-static-list
from
claude/sql-filter-all-sql-query
+1,124
−114
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@hyperdx/api': minor | ||
| '@hyperdx/app': minor | ||
| '@hyperdx/common-utils': minor | ||
| --- | ||
|
|
||
| feat: add an "All" option to SQL query dashboard filters. Turn on "Include "All" option" in the filter's settings, and "All" selects every option the query returns, up to 1,000; tiles show an error above that, so add a `LIMIT` if the query can return more. Tiles wait for the options to load before running. Other filters' queries see "All" as no constraint. The external API and MCP accept `allowAll` on SQL query filters, and MCP tile runs with "All" run the filter's query to fill in its options. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/api/src/mcp/tools/dashboards/sqlQueryAllValues.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { | ||
| getFilterVariableName, | ||
| isSqlQueryFilter, | ||
| renderSqlQueryFilterValuesQuery, | ||
| SQL_QUERY_FILTER_VALUES_SETTINGS, | ||
| toSqlQueryFilterValues, | ||
| } from '@hyperdx/common-utils/dist/filters'; | ||
| import { parseVariableReference } from '@hyperdx/common-utils/dist/referenceVariables'; | ||
| import { | ||
| type ChartVariable, | ||
| SourceKind, | ||
| } from '@hyperdx/common-utils/dist/types'; | ||
| import { | ||
| ALL_SELECTION_MAX_OPTIONS, | ||
| expandAllSelections, | ||
| tooManyOptionsForAllMessage, | ||
| } from '@hyperdx/common-utils/dist/variableSelection'; | ||
|
|
||
| import { | ||
| MCP_CLICKHOUSE_SETTINGS, | ||
| MCP_REQUEST_TIMEOUT, | ||
| toPlainMetricTables, | ||
| } from '@/mcp/tools/query/helpers'; | ||
| import type { McpContext } from '@/mcp/tools/types'; | ||
| import { | ||
| getMcpClickhouseClient, | ||
| McpConnectionNotFoundError, | ||
| } from '@/mcp/utils/clickhouse'; | ||
| import { getMcpSource } from '@/mcp/utils/sources'; | ||
| import type { ExternalDashboard } from '@/routers/external-api/v2/utils/dashboards'; | ||
| import type { | ||
| ExternalDashboardFilter, | ||
| ExternalDashboardFilterWithId, | ||
| } from '@/utils/zod'; | ||
|
|
||
| import { getVariableSelections, type McpVariableValues } from './variables'; | ||
|
|
||
| type ExternalFilter = ExternalDashboardFilter | ExternalDashboardFilterWithId; | ||
|
|
||
| type DashboardState = { | ||
| variables: ChartVariable[]; | ||
| /** Selected values by variable name, for `$name` connection and source references. */ | ||
| selections: ReadonlyMap<string, string[]>; | ||
| dateRange: [Date, Date]; | ||
| }; | ||
|
|
||
| /** | ||
| * Expand "All" on SQL query filter variables by running each filter's values | ||
| * query, as the dashboard does. The queries see the variables with "All" | ||
| * unexpanded, as the dashboard's dropdowns do. | ||
| */ | ||
| export async function expandSqlQueryAllVariables( | ||
| context: McpContext, | ||
| dashboard: Pick<ExternalDashboard, 'filters' | 'savedFilterValues'>, | ||
| { | ||
| variables, | ||
| variableValues, | ||
| dateRange, | ||
| }: { | ||
| /** From `resolveDashboardVariables`. */ | ||
| variables: ChartVariable[]; | ||
| variableValues: McpVariableValues | undefined; | ||
| dateRange: [Date, Date]; | ||
| }, | ||
| ): Promise<{ variables: ChartVariable[] } | { error: string }> { | ||
| const allNames = new Set( | ||
| variables.filter(variable => variable.all).map(({ name }) => name), | ||
| ); | ||
| const state: DashboardState = { | ||
| variables, | ||
| selections: getVariableSelections( | ||
| dashboard.savedFilterValues, | ||
| variableValues, | ||
| ), | ||
| dateRange, | ||
| }; | ||
| const optionsByName = new Map<string, string[]>(); | ||
| for (const filter of (dashboard.filters ?? []).filter(isSqlQueryFilter)) { | ||
| const name = getFilterVariableName(filter); | ||
| if (!name || !allNames.has(name)) continue; | ||
| const result = await loadOptions(context, filter, state); | ||
| if ('error' in result) { | ||
| return { | ||
| error: `Couldn't load the options for "All" on ${filter.name}: ${result.error}`, | ||
| }; | ||
| } | ||
| if (result.values.length > ALL_SELECTION_MAX_OPTIONS) { | ||
| return { error: tooManyOptionsForAllMessage(filter.name) }; | ||
| } | ||
| optionsByName.set(name, result.values); | ||
| } | ||
| return { variables: expandAllSelections(variables, optionsByName) }; | ||
| } | ||
|
|
||
| async function loadOptions( | ||
| context: McpContext, | ||
| filter: Extract<ExternalFilter, { type: 'SQL_QUERY' }>, | ||
| { variables, selections, dateRange }: DashboardState, | ||
| ): Promise<{ values: string[] } | { error: string }> { | ||
| const resolve = (value: string | undefined) => { | ||
| const name = parseVariableReference(value); | ||
| return name == null ? value : selections.get(name)?.[0]; | ||
| }; | ||
| const noSelection = (reference: string) => ({ | ||
| error: `${reference} has no selection. Pass one ID for it in variableValues, or save a selection on the dashboard.`, | ||
| }); | ||
| const connectionId = resolve(filter.connectionId); | ||
| if (!connectionId) return noSelection(filter.connectionId); | ||
| const sourceId = resolve(filter.sourceId); | ||
| if (filter.sourceId && !sourceId) return noSelection(filter.sourceId); | ||
|
|
||
| const source = sourceId ? await getMcpSource(context, sourceId) : undefined; | ||
| if (sourceId && !source) return { error: `source ${sourceId} not found` }; | ||
| if (source && source.connection.toString() !== connectionId) { | ||
| return { error: `source ${source.name} is not on the filter's connection` }; | ||
| } | ||
|
|
||
| let rendered: ReturnType<typeof renderSqlQueryFilterValuesQuery>; | ||
| try { | ||
| rendered = renderSqlQueryFilterValuesQuery({ | ||
| sqlTemplate: filter.sqlTemplate, | ||
| variables, | ||
| dateRange, | ||
| sourceTables: source | ||
| ? { | ||
| from: source.from, | ||
| metricTables: | ||
| source.kind === SourceKind.Metric | ||
| ? toPlainMetricTables(source.metricTables) | ||
| : undefined, | ||
| } | ||
| : undefined, | ||
| }); | ||
| } catch (e) { | ||
| return { error: e instanceof Error ? e.message : String(e) }; | ||
| } | ||
|
|
||
| try { | ||
| const client = await getMcpClickhouseClient(context, connectionId, { | ||
| requestTimeout: MCP_REQUEST_TIMEOUT, | ||
| }); | ||
| const response = await client.query<'JSON'>({ | ||
| query: rendered.sql, | ||
| query_params: rendered.params, | ||
| format: 'JSON', | ||
| clickhouse_settings: { | ||
| ...MCP_CLICKHOUSE_SETTINGS, | ||
| ...SQL_QUERY_FILTER_VALUES_SETTINGS, | ||
| }, | ||
| }); | ||
| const result = await response.json<{ value: string | null }>(); | ||
| return { values: toSqlQueryFilterValues(result.data ?? []) }; | ||
| } catch (e) { | ||
| if (e instanceof McpConnectionNotFoundError) { | ||
| return { error: `connection ${connectionId} not found` }; | ||
| } | ||
| return { error: e instanceof Error ? e.message : String(e) }; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it that other filters don't see the all option like tiles do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also leads to this misleading error message