diff --git a/.changeset/source-vars-3.md b/.changeset/source-vars-3.md new file mode 100644 index 000000000..743757dbd --- /dev/null +++ b/.changeset/source-vars-3.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +feat: add data source variables to dashboard filters and tiles diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index f5000d1a7..9a3c4417d 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -28,11 +28,9 @@ import { } from '@hyperdx/common-utils/dist/core/metadata'; import { convertToDashboardTemplate, - displayTypeSupportsBuilderAlerts, - displayTypeSupportsPromQLAlerts, - displayTypeSupportsRawSqlAlerts, Granularity, isTimeSeriesDisplayType, + savedChartConfigSupportsAlerts, } from '@hyperdx/common-utils/dist/core/utils'; import { getAdhocFilterTargetsForTile, @@ -44,6 +42,7 @@ import { } from '@hyperdx/common-utils/dist/filters'; import { displayTypeRequiresSource, + getSavedConfigAllowedSourceKinds, isBuilderChartConfig, isBuilderSavedChartConfig, isPromqlChartConfig, @@ -56,6 +55,11 @@ import { isImportableDashboard, } from '@hyperdx/common-utils/dist/iac'; import { isMissingFiltersMacro } from '@hyperdx/common-utils/dist/macros'; +import { + getConfigReferenceVariables, + resolveConfigReferences, + restoreConfigReferences, +} from '@hyperdx/common-utils/dist/referenceVariables'; import { AlertState, BroadcastFilter, @@ -455,6 +459,7 @@ const Tile = ({ filters, variables, unsatisfiedRequiredFilters, + referenceVariableNames, showAlertAnnotations, showReleaseAnnotations, isLive, @@ -489,6 +494,12 @@ const Tile = ({ variables?: ChartVariable[]; /** The dashboard's required filters that have nothing selected. */ unsatisfiedRequiredFilters?: DashboardFilter[]; + /** + * The data source and connection variables the tile's stored config + * references. `chart` carries their selections, or the references while + * unselected. + */ + referenceVariableNames?: string[]; // When true, draw alert firing/recovery annotations on this tile's chart. showAlertAnnotations?: boolean; // When true, draw release markers on this tile's chart. @@ -604,8 +615,23 @@ const Tile = ({ id: chart.config.source, }); + const usesReferenceVariable = !!referenceVariableNames?.length; + const isSourceMissing = !!chart.config.source && isSourceFetched && source == null; + // A data source or connection variable can select a source on another + // connection than the one a raw SQL tile queries. + const isSourceOnOtherConnection = + usesReferenceVariable && + source != null && + isRawSqlSavedChartConfig(chart.config) && + source.connection !== chart.config.connection; + // A variable's kinds can change after tiles use it, and saves don't check a + // source's kind against the tile's display type. + const isSourceKindUnsupported = + usesReferenceVariable && + source != null && + !getSavedConfigAllowedSourceKinds(chart.config).includes(source.kind); const isSourceUnset = !!chart.config && isBuilderSavedChartConfig(chart.config) && @@ -655,9 +681,15 @@ const Tile = ({ sourceId: chart.config.source, unsatisfiedRequiredFilters, referencedVariables: tileVariables, + referenceVariableNames, }), ), - [unsatisfiedRequiredFilters, chart.config, tileVariables], + [ + unsatisfiedRequiredFilters, + chart.config, + tileVariables, + referenceVariableNames, + ], ); const missingRequiredFilterNames = useMemo( () => JSON.parse(serializedMissingRequiredFilterNames), @@ -929,16 +961,13 @@ const Tile = ({ return url && addTimeZone(url); }, [dateRange, queriedConfig, source, addTimeZone]); + // An alert must query one fixed source and connection + const supportsAlerts = + !usesReferenceVariable && savedChartConfigSupportsAlerts(chart.config); + const hoverToolbar = useMemo(() => { if (readOnly) return null; - const isRawSql = isRawSqlSavedChartConfig(chart.config); - const isPromQL = isPromqlSavedChartConfig(chart.config); - const displayTypeSupportsAlerts = isRawSql - ? displayTypeSupportsRawSqlAlerts(chart.config.displayType) - : isPromQL - ? displayTypeSupportsPromQLAlerts(chart.config.displayType) - : displayTypeSupportsBuilderAlerts(chart.config.displayType); const canMoveToGroup = onMoveToGroup && moveTargets && moveTargets.length > 0; return ( @@ -968,7 +997,7 @@ const Tile = ({ )} - {displayTypeSupportsAlerts && + {supportsAlerts && (alert ? ( // Existing alert: bell with a colored status dot indicator. { if (readOnly) return null; - const isRawSql = isRawSqlSavedChartConfig(chart.config); - const isPromQL = isPromqlSavedChartConfig(chart.config); - const showAlerts = isRawSql - ? displayTypeSupportsRawSqlAlerts(chart.config.displayType) - : isPromQL - ? displayTypeSupportsPromQLAlerts(chart.config.displayType) - : displayTypeSupportsBuilderAlerts(chart.config.displayType); const canMoveToGroup = onMoveToGroup && moveTargets && moveTargets.length > 0; return ( @@ -1113,7 +1135,7 @@ const Tile = ({ Replay search )} - {showAlerts && ( + {supportsAlerts && ( <> - {`Missing required filters: ${missingRequiredFilterNames.join( + {`Missing required filters or variables: ${missingRequiredFilterNames.join( ', ', )}. Select a value for each to load this tile.`} @@ -1269,6 +1291,20 @@ const Tile = ({ The data source for this tile is not set. Edit the tile to select a data source. + ) : isSourceOnOtherConnection ? ( + + The selected data source is on a different connection than this + tile's query. Select a source on the same connection. + + ) : isSourceKindUnsupported ? ( + + The selected data source can't be used with this chart type. + Select a different source. + ) : ( <> {effectiveQueriedConfig && @@ -1501,6 +1537,8 @@ const Tile = ({ filterWarning, isSourceMissing, isSourceUnset, + isSourceOnOtherConnection, + isSourceKindUnsupported, isBlockedByRequiredFilters, missingRequiredFilterNames, hasBeenVisible, @@ -1900,28 +1938,6 @@ function DBDashboardPage({ const { data: allDashboards } = useDashboards(); const [highlightedTileId] = useQueryState('highlightedTileId'); - const tableConnections = useMemo(() => { - if (!dashboard) return []; - const tc: TableConnection[] = []; - - for (const { config } of dashboard.tiles) { - if (!isBuilderSavedChartConfig(config)) continue; - const source = sources?.find(v => v.id === config.source); - if (!source || source.disabled) continue; - // TODO: will need to update this when we allow for multiple metrics per chart - const firstSelect = config.select[0]; - const metricType = - typeof firstSelect !== 'string' ? firstSelect?.metricType : undefined; - const tableName = getMetricTableName(source, metricType); - if (!tableName) continue; - tc.push({ - ...tcFromSource(source), - tableName, - }); - } - - return tc; - }, [dashboard, sources]); const [granularity, setGranularity] = useQueryState( 'granularity', @@ -1966,8 +1982,61 @@ function DBDashboardPage({ ignoredVariableNames, getFilterQueriesForSource, variables, + referenceVariableValues, + resolvedFilters, + unresolvedReferenceVariablesByFilterId, + incompatibleReferenceVariablesByFilterId, unsatisfiedRequiredFilters, - } = useDashboardFilters(filters); + } = useDashboardFilters(filters, { sources }); + // Tiles as they query, by ID: source and connection variable references + // replaced by the selections. Edits and saves read `dashboard.tiles`, so the + // stored references survive. + const resolvedTiles = useMemo( + () => + new Map( + (dashboard?.tiles ?? []).map(tile => { + const config = resolveConfigReferences( + tile.config, + referenceVariableValues, + ); + return [tile.id, config === tile.config ? tile : { ...tile, config }]; + }), + ), + [dashboard?.tiles, referenceVariableValues], + ); + // Kept apart from `resolvedTiles` so a tile's list only changes with its config + const referenceVariableNamesByTileId = useMemo( + () => + new Map( + (dashboard?.tiles ?? []).map(tile => [ + tile.id, + getConfigReferenceVariables(tile.config), + ]), + ), + [dashboard?.tiles], + ); + + const tableConnections = useMemo(() => { + const tc: TableConnection[] = []; + + for (const { config } of resolvedTiles.values()) { + if (!isBuilderSavedChartConfig(config)) continue; + const source = sources?.find(v => v.id === config.source); + if (!source || source.disabled) continue; + // TODO: will need to update this when we allow for multiple metrics per chart + const firstSelect = config.select[0]; + const metricType = + typeof firstSelect !== 'string' ? firstSelect?.metricType : undefined; + const tableName = getMetricTableName(source, metricType); + if (!tableName) continue; + tc.push({ + ...tcFromSource(source), + tableName, + }); + } + + return tc; + }, [resolvedTiles, sources]); const dashboardReady = !!dashboard?.id && @@ -2406,10 +2475,10 @@ function DBDashboardPage({ // The filter bar is hidden in kiosk mode, so a click would add conditions // the viewer can't see or remove. if (isKioskMode) return handlers; - for (const tile of dashboard?.tiles ?? []) { + for (const tile of resolvedTiles.values()) { const sourceId = 'source' in tile.config ? tile.config.source : undefined; const targetIds = getAdhocFilterTargetsForTile( - filters, + resolvedFilters, tile.config, sourceId, ).map(filter => filter.id); @@ -2420,19 +2489,23 @@ function DBDashboardPage({ } } return handlers; - }, [isKioskMode, dashboard?.tiles, filters, applyAdhocAction]); + }, [isKioskMode, resolvedTiles, resolvedFilters, applyAdhocAction]); const renderTileComponent = useCallback( (chart: Tile) => { + const resolvedChart = resolvedTiles.get(chart.id) ?? chart; // Resolve the tile's source ID so per-source-scoped filters can be // narrowed to only the tiles they target. Builder and RawSQL configs // both carry a `source` field; markdown / other configs don't. const tileSourceId = - 'source' in chart.config ? chart.config.source : undefined; + 'source' in resolvedChart.config + ? resolvedChart.config.source + : undefined; return ( setEditedTile(chart)} readOnly={isKioskMode} @@ -2459,7 +2532,14 @@ function DBDashboardPage({ c => c.id === chart.id, ); if (chartIndex === -1) return; - draft.tiles[chartIndex] = newChart; + // Keep the source as stored, not as resolved for display + draft.tiles[chartIndex] = { + ...newChart, + config: restoreConfigReferences( + newChart.config, + chart.config, + ), + }; }), ); } @@ -2532,6 +2612,8 @@ function DBDashboardPage({ ); }, [ + resolvedTiles, + referenceVariableNamesByTileId, dashboard, searchedTimeRange, isRefreshEnabled, @@ -3324,7 +3406,7 @@ function DBDashboardPage({ )} {!isKioskMode && ( )} {/* Selection indicator */} diff --git a/packages/app/src/DashboardFilters.tsx b/packages/app/src/DashboardFilters.tsx index eb7846246..3e155c733 100644 --- a/packages/app/src/DashboardFilters.tsx +++ b/packages/app/src/DashboardFilters.tsx @@ -5,12 +5,14 @@ import { getFilterVariableName, getPendingFilterValuesVariables, isAdhocFilter, + isDataSourceFilter, isFilterGlobalRequirement, isFilterReferenceableVariable, isFilterRequired, isQueryExpressionFilter, isStaticListFilter, } from '@hyperdx/common-utils/dist/filters'; +import { ReferenceVariableValues } from '@hyperdx/common-utils/dist/referenceVariables'; import { AdhocFilterCondition, ChartVariable, @@ -25,9 +27,11 @@ import { DashboardFilterLabel, FilterCaution, FilterEffect, + getIncompatibleVariablesTooltip, getPendingVariablesTooltip, } from './components/DashboardFilterLabel'; import { FilterLinkToggle } from './components/FilterLinkToggle'; +import { DataSourceDashboardFilter } from './components/ReferenceVariableDashboardFilter'; import { VirtualMultiSelect } from './components/VirtualMultiSelect/VirtualMultiSelect'; import { useDashboardFilterValues } from './hooks/useDashboardFilterValues'; @@ -173,6 +177,16 @@ interface DashboardFilterProps { variables?: ChartVariable[]; /** Required filters with nothing selected, which can block ad hoc filters. */ unsatisfiedRequiredFilters?: DashboardFilter[]; + /** The selected ID of each data source and connection variable, by name. */ + referenceVariableValues?: ReferenceVariableValues; + /** + * The reference variables each filter's fields reference that leave it + * nothing to query, by filter ID. `filters` are otherwise expected with + * their references resolved. + */ + unresolvedReferenceVariablesByFilterId?: ReadonlyMap; + /** The subset of those selecting a source the filter can't use. */ + incompatibleReferenceVariablesByFilterId?: ReadonlyMap; } const DashboardFilters = ({ @@ -184,6 +198,9 @@ const DashboardFilters = ({ onSetAdhocConditions, variables, unsatisfiedRequiredFilters, + referenceVariableValues, + unresolvedReferenceVariablesByFilterId, + incompatibleReferenceVariablesByFilterId, }: DashboardFilterProps) => { // "Link" mode (opt-in, off by default): each dropdown's values are narrowed by // the others' selections. Off by default because contingent value lookups @@ -191,13 +208,22 @@ const DashboardFilters = ({ // on, all of a source's facets are computed in a single groupUniqArrayIf scan. const [linked, setLinked] = useState(false); + // A filter whose data source variable has no selection has nothing to query yet + const queryableFilters = useMemo( + () => + filters.filter( + filter => !unresolvedReferenceVariablesByFilterId?.has(filter.id), + ), + [filters, unresolvedReferenceVariablesByFilterId], + ); + const { data: filterValuesById, erroredFilterIds, filterErrorMessages, isFetching, } = useDashboardFilterValues({ - filters, + filters: queryableFilters, dateRange, variables, // Only narrow by sibling selections when linked. @@ -214,6 +240,31 @@ const DashboardFilters = ({ return ( {filters.map(filter => { + const incompatibleVariables = + incompatibleReferenceVariablesByFilterId?.get(filter.id) ?? []; + const unresolvedVariables = + unresolvedReferenceVariablesByFilterId?.get(filter.id) ?? []; + // Variables with no selection yet, as opposed to an unusable one + const unselectedVariables = unresolvedVariables.filter( + name => !incompatibleVariables.includes(name), + ); + if (isDataSourceFilter(filter)) { + return ( + + onSetFilterValue(filter.id, value ? [value] : []) + } + effect={getFilterEffect(filter)} + requiredTooltip={getRequiredFilterTooltip(filter)} + /> + ); + } if (isAdhocFilter(filter)) { return ( ); } @@ -238,21 +291,31 @@ const DashboardFilters = ({ // Fall back to the hook-level fetching state only until this filter's // query has produced an entry; once it has (even with empty values), // honor its own loading flag. - const isLoadingValues = queriedFilterValues - ? queriedFilterValues.isLoading - : isFetching; + const isLoadingValues = + unresolvedVariables.length === 0 && + (queriedFilterValues ? queriedFilterValues.isLoading : isFetching); // Only a queried dropdown has a values query that can await a - // variable's selection. - const pendingVariables = isQueryExpressionFilter(filter) - ? getPendingFilterValuesVariables(filter, variables) - : undefined; + // variable's selection, besides any data source variable it reads from. + const pendingVariables = [ + ...unselectedVariables, + ...(isQueryExpressionFilter(filter) + ? getPendingFilterValuesVariables(filter, variables) + : []), + ]; return ( 0 || + (erroredFilterIds?.has(filter.id) ?? false) + } + errorMessage={ + incompatibleVariables.length > 0 + ? getIncompatibleVariablesTooltip(incompatibleVariables) + : filterErrorMessages?.get(filter.id) + } pendingVariables={pendingVariables} onChange={values => onSetFilterValue(filter.id, values)} values={queriedFilterValues?.values} diff --git a/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx b/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx index 008860ab9..f89d1a71f 100644 --- a/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx +++ b/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx @@ -30,6 +30,7 @@ import { DashboardFilterLabel, FilterCaution, FilterEffect, + getIncompatibleVariablesTooltip, getPendingVariablesTooltip, } from '@/components/DashboardFilterLabel'; import { FilterPill, getFilterOperatorDisplay } from '@/components/FilterPill'; @@ -53,8 +54,17 @@ type AdhocDashboardFilterProps = { variables?: ChartVariable[]; /** Required filters with nothing selected. */ unsatisfiedRequiredFilters?: DashboardFilter[]; + /** + * Unselected data source variables the filter's sources reference. Until + * they have a selection the filter has no sources to suggest keys from. + */ + unresolvedVariables?: string[]; + /** Those of `unresolvedVariables` selecting a source the filter can't use. */ + incompatibleVariables?: string[]; }; +const NO_VARIABLES: string[] = []; + /** A dashboard filter bar control holding one ad hoc filter's conditions. */ export function AdhocDashboardFilter({ filter, @@ -64,6 +74,8 @@ export function AdhocDashboardFilter({ effect, variables, unsatisfiedRequiredFilters, + unresolvedVariables = NO_VARIABLES, + incompatibleVariables = NO_VARIABLES, }: AdhocDashboardFilterProps) { const [isAdding, setIsAdding] = useState(false); const [isExpanded, setIsExpanded] = useState(false); @@ -103,7 +115,13 @@ export function AdhocDashboardFilter({ ? `This filter depends on: ${blockingFilterNames.join(', ')}` : undefined; const disabledReason = dependsOn ?? suggestionsWhere.error; - const isDisabled = !!disabledReason; + const isDisabled = !!disabledReason || unresolvedVariables.length > 0; + const allPendingVariables = [ + ...unresolvedVariables.filter( + name => !incompatibleVariables.includes(name), + ), + ...pendingVariables, + ]; const toggleAdding = () => { if (isDisabled) return; @@ -136,15 +154,18 @@ export function AdhocDashboardFilter({ testId={`dashboard-filter-blocked-${filter.name}`} /> )} - {!isDisabled && pendingVariables.length > 0 && ( + {!disabledReason && allPendingVariables.length > 0 && ( )} - {suggestionsWhere.error && ( + {(suggestionsWhere.error || incompatibleVariables.length > 0) && ( diff --git a/packages/app/src/components/AdhocDashboardFilter/__tests__/AdhocDashboardFilter.test.tsx b/packages/app/src/components/AdhocDashboardFilter/__tests__/AdhocDashboardFilter.test.tsx index 3f767a0e3..467830eeb 100644 --- a/packages/app/src/components/AdhocDashboardFilter/__tests__/AdhocDashboardFilter.test.tsx +++ b/packages/app/src/components/AdhocDashboardFilter/__tests__/AdhocDashboardFilter.test.tsx @@ -151,6 +151,40 @@ describe('AdhocDashboardFilter', () => { ); }); + it('waits for an unselected data source variable its sources reference', async () => { + const onChange = renderFilter([existing], filter, { + unresolvedVariables: ['ds'], + }); + + expect( + screen.getByTestId('dashboard-filter-pending-variable-Conditions'), + ).toBeInTheDocument(); + expect(addButton()).toHaveAttribute('aria-disabled', 'true'); + await userEvent.click(addButton()); + await userEvent.click(screen.getByTestId('adhoc-condition-pill')); + expect(screen.queryByTestId(`${editorId}-key`)).not.toBeInTheDocument(); + + await userEvent.click( + screen.getByRole('button', { name: 'Remove filter' }), + ); + expect(onChange).toHaveBeenCalledWith([]); + }); + + it("reports a data source variable selecting a source it can't use", () => { + renderFilter([existing], filter, { + unresolvedVariables: ['ds'], + incompatibleVariables: ['ds'], + }); + + expect( + screen.getByTestId('dashboard-filter-error-Conditions'), + ).toBeInTheDocument(); + expect( + screen.queryByTestId('dashboard-filter-pending-variable-Conditions'), + ).not.toBeInTheDocument(); + expect(addButton()).toHaveAttribute('aria-disabled', 'true'); + }); + it('edits a condition from its pill', async () => { const onChange = renderFilter([ existing, diff --git a/packages/app/src/components/DashboardFilterLabel.tsx b/packages/app/src/components/DashboardFilterLabel.tsx index 893e70697..c6a5020e5 100644 --- a/packages/app/src/components/DashboardFilterLabel.tsx +++ b/packages/app/src/components/DashboardFilterLabel.tsx @@ -19,6 +19,16 @@ export const getPendingVariablesTooltip = ( } no selected value.`; }; +/** Explain a filter whose data source variable selects a source it can't use. */ +export const getIncompatibleVariablesTooltip = ( + incompatibleVariables: string[], +): string => { + const names = incompatibleVariables.map(name => `$${name}`).join(', '); + return `This filter can't use the source ${names} ${ + incompatibleVariables.length === 1 ? 'selects' : 'select' + }. Select a different source.`; +}; + /** One of the caution icons a filter's label row can carry, with its tooltip. */ export const FilterCaution = ({ label, diff --git a/packages/app/src/components/DashboardFiltersModal/DashboardFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/DashboardFilterEditForm.tsx index f1a384dd6..cd08b76a2 100644 --- a/packages/app/src/components/DashboardFiltersModal/DashboardFilterEditForm.tsx +++ b/packages/app/src/components/DashboardFiltersModal/DashboardFilterEditForm.tsx @@ -3,6 +3,7 @@ import { useForm, useWatch } from 'react-hook-form'; import { deriveVariableName, getFilterVariableName, + isReferenceVariableFilterType, } from '@hyperdx/common-utils/dist/filters'; import { ChartVariable, @@ -33,6 +34,7 @@ import { useZIndex } from '@/zIndex'; import { AdhocFilterEditForm } from './AdhocFilterEditForm'; import { MODAL_SIZE } from './constants'; import { CustomInputWrapper } from './CustomInputWrapper'; +import { DataSourceFilterEditForm } from './DataSourceFilterEditForm'; import { FilterFormValues, toFormValues, @@ -64,6 +66,7 @@ const FILTER_TYPE_OPTIONS = [ }, ] : []), + { value: DashboardFilterType.enum.DATA_SOURCE, label: 'Data source' }, ]; interface DashboardFilterEditFormProps { @@ -184,9 +187,30 @@ export const DashboardFilterEditForm = ({ return variables?.filter(variable => variable.name !== ownName); }, [variables, filter]); + // Ad hoc conditions and reference variables have fixed selection rules. + // Reference variables are always required, so only the global choice is left. + const isReferenceVariable = isReferenceVariableFilterType(formFilterType); + const showSelectionOptions = + showRequiredFilterOptions && + formFilterType !== 'ADHOC' && + !isReferenceVariable; + const showGlobalRequirementOption = + showRequiredFilterOptions && (isReferenceVariable || !!isRequired); + const isNew = !filter; const showTypeInput = !!showVariableOptions; + const globalRequirementCheckbox = ( + + ); + return ( - {formFilterType === 'STATIC_LIST' ? ( + {formFilterType === 'DATA_SOURCE' ? ( + + ) : formFilterType === 'STATIC_LIST' ? ( - {showRequiredFilterOptions && formFilterType !== 'ADHOC' && ( + {showSelectionOptions && ( - {!!isRequired && ( - - - + {showGlobalRequirementOption && ( + {globalRequirementCheckbox} )} )} + {isReferenceVariable && showGlobalRequirementOption && ( + + + {globalRequirementCheckbox} + + )} {formState.errors.root && ( IS_PROMQL_ENABLED || kind !== SourceKind.Promql) + .map(kind => ({ value: kind, label: SOURCE_KIND_LABELS[kind] })); + +interface DataSourceFilterEditFormProps { + control: FilterFormControl; + /** Filters other than the one being edited, used to keep variable names unique. */ + otherFilters: DashboardFilter[]; +} + +/** Form for editing a variable whose value is a source. */ +export const DataSourceFilterEditForm = ({ + control, + otherFilters, +}: DataSourceFilterEditFormProps) => { + const { errors } = useFormState({ control }); + + return ( + <> + + + (value?.length ?? 0) > 0 || 'Select at least one source kind', + }} + render={({ field: { onChange, value } }) => ( + ( + + {SOURCE_KIND_ICONS[option.value]} + {option.label} + + )} + /> + )} + /> + + + + + ); +}; diff --git a/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts b/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts index 27a9bcb66..172959493 100644 --- a/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts +++ b/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts @@ -1,4 +1,4 @@ -import { DashboardFilter } from '@hyperdx/common-utils/dist/types'; +import { DashboardFilter, SourceKind } from '@hyperdx/common-utils/dist/types'; import { FilterFormValues, @@ -318,6 +318,74 @@ describe('toSavedFilter', () => { ).toThrow(); }); + it('saves a data source filter as single-select and required', () => { + const saved = toSavedFilter( + formValues({ + type: 'DATA_SOURCE', + name: 'Logs source', + allowedSourceKinds: [SourceKind.Log], + isRequired: false, + isSingleSelect: false, + expression: 'ignored', + source: 'ignored', + }), + ); + + expect(saved).toEqual({ + id: expect.any(String), + type: 'DATA_SOURCE', + name: 'Logs source', + variableName: 'Logs_source', + allowedSourceKinds: [SourceKind.Log], + minSelections: 1, + maxSelections: 1, + isBroadcastEnabled: false, + isVariableEnabled: true, + }); + expect(toFormValues(saved).allowedSourceKinds).toEqual([SourceKind.Log]); + }); + + it('saves a data source filter limited to a connection variable', () => { + expect( + toSavedFilter( + formValues({ + type: 'DATA_SOURCE', + name: 'Logs source', + allowedSourceKinds: [SourceKind.Log], + connection: '$conn', + }), + ), + ).toMatchObject({ connection: '$conn' }); + }); + + it('saves a connection filter as single-select and required', () => { + const saved = toSavedFilter( + formValues({ + type: 'CONNECTION', + name: 'Cluster', + isSingleSelect: false, + source: 'ignored', + }), + ); + + expect(saved).toEqual({ + id: expect.any(String), + type: 'CONNECTION', + name: 'Cluster', + variableName: 'Cluster', + minSelections: 1, + maxSelections: 1, + isBroadcastEnabled: false, + isVariableEnabled: true, + }); + }); + + it('rejects a data source filter without source kinds', () => { + expect(() => + toSavedFilter(formValues({ type: 'DATA_SOURCE', name: 'Source' })), + ).toThrow(); + }); + it('rejects values the schema cannot accept', () => { expect(() => toSavedFilter(formValues({ type: 'STATIC_LIST', options: [] })), @@ -325,7 +393,10 @@ describe('toSavedFilter', () => { }); }); -/** The minimum a form needs to save, per filter type the form supports. */ +/** + * The minimum a form needs to save, per filter type with configurable + * selection limits. + */ const byType: Record< Exclude< DashboardFilter['type'], @@ -435,6 +506,36 @@ describe('isGlobalRequirement', () => { ).not.toHaveProperty('isGlobalRequirement'); }, ); + + // Reference variables are always required, so the form has no "Required" + // checkbox for them to gate the requirement's scope. + it('round-trips a dashboard-wide data source requirement without isRequired', () => { + const saved = toSavedFilter( + formValues({ + type: 'DATA_SOURCE', + name: 'Source', + allowedSourceKinds: [SourceKind.Log], + isRequired: false, + isGlobalRequirement: true, + }), + ); + + expect(saved).toMatchObject({ isGlobalRequirement: true }); + expect(toFormValues(saved).isGlobalRequirement).toBe(true); + }); + + it('emits no key for a data source filter that blocks only its own tiles', () => { + expect( + toSavedFilter( + formValues({ + type: 'DATA_SOURCE', + name: 'Source', + allowedSourceKinds: [SourceKind.Log], + isGlobalRequirement: false, + }), + ), + ).not.toHaveProperty('isGlobalRequirement'); + }); }); describe('maxSelections', () => { diff --git a/packages/app/src/components/DashboardFiltersModal/filterFormState.ts b/packages/app/src/components/DashboardFiltersModal/filterFormState.ts index 7b681fe2f..caa45a695 100644 --- a/packages/app/src/components/DashboardFiltersModal/filterFormState.ts +++ b/packages/app/src/components/DashboardFiltersModal/filterFormState.ts @@ -5,11 +5,13 @@ import { isFilterGlobalRequirement, isFilterRequired, isFilterVariableEnabled, + isReferenceVariableFilterType, } from '@hyperdx/common-utils/dist/filters'; import { AdhocDashboardFilter, DashboardFilter, DashboardFilterSchema, + DataSourceDashboardFilter, PromqlLabelDashboardFilter, QueryExpressionDashboardFilter, StaticListDashboardFilter, @@ -53,7 +55,8 @@ export type FilterFormValues = { | 'isGlobalRequirement' | 'maxSelections' > & - Pick; + Pick & + Pick; export type FilterFormControl = Control; @@ -65,6 +68,7 @@ export const toFormValues = ( const staticList = filter?.type === 'STATIC_LIST' ? filter : undefined; const promqlLabel = filter?.type === 'PROMETHEUS_LABEL' ? filter : undefined; const adhoc = filter?.type === 'ADHOC' ? filter : undefined; + const dataSource = filter?.type === 'DATA_SOURCE' ? filter : undefined; return { id: filter?.id ?? crypto.randomUUID(), @@ -100,6 +104,10 @@ export const toFormValues = ( // ADHOC fields sourceType: adhoc?.sourceType ?? 'sql', sources: adhoc?.sources ?? [], + + // DATA_SOURCE fields + allowedSourceKinds: dataSource?.allowedSourceKinds ?? [], + connection: dataSource?.connection, }; }; @@ -140,6 +148,28 @@ export const toSavedFilter = (values: FilterFormValues): DashboardFilter => { }); } + // Reference variables are always single-select and required, so only the + // global requirement checkbox is consulted + if (isReferenceVariableFilterType(values.type)) { + return DashboardFilterSchema.parse({ + id: values.id, + type: values.type, + name: values.name, + ...(values.type === 'DATA_SOURCE' + ? { + allowedSourceKinds: values.allowedSourceKinds, + connection: values.connection || undefined, + } + : {}), + minSelections: 1, + maxSelections: 1, + ...(isGlobalRequirement ? { isGlobalRequirement: true } : {}), + isBroadcastEnabled: false, + isVariableEnabled: true, + variableName: getFilterVariableName(values), + }); + } + const appliesTo = values.appliesToSourceIds?.filter(id => !!id?.length); const trimmedWhere = values.where?.trim() ?? ''; const whereFields = { diff --git a/packages/app/src/components/ReferenceVariableDashboardFilter.tsx b/packages/app/src/components/ReferenceVariableDashboardFilter.tsx new file mode 100644 index 000000000..27b494358 --- /dev/null +++ b/packages/app/src/components/ReferenceVariableDashboardFilter.tsx @@ -0,0 +1,166 @@ +import { useCallback, useMemo } from 'react'; +import { isSourceOfferedByVariable } from '@hyperdx/common-utils/dist/referenceVariables'; +import { + DataSourceDashboardFilter as DataSourceFilter, + TSource, +} from '@hyperdx/common-utils/dist/types'; +import { + Center, + CheckIcon, + ComboboxItem, + Group, + Select, + Stack, + Text, +} from '@mantine/core'; +import { IconStack } from '@tabler/icons-react'; + +import { isValueInSelectData } from '@/components/SelectControlled'; +import { + getSourceOptionIcon, + sourceSelectFilter, + SourceSelectGroup, + useFilteredSortedSourceItems, + useSourceKindMap, +} from '@/components/sourceSelectUtils'; +import { useSources } from '@/source'; + +import { + DashboardFilterLabel, + FilterCaution, + FilterEffect, + getPendingVariablesTooltip, +} from './DashboardFilterLabel'; + +import sourceSelectStyles from '@styles/SourceSelectControlled.module.scss'; + +type ReferenceVariableFilterProps = { + value: string | undefined; + onChange: (id: string | undefined) => void; + effect: FilterEffect; + requiredTooltip: string; +}; + +/** + * The filter bar control for a reference variable: a single picker over the + * IDs the variable can select. + */ +function ReferenceVariableSelect({ + name, + items, + value, + onChange, + effect, + requiredTooltip, + pendingVariables, + placeholder, + renderIcon, +}: ReferenceVariableFilterProps & { + name: string; + items: ComboboxItem[] | SourceSelectGroup[]; + pendingVariables?: string[]; + placeholder: string; + renderIcon: (value: string | null) => React.ReactNode; +}) { + // A selection that is no longer offered (deleted, disabled, or no longer + // allowed by the variable) shows as empty, so it reads as unselected. + const isOffered = useMemo( + () => isValueInSelectData(items, value), + [items, value], + ); + const selected = value && isOffered ? value : null; + + const renderOption = useCallback( + ({ option, checked }: { option: ComboboxItem; checked?: boolean }) => ( + + {checked ? ( +
+ +
+ ) : ( + renderIcon(option.value) + )} + {option.label} +
+ ), + [renderIcon], + ); + + return ( + + + {selected == null && ( + + )} + {!!pendingVariables?.length && ( + + )} + +