Repository navigation
feat: let tiles and filters use a data source variable as their source #3655
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
05260bf
e5613ee
64ab3f3
a808f3c
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: let tiles and filters use a data source variable as their source |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ import { Controller, useForm, useWatch } from 'react-hook-form'; | |
| import { z } from 'zod'; | ||
| import { zodResolver } from '@hookform/resolvers/zod'; | ||
| import { convertToDashboardDocument } from '@hyperdx/common-utils/dist/core/utils'; | ||
| import { | ||
| collectRefinementIssues, | ||
| validateDashboardVariableReferences, | ||
| } from '@hyperdx/common-utils/dist/dashboardValidation'; | ||
| import { | ||
| type DashboardFilterQueryIssue, | ||
| getAdhocFilterSourceKinds, | ||
|
|
@@ -34,6 +38,7 @@ import { | |
| displayTypeRequiresSource, | ||
| isRawSqlSavedChartConfig, | ||
| } from '@hyperdx/common-utils/dist/guards'; | ||
| import { parseVariableReference } from '@hyperdx/common-utils/dist/referenceVariables'; | ||
| import { | ||
| type DashboardFilter, | ||
| type DashboardTemplate, | ||
|
|
@@ -130,6 +135,32 @@ function CollapsibleMessage({ | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A `$name` that names no variable of the right type in the template. The API | ||
| * rejects these too, but only once the mappings are filled in, and local mode | ||
| * doesn't check at all. | ||
| */ | ||
| export function getTemplateVariableReferenceIssues( | ||
| template: DashboardTemplate, | ||
| ): string[] { | ||
| return collectRefinementIssues(ctx => | ||
| validateDashboardVariableReferences( | ||
| template.tiles, | ||
| template.filters ?? [], | ||
| ctx, | ||
| ), | ||
| ).map(({ path, message }) => { | ||
| const [collection, index] = path; | ||
| const owner = | ||
| collection === 'tiles' && typeof index === 'number' | ||
| ? `Tile "${template.tiles[index]?.config.name ?? index}"` | ||
| : collection === 'filters' && typeof index === 'number' | ||
| ? `Filter "${template.filters?.[index]?.name ?? index}"` | ||
| : path.join('.'); | ||
| return `${owner}: ${message}`; | ||
| }); | ||
| } | ||
|
|
||
| function FileSelection({ | ||
| onComplete, | ||
| }: { | ||
|
|
@@ -203,6 +234,24 @@ function FileSelection({ | |
| return; | ||
| } | ||
|
|
||
| const referenceIssues = getTemplateVariableReferenceIssues(result.data); | ||
| if (referenceIssues.length > 0) { | ||
| onComplete(null); | ||
| setError({ | ||
| message: 'Failed to Import Dashboard', | ||
| details: ( | ||
| <Stack gap={0}> | ||
| {referenceIssues.map(issue => ( | ||
| <Text key={issue} c="red"> | ||
| {issue} | ||
| </Text> | ||
| ))} | ||
| </Stack> | ||
| ), | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // The schema only guarantees saved filter values are well-shaped, not that | ||
| // their condition strings parse. Surface a non-blocking warning so users | ||
| // who hand-edited or generated the file know a value won't actually filter, | ||
|
|
@@ -398,11 +447,25 @@ function resolveAppliesToSources( | |
| return { ids: slots.filter(Boolean), slots }; | ||
| } | ||
|
|
||
| /** | ||
| * A data source variable reference (`$name`) is portable as written, so | ||
| * import neither maps it nor asks the user to. | ||
| */ | ||
| const isVariableRef = (name: string | undefined) => | ||
|
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. What happens on import if a tile references
Contributor
Author
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. Before it would have failed when saving due to API validations, now I have added front-end validations on the import page 👍 |
||
| parseVariableReference(name) != null; | ||
|
|
||
| const sourceVariableRefs = (names: string[] | undefined) => | ||
| names?.filter(isVariableRef) ?? []; | ||
|
|
||
| const mappableSourceNames = (names: string[] | undefined) => | ||
| names?.filter(name => !isVariableRef(name)); | ||
|
|
||
| /** Whether the filter names a single source that import must map. */ | ||
| const isSourceBackedTemplateFilter = ( | ||
| filter: DashboardFilter, | ||
| ): filter is QueryExpressionDashboardFilter | PromqlLabelDashboardFilter => | ||
| isQueryExpressionFilter(filter) || isPrometheusLabelFilter(filter); | ||
| (isQueryExpressionFilter(filter) || isPrometheusLabelFilter(filter)) && | ||
| !isVariableRef(filter.source); | ||
|
|
||
| /** Returns the filter if it names a source, or `undefined` if it doesn't. */ | ||
| const sourceBackedTemplateFilter = (filter: DashboardFilter | undefined) => | ||
|
|
@@ -497,6 +560,7 @@ export function buildMappingFormSchema( | |
| // meaningful source even when the template names one. | ||
| if ( | ||
| config.source && | ||
| !isVariableRef(config.source) && | ||
| displayTypeRequiresSource(config.displayType) && | ||
| !data.tileSourceMappings[idx] | ||
| ) { | ||
|
|
@@ -533,7 +597,10 @@ export function buildMappingFormSchema( | |
| const mappedSourceIds = data.filterAdhocSourceMappings?.[idx]?.filter( | ||
| id => !!id, | ||
| ); | ||
| if (!mappedSourceIds?.length) { | ||
| if ( | ||
| !mappedSourceIds?.length && | ||
| sourceVariableRefs(filter.sources).length === 0 | ||
| ) { | ||
| ctx.addIssue({ | ||
| code: z.ZodIssueCode.custom, | ||
| path: ['filterAdhocSourceMappings', idx], | ||
|
|
@@ -646,7 +713,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { | |
|
|
||
| const tileSourceMappings = input.tiles.map(tile => { | ||
| const config = tile.config as SavedChartConfig; | ||
| if (!config.source) return ''; | ||
| if (!config.source || isVariableRef(config.source)) return ''; | ||
| const match = sources.find( | ||
| source => source.name.toLowerCase() === config.source!.toLowerCase(), | ||
| ); | ||
|
|
@@ -1118,15 +1185,24 @@ export function Mapping({ input }: { input: DashboardTemplate }) { | |
|
|
||
| // Zip the source mappings with the input filters | ||
| const zippedFilters = input.filters?.map((filter, idx) => { | ||
| const appliesTo = data.filterAppliesToSourceMappings?.[idx]?.filter( | ||
| id => !!id?.length, | ||
| ); | ||
| const appliesToSourceIds = appliesTo?.length ? appliesTo : undefined; | ||
| // Data source variable references carry over unmapped | ||
| const appliesTo = [ | ||
| ...sourceVariableRefs( | ||
| appliesToTemplateFilter(filter)?.appliesToSourceIds, | ||
| ), | ||
| ...(data.filterAppliesToSourceMappings?.[idx]?.filter( | ||
| id => !!id?.length, | ||
| ) ?? []), | ||
| ]; | ||
| const appliesToSourceIds = appliesTo.length ? appliesTo : undefined; | ||
| if (isAdhocFilter(filter)) { | ||
| return { | ||
| ...filter, | ||
| sources: | ||
| data.filterAdhocSourceMappings?.[idx]?.filter(id => !!id) ?? [], | ||
| sources: [ | ||
| ...sourceVariableRefs(filter.sources), | ||
| ...(data.filterAdhocSourceMappings?.[idx]?.filter(id => !!id) ?? | ||
| []), | ||
| ], | ||
| appliesToSourceIds, | ||
| }; | ||
| } | ||
|
|
@@ -1227,7 +1303,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { | |
| return ( | ||
| <Fragment key={tile.id}> | ||
| {/** Mapping for the tile's source, if one exists (they're optional for raw sql tiles) */} | ||
| {tile.config.source && ( | ||
| {tile.config.source && !isVariableRef(tile.config.source) && ( | ||
| <Table.Tr> | ||
| <Table.Td>{tile.config.name}</Table.Td> | ||
| <Table.Td>Data source</Table.Td> | ||
|
|
@@ -1310,8 +1386,12 @@ export function Mapping({ input }: { input: DashboardTemplate }) { | |
|
|
||
| {/** Map filter sources */} | ||
| {input.filters?.map((filter, i) => { | ||
| const appliesToSourceIds = | ||
| appliesToTemplateFilter(filter)?.appliesToSourceIds; | ||
| const appliesToSourceIds = mappableSourceNames( | ||
| appliesToTemplateFilter(filter)?.appliesToSourceIds, | ||
| ); | ||
| const adhocSourceNames = isAdhocFilter(filter) | ||
| ? mappableSourceNames(filter.sources) | ||
| : undefined; | ||
| return ( | ||
| <Fragment key={filter.id}> | ||
| {isSourceBackedTemplateFilter(filter) && ( | ||
|
|
@@ -1356,11 +1436,11 @@ export function Mapping({ input }: { input: DashboardTemplate }) { | |
| </Table.Td> | ||
| </Table.Tr> | ||
| )} | ||
| {isAdhocFilter(filter) && ( | ||
| {isAdhocFilter(filter) && !!adhocSourceNames?.length && ( | ||
| <Table.Tr> | ||
| <Table.Td>{filter.name} (Filter)</Table.Td> | ||
| <Table.Td>Data sources</Table.Td> | ||
| <Table.Td>{filter.sources.join(', ')}</Table.Td> | ||
| <Table.Td>{adhocSourceNames.join(', ')}</Table.Td> | ||
| <Table.Td> | ||
| <SourceMultiSelectControlled | ||
| control={control} | ||
|
|
||
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.
🔵 minor — New import error list uses raw
c="red", Title Case copy, and keys that can collideThe conventions say new status text should use
<Text variant="danger">, notc="red", and UI copy should be sentence case ("Failed to import dashboard"). Also,key={issue}collides when two tiles or filters have the same name and the same bad reference, becausegetTemplateVariableReferenceIssuesbuilds identical strings for them. That causes React duplicate-key warnings. This block also repeats the schema-error block just above it. Fix: move both into one helper that rendersstring[]withvariant="danger"and index-based keys.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