|
| 1 | +import { useMemo, useState } from 'react'; |
| 2 | +import { useFormState, useWatch } from 'react-hook-form'; |
| 3 | +import { MACRO_SUGGESTIONS } from '@hyperdx/common-utils/dist/macros'; |
| 4 | +import { |
| 5 | + DashboardFilter, |
| 6 | + SqlQueryDashboardFilter, |
| 7 | +} from '@hyperdx/common-utils/dist/types'; |
| 8 | +import { Alert, Button, Group, Loader, Stack, Text } from '@mantine/core'; |
| 9 | +import { IconAlertTriangle, IconPlayerPlay } from '@tabler/icons-react'; |
| 10 | + |
| 11 | +import { ConnectionSelectControlled } from '@/components/ConnectionSelect'; |
| 12 | +import EmptyState from '@/components/EmptyState'; |
| 13 | +import { SQLEditorControlled } from '@/components/SQLEditor/SQLEditor'; |
| 14 | +import { |
| 15 | + buildSqlVariableCompletions, |
| 16 | + toMacroCompletion, |
| 17 | + useChartVariables, |
| 18 | +} from '@/components/SQLEditor/variableCompletions'; |
| 19 | +import { NOW } from '@/config'; |
| 20 | +import { useSqlQueryFilterValues } from '@/hooks/useSqlQueryFilterValues'; |
| 21 | +import { truncateText } from '@/utils'; |
| 22 | + |
| 23 | +import { CustomInputWrapper } from './CustomInputWrapper'; |
| 24 | +import { FilterFormControl } from './filterFormState'; |
| 25 | +import { VariableNameInput } from './VariableNameInput'; |
| 26 | + |
| 27 | +/** Values the preview lists before summarizing the rest as a count. */ |
| 28 | +const PREVIEW_VALUES_SHOWN = 20; |
| 29 | +/** Characters of each value the preview shows before truncating it. */ |
| 30 | +const PREVIEW_VALUE_MAX_LENGTH = 20; |
| 31 | + |
| 32 | +/** Macros that need a source, which a SQL query filter doesn't have. */ |
| 33 | +const SOURCE_MACROS = new Set(['filters', 'sourceTable']); |
| 34 | + |
| 35 | +const PLACEHOLDER_SQL = `SELECT DISTINCT env |
| 36 | +FROM default.environments |
| 37 | +WHERE $__filter(cloud, $cloud) |
| 38 | +ORDER BY env`; |
| 39 | + |
| 40 | +interface SqlQueryFilterEditFormProps { |
| 41 | + control: FilterFormControl; |
| 42 | + /** Filters other than the one being edited, used to keep variable names unique. */ |
| 43 | + otherFilters: DashboardFilter[]; |
| 44 | + /** The range the preview runs over. Defaults to the last hour. */ |
| 45 | + dateRange?: [Date, Date]; |
| 46 | +} |
| 47 | + |
| 48 | +/** Form describing a dashboard filter whose values come from a SQL query */ |
| 49 | +export const SqlQueryFilterEditForm = ({ |
| 50 | + control, |
| 51 | + otherFilters, |
| 52 | + dateRange, |
| 53 | +}: SqlQueryFilterEditFormProps) => { |
| 54 | + const { errors } = useFormState({ control }); |
| 55 | + const variables = useChartVariables(); |
| 56 | + const completions = useMemo( |
| 57 | + () => [ |
| 58 | + ...MACRO_SUGGESTIONS.filter( |
| 59 | + // `sourceTable(gauge)` etc. are per-metric variants of `sourceTable`. |
| 60 | + macro => !SOURCE_MACROS.has(macro.name.split('(')[0]), |
| 61 | + ).map(toMacroCompletion), |
| 62 | + ...buildSqlVariableCompletions(variables), |
| 63 | + ], |
| 64 | + [variables], |
| 65 | + ); |
| 66 | + |
| 67 | + const [connection, sqlTemplate] = useWatch({ |
| 68 | + control, |
| 69 | + name: ['connection', 'sqlTemplate'], |
| 70 | + }); |
| 71 | + // What the preview last ran, so editing doesn't re-query on every keystroke. |
| 72 | + const [previewed, setPreviewed] = useState<{ |
| 73 | + connection: string; |
| 74 | + sqlTemplate: string; |
| 75 | + }>(); |
| 76 | + const canPreview = !!connection && !!sqlTemplate?.trim(); |
| 77 | + const runPreview = () => { |
| 78 | + if (canPreview) setPreviewed({ connection, sqlTemplate }); |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <CustomInputWrapper |
| 84 | + label="Connection" |
| 85 | + tooltipText="The ClickHouse connection the query runs on" |
| 86 | + error={errors.connection} |
| 87 | + > |
| 88 | + <div data-testid="filter-connection-select"> |
| 89 | + <ConnectionSelectControlled |
| 90 | + control={control} |
| 91 | + name="connection" |
| 92 | + rules={{ required: 'Select a connection' }} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + </CustomInputWrapper> |
| 96 | + |
| 97 | + <CustomInputWrapper |
| 98 | + label="SQL query" |
| 99 | + 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." |
| 100 | + error={errors.sqlTemplate} |
| 101 | + > |
| 102 | + <Stack gap="xs" data-testid="filter-sql-template-input"> |
| 103 | + <SQLEditorControlled |
| 104 | + control={control} |
| 105 | + name="sqlTemplate" |
| 106 | + rules={{ |
| 107 | + validate: (value: unknown) => |
| 108 | + (typeof value === 'string' && value.trim().length > 0) || |
| 109 | + 'This field is required', |
| 110 | + }} |
| 111 | + height="150px" |
| 112 | + enableLineWrapping |
| 113 | + placeholder={PLACEHOLDER_SQL} |
| 114 | + additionalCompletions={completions} |
| 115 | + onSubmit={runPreview} |
| 116 | + /> |
| 117 | + <Group justify="flex-end"> |
| 118 | + <Button |
| 119 | + variant="secondary" |
| 120 | + size="compact-sm" |
| 121 | + leftSection={<IconPlayerPlay size={14} />} |
| 122 | + disabled={!canPreview} |
| 123 | + onClick={runPreview} |
| 124 | + data-testid="filter-sql-preview-button" |
| 125 | + > |
| 126 | + Preview values |
| 127 | + </Button> |
| 128 | + </Group> |
| 129 | + {previewed && ( |
| 130 | + <SqlQueryFilterPreview |
| 131 | + {...previewed} |
| 132 | + dateRange={dateRange} |
| 133 | + variables={variables} |
| 134 | + /> |
| 135 | + )} |
| 136 | + </Stack> |
| 137 | + </CustomInputWrapper> |
| 138 | + |
| 139 | + <VariableNameInput control={control} otherFilters={otherFilters} /> |
| 140 | + </> |
| 141 | + ); |
| 142 | +}; |
| 143 | + |
| 144 | +const SqlQueryFilterPreview = ({ |
| 145 | + connection, |
| 146 | + sqlTemplate, |
| 147 | + dateRange, |
| 148 | + variables, |
| 149 | +}: { |
| 150 | + connection: string; |
| 151 | + sqlTemplate: string; |
| 152 | + dateRange?: [Date, Date]; |
| 153 | + variables: ReturnType<typeof useChartVariables>; |
| 154 | +}) => { |
| 155 | + const lastHour = useMemo( |
| 156 | + (): [Date, Date] => [new Date(NOW - 60 * 60 * 1000), new Date(NOW)], |
| 157 | + [], |
| 158 | + ); |
| 159 | + const filters = useMemo( |
| 160 | + (): SqlQueryDashboardFilter[] => [ |
| 161 | + { |
| 162 | + id: 'preview', |
| 163 | + type: 'SQL_QUERY', |
| 164 | + name: 'Preview', |
| 165 | + connection, |
| 166 | + sqlTemplate, |
| 167 | + isBroadcastEnabled: false, |
| 168 | + isVariableEnabled: true, |
| 169 | + }, |
| 170 | + ], |
| 171 | + [connection, sqlTemplate], |
| 172 | + ); |
| 173 | + const { data, filterErrorMessages, isLoading, isError } = |
| 174 | + useSqlQueryFilterValues({ |
| 175 | + filters, |
| 176 | + dateRange: dateRange ?? lastHour, |
| 177 | + variables, |
| 178 | + }); |
| 179 | + const values = data.get('preview')?.values ?? []; |
| 180 | + |
| 181 | + if (isLoading) { |
| 182 | + return ( |
| 183 | + <Group gap="xs" data-testid="filter-sql-preview"> |
| 184 | + <Loader size="xs" /> |
| 185 | + <Text size="xs">Running query</Text> |
| 186 | + </Group> |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + if (isError) { |
| 191 | + return ( |
| 192 | + <Alert |
| 193 | + variant="danger" |
| 194 | + icon={<IconAlertTriangle size={16} />} |
| 195 | + data-testid="filter-sql-preview" |
| 196 | + > |
| 197 | + {filterErrorMessages.get('preview') ?? 'The query failed.'} |
| 198 | + </Alert> |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + if (values.length === 0) { |
| 203 | + return ( |
| 204 | + <div data-testid="filter-sql-preview"> |
| 205 | + <EmptyState |
| 206 | + title="No values" |
| 207 | + description="The query returned no rows for the dashboard's time range." |
| 208 | + /> |
| 209 | + </div> |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + const notShown = values.length - PREVIEW_VALUES_SHOWN; |
| 214 | + return ( |
| 215 | + <Stack gap={4} data-testid="filter-sql-preview"> |
| 216 | + <Text size="xs" fw={500}> |
| 217 | + {values.length} {values.length === 1 ? 'value' : 'values'} |
| 218 | + </Text> |
| 219 | + <Text size="xs" data-testid="filter-sql-preview-values"> |
| 220 | + {values.slice(0, PREVIEW_VALUES_SHOWN).map((value, index) => ( |
| 221 | + <span key={value}> |
| 222 | + {index > 0 && ', '} |
| 223 | + <span title={value}> |
| 224 | + {truncateText(value, PREVIEW_VALUE_MAX_LENGTH, '…')} |
| 225 | + </span> |
| 226 | + </span> |
| 227 | + ))} |
| 228 | + {notShown > 0 && `, and ${notShown} more`} |
| 229 | + </Text> |
| 230 | + </Stack> |
| 231 | + ); |
| 232 | +}; |
0 commit comments