Repository navigation
feat: create SQL query dashboard filters and fill their dropdowns #3678
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
2
commits into
sql-dashboard-variables-api
from
sql-dashboard-variables-app
+1,146
−29
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,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. |
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
251 changes: 251 additions & 0 deletions
251
packages/app/src/components/DashboardFiltersModal/SqlQueryFilterEditForm.tsx
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,251 @@ | ||
| import { useMemo, useState } from 'react'; | ||
| import { useFormState, useWatch } from 'react-hook-form'; | ||
| import { validateSqlQueryFilterTemplate } from '@hyperdx/common-utils/dist/filters'; | ||
| import { | ||
| INTERVAL_MACROS, | ||
| MACRO_SUGGESTIONS, | ||
| SOURCE_DEPENDENT_MACROS, | ||
| } from '@hyperdx/common-utils/dist/macros'; | ||
| import { | ||
| DashboardFilter, | ||
| SqlQueryDashboardFilter, | ||
| } from '@hyperdx/common-utils/dist/types'; | ||
| import { Alert, Button, Group, Loader, Stack, Text } from '@mantine/core'; | ||
| import { IconAlertTriangle, IconPlayerPlay } from '@tabler/icons-react'; | ||
|
|
||
| import { ConnectionSelectControlled } from '@/components/ConnectionSelect'; | ||
| import EmptyState from '@/components/EmptyState'; | ||
| import { SQLEditorControlled } from '@/components/SQLEditor/SQLEditor'; | ||
| import { | ||
| buildSqlVariableCompletions, | ||
| toMacroCompletion, | ||
| useChartVariables, | ||
| } from '@/components/SQLEditor/variableCompletions'; | ||
| import { NOW } from '@/config'; | ||
| import { useSqlQueryFilterValues } from '@/hooks/useSqlQueryFilterValues'; | ||
| import { truncateText } from '@/utils'; | ||
|
|
||
| import { CustomInputWrapper } from './CustomInputWrapper'; | ||
| import { FilterFormControl } from './filterFormState'; | ||
| import { VariableNameInput } from './VariableNameInput'; | ||
|
|
||
| /** Values the preview lists before summarizing the rest as a count. */ | ||
| const PREVIEW_VALUES_SHOWN = 20; | ||
| /** Characters of each value the preview shows before truncating it. */ | ||
| const PREVIEW_VALUE_MAX_LENGTH = 20; | ||
|
|
||
| /** | ||
| * Macros a SQL query filter can't expand: it has no source, and no time buckets. | ||
| */ | ||
| const UNSUPPORTED_MACROS = new Set<string>([ | ||
| ...SOURCE_DEPENDENT_MACROS, | ||
| ...INTERVAL_MACROS, | ||
| ]); | ||
|
|
||
| const PLACEHOLDER_SQL = `SELECT DISTINCT env | ||
| FROM default.environments | ||
| WHERE $__filter(cloud, $cloud) | ||
| ORDER BY env`; | ||
|
|
||
| interface SqlQueryFilterEditFormProps { | ||
| control: FilterFormControl; | ||
| /** Filters other than the one being edited, used to keep variable names unique. */ | ||
| otherFilters: DashboardFilter[]; | ||
| /** The range the preview runs over. Defaults to the last hour. */ | ||
| dateRange?: [Date, Date]; | ||
| } | ||
|
|
||
| /** Form describing a dashboard filter whose values come from a SQL query */ | ||
| export const SqlQueryFilterEditForm = ({ | ||
| control, | ||
| otherFilters, | ||
| dateRange, | ||
| }: SqlQueryFilterEditFormProps) => { | ||
| const { errors } = useFormState({ control }); | ||
| const variables = useChartVariables(); | ||
| const completions = useMemo( | ||
| () => [ | ||
| ...MACRO_SUGGESTIONS.filter( | ||
| // `sourceTable(gauge)` etc. are per-metric variants of `sourceTable`. | ||
| macro => !UNSUPPORTED_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 ( | ||
| <> | ||
| <CustomInputWrapper | ||
| label="Connection" | ||
| tooltipText="The ClickHouse connection the query runs on" | ||
| error={errors.connection} | ||
| > | ||
| <div data-testid="filter-connection-select"> | ||
| <ConnectionSelectControlled | ||
| control={control} | ||
| name="connection" | ||
| rules={{ required: 'Select a connection' }} | ||
| /> | ||
| </div> | ||
| </CustomInputWrapper> | ||
|
|
||
| <CustomInputWrapper | ||
| label="SQL query" | ||
| tooltipText="The first column of the results fills the dropdown, in the order the query returns them. The query may use the time macros and reference the dashboard's other variables." | ||
| error={errors.sqlTemplate} | ||
| > | ||
| <Stack gap="xs" data-testid="filter-sql-template-input"> | ||
| <SQLEditorControlled | ||
| control={control} | ||
| name="sqlTemplate" | ||
| rules={{ | ||
| validate: (value: unknown) => { | ||
| if (typeof value !== 'string' || !value.trim()) { | ||
| return 'This field is required'; | ||
| } | ||
| // `variables` leaves out this filter's own, so a reference to | ||
| // it is rejected as unknown. | ||
| return ( | ||
| validateSqlQueryFilterTemplate(value, variables ?? []) ?? true | ||
| ); | ||
| }, | ||
| }} | ||
| height="150px" | ||
| enableLineWrapping | ||
| placeholder={PLACEHOLDER_SQL} | ||
| additionalCompletions={completions} | ||
| onSubmit={runPreview} | ||
| /> | ||
| <Group justify="flex-end"> | ||
| <Button | ||
| variant="secondary" | ||
| size="compact-sm" | ||
| leftSection={<IconPlayerPlay size={14} />} | ||
| disabled={!canPreview} | ||
| onClick={runPreview} | ||
| data-testid="filter-sql-preview-button" | ||
| > | ||
| Preview values | ||
| </Button> | ||
| </Group> | ||
| {previewed && ( | ||
| <SqlQueryFilterPreview | ||
| {...previewed} | ||
| dateRange={dateRange} | ||
| variables={variables} | ||
| /> | ||
| )} | ||
| </Stack> | ||
| </CustomInputWrapper> | ||
|
|
||
| <VariableNameInput control={control} otherFilters={otherFilters} /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| const SqlQueryFilterPreview = ({ | ||
| connection, | ||
| sqlTemplate, | ||
| dateRange, | ||
| variables, | ||
| }: { | ||
| connection: string; | ||
| sqlTemplate: string; | ||
| dateRange?: [Date, Date]; | ||
| variables: ReturnType<typeof useChartVariables>; | ||
| }) => { | ||
| 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, isFetching, isError } = | ||
| useSqlQueryFilterValues({ | ||
| filters, | ||
| dateRange: dateRange ?? lastHour, | ||
| variables, | ||
| }); | ||
| const values = data.get('preview')?.values ?? []; | ||
|
|
||
| // While a re-run loads, the previous query's values stand in as placeholder | ||
| // data, so isLoading alone would show them as this query's results. | ||
| if (isLoading || isFetching) { | ||
| return ( | ||
| <Group gap="xs" data-testid="filter-sql-preview"> | ||
| <Loader size="xs" /> | ||
| <Text size="xs">Running query</Text> | ||
| </Group> | ||
| ); | ||
| } | ||
|
|
||
| if (isError) { | ||
| return ( | ||
| <Alert | ||
| variant="danger" | ||
| icon={<IconAlertTriangle size={16} />} | ||
| data-testid="filter-sql-preview" | ||
| > | ||
| {filterErrorMessages.get('preview') ?? 'The query failed.'} | ||
| </Alert> | ||
| ); | ||
| } | ||
|
|
||
| if (values.length === 0) { | ||
| return ( | ||
| <div data-testid="filter-sql-preview"> | ||
| <EmptyState | ||
| title="No values" | ||
| description="The query returned no rows for the dashboard's time range." | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const notShown = values.length - PREVIEW_VALUES_SHOWN; | ||
| return ( | ||
| <Stack gap={4} data-testid="filter-sql-preview"> | ||
| <Text size="xs" fw={500}> | ||
| {values.length} {values.length === 1 ? 'value' : 'values'} | ||
| </Text> | ||
| <Text size="xs" data-testid="filter-sql-preview-values"> | ||
| {values.slice(0, PREVIEW_VALUES_SHOWN).map((value, index) => ( | ||
| <span key={value}> | ||
| {index > 0 && ', '} | ||
| <span title={value}> | ||
| {truncateText(value, PREVIEW_VALUE_MAX_LENGTH, '…')} | ||
| </span> | ||
| </span> | ||
| ))} | ||
| {notShown > 0 && `, and ${notShown} more`} | ||
| </Text> | ||
| </Stack> | ||
| ); | ||
| }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.