Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/source-variable-field-references.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/common-utils': minor
'@hyperdx/app': minor
'@hyperdx/api': patch
---

feat: Add the sqlidentifier variable format and field references to data source and connection variables
1 change: 1 addition & 0 deletions packages/api/src/mcp/prompts/dashboards/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,7 @@ Only add one when the default is wrong. \${service:sqlstring} is redundant in a
promql a or (a|b) "" or .* the default in PromQL; a single-select variable (maxSelections: 1) renders its raw value for {label="$var"}, a multi-select one an escaped regex for {label=~"$var"}
csv a,b <empty> use INSIDE a string literal
markdown a, b <empty> the default in markdown tiles; markdown syntax in values is escaped
sqlidentifier \`a\`, \`b c\` <empty> backtick-quotes each value, for a table, database, or column name

BUILDER TILES

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('buildSqlVariableCompletions', () => {
'${service:csv}',
'${service:regex}',
'${service:lucene}',
'${service:sqlidentifier}',
]),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const COMPLETION_FORMATS: Record<
sqlstring:
"Quoted and comma-separated, escaped for SQL. e.g. 'a', 'b', 'c'",
csv: 'Comma-separated and unquoted. Not SQL-escaped. e.g. a,b,c',
sqlidentifier:
'Comma-separated identifiers, each backtick-quoted. e.g. `a`, `b c`',
regex: 'A regex alternation. Regex escaped. e.g. (a|b|c)',
lucene:
'An OR of quoted terms, for Lucene inputs. e.g. ("a" OR "b" OR "c"). Quote the reference (field:"$var") for exact-match behavior. Leave unquoted (field:$var) for substring matching.',
Expand Down
3 changes: 2 additions & 1 deletion packages/app/tests/e2e/features/dashboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
await dashboardPage.chartEditor.setChartName('My Unsaved Chart');

await dashboardPage.page.keyboard.press('Escape');
await expect(dashboardPage.unsavedChangesConfirmModal).toBeAttached({

Check failure on line 317 in packages/app/tests/e2e/features/dashboard.spec.ts

View workflow job for this annotation

GitHub Actions / e2e-tests / E2E Tests - Shard 2

[chromium] › tests/e2e/features/dashboard.spec.ts:312:7 › Dashboard › should warn when closing tile editor with unsaved changes @dashboard

9) [chromium] › tests/e2e/features/dashboard.spec.ts:312:7 › Dashboard › should warn when closing tile editor with unsaved changes @dashboard Error: expect(locator).toBeAttached() failed Locator: getByRole('dialog').filter({ has: getByTestId('confirm-confirm-button') }) Expected: attached Timeout: 5000ms Error: element(s) not found Call log: - Expect "toBeAttached" with timeout 5000ms - waiting for getByRole('dialog').filter({ has: getByTestId('confirm-confirm-button') }) 315 | 316 | await dashboardPage.page.keyboard.press('Escape'); > 317 | await expect(dashboardPage.unsavedChangesConfirmModal).toBeAttached({ | ^ 318 | timeout: 5000, 319 | }); 320 | at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard.spec.ts:317:60
timeout: 5000,
});

Expand Down Expand Up @@ -1945,13 +1945,14 @@
// entries the popup is empty the moment the brace is typed.
await dashboardPage.chartEditor.replaceSqlQuery('${');
const options = dashboardPage.chartEditor.sqlCompletionOptions();
await expect(options).toHaveCount(5);
await expect(options).toHaveCount(6);
// Compared as a set: ranking between equally-good matches is
// CodeMirror's business, not something worth pinning here.
expect((await options.allTextContents()).sort()).toEqual(
[
'${svc}',
'${svc:sqlstring}',
'${svc:sqlidentifier}',
'${svc:regex}',
'${svc:csv}',
'${svc:lucene}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getAdhocFilterTargetsForTile,
getBlockingRequiredFilterNames,
getBlockingRequiredFilters,
getFiltersDeclaringVariables,
getUnsatisfiedRequiredFilters,
parseDashboardFilterValues,
resolveFilterSelection,
Expand All @@ -22,6 +23,7 @@ import type {
SavedChartConfig,
StaticListDashboardFilter,
} from '@/types';
import { SourceKind } from '@/types';

const filter = (
overrides: Partial<QueryExpressionDashboardFilter> = {},
Expand Down Expand Up @@ -655,6 +657,30 @@ describe('dashboardFilterValues', () => {
});
});

describe('getFiltersDeclaringVariables', () => {
const dataSource: DashboardFilter = {
id: 'ds',
type: 'DATA_SOURCE',
name: 'Logs source',
variableName: 'logs',
allowedSourceKinds: [SourceKind.Log],
minSelections: 1,
maxSelections: 1,
isBroadcastEnabled: false,
isVariableEnabled: true,
};
const env = staticFilter();

it('matches value and data source variables by name', () => {
expect(
getFiltersDeclaringVariables([env, dataSource], ['logs', 'env']),
).toEqual([env, dataSource]);
expect(getFiltersDeclaringVariables([env, dataSource], ['logs'])).toEqual(
[dataSource],
);
});
});

describe('getBlockingRequiredFilters', () => {
const names = (
filters: DashboardFilter[],
Expand Down Expand Up @@ -704,6 +730,33 @@ describe('dashboardFilterValues', () => {
expect(names(filters, { referencedVariableNames: [] })).toEqual([]);
});

it('blocks a tile whose text references a data source variable', () => {
const dataSource: DashboardFilter = {
id: 'ds',
type: 'DATA_SOURCE',
name: 'Logs source',
variableName: 'logs',
allowedSourceKinds: [SourceKind.Log],
minSelections: 1,
maxSelections: 1,
isBroadcastEnabled: false,
isVariableEnabled: true,
};

expect(
names([dataSource], {
referencedVariableNames: ['logs'],
consumesBroadcastFilters: false,
}),
).toEqual(['Logs source']);
expect(
names([dataSource], {
referencedVariableNames: [],
consumesBroadcastFilters: false,
}),
).toEqual([]);
});

// A filter with no explicit variableName is still referenced by the token
// derived from its display name.
it('matches a filter by its derived variable name', () => {
Expand Down
35 changes: 35 additions & 0 deletions packages/common-utils/src/__tests__/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
validateSavedQuery,
validateVariableName,
} from '@/filters';
import { buildReferenceChartVariables } from '@/referenceVariables';
import type {
AdhocDashboardFilter,
AdhocFilterCondition,
Expand All @@ -53,6 +54,7 @@ import type {
import {
DASHBOARD_VARIABLE_NAME_MAX_LENGTH,
DASHBOARD_VARIABLE_NAME_PATTERN_ANCHORED,
SourceKind,
} from '@/types';
import { isFilterSingleSelect } from '@/variables';

Expand Down Expand Up @@ -1436,6 +1438,39 @@ describe('filters', () => {
).toEqual(['svc']);
});

it('reports a data source variable only until it selects a source', () => {
const where = 'ServiceName IN (SELECT s FROM $src)';
const src = (selected: boolean) =>
buildReferenceChartVariables(
[
{
type: 'DATA_SOURCE',
name: 'Source',
variableName: 'src',
allowedSourceKinds: [SourceKind.Log],
},
],
{
values: new Map([['src', selected ? 'logs' : undefined]]),
sources: [
{
id: 'logs',
name: 'Logs',
kind: SourceKind.Log,
from: { databaseName: 'default', tableName: 'logs' },
},
],
},
)[0];

expect(getPendingFilterValuesVariables({ where }, [src(false)])).toEqual([
'src',
]);
expect(getPendingFilterValuesVariables({ where }, [src(true)])).toEqual(
[],
);
});

it('reports a braced reference once, however often it appears', () => {
expect(
getPendingFilterValuesVariables(
Expand Down
Loading
Loading