Repository navigation
feat: add data source variables to dashboard filters and tiles #3654
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
Changes from all commits
f408b82
8f615c8
335259d
1017e51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@hyperdx/app': minor | ||
| --- | ||
|
|
||
| feat: add data source variables to dashboard filters and tiles |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = | ||
|
pulpdrew marked this conversation as resolved.
Resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 minor — New tile resolution and placeholder logic has no tests Add a component or E2E test where a tile's stored Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond |
||
| 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<string[]>( | ||
| () => 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 = ({ | |
| </Tooltip> | ||
| )} | ||
|
|
||
| {displayTypeSupportsAlerts && | ||
| {supportsAlerts && | ||
| (alert ? ( | ||
| // Existing alert: bell with a colored status dot indicator. | ||
| <Indicator | ||
|
|
@@ -1073,7 +1102,7 @@ const Tile = ({ | |
| alertTooltip, | ||
| moveTargets, | ||
| replaySearchUrl, | ||
| chart.config, | ||
| supportsAlerts, | ||
| chart.id, | ||
| chart.containerId, | ||
| chart.tabId, | ||
|
|
@@ -1091,13 +1120,6 @@ const Tile = ({ | |
| const collapsedMenuItems = useMemo(() => { | ||
| 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 | ||
| </Menu.Item> | ||
| )} | ||
| {showAlerts && ( | ||
| {supportsAlerts && ( | ||
| <> | ||
| <Menu.Item | ||
| leftSection={ | ||
|
|
@@ -1164,7 +1186,7 @@ const Tile = ({ | |
| alertTooltip, | ||
| moveTargets, | ||
| replaySearchUrl, | ||
| chart.config, | ||
| supportsAlerts, | ||
| chart.containerId, | ||
| chart.tabId, | ||
| onDeleteClick, | ||
|
|
@@ -1255,7 +1277,7 @@ const Tile = ({ | |
| toolbarItems={toolbar} | ||
| data-testid="tile-missing-required-filters" | ||
| > | ||
| {`Missing required filters: ${missingRequiredFilterNames.join( | ||
| {`Missing required filters or variables: ${missingRequiredFilterNames.join( | ||
| ', ', | ||
| )}. Select a value for each to load this tile.`} | ||
| </TilePlaceholder> | ||
|
|
@@ -1269,6 +1291,20 @@ const Tile = ({ | |
| The data source for this tile is not set. Edit the tile to select | ||
| a data source. | ||
| </TilePlaceholder> | ||
| ) : isSourceOnOtherConnection ? ( | ||
| <TilePlaceholder title={title} toolbarItems={toolbar}> | ||
| The selected data source is on a different connection than this | ||
| tile's query. Select a source on the same connection. | ||
| </TilePlaceholder> | ||
| ) : isSourceKindUnsupported ? ( | ||
| <TilePlaceholder | ||
| title={title} | ||
| toolbarItems={toolbar} | ||
| data-testid="tile-unsupported-source-kind" | ||
| > | ||
| The selected data source can't be used with this chart type. | ||
| Select a different source. | ||
| </TilePlaceholder> | ||
| ) : ( | ||
| <> | ||
| {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 ( | ||
| <Tile | ||
| key={chart.id} | ||
| chart={chart} | ||
| chart={resolvedChart} | ||
| referenceVariableNames={referenceVariableNamesByTileId.get(chart.id)} | ||
| dateRange={searchedTimeRange} | ||
| onEditClick={() => 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( | ||
|
pulpdrew marked this conversation as resolved.
Resolved
|
||
| newChart.config, | ||
| chart.config, | ||
| ), | ||
| }; | ||
| }), | ||
| ); | ||
| } | ||
|
|
@@ -2532,6 +2612,8 @@ function DBDashboardPage({ | |
| ); | ||
| }, | ||
| [ | ||
| resolvedTiles, | ||
| referenceVariableNamesByTileId, | ||
| dashboard, | ||
| searchedTimeRange, | ||
| isRefreshEnabled, | ||
|
|
@@ -3324,14 +3406,21 @@ function DBDashboardPage({ | |
| )} | ||
| {!isKioskMode && ( | ||
| <DashboardFilters | ||
| filters={filters} | ||
| filters={resolvedFilters} | ||
| selectionByFilterId={selectionByFilterId} | ||
| onSetFilterValue={setFilterValue} | ||
| adhocConditionsByFilterId={adhocConditionsByFilterId} | ||
| onSetAdhocConditions={setAdhocConditions} | ||
| dateRange={searchedTimeRange} | ||
| variables={variables} | ||
| unsatisfiedRequiredFilters={unsatisfiedRequiredFilters} | ||
| referenceVariableValues={referenceVariableValues} | ||
| unresolvedReferenceVariablesByFilterId={ | ||
| unresolvedReferenceVariablesByFilterId | ||
| } | ||
| incompatibleReferenceVariablesByFilterId={ | ||
| incompatibleReferenceVariablesByFilterId | ||
| } | ||
| /> | ||
| )} | ||
| {/* Selection indicator */} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.