diff --git a/.changeset/source-vars-4.md b/.changeset/source-vars-4.md
new file mode 100644
index 000000000..312539ba4
--- /dev/null
+++ b/.changeset/source-vars-4.md
@@ -0,0 +1,5 @@
+---
+'@hyperdx/app': minor
+---
+
+feat: let tiles and filters use a data source variable as their source
diff --git a/packages/app/src/DBDashboardImportPage.tsx b/packages/app/src/DBDashboardImportPage.tsx
index 21c5936fd..9f7eda309 100644
--- a/packages/app/src/DBDashboardImportPage.tsx
+++ b/packages/app/src/DBDashboardImportPage.tsx
@@ -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: (
+
+ {referenceIssues.map(issue => (
+
+ {issue}
+
+ ))}
+
+ ),
+ });
+ 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) =>
+ 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 (
{/** 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) && (
{tile.config.name}
Data source
@@ -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 (
{isSourceBackedTemplateFilter(filter) && (
@@ -1356,11 +1436,11 @@ export function Mapping({ input }: { input: DashboardTemplate }) {
)}
- {isAdhocFilter(filter) && (
+ {isAdhocFilter(filter) && !!adhocSourceNames?.length && (
{filter.name} (Filter)
Data sources
- {filter.sources.join(', ')}
+ {adhocSourceNames.join(', ')}
` return shallow and prevents the tile-grid tree below
// from being wrapped in an extra indentation level.
const dashboardBody = (
- <>
+
{title}
@@ -3601,7 +3605,7 @@ function DBDashboardPage({
variables={variables}
/>
)}
- >
+
);
return (
diff --git a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx
index 2bfec3b19..1083ba8ca 100644
--- a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx
+++ b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx
@@ -6,7 +6,11 @@ import {
} from '@hyperdx/common-utils/dist/types';
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
-import { buildMappingFormSchema, Mapping } from '@/DBDashboardImportPage';
+import {
+ buildMappingFormSchema,
+ getTemplateVariableReferenceIssues,
+ Mapping,
+} from '@/DBDashboardImportPage';
// Sources/connections the import maps the template's source *names* onto. The
// auto-mapping effect in `Mapping` matches by name (case-insensitive), so a
@@ -724,3 +728,86 @@ describe('Dashboard import - ad hoc filter sources', () => {
expect(issuesFor(['src-logs'])).toBeUndefined();
});
});
+
+describe('Dashboard import - data source variables', () => {
+ const sourceVariableTemplate = DashboardTemplateSchema.parse({
+ version: '0.1.0',
+ name: 'Source Variables',
+ tiles: [builderTile('tile-line', 'line', '$logs', 0)],
+ filters: [
+ {
+ id: 'ds',
+ type: 'DATA_SOURCE',
+ name: 'Logs source',
+ variableName: 'logs',
+ allowedSourceKinds: [SourceKind.Log],
+ minSelections: 1,
+ maxSelections: 1,
+ isBroadcastEnabled: false,
+ isVariableEnabled: true,
+ },
+ {
+ id: 'svc',
+ type: 'QUERY_EXPRESSION',
+ name: 'Service',
+ expression: 'ServiceName',
+ source: '$logs',
+ },
+ {
+ id: 'adhoc',
+ type: 'ADHOC',
+ name: 'Conditions',
+ sourceType: 'sql',
+ sources: ['$logs', 'Traces'],
+ isBroadcastEnabled: true,
+ isVariableEnabled: true,
+ },
+ ],
+ });
+
+ it('keeps references as written without asking for a mapping', async () => {
+ renderWithMantine();
+
+ const finish = await screen.findByRole('button', {
+ name: /finish import/i,
+ });
+ expect(screen.queryByText('$logs')).not.toBeInTheDocument();
+ await act(async () => {
+ fireEvent.click(finish);
+ });
+
+ await waitFor(() => expect(mockMutateAsync).toHaveBeenCalledTimes(1));
+ const payload = mockMutateAsync.mock.calls[0][0];
+ expect(payload.tiles[0].config.source).toBe('$logs');
+ expect(payload.filters[1].source).toBe('$logs');
+ expect(payload.filters[2].sources).toEqual(['$logs', 'src-traces']);
+ });
+
+ it('accepts references to the variables the template declares', () => {
+ expect(getTemplateVariableReferenceIssues(sourceVariableTemplate)).toEqual(
+ [],
+ );
+ });
+
+ it('reports references to variables the template does not declare', () => {
+ const template = DashboardTemplateSchema.parse({
+ version: '0.1.0',
+ name: 'Undeclared',
+ tiles: [builderTile('tile-line', 'line', '$traces', 0)],
+ filters: [
+ {
+ id: 'svc',
+ type: 'QUERY_EXPRESSION',
+ name: 'Service',
+ expression: 'ServiceName',
+ source: '$traces',
+ },
+ ],
+ });
+
+ expect(getTemplateVariableReferenceIssues(template)).toEqual([
+ 'Tile "tile-line": "$traces" does not name a data source variable on this dashboard',
+ 'Filter "Service": "$traces" does not name a data source variable on this dashboard',
+ ]);
+ });
+});
diff --git a/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx b/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx
index 838e3ec73..9a7942579 100644
--- a/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx
+++ b/packages/app/src/components/ChartEditor/PromqlChartEditor.tsx
@@ -21,6 +21,7 @@ import { IconCirclePlus } from '@tabler/icons-react';
import { HeatmapModeControl } from '@/components/DBEditTimeChartForm/HeatmapModeControl';
import { SourceSelectControlled } from '@/components/SourceSelect';
+import { useResolvedReference } from '@/components/sourceSelectUtils';
import { usePromqlMetricNames } from '@/hooks/usePromqlMetadata';
import { useSource } from '@/source';
@@ -80,7 +81,7 @@ export default function PromqlChartEditor({
);
}, []);
- const sourceId = useWatch({ control, name: 'source' });
+ const sourceId = useResolvedReference(useWatch({ control, name: 'source' }));
const displayType = useWatch({ control, name: 'displayType' });
const { data: source } = useSource({ id: sourceId });
// The form can still hold a non-PromQL source right after switching a tile
@@ -107,6 +108,7 @@ export default function PromqlChartEditor({
name="source"
data-testid="source-selector"
allowedSourceKinds={allowedSourceKinds}
+ allowSourceVariables
/>
{displayType === DisplayType.Heatmap && (
diff --git a/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx b/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx
index ea2304d03..bfc60685a 100644
--- a/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx
+++ b/packages/app/src/components/ChartEditor/RawSqlChartEditor.tsx
@@ -9,6 +9,7 @@ import {
validateRawSqlChartConfig,
validateRawSqlForAlert,
} from '@hyperdx/common-utils/dist/core/utils';
+import { parseVariableReference } from '@hyperdx/common-utils/dist/referenceVariables';
import {
ChartVariable,
RawSqlChartConfig,
@@ -41,6 +42,7 @@ import SourceSchemaPreview, {
isSourceSchemaPreviewEnabled,
} from '@/components/SourceSchemaPreview';
import { SourceSelectControlled } from '@/components/SourceSelect';
+import { useResolvedReference } from '@/components/sourceSelectUtils';
import { SQLEditorControlled } from '@/components/SQLEditor/SQLEditor';
import { type SQLCompletion } from '@/components/SQLEditor/utils';
import { IS_LOCAL_MODE } from '@/config';
@@ -80,9 +82,14 @@ export function resolveConnectionSourceSync({
sources: { id: string; connection: string }[] | undefined;
}): ConnectionSourceSyncResult {
if (!sources) return null;
+ // A data source variable reference isn't bound to a connection here: the
+ // filter bar decides what it selects, so it neither moves the connection
+ // nor is cleared by a connection change.
+ const isSourceReference = parseVariableReference(source) != null;
// When the source changes, sync the connection to match.
if (source !== prevSource) {
+ if (isSourceReference) return null;
const sourceConnection = sources.find(s => s.id === source)?.connection;
if (sourceConnection && sourceConnection !== connection) {
return { field: 'connection', value: sourceConnection };
@@ -105,7 +112,7 @@ export function resolveConnectionSourceSync({
// connection change above was itself triggered by the source.
if (connection !== prevConnection && prevConnection !== undefined) {
const sourceConnection = sources.find(s => s.id === source)?.connection;
- if (source && sourceConnection !== connection) {
+ if (source && !isSourceReference && sourceConnection !== connection) {
return { field: 'source', value: '' };
}
}
@@ -148,7 +155,10 @@ export default function RawSqlChartEditor({
const displayType = useWatch({ control, name: 'displayType' });
const connection = useWatch({ control, name: 'connection' });
- const source = useWatch({ control, name: 'source' });
+ const storedSource = useWatch({ control, name: 'source' });
+ // A data source variable reference expands macros as the source it
+ // currently selects.
+ const source = useResolvedReference(storedSource);
const sqlTemplate = useWatch({ control, name: 'sqlTemplate' });
const chartName = useWatch({ control, name: 'name' });
const sourceObject = sources?.find(s => s.id === source);
@@ -196,12 +206,12 @@ export default function RawSqlChartEditor({
};
}, [debouncedRawSqlConfig, isDashboardForm]);
- const prevSource = usePrevious(source);
+ const prevSource = usePrevious(storedSource);
const prevConnection = usePrevious(connection);
useEffect(() => {
const update = resolveConnectionSourceSync({
- source,
+ source: storedSource,
connection,
prevSource,
prevConnection,
@@ -212,7 +222,7 @@ export default function RawSqlChartEditor({
} else if (update?.field === 'source') {
setValue('source', update.value);
}
- }, [connection, prevConnection, prevSource, setValue, source, sources]);
+ }, [connection, prevConnection, prevSource, setValue, storedSource, sources]);
const placeholderSQl = SQL_PLACEHOLDERS[displayType ?? DisplayType.Table];
@@ -293,6 +303,7 @@ export default function RawSqlChartEditor({
name="source"
data-testid="source-selector"
allowedSourceKinds={allowedSourceKinds}
+ allowSourceVariables
connectionId={connection}
size="xs"
clearable
diff --git a/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts b/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts
index 7e979f0a1..57efcda34 100644
--- a/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts
+++ b/packages/app/src/components/ChartEditor/__tests__/resolveConnectionSourceSync.test.ts
@@ -107,4 +107,28 @@ describe('resolveConnectionSourceSync', () => {
}),
).toBeNull();
});
+
+ it('leaves the connection alone when a data source variable is selected', () => {
+ expect(
+ resolveConnectionSourceSync({
+ source: '$logs',
+ connection: 'conn-2',
+ prevSource: 'src-b',
+ prevConnection: 'conn-2',
+ sources,
+ }),
+ ).toBeNull();
+ });
+
+ it('keeps a data source variable when the connection changes', () => {
+ expect(
+ resolveConnectionSourceSync({
+ source: '$logs',
+ connection: 'conn-2',
+ prevSource: '$logs',
+ prevConnection: 'conn-1',
+ sources,
+ }),
+ ).toBeNull();
+ });
});
diff --git a/packages/app/src/components/ChartEditor/__tests__/utils.test.ts b/packages/app/src/components/ChartEditor/__tests__/utils.test.ts
index 63f4e2ded..24f910d61 100644
--- a/packages/app/src/components/ChartEditor/__tests__/utils.test.ts
+++ b/packages/app/src/components/ChartEditor/__tests__/utils.test.ts
@@ -19,6 +19,7 @@ import {
convertFormStateToSavedChartConfig,
convertSavedChartConfigToFormState,
getAllowedSourceKinds,
+ getInterchangeableSourceKind,
promqlExpressionPlaceholder,
validateChartForm,
} from '@/components/ChartEditor/utils';
@@ -2705,3 +2706,86 @@ describe('promqlExpressionPlaceholder', () => {
);
});
});
+
+describe('data source variable references', () => {
+ it('saves the reference rather than the source it resolves to', () => {
+ const form: ChartEditorFormState = {
+ displayType: DisplayType.Line,
+ source: '$logs',
+ series: [seriesItem],
+ };
+ expect(convertFormStateToSavedChartConfig(form, logSource)).toMatchObject({
+ source: '$logs',
+ });
+ });
+
+ it('queries the resolved source for a raw SQL chart', () => {
+ const form: ChartEditorFormState = {
+ configType: 'sql',
+ displayType: DisplayType.Table,
+ source: '$logs',
+ connection: 'conn-1',
+ sqlTemplate: 'SELECT 1',
+ series: [],
+ };
+ expect(convertFormStateToSavedChartConfig(form, logSource)).toMatchObject({
+ source: '$logs',
+ });
+ expect(
+ convertFormStateToChartConfig(form, dateRange, logSource),
+ ).toMatchObject({ source: logSource.id });
+ });
+
+ it('saves a reference with no selected source when its kind is known', () => {
+ const form: ChartEditorFormState = {
+ displayType: DisplayType.Line,
+ source: '$logs',
+ series: [
+ { ...seriesItem, metricName: 'cpu', metricType: MetricsDataType.Gauge },
+ ],
+ };
+ const saved = convertFormStateToSavedChartConfig(
+ form,
+ undefined,
+ SourceKind.Log,
+ );
+ expect(saved).toMatchObject({ source: '$logs' });
+ expect(saved).toMatchObject({
+ select: [expect.not.objectContaining({ metricName: 'cpu' })],
+ });
+ });
+
+ it('validates a reference with no selected source by its kind', () => {
+ const form: ChartEditorFormState = {
+ displayType: DisplayType.Line,
+ source: '$logs',
+ series: [seriesItem],
+ };
+ expect(
+ validateChartForm(form, undefined, jest.fn(), {
+ sourceKind: SourceKind.Log,
+ }),
+ ).toEqual([]);
+ expect(
+ validateChartForm(form, undefined, jest.fn(), {
+ missingSourceMessage: 'Select a source for $logs',
+ }),
+ ).toEqual([{ path: 'source', message: 'Select a source for $logs' }]);
+ });
+});
+
+describe('getInterchangeableSourceKind', () => {
+ it.each<[SourceKind[], SourceKind | undefined]>([
+ [[SourceKind.Log], SourceKind.Log],
+ [[SourceKind.Log, SourceKind.Trace], SourceKind.Log],
+ [[SourceKind.Log, SourceKind.Metric], undefined],
+ [[SourceKind.Log, SourceKind.Session], undefined],
+ [[SourceKind.Session, SourceKind.Promql], undefined],
+ [[], undefined],
+ ])(
+ 'returns a stand-in for %p only when charts treat them alike',
+ (kinds, expected) => {
+ expect(getInterchangeableSourceKind(kinds)).toBe(expected);
+ },
+ );
+});
diff --git a/packages/app/src/components/ChartEditor/utils.ts b/packages/app/src/components/ChartEditor/utils.ts
index ba2239df5..3d6babc24 100644
--- a/packages/app/src/components/ChartEditor/utils.ts
+++ b/packages/app/src/components/ChartEditor/utils.ts
@@ -23,6 +23,7 @@ import {
} from '@hyperdx/common-utils/dist/guards';
import { MACRO_SUGGESTIONS } from '@hyperdx/common-utils/dist/macros';
import { QUERY_PARAMS_BY_DISPLAY_TYPE } from '@hyperdx/common-utils/dist/rawSqlParams';
+import { getConfigReferenceVariables } from '@hyperdx/common-utils/dist/referenceVariables';
import {
BuilderSavedChartConfig,
ChartConfigWithDateRange,
@@ -85,6 +86,32 @@ export function buildRawSqlCompletions({
];
}
+/**
+ * Groups kinds that charts treat alike: validation and normalization only ask
+ * whether a source is a metric source and whether it supports formulas, and
+ * PromQL sources only serve PromQL charts.
+ */
+function chartSourceKindClass(kind: SourceKind): string {
+ if (kind === SourceKind.Promql) return 'promql';
+ if (kind === SourceKind.Metric) return 'metric';
+ return isFormulaSourceKind(kind) ? 'events' : 'session';
+}
+
+/**
+ * A kind that stands for all of `kinds` when charts treat them alike, so a
+ * chart built for one is valid on the others. Undefined when they differ.
+ */
+export function getInterchangeableSourceKind(
+ kinds: readonly SourceKind[],
+): SourceKind | undefined {
+ const [first] = kinds;
+ if (first == null) return undefined;
+ const firstClass = chartSourceKindClass(first);
+ return kinds.every(kind => chartSourceKindClass(kind) === firstClass)
+ ? first
+ : undefined;
+}
+
function normalizeChartConfig<
C extends Pick<
BuilderSavedChartConfig,
@@ -100,15 +127,15 @@ function normalizeChartConfig<
| 'heatmap'
| 'where'
>,
->(config: C, source: TSource): C {
- const isMetricSource = source.kind === SourceKind.Metric;
+>(config: C, sourceKind: SourceKind): C {
+ const isMetricSource = sourceKind === SourceKind.Metric;
// Formulas (HDX-5080) render on metric and event (log/trace) sources, and
// only on the display types the formula query paths support (time series /
// table / number). Strip them elsewhere so a source or display-type switch
// can't persist a config the renderer would reject. The form state keeps
// them, so switching back restores the formula rows.
const keepFormulas =
- isFormulaSourceKind(source.kind) &&
+ isFormulaSourceKind(sourceKind) &&
(config.formulas?.length ?? 0) > 0 &&
isFormulaDisplayType(config.displayType);
return {
@@ -179,6 +206,22 @@ export const isRawSqlDisplayType = (
displayType === DisplayType.Bar ||
displayType === DisplayType.Number;
+/** The variables a chart form's source, or a raw SQL tile's connection, references. */
+export function getFormReferenceVariables(
+ form: Pick<
+ ChartEditorFormState,
+ 'source' | 'connection' | 'configType' | 'displayType'
+ >,
+): string[] {
+ // Only raw SQL tiles store a connection of their own
+ const isRawSql =
+ form.configType === 'sql' && isRawSqlDisplayType(form.displayType);
+ return getConfigReferenceVariables({
+ source: form.source,
+ connection: isRawSql ? form.connection : undefined,
+ });
+}
+
const isCustomOrderByDisplayType = (
displayType: DisplayType | undefined,
): displayType is DisplayType.Table | DisplayType.Bar | DisplayType.Pie =>
@@ -200,9 +243,14 @@ export {
toBuilderHeatmapMode,
};
+/**
+ * `sourceKind` stands in for `source` when there is no source to read it from,
+ * as for a data source variable with no selection.
+ */
export function convertFormStateToSavedChartConfig(
form: ChartEditorFormState,
source: TSource | undefined,
+ sourceKind: SourceKind | undefined = source?.kind,
): SavedChartConfig | undefined {
if (form.configType === 'promql' && isPromqlDisplayType(form.displayType)) {
const promqlConfig: PromqlSavedChartConfig = {
@@ -225,7 +273,7 @@ export function convertFormStateToSavedChartConfig(
// 'alert', // TODO: Support alerts on PromQL (HDX-4636)
]),
promqlExpression: formPromqlExpressions(form),
- source: source?.id ?? form.source ?? '',
+ source: form.source || (source?.id ?? ''),
legendTemplate: form.legendTemplate?.trim() || undefined,
heatmap: formPromqlHeatmap(form),
};
@@ -276,12 +324,12 @@ export function convertFormStateToSavedChartConfig(
heatmap: formBuilderHeatmap(form),
select: [],
where: form.where ?? '',
- source: source?.id ?? form.source ?? '',
+ source: form.source || (source?.id ?? ''),
};
return config;
}
- if (source) {
+ if (sourceKind) {
// Merge the series and select fields back together, and prevent the series field from being submitted
const config: BuilderSavedChartConfig = {
...omit(form, [
@@ -299,10 +347,10 @@ export function convertFormStateToSavedChartConfig(
: ''
: form.series,
where: form.where ?? '',
- source: source.id,
+ source: form.source || (source?.id ?? ''),
};
- return normalizeChartConfig(config, source);
+ return normalizeChartConfig(config, sourceKind);
}
}
@@ -361,7 +409,7 @@ export function convertFormStateToChartConfig(
]),
sqlTemplate: form.sqlTemplate ?? '',
connection: form.connection ?? '',
- source: form.source || undefined,
+ source: source?.id ?? (form.source || undefined),
from: source?.from,
implicitColumnExpression:
source && (isLogSource(source) || isTraceSource(source))
@@ -434,7 +482,7 @@ export function convertFormStateToChartConfig(
: mergedSelect,
};
- return structuredClone(normalizeChartConfig(newConfig, source));
+ return structuredClone(normalizeChartConfig(newConfig, source.kind));
}
}
@@ -550,6 +598,8 @@ export const validateChartForm = (
setError: UseFormSetError,
{
requireAlertDisplayName = false,
+ sourceKind = source?.kind,
+ missingSourceMessage = 'Source is required',
}: {
/**
* Whether the alert must carry a name. Set for inline alerts, which have
@@ -557,6 +607,13 @@ export const validateChartForm = (
* name the alert after whatever the chart happens to be called.
*/
requireAlertDisplayName?: boolean;
+ /**
+ * Stands in for `source` when there is no source to read the kind from,
+ * as for a data source variable with no selection.
+ */
+ sourceKind?: SourceKind;
+ /** Replaces the generic error for a chart with no usable source. */
+ missingSourceMessage?: string;
} = {},
) => {
const errors: { path: Path; message: string }[] = [];
@@ -580,9 +637,9 @@ export const validateChartForm = (
if (
!isRawSqlChart &&
form.displayType !== DisplayType.Markdown &&
- (!form.source || !source)
+ (!form.source || !sourceKind)
) {
- errors.push({ path: `source`, message: 'Source is required' });
+ errors.push({ path: `source`, message: missingSourceMessage });
}
// Validate that valueExpressions are specified for each series.
@@ -591,7 +648,7 @@ export const validateChartForm = (
if (
!isRawSqlChart &&
Array.isArray(form.series) &&
- source?.kind !== SourceKind.Metric &&
+ sourceKind !== SourceKind.Metric &&
form.displayType !== DisplayType.Markdown &&
!isStringSelectDisplayType(form.displayType)
) {
@@ -607,7 +664,7 @@ export const validateChartForm = (
// Validate metric names for metric sources
if (
- source?.kind === SourceKind.Metric &&
+ sourceKind === SourceKind.Metric &&
Array.isArray(form.series) &&
form.displayType !== DisplayType.Markdown &&
form.displayType !== DisplayType.Search &&
@@ -629,7 +686,7 @@ export const validateChartForm = (
// source kind + display type).
if (
!isRawSqlChart &&
- isFormulaSourceKind(source?.kind) &&
+ isFormulaSourceKind(sourceKind) &&
isFormulaDisplayType(form.displayType) &&
Array.isArray(form.formulas)
) {
@@ -727,7 +784,7 @@ export const validateChartForm = (
!isRawSqlChart &&
Array.isArray(form.series) &&
form.displayType === DisplayType.Number &&
- !(form.formulas?.length && isFormulaSourceKind(source?.kind)) &&
+ !(form.formulas?.length && isFormulaSourceKind(sourceKind)) &&
form.series.length > (form.seriesReturnType === 'ratio' ? 2 : 1)
) {
errors.push({
diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx
index 4524f411a..7c5d577cd 100644
--- a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx
+++ b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx
@@ -211,6 +211,7 @@ export function ChartEditorControls({
name="source"
data-testid="source-selector"
allowedSourceKinds={allowedSourceKinds}
+ allowSourceVariables
onSchemaPreview={() => setIsSourceSchemaPreviewOpen(true)}
isSchemaPreviewEnabled={isSourceSchemaPreviewEnabled(tableSource)}
/>
diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx
index 61e1b47af..a061b8811 100644
--- a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx
+++ b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx
@@ -178,6 +178,10 @@ type ChartPreviewPanelProps = {
dbTimeChartConfig?: ChartConfigWithDateRange;
/** Required dashboard filters with nothing selected that block this tile. */
missingRequiredFilterNames?: string[];
+ /** Whether turning off "Apply filters" would clear the required-filter block. */
+ canPreviewWithoutFilters?: boolean;
+ /** Why the current selections can't be queried, shown instead of the chart. */
+ blockedReason?: string;
setValue: (name: 'orderBy', value: string) => void;
setDisplayType: (displayType: DisplayType) => void;
onSubmit: () => void;
@@ -197,6 +201,8 @@ export function ChartPreviewPanel({
showGeneratedPromql,
dbTimeChartConfig,
missingRequiredFilterNames,
+ canPreviewWithoutFilters = false,
+ blockedReason,
setValue,
setDisplayType,
onSubmit,
@@ -213,8 +219,12 @@ export function ChartPreviewPanel({
const blockingFilterNames = missingRequiredFilterNames ?? [];
const isBlockedByRequiredFilters =
blockingFilterNames.length > 0 && tabQueriesData(activeTab);
+ const isBlocked =
+ blockedReason != null &&
+ !isBlockedByRequiredFilters &&
+ tabQueriesData(activeTab);
const queryReady =
- !isBlockedByRequiredFilters && !!isQueryReady(queriedConfig);
+ !isBlockedByRequiredFilters && !isBlocked && !!isQueryReady(queriedConfig);
const onTableSortingChange = useCallback(
(sortState: SortingState | null) => {
@@ -262,13 +272,24 @@ export function ChartPreviewPanel({
<>
{isBlockedByRequiredFilters ? (
+ ) : isBlocked ? (
+
) : !queryReady && tabQueriesData(activeTab) ? (
= (
+ values,
+ context,
+ options,
+) =>
+ zodChartFormResolver(
+ getFormReferenceVariables(values).length > 0
+ ? { ...values, alert: undefined }
+ : values,
+ context,
+ options,
+ );
+
const ALERT_IGNORES_DASHBOARD_FILTERS =
'Dashboard-level filter and variable selections cannot be applied when an alert is configured.';
@@ -243,7 +276,6 @@ export default function EditTimeChartForm({
isAlertRequired = false,
showSaveToDashboard,
}: EditTimeChartFormProps) {
- const alertsEnabled = enableAlerts ?? dashboardId != null;
const formValue: ChartEditorFormState = useMemo(
() => convertSavedChartConfigToFormState(chartConfig),
[chartConfig],
@@ -266,7 +298,7 @@ export default function EditTimeChartForm({
} = useForm({
defaultValues: formValue,
values: formValue,
- resolver: zodResolver(zSavedChartConfig),
+ resolver: chartFormResolver,
});
const {
@@ -346,13 +378,75 @@ export default function EditTimeChartForm({
[configType, displayType, heatmapMode],
);
+ // The form keeps a data source variable reference as written; everything
+ // that queries uses the source the variable currently selects.
+ const sourceVariableName = parseVariableReference(sourceId);
+ const resolvedSourceId = useResolvedReference(sourceId);
+ const resolvedConnection = useResolvedReference(connection);
+ const sourceVariableDeclarations = useSourceVariableDeclarations();
+
+ const referenceVariableValues = useReferenceVariableValues();
+ const referenceVariableNames = useMemo(
+ () =>
+ getFormReferenceVariables({
+ source: sourceId,
+ connection,
+ configType,
+ displayType,
+ }),
+ [sourceId, connection, configType, displayType],
+ );
+ const usesReferenceVariable = referenceVariableNames.length > 0;
+
+ // An alert must query one fixed source and connection, not the viewer's
+ // current selection.
+ const alertsEnabled =
+ (enableAlerts ?? dashboardId != null) && !usesReferenceVariable;
+ // The alert as it will save
+ const activeAlert = usesReferenceVariable ? undefined : alert;
+
// A source selection that current mode can't query counts as no source,
// so that nothing downstream builds a query from it.
const { data: tableSource } = useSource({
- id: sourceId,
+ id: resolvedSourceId,
kinds: allowedSourceKinds,
});
+ // Saving reads only the source's kind. With no source selected, a variable
+ // whose kinds charts all treat alike still determines it.
+ const sourceVariable = sourceVariableDeclarations.find(
+ variable => variable.name === sourceVariableName,
+ );
+ const sourceKind = useMemo(() => {
+ if (tableSource) return tableSource.kind;
+ if (resolvedSourceId != null) return;
+ const kinds = sourceVariable?.allowedSourceKinds.filter(kind =>
+ allowedSourceKinds.includes(kind),
+ );
+ return kinds && getInterchangeableSourceKind(kinds);
+ }, [tableSource, resolvedSourceId, sourceVariable, allowedSourceKinds]);
+
+ // Why a `$name` source gave no usable source kind
+ const missingSourceMessage =
+ sourceVariableName == null
+ ? undefined
+ : sourceVariable == null
+ ? `This dashboard has no data source variable named $${sourceVariableName}`
+ : resolvedSourceId == null
+ ? `Select a source for $${sourceVariableName} in the filter bar to save this tile`
+ : `The source selected for $${sourceVariableName} can't be used with this chart type`;
+
+ // A variable can select a source on another connection than the one a raw
+ // SQL query runs on, which the dashboard blocks too
+ const previewBlockedReason =
+ isRawSqlInput &&
+ usesReferenceVariable &&
+ tableSource != null &&
+ resolvedConnection != null &&
+ tableSource.connection !== resolvedConnection
+ ? "The selected data source is on a different connection than this tile's query. Select a source on the same connection."
+ : undefined;
+
const databaseName = tableSource?.from.databaseName;
const tableName = tableSource?.from.tableName;
@@ -501,7 +595,7 @@ export default function EditTimeChartForm({
// Alerts ignore dashboard-level filters and variables,
// so disable the toggle when an alert is configured.
const [applyFilters, setApplyFilters] = useState(true);
- const resolvedApplyFilters = applyFilters && alert == null;
+ const resolvedApplyFilters = applyFilters && activeAlert == null;
const dashboardFiltersToggleProps = useMemo<
DashboardFiltersToggleProps | undefined
@@ -512,10 +606,10 @@ export default function EditTimeChartForm({
: {
checked: resolvedApplyFilters,
disabledReason:
- alert != null ? ALERT_IGNORES_DASHBOARD_FILTERS : undefined,
+ activeAlert != null ? ALERT_IGNORES_DASHBOARD_FILTERS : undefined,
onChange: setApplyFilters,
},
- [getDashboardFilters, resolvedApplyFilters, alert],
+ [getDashboardFilters, resolvedApplyFilters, activeAlert],
);
const previewDashboardFilters = useMemo(() => {
@@ -534,6 +628,7 @@ export default function EditTimeChartForm({
variables,
unsatisfiedRequiredFilters,
applySelections: resolvedApplyFilters,
+ referenceVariableNames,
});
}, [
queriedConfig,
@@ -541,6 +636,7 @@ export default function EditTimeChartForm({
variables,
unsatisfiedRequiredFilters,
resolvedApplyFilters,
+ referenceVariableNames,
]);
// Attach the dashboard's filters and variables so that the preview queries
@@ -555,17 +651,42 @@ export default function EditTimeChartForm({
};
}, [queriedConfig, previewDashboardFilters]);
+ const debouncedForm = useDebouncedFormValues(watch, getValues, 300);
+
+ // With no selection for a variable it references the tile has nothing to
+ // query, so there may be no queried config to compute the block from.
+ const previewBlockingFilters = useMemo(() => {
+ if (previewDashboardFilters) return previewDashboardFilters;
+ if (
+ !referenceVariableNames.some(name => !referenceVariableValues.get(name))
+ )
+ return undefined;
+ return getPreviewBlockingFilters({
+ config: convertFormStateToSavedChartConfig(debouncedForm, tableSource),
+ sourceId: undefined,
+ variables,
+ unsatisfiedRequiredFilters,
+ applySelections: resolvedApplyFilters,
+ referenceVariableNames,
+ });
+ }, [
+ previewDashboardFilters,
+ referenceVariableNames,
+ referenceVariableValues,
+ debouncedForm,
+ tableSource,
+ variables,
+ unsatisfiedRequiredFilters,
+ resolvedApplyFilters,
+ ]);
+
const dbTimeChartConfig = useMemo(
- () => computeDbTimeChartConfig(previewConfig, alert),
- [previewConfig, alert],
+ () => computeDbTimeChartConfig(previewConfig, activeAlert),
+ [previewConfig, activeAlert],
);
- // Casting because `useWatch` returns a deep partial type, but we know that the
- // form state is complete due to default values set above.
- const watchedForm = useWatch({ control }) as ChartEditorFormState;
- const [debouncedForm] = useDebouncedValue(watchedForm, 300);
const additionalAlertWarnings = useMemo(() => {
- if (alert == null) return [];
+ if (activeAlert == null) return [];
const config = convertFormStateToSavedChartConfig(
debouncedForm,
tableSource,
@@ -574,7 +695,7 @@ export default function EditTimeChartForm({
? getAlertVariableWarning(config, variables)
: undefined;
return variableWarning ? [variableWarning] : [];
- }, [alert, debouncedForm, tableSource, variables]);
+ }, [activeAlert, debouncedForm, tableSource, variables]);
const [saveToDashboardModalOpen, setSaveToDashboardModalOpen] =
useState(false);
@@ -584,10 +705,16 @@ export default function EditTimeChartForm({
const errors = validateChartForm(form, tableSource, setError, {
// An inline alert has no tile to inherit a name from.
requireAlertDisplayName: alertsEnabled && dashboardId == null,
+ sourceKind,
+ missingSourceMessage,
});
if (errors.length > 0) return { errors, config: null };
- const savedConfig = convertFormStateToSavedChartConfig(form, tableSource);
+ const savedConfig = convertFormStateToSavedChartConfig(
+ form,
+ tableSource,
+ sourceKind,
+ );
if (!savedConfig) {
console.error(
'convertFormStateToSavedChartConfig returned undefined after validation passed. ' +
@@ -621,6 +748,8 @@ export default function EditTimeChartForm({
},
[
tableSource,
+ sourceKind,
+ missingSourceMessage,
setError,
alertsEnabled,
dashboardId,
@@ -646,11 +775,19 @@ export default function EditTimeChartForm({
return;
}
- const queriedConfig = convertFormStateToChartConfig(
+ // A raw SQL tile's connection may be a variable reference; the
+ // source is already resolved through `tableSource`.
+ const convertedQueriedConfig = convertFormStateToChartConfig(
form,
dateRange,
tableSource,
);
+ const queriedConfig =
+ convertedQueriedConfig &&
+ resolveConfigReferences(
+ convertedQueriedConfig,
+ referenceVariableValues,
+ );
if (config && queriedConfig) {
const isRawSqlChart =
@@ -670,6 +807,7 @@ export default function EditTimeChartForm({
setQueriedConfigAndSource,
tableSource,
dateRange,
+ referenceVariableValues,
],
);
@@ -784,7 +922,7 @@ export default function EditTimeChartForm({
const allowedSourceItems = useFilteredSortedSourceItems({
sources,
allowedSourceKinds,
- connectionId: configType === 'sql' ? connection : undefined,
+ connectionId: configType === 'sql' ? resolvedConnection : undefined,
groupBySection: true,
});
@@ -817,6 +955,17 @@ export default function EditTimeChartForm({
// should be filled in. Raw SQL source is optional, so leave it.
if (!sourceId && configType === 'sql') return;
+ // Kept on the same terms the picker offers it
+ if (
+ isSourceVariableReferenceAccepted(
+ sourceId,
+ sourceVariableDeclarations,
+ allowedSourceKinds,
+ )
+ ) {
+ return;
+ }
+
// If the currently selected source is still allowed, keep it.
const selected = sources.find(s => s.id === sourceId);
if (
@@ -846,6 +995,7 @@ export default function EditTimeChartForm({
setValue,
sourceId,
sources,
+ sourceVariableDeclarations,
]);
useEffect(() => {
@@ -1320,7 +1470,7 @@ export default function EditTimeChartForm({
onOpenDisplaySettings={openDisplaySettings}
onSubmit={onSubmit}
isDashboardForm={isDashboardForm}
- alert={alert}
+ alert={activeAlert}
additionalWarnings={additionalAlertWarnings}
alertsEnabled={alertsEnabled}
isAlertRequired={isAlertRequired}
@@ -1349,7 +1499,7 @@ export default function EditTimeChartForm({
activeTab={activeTab}
seriesReturnType={seriesReturnType}
ratioMode={ratioMode}
- alert={alert}
+ alert={activeAlert}
additionalWarnings={additionalAlertWarnings}
alertsEnabled={alertsEnabled}
isAlertRequired={isAlertRequired}
@@ -1363,6 +1513,18 @@ export default function EditTimeChartForm({
onHeatmapModeChange={onHeatmapModeChange}
/>
)}
+ {alert != null && usesReferenceVariable && (
+ }
+ p="xs"
+ mt="sm"
+ >
+ Alerts aren't available on tiles that use a data source or
+ connection variable. This tile's alert will be removed when
+ you save.
+
+ )}
setValue(name, value)}
setDisplayType={type =>
setValue('displayType', type, { shouldDirty: true })
diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx
index 1b50d0218..878ccf494 100644
--- a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx
+++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx
@@ -514,6 +514,7 @@ describe('ChartPreviewPanel', () => {
queriedConfig: baseBuilderConfig,
dbTimeChartConfig: baseBuilderConfig,
missingRequiredFilterNames: ['Service', 'Environment'],
+ canPreviewWithoutFilters: true,
};
it('names them instead of querying', () => {
@@ -521,7 +522,7 @@ describe('ChartPreviewPanel', () => {
expect(
screen.getByText(
- 'Missing required filters: Service, Environment. Select a value for each required filter, or turn off “Apply filters” to preview this tile without them.',
+ 'Missing required filters or variables: Service, Environment. Select a value for each required filter, or turn off “Apply filters” to preview this tile without them.',
),
).toBeInTheDocument();
expect(screen.queryByTestId('db-time-chart')).not.toBeInTheDocument();
@@ -531,6 +532,16 @@ describe('ChartPreviewPanel', () => {
).not.toBeInTheDocument();
});
+ it('leaves out the "Apply filters" hint when turning it off would not help', () => {
+ renderPanel({ ...blockedProps, canPreviewWithoutFilters: false });
+
+ expect(
+ screen.getByTestId('preview-missing-required-filters'),
+ ).toHaveTextContent(
+ /^Missing required filters or variables: Service, Environment\. Select a value for each one to preview this tile\.$/,
+ );
+ });
+
it('does not block a markdown tile, which queries nothing', () => {
renderPanel({ ...blockedProps, activeTab: 'markdown' });
@@ -544,8 +555,40 @@ describe('ChartPreviewPanel', () => {
expect(screen.getByTestId('db-time-chart')).toBeInTheDocument();
expect(
- screen.queryByText(/Missing required filters/),
+ screen.queryByText(/Missing required filters or variables/),
).not.toBeInTheDocument();
});
});
+
+ describe('when the selections are blocked', () => {
+ const blockedProps = {
+ queriedConfig: baseBuilderConfig,
+ dbTimeChartConfig: baseBuilderConfig,
+ blockedReason: 'The selected data source is on another connection.',
+ };
+
+ it('shows the reason instead of querying', () => {
+ renderPanel(blockedProps);
+
+ expect(screen.getByTestId('preview-blocked')).toHaveTextContent(
+ 'The selected data source is on another connection.',
+ );
+ expect(screen.queryByTestId('db-time-chart')).not.toBeInTheDocument();
+ });
+
+ it('shows missing required filters first', () => {
+ renderPanel({ ...blockedProps, missingRequiredFilterNames: ['Service'] });
+
+ expect(
+ screen.getByTestId('preview-missing-required-filters'),
+ ).toBeInTheDocument();
+ expect(screen.queryByTestId('preview-blocked')).not.toBeInTheDocument();
+ });
+
+ it('does not block a markdown tile, which queries nothing', () => {
+ renderPanel({ ...blockedProps, activeTab: 'markdown' });
+
+ expect(screen.queryByTestId('preview-blocked')).not.toBeInTheDocument();
+ });
+ });
});
diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx b/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx
index bd9dd337b..8b35f1ada 100644
--- a/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx
+++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/DBEditTimeChartForm.test.tsx
@@ -3,6 +3,8 @@ import React from 'react';
import { useController as mockUseController } from 'react-hook-form';
import {
AlertThresholdType,
+ DashboardFilter,
+ DataSourceDashboardFilter,
DisplayType,
MetricsDataType,
SavedChartConfig,
@@ -13,6 +15,7 @@ import { fireEvent, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import DBEditTimeChartForm from '@/components/DBEditTimeChartForm';
+import { ReferenceVariablesProvider } from '@/components/sourceSelectUtils';
import { useSource } from '@/source';
import { DEFAULT_TILE_ALERT } from '@/utils/alerts';
@@ -40,6 +43,8 @@ jest.mock('@/hooks/useFetchMetricMetadata', () => ({
jest.mock('@/hooks/useMetadata', () => ({
useMetadataWithSettings: jest.fn().mockReturnValue({}),
+ // The raw SQL editor's completions
+ useMultipleAllFields: jest.fn().mockReturnValue({ data: [] }),
useGetKeyValues: jest.fn().mockReturnValue({
data: [
{
@@ -188,6 +193,7 @@ jest.mock('../../SourceSelect', () => ({
+
);
},
@@ -1585,3 +1591,221 @@ describe('DBEditTimeChartForm - Inline alerts', () => {
expect(screen.queryByTestId('remove-alert-button')).not.toBeInTheDocument();
});
});
+
+describe('DBEditTimeChartForm - Raw SQL data source variables', () => {
+ const logsVariable: DataSourceDashboardFilter = {
+ id: 'logs',
+ type: 'DATA_SOURCE',
+ name: 'logs',
+ variableName: 'logs',
+ allowedSourceKinds: [SourceKind.Log],
+ minSelections: 1,
+ maxSelections: 1,
+ isBroadcastEnabled: false,
+ isVariableEnabled: true,
+ };
+
+ const rawSqlConfig: SavedChartConfig = {
+ configType: 'sql',
+ displayType: DisplayType.Table,
+ name: 'Raw SQL tile',
+ source: '$logs',
+ connection: 'conn-2',
+ sqlTemplate: 'SELECT count() FROM $__sourceTable',
+ };
+
+ const renderWithSelection = (sourceId: string) =>
+ renderWithMantine(
+
+
+
+
+ ,
+ );
+
+ const logSource = (connection: string) => ({
+ id: 'logs-source',
+ kind: SourceKind.Log,
+ name: 'Logs',
+ from: { databaseName: 'default', tableName: 'otel_logs' },
+ connection,
+ timestampValueExpression: 'Timestamp',
+ });
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it("blocks the preview when the variable's source is on another connection", async () => {
+ mockUseSourceData(logSource('conn-1'));
+ renderWithSelection('logs-source');
+
+ expect(await screen.findByTestId('preview-blocked')).toHaveTextContent(
+ "The selected data source is on a different connection than this tile's query.",
+ );
+ });
+
+ it('blocks only on the unselected variable once filters are not applied', async () => {
+ const globalFilter: DashboardFilter = {
+ id: 'service',
+ type: 'QUERY_EXPRESSION',
+ name: 'Service',
+ expression: 'ServiceName',
+ source: 'logs-source',
+ minSelections: 1,
+ isGlobalRequirement: true,
+ };
+ mockUseSourceData(undefined);
+ renderWithMantine(
+
+
+ []}
+ unsatisfiedRequiredFilters={[logsVariable, globalFilter]}
+ />
+
+ ,
+ );
+
+ expect(
+ await screen.findByTestId('preview-missing-required-filters'),
+ ).toHaveTextContent(
+ /^Missing required filters or variables: logs, Service\. Select a value for each required filter, or turn off “Apply filters” to preview this tile without them\.$/,
+ );
+
+ await userEvent.click(
+ screen.getByRole('switch', { name: 'Apply filters' }),
+ );
+
+ expect(
+ screen.getByTestId('preview-missing-required-filters'),
+ ).toHaveTextContent(
+ /^Missing required filters or variables: logs\. Select a value for each one to preview this tile\.$/,
+ );
+ });
+
+ it("previews when the variable's source is on the tile's connection", async () => {
+ mockUseSourceData(logSource('conn-2'));
+ renderWithSelection('logs-source');
+
+ await screen.findByTestId('chart-run-query-button');
+ expect(screen.queryByTestId('preview-blocked')).not.toBeInTheDocument();
+ });
+});
+
+describe('DBEditTimeChartForm - Alerts on data source variables', () => {
+ const metricsVariable: DataSourceDashboardFilter = {
+ id: 'metrics',
+ type: 'DATA_SOURCE',
+ name: 'metrics',
+ variableName: 'metrics',
+ allowedSourceKinds: [SourceKind.Metric],
+ minSelections: 1,
+ maxSelections: 1,
+ isBroadcastEnabled: false,
+ isVariableEnabled: true,
+ };
+
+ const series = {
+ aggFn: 'avg' as const,
+ aggCondition: '',
+ aggConditionLanguage: 'lucene' as const,
+ valueExpression: 'Value',
+ metricType: MetricsDataType.Gauge,
+ metricName: 'test.metric.gauge',
+ };
+
+ const completeAlert = {
+ ...DEFAULT_TILE_ALERT,
+ channels: [{ type: 'webhook' as const, webhookId: 'webhook-1' }],
+ };
+
+ const renderWithAlert = (
+ alert: NonNullable,
+ onSave: jest.Mock,
+ ) =>
+ renderWithMantine(
+
+
+
+
+ ,
+ );
+
+ const removalNotice = /This tile's alert will be removed when you save\./;
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ mockUseSource.mockImplementation(({ id }: { id?: string }) => ({
+ data:
+ id === 'metric-source'
+ ? {
+ id: 'metric-source',
+ kind: SourceKind.Metric,
+ name: 'Test Metric Source',
+ from: { databaseName: 'default', tableName: '' },
+ connection: 'default',
+ timestampValueExpression: 'Timestamp',
+ metricTables: { gauge: 'metrics.gauge' },
+ }
+ : undefined,
+ }));
+ });
+
+ it('keeps the alert when the source goes to a variable and back', async () => {
+ const onSave = jest.fn();
+ renderWithAlert(completeAlert, onSave);
+ const sourceSelector = screen.getByTestId('source-selector');
+
+ await userEvent.selectOptions(sourceSelector, '$metrics');
+ expect(screen.getByText(removalNotice)).toBeInTheDocument();
+ expect(screen.queryByTestId('alert-button')).not.toBeInTheDocument();
+
+ await userEvent.selectOptions(sourceSelector, 'metric-source');
+ expect(screen.queryByText(removalNotice)).not.toBeInTheDocument();
+ await userEvent.click(screen.getByTestId('chart-save-button'));
+
+ await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
+ expect(onSave.mock.calls[0][0].alert).toMatchObject(completeAlert);
+ });
+
+ it('saves without the alert while the source is a variable, even an invalid one', async () => {
+ const onSave = jest.fn();
+ // No webhook picked, so the alert alone would block the save
+ renderWithAlert(DEFAULT_TILE_ALERT, onSave);
+
+ await userEvent.selectOptions(
+ screen.getByTestId('source-selector'),
+ '$metrics',
+ );
+ await userEvent.click(screen.getByTestId('chart-save-button'));
+
+ await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
+ const saved = onSave.mock.calls[0][0];
+ expect(saved.source).toBe('$metrics');
+ expect(saved.alert).toBeUndefined();
+ });
+});
diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts
index 2aa241098..6635957e5 100644
--- a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts
+++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts
@@ -19,6 +19,7 @@ import {
buildSampleEventsConfig,
computeDbTimeChartConfig,
displayTypeToActiveTab,
+ getPreviewBlockingFilters,
isQueryReady,
resolvePreviewVariables,
resolveTilePreviewFilters,
@@ -696,6 +697,7 @@ describe('resolveTilePreviewFilters', () => {
filters: dashboardFilters,
variables: [],
missingRequiredFilterNames: [],
+ canPreviewWithoutFilters: false,
});
});
@@ -708,6 +710,7 @@ describe('resolveTilePreviewFilters', () => {
filters: undefined,
variables: [{ name: 'service', values: [] }],
missingRequiredFilterNames: [],
+ canPreviewWithoutFilters: false,
});
});
@@ -796,6 +799,107 @@ describe('resolveTilePreviewFilters', () => {
}).missingRequiredFilterNames,
).toEqual([]);
});
+
+ it('still blocks on a referenced source variable while the filters are turned off', () => {
+ const sourceVariable: DashboardFilter = {
+ id: 'f3',
+ type: 'DATA_SOURCE',
+ name: 'Logs source',
+ allowedSourceKinds: [SourceKind.Log],
+ isBroadcastEnabled: false,
+ isVariableEnabled: true,
+ variableName: 'logs',
+ minSelections: 1,
+ maxSelections: 1,
+ };
+ const unsatisfiedRequiredFilters = [requiredFilter, sourceVariable];
+
+ expect(
+ resolve({
+ config: rawSqlWithFiltersMacro,
+ unsatisfiedRequiredFilters,
+ applySelections: false,
+ referenceVariableNames: ['logs'],
+ }).missingRequiredFilterNames,
+ ).toEqual(['Logs source']);
+ expect(
+ resolve({ unsatisfiedRequiredFilters, applySelections: false })
+ .missingRequiredFilterNames,
+ ).toEqual([]);
+ });
+});
+
+describe('getPreviewBlockingFilters', () => {
+ const sourceVariable: DashboardFilter = {
+ id: 'logs',
+ type: 'DATA_SOURCE',
+ name: 'Logs source',
+ allowedSourceKinds: [SourceKind.Log],
+ isBroadcastEnabled: false,
+ isVariableEnabled: true,
+ variableName: 'logs',
+ minSelections: 1,
+ maxSelections: 1,
+ };
+ const requiredVariable: DashboardFilter = {
+ id: 'env',
+ type: 'STATIC_LIST',
+ name: 'Environment',
+ options: ['prod'],
+ isBroadcastEnabled: false,
+ isVariableEnabled: true,
+ variableName: 'env',
+ minSelections: 1,
+ };
+ const rawSqlWithVariable: ChartConfigWithDateRange = {
+ configType: 'sql',
+ sqlTemplate: "SELECT count() FROM logs WHERE env = '$env'",
+ connection: 'clickhouse',
+ dateRange,
+ };
+
+ const getBlocking = (
+ overrides: Partial[0]> = {},
+ ) =>
+ getPreviewBlockingFilters({
+ config: rawSqlWithVariable,
+ sourceId: undefined,
+ variables: [{ name: 'env', values: [] }],
+ unsatisfiedRequiredFilters: [sourceVariable, requiredVariable],
+ applySelections: true,
+ referenceVariableNames: ['logs'],
+ ...overrides,
+ });
+
+ it('offers turning off the filters when that clears part of the block', () => {
+ expect(getBlocking()).toEqual({
+ missingRequiredFilterNames: ['Logs source', 'Environment'],
+ canPreviewWithoutFilters: true,
+ });
+ });
+
+ it('does not offer turning off the filters when only a reference variable blocks', () => {
+ expect(
+ getBlocking({ unsatisfiedRequiredFilters: [sourceVariable] }),
+ ).toEqual({
+ missingRequiredFilterNames: ['Logs source'],
+ canPreviewWithoutFilters: false,
+ });
+ });
+
+ it('does not offer turning off the filters when they are already off', () => {
+ expect(getBlocking({ applySelections: false })).toEqual({
+ missingRequiredFilterNames: ['Logs source'],
+ canPreviewWithoutFilters: false,
+ });
+ });
+
+ it('blocks only on reference variables when no query could be built', () => {
+ expect(getBlocking({ config: undefined })).toEqual({
+ missingRequiredFilterNames: ['Logs source'],
+ canPreviewWithoutFilters: false,
+ });
+ });
});
describe('withBroadcastFilters', () => {
diff --git a/packages/app/src/components/DBEditTimeChartForm/useDebouncedFormValues.ts b/packages/app/src/components/DBEditTimeChartForm/useDebouncedFormValues.ts
new file mode 100644
index 000000000..80c504a1c
--- /dev/null
+++ b/packages/app/src/components/DBEditTimeChartForm/useDebouncedFormValues.ts
@@ -0,0 +1,25 @@
+import { useEffect, useState } from 'react';
+import { FieldValues, UseFormGetValues, UseFormWatch } from 'react-hook-form';
+import { useDebouncedCallback } from '@mantine/hooks';
+
+/**
+ * The form's values, updated `delay` ms after the last change. Unlike
+ * `useWatch({ control })`, which types them as a deep partial, they're typed
+ * as complete.
+ *
+ * Kept out of the form component because the React Compiler skips any
+ * component that calls `watch`.
+ */
+export function useDebouncedFormValues(
+ watch: UseFormWatch,
+ getValues: UseFormGetValues,
+ delay: number,
+): T {
+ const [values, setValues] = useState(() => getValues());
+ const update = useDebouncedCallback(() => setValues(getValues()), delay);
+ useEffect(() => {
+ const subscription = watch(() => update());
+ return () => subscription.unsubscribe();
+ }, [watch, update]);
+ return values;
+}
diff --git a/packages/app/src/components/DBEditTimeChartForm/utils.ts b/packages/app/src/components/DBEditTimeChartForm/utils.ts
index d49cc65bc..4ee56423e 100644
--- a/packages/app/src/components/DBEditTimeChartForm/utils.ts
+++ b/packages/app/src/components/DBEditTimeChartForm/utils.ts
@@ -10,7 +10,7 @@ import {
import { isTimeSeriesDisplayType } from '@hyperdx/common-utils/dist/core/utils';
import {
configConsumesBroadcastFilters,
- getBlockingRequiredFilterNames,
+ getBlockingRequiredFilters,
} from '@hyperdx/common-utils/dist/dashboardFilterValues';
import {
getPromqlFilters,
@@ -23,11 +23,13 @@ import {
isRawSqlSavedChartConfig,
} from '@hyperdx/common-utils/dist/guards';
import { substitutePromqlChartConfigTemplates } from '@hyperdx/common-utils/dist/macros';
+import { getReferenceVariableFilters } from '@hyperdx/common-utils/dist/referenceVariables';
import {
BroadcastFilter,
BuilderChartConfigWithDateRange,
ChartAlertBaseSchema,
ChartConfigWithDateRange,
+ ChartConfigWithOptDateRange,
ChartConfigWithOptTimestamp,
ChartVariable,
DashboardFilter,
@@ -204,21 +206,97 @@ export function resolvePreviewVariables({
: referenced.map(variable => ({ ...variable, values: [] }));
}
+/**
+ * The required filters that block a preview with `applySelections` off: only
+ * the reference variables the tile can't be queried without.
+ */
+function getUnappliedPreviewBlockingFilterNames(
+ unsatisfiedRequiredFilters: DashboardFilter[] | undefined,
+ referenceVariableNames: readonly string[] | undefined,
+): string[] {
+ const referenced = new Set(referenceVariableNames);
+ return getReferenceVariableFilters(unsatisfiedRequiredFilters)
+ .filter(({ name }) => referenced.has(name))
+ .map(({ filter }) => filter.name);
+}
+
+export type PreviewBlockingFilters = {
+ /** Names of the required filters that have nothing selected and block the preview. */
+ missingRequiredFilterNames: string[];
+ /** Whether turning off "Apply filters" would clear the block. */
+ canPreviewWithoutFilters: boolean;
+};
+
+/**
+ * The required filters with nothing selected that block a tile preview.
+ *
+ * With `applySelections` off, only the reference variables the tile can't be
+ * queried without still block. `config` is undefined when no query could be
+ * built, e.g. because a source variable has no selection; the tile then
+ * consumes no broadcast filters and references no template variables.
+ */
+export function getPreviewBlockingFilters({
+ config,
+ sourceId,
+ variables,
+ unsatisfiedRequiredFilters,
+ applySelections,
+ referenceVariableNames,
+}: {
+ config: ChartConfigWithOptDateRange | SavedChartConfig | undefined;
+ sourceId: string | undefined;
+ variables: ChartVariable[] | undefined;
+ unsatisfiedRequiredFilters: DashboardFilter[] | undefined;
+ applySelections: boolean;
+ /** The data source and connection variables the tile's stored config references. */
+ referenceVariableNames: readonly string[] | undefined;
+}): PreviewBlockingFilters {
+ const unappliedNames = getUnappliedPreviewBlockingFilterNames(
+ unsatisfiedRequiredFilters,
+ referenceVariableNames,
+ );
+ if (!applySelections) {
+ return {
+ missingRequiredFilterNames: unappliedNames,
+ canPreviewWithoutFilters: false,
+ };
+ }
+
+ const missingRequiredFilterNames = getBlockingRequiredFilters(
+ unsatisfiedRequiredFilters ?? [],
+ {
+ sourceId,
+ referenceVariableNames,
+ referencedVariableNames:
+ config && variables
+ ? filterReferencedVariables(config, variables).map(v => v.name)
+ : undefined,
+ consumesBroadcastFilters:
+ config != null && configConsumesBroadcastFilters(config, sourceId),
+ },
+ ).map(filter => filter.name);
+ return {
+ missingRequiredFilterNames,
+ canPreviewWithoutFilters: missingRequiredFilterNames.some(
+ name => !unappliedNames.includes(name),
+ ),
+ };
+}
+
/** What the dashboard's filter state contributes to a tile preview. */
-export type TilePreviewFilters = {
+export type TilePreviewFilters = PreviewBlockingFilters & {
/** Broadcast filter conditions to query the preview with, in its query language. */
filters: BroadcastFilter[] | undefined;
/** The referenced variables, with or without their selections. */
variables: ChartVariable[] | undefined;
- /** Names of the required filters that have nothing selected. */
- missingRequiredFilterNames: string[];
};
/**
* Applies the parent dashboard's filter selections to a tile preview.
*
* With `applySelections` off, the preview runs as an alert would: no broadcast
- * filters, empty variable selections, and no required-filter block.
+ * filters, empty variable selections, and no required-filter block other than
+ * the reference variables the tile can't be queried without.
*/
export function resolveTilePreviewFilters({
config,
@@ -227,14 +305,25 @@ export function resolveTilePreviewFilters({
variables,
unsatisfiedRequiredFilters,
applySelections,
+ referenceVariableNames,
}: {
config: ChartConfigWithDateRange;
sourceId: string | undefined;
+ /** The data source and connection variables the tile's stored config references. */
+ referenceVariableNames?: readonly string[];
filters: BroadcastFilter[] | undefined;
variables: ChartVariable[] | undefined;
unsatisfiedRequiredFilters: DashboardFilter[] | undefined;
applySelections: boolean;
}): TilePreviewFilters {
+ const blocking = getPreviewBlockingFilters({
+ config,
+ sourceId,
+ variables,
+ unsatisfiedRequiredFilters,
+ applySelections,
+ referenceVariableNames,
+ });
const previewVariables = resolvePreviewVariables({
config,
variables,
@@ -242,11 +331,7 @@ export function resolveTilePreviewFilters({
});
if (!applySelections) {
- return {
- filters: undefined,
- variables: previewVariables,
- missingRequiredFilterNames: [],
- };
+ return { ...blocking, filters: undefined, variables: previewVariables };
}
// PromQL tiles take only PromQL conditions; `configConsumesBroadcastFilters`
@@ -257,16 +342,7 @@ export function resolveTilePreviewFilters({
? getSqlFilters(filters)
: undefined;
- return {
- filters: previewFilters,
- variables: previewVariables,
- missingRequiredFilterNames: getBlockingRequiredFilterNames({
- config,
- sourceId,
- unsatisfiedRequiredFilters,
- referencedVariables: previewVariables,
- }),
- };
+ return { ...blocking, filters: previewFilters, variables: previewVariables };
}
/** The config with the broadcast filters in its own query language attached. */
diff --git a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx
index 6cf870d08..8eb2c5293 100644
--- a/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx
+++ b/packages/app/src/components/DashboardFiltersModal/AdhocFilterEditForm.tsx
@@ -7,6 +7,11 @@ import {
getAdhocFilterSourceKinds,
isAdhocFilter,
} from '@hyperdx/common-utils/dist/filters';
+import {
+ isSourceVariableReferenceAccepted,
+ parseVariableReference,
+ resolveVariableReference,
+} from '@hyperdx/common-utils/dist/referenceVariables';
import {
AdhocFilterSourceType,
DashboardFilter,
@@ -17,6 +22,10 @@ import { IconAlertTriangle } from '@tabler/icons-react';
import SearchWhereInput from '@/components/SearchInput/SearchWhereInput';
import { SourceMultiSelectControlled } from '@/components/SourceMultiSelect';
+import {
+ useReferenceVariableValues,
+ useSourceVariableDeclarations,
+} from '@/components/sourceSelectUtils';
import { IS_PROMQL_ENABLED } from '@/config';
import { useSources } from '@/source';
@@ -44,6 +53,7 @@ export const AdhocFilterEditForm = ({
otherFilters,
}: AdhocFilterEditFormProps) => {
const { data: allSources } = useSources();
+ const sourceVariables = useSourceVariableDeclarations();
const [sourceType, sourceIds, appliesToSourceIds] = useWatch({
control,
name: ['sourceType', 'sources', 'appliesToSourceIds'],
@@ -64,14 +74,26 @@ export const AdhocFilterEditForm = ({
),
);
}, [otherFilters, sourceType, sourceIds, appliesToSourceIds]);
- const allowedSourceKinds = getAdhocFilterSourceKinds(sourceType);
- const tableConnections = useMemo(
- () =>
- (allSources ?? [])
- .filter(source => sourceIds?.includes(source.id))
- .map(tcFromSource),
- [allSources, sourceIds],
+ const allowedSourceKinds = useMemo(
+ () => getAdhocFilterSourceKinds(sourceType),
+ [sourceType],
);
+ const referenceVariableValues = useReferenceVariableValues();
+ const tableConnections = useMemo(() => {
+ const resolvedIds = new Set(
+ sourceIds?.map(id =>
+ resolveVariableReference(id, referenceVariableValues),
+ ),
+ );
+ // A data source variable can select a source of a kind this filter can't use
+ return (allSources ?? [])
+ .filter(
+ source =>
+ resolvedIds.has(source.id) &&
+ allowedSourceKinds.includes(source.kind),
+ )
+ .map(tcFromSource);
+ }, [allSources, sourceIds, referenceVariableValues, allowedSourceKinds]);
// Kept visible for a saved PromQL filter so its source type can be changed
const showSourceType =
sourceType === 'promql' ||
@@ -82,6 +104,13 @@ export const AdhocFilterEditForm = ({
// hold after switching from another filter type.
const validateSourceKinds = (ids: string[] | undefined) => {
const hasOtherKind = ids?.some(id => {
+ if (parseVariableReference(id) != null) {
+ return !isSourceVariableReferenceAccepted(
+ id,
+ sourceVariables,
+ allowedSourceKinds,
+ );
+ }
const source = allSources?.find(s => s.id === id);
return source && !allowedSourceKinds.includes(source.kind);
});
@@ -131,6 +160,7 @@ export const AdhocFilterEditForm = ({
comboboxProps={{ withinPortal: true }}
placeholder="Select sources"
allowedSourceKinds={allowedSourceKinds}
+ allowSourceVariables
rules={{
validate: (value: string[] | undefined) =>
!value?.length
@@ -173,6 +203,7 @@ export const AdhocFilterEditForm = ({
comboboxProps={{ withinPortal: true }}
placeholder="Same as data sources"
allowedSourceKinds={allowedSourceKinds}
+ allowSourceVariables
rules={{ validate: validateSourceKinds }}
/>
diff --git a/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx
index fdb6e8bc4..b7348e491 100644
--- a/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx
+++ b/packages/app/src/components/DashboardFiltersModal/DataSourceFilterEditForm.tsx
@@ -1,7 +1,9 @@
-import { Controller, useFormState } from 'react-hook-form';
+import { Controller, useFormState, useWatch } from 'react-hook-form';
import { DashboardFilter, SourceKind } from '@hyperdx/common-utils/dist/types';
-import { Group, MultiSelect } from '@mantine/core';
+import { Alert, Group, MultiSelect } from '@mantine/core';
+import { IconAlertTriangle } from '@tabler/icons-react';
+import { getInterchangeableSourceKind } from '@/components/ChartEditor/utils';
import { SOURCE_KIND_LABELS } from '@/components/Sources/SourceForm/constants';
import { SOURCE_KIND_ICONS } from '@/components/sourceSelectUtils';
import { IS_PROMQL_ENABLED } from '@/config';
@@ -26,6 +28,10 @@ export const DataSourceFilterEditForm = ({
otherFilters,
}: DataSourceFilterEditFormProps) => {
const { errors } = useFormState({ control });
+ const allowedSourceKinds = useWatch({ control, name: 'allowedSourceKinds' });
+ const hasMixedKinds =
+ (allowedSourceKinds?.length ?? 0) > 1 &&
+ getInterchangeableSourceKind(allowedSourceKinds) == null;
return (
<>
@@ -60,6 +66,19 @@ export const DataSourceFilterEditForm = ({
/>
+ {hasMixedKinds && (
+ }
+ data-testid="filter-mixed-source-kinds-warning"
+ >
+ These kinds support different chart options, such as metrics and
+ formulas. A tile built while one kind is selected may not load when
+ the variable is switched to another, and a new tile can't be
+ saved until a source is selected.
+
+ )}
+
>
);
diff --git a/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx
index bb3d90a8f..32092ff43 100644
--- a/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx
+++ b/packages/app/src/components/DashboardFiltersModal/PromqlLabelFilterEditForm.tsx
@@ -8,6 +8,7 @@ import {
import { AutocompleteControlled } from '@/components/InputControlled';
import PromQLEditor from '@/components/PromQLEditor/PromQLEditor';
import { SourceSelectControlled } from '@/components/SourceSelect';
+import { useResolvedReference } from '@/components/sourceSelectUtils';
import {
usePromqlLabelNames,
usePromqlMetricNames,
@@ -42,7 +43,7 @@ export const PromqlLabelFilterEditForm = ({
// Only a PromQL source can answer the label lookup; the form may briefly hold
// another kind while the user switches types.
- const sourceId = useWatch({ control, name: 'source' });
+ const sourceId = useResolvedReference(useWatch({ control, name: 'source' }));
const source = sources?.find(s => s.id === sourceId);
const promqlSource = source && isPromqlSource(source) ? source : undefined;
const { data: labelNames } = usePromqlLabelNames(promqlSource?.id);
@@ -64,6 +65,7 @@ export const PromqlLabelFilterEditForm = ({
}}
comboboxProps={{ withinPortal: true }}
allowedSourceKinds={[SourceKind.Promql]}
+ allowSourceVariables
/>
diff --git a/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx b/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx
index 8d88a40b0..a806135d4 100644
--- a/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx
+++ b/packages/app/src/components/DashboardFiltersModal/QueryExpressionFilterEditForm.tsx
@@ -27,6 +27,7 @@ import SourceSchemaPreview, {
isSourceSchemaPreviewEnabled,
} from '@/components/SourceSchemaPreview';
import { SourceSelectControlled } from '@/components/SourceSelect';
+import { useResolvedReference } from '@/components/sourceSelectUtils';
import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor';
import { useSource } from '@/source';
import { getMetricTableName } from '@/utils';
@@ -60,7 +61,11 @@ export const QueryExpressionFilterEditForm = ({
showVariableOptions,
}: QueryExpressionFilterEditFormProps) => {
const sourceId = useWatch({ control, name: 'source' });
- const { data: source } = useSource({ id: sourceId });
+ // A data source variable can select a source of a kind this filter can't use
+ const { data: source } = useSource({
+ id: useResolvedReference(sourceId),
+ kinds: QUERY_EXPRESSION_FILTER_SOURCE_KINDS,
+ });
const metricType = useWatch({ control, name: 'sourceMetricType' });
const tableName = source && getMetricTableName(source, metricType);
@@ -137,6 +142,7 @@ export const QueryExpressionFilterEditForm = ({
isSchemaPreviewEnabled={isSourceSchemaPreviewEnabled(source)}
disabled={!!presetSource}
allowedSourceKinds={QUERY_EXPRESSION_FILTER_SOURCE_KINDS}
+ allowSourceVariables
/>
diff --git a/packages/app/src/components/DashboardFiltersModal/__tests__/DataSourceFilterEditForm.test.tsx b/packages/app/src/components/DashboardFiltersModal/__tests__/DataSourceFilterEditForm.test.tsx
new file mode 100644
index 000000000..63dd72a6f
--- /dev/null
+++ b/packages/app/src/components/DashboardFiltersModal/__tests__/DataSourceFilterEditForm.test.tsx
@@ -0,0 +1,38 @@
+import { useForm } from 'react-hook-form';
+import { SourceKind } from '@hyperdx/common-utils/dist/types';
+import { screen } from '@testing-library/react';
+
+import { DataSourceFilterEditForm } from '@/components/DashboardFiltersModal/DataSourceFilterEditForm';
+import {
+ FilterFormValues,
+ toFormValues,
+} from '@/components/DashboardFiltersModal/filterFormState';
+
+function Harness({ allowedSourceKinds }: { allowedSourceKinds: SourceKind[] }) {
+ const { control } = useForm({
+ defaultValues: {
+ ...toFormValues(),
+ type: 'DATA_SOURCE',
+ allowedSourceKinds,
+ },
+ });
+ return ;
+}
+
+const warning = () => screen.queryByTestId('filter-mixed-source-kinds-warning');
+
+describe('DataSourceFilterEditForm', () => {
+ it('warns when the kinds support different chart options', () => {
+ renderWithMantine(
+ ,
+ );
+ expect(warning()).toBeInTheDocument();
+ });
+
+ it('does not warn for kinds that charts treat alike', () => {
+ renderWithMantine(
+ ,
+ );
+ expect(warning()).not.toBeInTheDocument();
+ });
+});
diff --git a/packages/app/src/components/SourceMultiSelect.tsx b/packages/app/src/components/SourceMultiSelect.tsx
index 665761057..2eccb1e30 100644
--- a/packages/app/src/components/SourceMultiSelect.tsx
+++ b/packages/app/src/components/SourceMultiSelect.tsx
@@ -10,21 +10,25 @@ import {
import { IconCheck } from '@tabler/icons-react';
import {
- SOURCE_KIND_ICONS,
+ getSourceOptionIcon,
useFilteredSortedSourceItems,
useSourceKindMap,
+ useWithSourceVariableItems,
} from '@/components/sourceSelectUtils';
import { useSources } from '@/source';
function SourceMultiSelectControlledComponent({
allowedSourceKinds,
connectionId,
+ allowSourceVariables = false,
size,
placeholder,
...props
}: {
allowedSourceKinds?: SourceKind[];
connectionId?: string;
+ /** Whether to offer the dashboard's data source variables. */
+ allowSourceVariables?: boolean;
size?: string;
placeholder?: string;
} & UseControllerProps &
@@ -37,18 +41,22 @@ function SourceMultiSelectControlledComponent({
const sourceKindMap = useSourceKindMap(data);
- const items = useFilteredSortedSourceItems({
+ const sourceItems = useFilteredSortedSourceItems({
sources: data,
allowedSourceKinds,
connectionId,
});
+ const items = useWithSourceVariableItems(sourceItems, {
+ enabled: allowSourceVariables,
+ allowedSourceKinds,
+ });
// Mantine passes `checked` to renderOption for MultiSelect items; show a
// check on selected entries so the dropdown reflects current selection
// without forcing the user to scan pills above.
const renderOption = useCallback(
({ option, checked }: { option: ComboboxItem; checked?: boolean }) => {
- const icon = SOURCE_KIND_ICONS[sourceKindMap.get(option.value) ?? ''];
+ const icon = getSourceOptionIcon(option.value, sourceKindMap);
return (
{icon}
diff --git a/packages/app/src/components/SourceSelect.tsx b/packages/app/src/components/SourceSelect.tsx
index c17bfae30..2dd72a808 100644
--- a/packages/app/src/components/SourceSelect.tsx
+++ b/packages/app/src/components/SourceSelect.tsx
@@ -1,4 +1,4 @@
-import { memo, useCallback, useMemo } from 'react';
+import { memo, useCallback } from 'react';
import { UseControllerProps, useWatch } from 'react-hook-form';
import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types';
import {
@@ -22,10 +22,11 @@ import {
import SelectControlled from '@/components/SelectControlled';
import {
- SOURCE_KIND_ICONS,
+ getSourceOptionIcon,
sourceSelectFilter,
useFilteredSortedSourceItems,
useSourceKindMap,
+ useWithSourceVariableItems,
} from '@/components/sourceSelectUtils';
import { useSources } from '@/source';
@@ -161,6 +162,7 @@ function SourceSelectControlledComponent({
allowedSourceKinds,
connectionId,
isSourceAllowed,
+ allowSourceVariables = false,
comboboxProps,
...props
}: {
@@ -174,6 +176,8 @@ function SourceSelectControlledComponent({
connectionId?: string;
/** Extra per-source gate for restrictions a kind can't express. */
isSourceAllowed?: (source: TSource) => boolean;
+ /** Whether to offer the dashboard's data source variables. */
+ allowSourceVariables?: boolean;
} & UseControllerProps &
SelectProps) {
const { data } = useSources();
@@ -182,23 +186,18 @@ function SourceSelectControlledComponent({
name: props.name,
});
- const selectedSourceKind = useMemo(
- () => data?.find(s => s.id === selectedSourceId)?.kind,
- [data, selectedSourceId],
- );
+ const sourceKindMap = useSourceKindMap(data);
- const leftIcon = SOURCE_KIND_ICONS[selectedSourceKind ?? ''] ?? (
+ const leftIcon = getSourceOptionIcon(selectedSourceId, sourceKindMap) ?? (
);
- const sourceKindMap = useSourceKindMap(data);
-
// Mantine passes `checked` to renderOption for the currently selected
// option; render a trailing check so the active source is obvious in the
// dropdown (the closed input only shows the kind icon + label).
const renderOption = useCallback(
({ option, checked }: { option: ComboboxItem; checked?: boolean }) => {
- const icon = SOURCE_KIND_ICONS[sourceKindMap.get(option.value) ?? ''];
+ const icon = getSourceOptionIcon(option.value, sourceKindMap);
return (
{icon}
@@ -219,6 +218,10 @@ function SourceSelectControlledComponent({
isSourceAllowed,
groupBySection: true,
});
+ const items = useWithSourceVariableItems(sourceItems, {
+ enabled: allowSourceVariables,
+ allowedSourceKinds,
+ });
const hasSelection = !!selectedSourceId;
const hasMenu =
@@ -233,7 +236,7 @@ function SourceSelectControlledComponent({
>
({
@@ -265,3 +270,75 @@ describe('SourceSelectControlled (grouped rendering + tag-style search)', () =>
expect(screen.queryByText('Other')).not.toBeInTheDocument();
});
});
+
+describe('SourceSelectControlled (data source variables)', () => {
+ const SOURCES = [makeSource('logs', 'Logs', SourceKind.Log)];
+ const sourceVariable = (
+ variableName: string,
+ allowedSourceKinds: SourceKind[],
+ connection?: string,
+ ): DataSourceDashboardFilter => ({
+ id: variableName,
+ type: 'DATA_SOURCE',
+ name: variableName,
+ variableName,
+ allowedSourceKinds,
+ connection,
+ minSelections: 1,
+ maxSelections: 1,
+ isBroadcastEnabled: false,
+ isVariableEnabled: true,
+ });
+ const VARIABLES = [
+ sourceVariable('logs_source', [SourceKind.Log]),
+ sourceVariable('any_source', [SourceKind.Log, SourceKind.Promql]),
+ sourceVariable('prom_source', [SourceKind.Promql]),
+ ];
+
+ function VariableHarness({
+ allowSourceVariables,
+ }: {
+ allowSourceVariables?: boolean;
+ }) {
+ const { control } = useForm();
+ return (
+
+
+
+ );
+ }
+
+ it('offers only variables with at least one kind the picker allows', async () => {
+ asMock(useSources).mockReturnValue({ data: SOURCES });
+ renderWithMantine();
+ await userEvent.click(screen.getByPlaceholderText('Data Source'));
+
+ expect(await screen.findByText('Variables')).toBeInTheDocument();
+ expect(screen.getByText('$logs_source')).toBeInTheDocument();
+ expect(screen.getByText('$any_source')).toBeInTheDocument();
+ expect(screen.queryByText('$prom_source')).not.toBeInTheDocument();
+ });
+
+ it('puts unsectioned sources under their own header after the variables', async () => {
+ asMock(useSources).mockReturnValue({ data: SOURCES });
+ renderWithMantine();
+ await userEvent.click(screen.getByPlaceholderText('Data Source'));
+
+ expect(await screen.findByText('Variables')).toBeInTheDocument();
+ expect(screen.getByText('Sources')).toBeInTheDocument();
+ });
+
+ it('offers no variables unless the picker opts in', async () => {
+ asMock(useSources).mockReturnValue({ data: SOURCES });
+ renderWithMantine();
+ await userEvent.click(screen.getByPlaceholderText('Data Source'));
+
+ expect(await screen.findByText('Logs')).toBeInTheDocument();
+ expect(screen.queryByText('$logs_source')).not.toBeInTheDocument();
+ });
+});
diff --git a/packages/app/src/components/sourceSelectUtils.tsx b/packages/app/src/components/sourceSelectUtils.tsx
index 1533d6e56..4a06f779b 100644
--- a/packages/app/src/components/sourceSelectUtils.tsx
+++ b/packages/app/src/components/sourceSelectUtils.tsx
@@ -1,6 +1,18 @@
-import { useMemo } from 'react';
-import { parseVariableReference } from '@hyperdx/common-utils/dist/referenceVariables';
-import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types';
+import { createContext, use, useMemo } from 'react';
+import {
+ getSourceVariableDeclarations,
+ isSourceVariableAccepted,
+ parseVariableReference,
+ ReferenceVariableValues,
+ resolveVariableReference,
+ SourceVariableDeclaration,
+ toVariableReference,
+} from '@hyperdx/common-utils/dist/referenceVariables';
+import {
+ DashboardFilter,
+ SourceKind,
+ TSource,
+} from '@hyperdx/common-utils/dist/types';
import { ComboboxItem, OptionsFilter } from '@mantine/core';
import {
IconChartLine,
@@ -28,6 +40,107 @@ export function getSourceOptionIcon(
return SOURCE_KIND_ICONS[(value && sourceKindMap.get(value)) ?? ''];
}
+type ReferenceVariablesContextValue = {
+ sourceVariables: SourceVariableDeclaration[];
+ values: ReferenceVariableValues;
+};
+
+const ReferenceVariablesContext = createContext(
+ { sourceVariables: [], values: new Map() },
+);
+
+/**
+ * Offers the data source variables `filters` declare to the pickers below it
+ * that opt in, and their selections to `useResolvedReference`.
+ */
+export function ReferenceVariablesProvider({
+ filters,
+ values,
+ children,
+}: {
+ filters: DashboardFilter[];
+ values: ReferenceVariableValues;
+ children: React.ReactNode;
+}) {
+ const value = useMemo(
+ () => ({
+ sourceVariables: getSourceVariableDeclarations(filters),
+ values,
+ }),
+ [filters, values],
+ );
+ return (
+
+ {children}
+
+ );
+}
+
+/**
+ * The ID a source field currently queries: an ID as is, or the selection of
+ * the variable it references.
+ */
+export function useResolvedReference(
+ value: string | undefined,
+): string | undefined {
+ const { values } = use(ReferenceVariablesContext);
+ return resolveVariableReference(value, values);
+}
+
+export function useReferenceVariableValues(): ReferenceVariableValues {
+ return use(ReferenceVariablesContext).values;
+}
+
+export function useSourceVariableDeclarations(): SourceVariableDeclaration[] {
+ return use(ReferenceVariablesContext).sourceVariables;
+}
+
+const toVariableItem = (name: string): ComboboxItem => {
+ const reference = toVariableReference(name);
+ return { value: reference, label: reference };
+};
+
+/**
+ * A source picker's items, preceded by a "Variables" group of the data source
+ * variables it can offer. A variable is offered if at least one kind it can
+ * resolve to is allowed; a selection of another kind shows as an error where
+ * the reference is used.
+ */
+export function useWithSourceVariableItems(
+ sourceItems: ComboboxItem[] | SourceSelectGroup[],
+ {
+ enabled,
+ allowedSourceKinds,
+ }: {
+ enabled: boolean;
+ allowedSourceKinds?: SourceKind[];
+ },
+): (ComboboxItem | SourceSelectGroup)[] {
+ const { sourceVariables } = use(ReferenceVariablesContext);
+ return useMemo(() => {
+ if (!enabled) return sourceItems;
+ const items = sourceVariables
+ .filter(
+ variable =>
+ !allowedSourceKinds ||
+ isSourceVariableAccepted(variable, allowedSourceKinds),
+ )
+ .map(variable => toVariableItem(variable.name));
+ if (items.length === 0) return sourceItems;
+ // Mantine renders ungrouped items after a group as part of it, so a flat
+ // source list gets a header of its own
+ const ungrouped = sourceItems.filter(
+ (item): item is ComboboxItem => !('group' in item),
+ );
+ return [
+ { group: 'Variables', items },
+ ...(ungrouped.length > 0
+ ? [{ group: 'Sources', items: ungrouped }]
+ : sourceItems),
+ ];
+ }, [enabled, sourceVariables, allowedSourceKinds, sourceItems]);
+}
+
/** Header for sources that have no section assigned. Internal to this module. */
const UNSECTIONED_SOURCE_GROUP = 'Other';
diff --git a/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts b/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts
index 1929d8705..8c5d73610 100644
--- a/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts
+++ b/packages/app/tests/e2e/features/dashboard-source-variables.spec.ts
@@ -1,11 +1,14 @@
/**
* A `DATA_SOURCE` dashboard filter is a single-select variable whose value is
- * a source, picked from the filter bar.
+ * a source. Tiles and other filters reference it as `$name` in place of a
+ * source, block until it has a selection, and cannot carry alerts.
*/
import { DashboardPage } from '../page-objects/DashboardPage';
+import { SERVICES } from '../seed-clickhouse';
import { expect, test } from '../utils/base-test';
import {
DEFAULT_LOGS_SOURCE_NAME,
+ DEFAULT_METRICS_SOURCE_NAME,
DEFAULT_TRACES_SOURCE_NAME,
} from '../utils/constants';
@@ -20,9 +23,17 @@ test.describe(
await dashboardPage.goto();
});
- test('picks a source of the allowed kinds from the filter bar', async ({
+ test('drives tiles and filters from the selected source', async ({
page,
}) => {
+ const editor = dashboardPage.chartEditor;
+ const reloadWithoutFilterState = async () => {
+ const url = new URL(page.url());
+ url.search = '';
+ await page.goto(url.toString());
+ await dashboardPage.waitForLoaded();
+ };
+
await test.step('Create a data source variable limited to logs', async () => {
await dashboardPage.createNewDashboard();
await dashboardPage.openEditFiltersModal();
@@ -34,7 +45,21 @@ test.describe(
await expect(
dashboardPage.getFilterItemByName('Logs source'),
).toContainText('($logs)');
+ });
+
+ await test.step('A queried filter can read from the variable', async () => {
+ await dashboardPage.addFilterToDashboard(
+ 'Service',
+ '$logs',
+ 'ServiceName',
+ );
+ await expect(
+ dashboardPage.getFilterItemByName('Service'),
+ ).toContainText('$logs');
await dashboardPage.closeFiltersModal();
+ await expect(
+ dashboardPage.getFilterPendingVariableWarning('Service'),
+ ).toBeVisible();
});
await test.step('The variable offers only allowed kinds', async () => {
@@ -48,20 +73,76 @@ test.describe(
await page.keyboard.press('Escape');
});
- await test.step('A saved selection is the initial value', async () => {
+ await test.step('A tile can be saved on the variable before it has a value', async () => {
+ await dashboardPage.addTile();
+ await expect(editor.nameInput).toBeVisible();
+ await editor.waitForDataToLoad();
+ await expect(editor.alertButton).toBeVisible();
+ await editor.setChartName('Variable tile');
+ await editor.selectSource('$logs');
+ await expect(editor.alertButton).toHaveCount(0);
+ await editor.save();
+ await expect(dashboardPage.getTiles()).toHaveCount(1);
+ await expect(
+ dashboardPage.getTileMissingRequiredFilters(0),
+ ).toContainText('Logs source');
+ });
+
+ await test.step('A selection loads the tile and the filter', async () => {
await dashboardPage.selectReferenceFilterValue(
'Logs source',
DEFAULT_LOGS_SOURCE_NAME,
);
+ await expect(
+ dashboardPage.getTileMissingRequiredFilters(0),
+ ).toHaveCount(0);
+ await expect(dashboardPage.getTileTimeChart(0)).toBeVisible();
+ await expect(
+ dashboardPage.getFilterPendingVariableWarning('Service'),
+ ).toHaveCount(0);
+ await dashboardPage.openFilterDropdown('Service');
+ await expect(dashboardPage.getFilterOption(SERVICES[0])).toBeVisible();
+ await page.keyboard.press('Escape');
+ });
+
+ await test.step('The editor keeps the reference', async () => {
+ await dashboardPage.editTile(0);
+ await expect(editor.source).toHaveValue('$logs');
+ await expect(editor.alertButton).toHaveCount(0);
+ await editor.save();
+ });
+
+ await test.step('A saved selection is the initial value', async () => {
await dashboardPage.saveQueryAndFiltersAsDefault();
- const url = new URL(page.url());
- url.search = '';
- await page.goto(url.toString());
- await dashboardPage.waitForLoaded();
+ await reloadWithoutFilterState();
await expect(
dashboardPage.getFilterSelectByName('Logs source'),
).toHaveValue(DEFAULT_LOGS_SOURCE_NAME);
+ await expect(
+ dashboardPage.getTileMissingRequiredFilters(0),
+ ).toHaveCount(0);
+ });
+
+ await test.step('A selection the variable no longer offers stops applying', async () => {
+ await dashboardPage.openEditFiltersModal();
+ await dashboardPage.openEditFilterForm('Logs source');
+ const kindsInput = page.getByTestId(
+ 'filter-allowed-source-kinds-input',
+ );
+ await kindsInput.click();
+ await dashboardPage.getFilterOption('Traces').click();
+ await dashboardPage.getFilterOption('Logs').click();
+ await dashboardPage.getFilterNameInput().click();
+ await page.getByTestId('save-filter-button').click();
+ await dashboardPage.closeFiltersModal();
+
+ await expect(
+ dashboardPage.getFilterSelectByName('Logs source'),
+ ).toHaveValue('');
+ await expect(
+ dashboardPage.getTileMissingRequiredFilters(0),
+ ).toContainText('Logs source');
});
});
@@ -105,5 +186,59 @@ test.describe(
});
});
});
+
+ // A filter edit can leave other filters with a variable they can't always
+ // use. It still saves, and the filter shows an error while it can't.
+ test("saves a kind other filters can't use, and flags them while it's selected", async ({
+ page,
+ }) => {
+ await test.step('An ad hoc filter reads from a logs variable', async () => {
+ await dashboardPage.createNewDashboard();
+ await dashboardPage.openEditFiltersModal();
+ await dashboardPage.addDataSourceFilterToDashboard(
+ 'Logs source',
+ ['Logs'],
+ 'logs',
+ );
+ await dashboardPage.addAdhocFilterToDashboard('Conditions', ['$logs']);
+ });
+
+ await test.step('Metrics can be added to the variable', async () => {
+ await dashboardPage.openEditFilterForm('Logs source');
+ await page.getByTestId('filter-allowed-source-kinds-input').click();
+ await dashboardPage.getFilterOption('OTel metrics').click();
+ await dashboardPage.getFilterNameInput().click();
+ await page.getByTestId('save-filter-button').click();
+ await dashboardPage.closeFiltersModal();
+ await expect(page.getByText('Unable to save dashboard')).toHaveCount(0);
+ });
+
+ await test.step('Selecting a metric source flags the ad hoc filter', async () => {
+ await dashboardPage.selectReferenceFilterValue(
+ 'Logs source',
+ DEFAULT_METRICS_SOURCE_NAME,
+ );
+ await expect(
+ dashboardPage.getFilterErrorIcon('Conditions'),
+ ).toBeVisible();
+
+ await dashboardPage.selectReferenceFilterValue(
+ 'Logs source',
+ DEFAULT_LOGS_SOURCE_NAME,
+ );
+ await expect(
+ dashboardPage.getFilterErrorIcon('Conditions'),
+ ).toHaveCount(0);
+ });
+
+ await test.step('The added kind was saved', async () => {
+ await page.reload();
+ await dashboardPage.waitForLoaded();
+ await dashboardPage.getFilterSelectByName('Logs source').click();
+ await expect(
+ dashboardPage.getFilterOption(DEFAULT_METRICS_SOURCE_NAME),
+ ).toBeVisible();
+ });
+ });
},
);
diff --git a/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts b/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts
index 314cf2ac1..cd25f4f0c 100644
--- a/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts
+++ b/packages/app/tests/e2e/features/dashboard-tile-editor-filters.spec.ts
@@ -120,7 +120,7 @@ test.describe(
dashboardPage.chartEditor.previewMissingRequiredFilters();
await expect(placeholder).toBeVisible({ timeout: 20000 });
await expect(placeholder).toContainText(
- 'Missing required filters: Service',
+ 'Missing required filters or variables: Service',
);
});
diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts
index 9b6fdd00d..86130cde3 100644
--- a/packages/app/tests/e2e/page-objects/DashboardPage.ts
+++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts
@@ -632,6 +632,12 @@ export class DashboardPage {
return new HeatmapComponent(this.page, this.getTile(tileIndex));
}
+ getTileTimeChart(tileIndex = 0): Locator {
+ return this.getTile(tileIndex)
+ .locator('.recharts-responsive-container')
+ .first();
+ }
+
/**
* Hover over a tile to reveal action buttons
*/