:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(metrics): add dashboard variable selects and editor (#118636)
Frank Hamand95f86f101601
diff --git a/frontend/src/initKea.ts b/frontend/src/initKea.tsindex 62af0e543fab..d6f6d94babb9 100644--- a/frontend/src/initKea.ts+++ b/frontend/src/initKea.ts@@ -114,6 +114,7 @@ const ERROR_FILTER_ALLOW_LIST_BY_LOGIC_PATH: Record<string, string> = {loadPage: 'scenes.project-homepage.today.todayReportLogic',loadReportDiff: 'scenes.inbox.logics.inboxReportDetailLogic',loadTraceSpans: 'products.tracing.frontend.components.TraceDialog.traceDialogLogic', // The dialog shows its own retry+ loadPreview: 'products.metrics.frontend.components.metricVariableEditorLogic', // The variable editor shows the error under the preview}// A keyed logic's path ends with its key, so match the owner path as a prefix.diff --git a/frontend/src/lib/utils/eventUsageLogic.test.ts b/frontend/src/lib/utils/eventUsageLogic.test.tsindex b050e7c25ac3..05dbf73f50ab 100644--- a/frontend/src/lib/utils/eventUsageLogic.test.ts+++ b/frontend/src/lib/utils/eventUsageLogic.test.ts@@ -26,6 +26,7 @@ import {dashboardViewedProperties,eventUsageLogic,getEventPropertiesForMetric,+ sanitizeDashboard,sanitizeQuery,} from './eventUsageLogic'@@ -376,6 +377,16 @@ describe('eventUsageLogic', () => {})})+ it('keeps metric variable definitions out of a sanitized dashboard', () => {+ const metricVariables = [{ name: 'env', source: 'sql', query: 'SELECT private_value' }]+ const tile = { id: 1, insight: { id: 2, filter_override_context: { dashboard: { metricVariables } } } }+ const dashboard = { id: 7, tiles: [tile], filters: { metricVariables }, persisted_filters: { metricVariables } }++ const sanitized = sanitizeDashboard(dashboard as unknown as DashboardType)++ expect(JSON.stringify(sanitized)).not.toContain('private_value')+ })+describe('dashboardViewedProperties', () => {const dashboard = (tiles: Partial<DashboardTile>[]): DashboardType =>({diff --git a/frontend/src/lib/utils/eventUsageLogic.ts b/frontend/src/lib/utils/eventUsageLogic.tsindex 2affe0d0fd49..2f1caab03c23 100644--- a/frontend/src/lib/utils/eventUsageLogic.ts+++ b/frontend/src/lib/utils/eventUsageLogic.ts@@ -105,6 +105,7 @@ export type DashboardFilterChangeType =| 'test_accounts'| 'compare'| 'metric_labels'+ | 'metric_variables'export enum InsightEventSource {LongPress = 'long_press',@@ -569,13 +570,32 @@ export function getEventPropertiesForExperiment(experiment: Experiment): object}}+// Metric variable definitions hold queries and option lists, which do not belong in analytics.+export function filtersForAnalytics(filters: Record<string, any>): Record<string, any> {+ const { metricVariables, ...rest } = filters+ return metricVariables ? { ...rest, metric_variable_count: metricVariables.length } : rest+}+export function sanitizeInsight(insight: Partial<InsightModel> | null): object | undefined {if (!insight) {return undefined}// Remove results- const { result, ...sanitizedInsight } = insight+ const { result, ...rest } = insight+ const context = rest.filter_override_context+ const sanitizedInsight = context+ ? {+ ...rest,+ filter_override_context: {+ ...context,+ dashboard: context.dashboard ? filtersForAnalytics(context.dashboard) : context.dashboard,+ overridden_dashboard: context.overridden_dashboard+ ? filtersForAnalytics(context.overridden_dashboard)+ : context.overridden_dashboard,+ },+ }+ : restif (sanitizedInsight.query) {return {@@ -609,6 +629,8 @@ export function sanitizeDashboard(dashboard: DashboardType | null): object | nulreturn {...sanitizedDashboard,breakdown_colors_count: breakdown_colors?.length ?? 0,+ filters: filtersForAnalytics(dashboard.filters ?? {}),+ persisted_filters: dashboard.persisted_filters ? filtersForAnalytics(dashboard.persisted_filters) : null,tiles: dashboard.tiles?.map((tile) => sanitizeTile(tile)) || [],}}diff --git a/frontend/src/lib/utils/metricVariableUrlRedaction.test.ts b/frontend/src/lib/utils/metricVariableUrlRedaction.test.tsnew file mode 100644index 000000000000..355c0e8ae0c2--- /dev/null+++ b/frontend/src/lib/utils/metricVariableUrlRedaction.test.ts@@ -0,0 +1,92 @@+import { redactMetricVariableUrl, redactMetricVariableUrls } from './metricVariableUrlRedaction'++const filters = encodeURIComponent(+ JSON.stringify({ metricVariables: [{ name: 'env', query: "SELECT 'private_value'", current: { values: ['a'] } }] })+)++describe('redactMetricVariableUrl', () => {+ it.each([+ [+ 'an insight link',+ `https://us.posthog.com/insights/abc?filters_override=${filters}#x`,+ 'https://us.posthog.com/insights/abc',+ ],+ [+ 'a dashboard',+ `https://us.posthog.com/dashboard/1?query_filters=${filters}`,+ 'https://us.posthog.com/dashboard/1',+ ],+ [+ 'a login redirect',+ `https://us.posthog.com/login?next=${encodeURIComponent(`/dashboard/1?query_filters=${filters}`)}`,+ 'https://us.posthog.com/login',+ ],+ ['a relative link', `/insights/abc?filters_override=${filters}`, '/insights/abc'],+ ['an element chain', `a:attr__href="/insights/abc?filters_override=${filters}"`, ''],+ ])('keeps only the path of %s', (_name, text, expected) => {+ expect(redactMetricVariableUrl(text)).toBe(expected)+ })++ it('redacts every property of an event', () => {+ const url = `https://us.posthog.com/dashboard/1?query_filters=${filters}`++ const event = redactMetricVariableUrls({+ event: '$pageview',+ properties: {+ $current_url: url,+ $session_entry_url: url,+ $elements: [{ tag_name: 'a', attr__href: url.replace('https://us.posthog.com', '') }],+ $elements_chain: `a:attr__href="${url}"`,+ },+ } as any)++ expect(JSON.stringify(event)).not.toContain('private_value')+ })++ it('redacts a URL deep inside a replay snapshot', () => {+ const href = `/insights/abc?filters_override=${filters}`+ const link = { type: 2, tagName: 'a', attributes: { href }, childNodes: [] }+ const page = { type: 0, childNodes: [{ type: 2, childNodes: [{ type: 2, childNodes: [link] }] }] }+ const snapshot = { type: 2, data: { node: page } }++ const event = redactMetricVariableUrls({+ event: '$snapshot',+ properties: { $snapshot_data: [snapshot] },+ } as any)++ expect(JSON.stringify(event)).not.toContain('private_value')+ expect(+ event!.properties.$snapshot_data[0].data.node.childNodes[0].childNodes[0].childNodes[0].attributes.href+ ).toBe('/insights/abc')+ })++ it('leaves an event without metric variables as the same objects', () => {+ const properties = { $snapshot_data: [{ data: { payload: { requests: [{ name: '/api/x' }] } } }] }++ expect(redactMetricVariableUrls({ event: '$snapshot', properties } as any)!.properties).toBe(properties)+ })++ it('leaves the requests that carry metric variable data out of a replay snapshot', () => {+ const kept = { name: '/api/environments/1/insights/', requestBody: '{"name":"Signups"}' }+ const requests = [+ kept,+ { name: `/api/environments/1/insights/?filters_override=${filters}` },+ { name: '/api/environments/1/metrics/variable_options/', requestBody: '{"query":"SELECT 1"}' },+ { name: '/api/dashboards/1', requestBody: JSON.stringify({ filters: { metricVariables: [] } }) },+ ]+ const snapshot = { type: 6, data: { plugin: 'rrweb/network@1', payload: { requests } } }++ const event = redactMetricVariableUrls({+ event: '$snapshot',+ properties: { $snapshot_data: [snapshot] },+ } as any)++ expect(event!.properties.$snapshot_data[0].data.payload.requests).toEqual([kept])+ })++ it('leaves a URL without metric variables as it is', () => {+ const url = 'https://us.posthog.com/dashboard/1?query_filters=%7B%22date_from%22%3A%22-7d%22%7D'++ expect(redactMetricVariableUrl(url)).toBe(url)+ })+})diff --git a/frontend/src/lib/utils/metricVariableUrlRedaction.ts b/frontend/src/lib/utils/metricVariableUrlRedaction.tsnew file mode 100644index 000000000000..cb6a3f5cf810--- /dev/null+++ b/frontend/src/lib/utils/metricVariableUrlRedaction.ts@@ -0,0 +1,99 @@+import type { BeforeSendFn } from 'posthog-js'++const MARKER = 'metricVariables'+// The options requests of dashboard variables carry queries and selected values, and return query results.+const VARIABLE_OPTIONS_PATH = '/metrics/variable_options'+const NETWORK_PLUGIN = 'rrweb/network@1'++/** A URL that holds metric variables, in any parameter or encoding, keeps only its path, so the queries, options and+ * selected values of the variables stay out of analytics. Other text that holds them is dropped. */+export function redactMetricVariableUrl(text: string): string {+ if (!text.includes(MARKER)) {+ return text+ }+ if (text.startsWith('/') || /^https?:\/\/\S+$/.test(text)) {+ return text.split(/[?#]/)[0]+ }+ return ''+}++/** Every string at any depth, such as `$current_url`, an autocaptured `href`, or a request in a replay snapshot.+ * A value with nothing to redact is returned as it is, so a large snapshot is not copied. */+function redactValue(value: unknown, seen: WeakSet<object>): unknown {+ if (typeof value === 'string') {+ return redactMetricVariableUrl(value)+ }+ if (!value || typeof value !== 'object' || seen.has(value)) {+ return value+ }+ seen.add(value)+ if (Array.isArray(value)) {+ let copy: unknown[] | null = null+ value.forEach((item, index) => {+ const redacted = redactValue(item, seen)+ if (redacted !== item) {+ copy ??= [...value]+ copy[index] = redacted+ }+ })+ return copy ?? value+ }+ let copy: Record<string, unknown> | null = null+ for (const [key, item] of Object.entries(value)) {+ const redacted = redactValue(item, seen)+ if (redacted !== item) {+ copy ??= { ...value }+ copy[key] = redacted+ }+ }+ return copy ?? value+}++function holdsVariableData(request: unknown): boolean {+ if (!request || typeof request !== 'object') {+ return false+ }+ const { name, requestBody, responseBody } = request as Record<string, unknown>+ return (+ (typeof name === 'string' && (name.includes(VARIABLE_OPTIONS_PATH) || name.includes(MARKER))) ||+ [requestBody, responseBody].some((body) => typeof body === 'string' && body.includes(MARKER))+ )+}++/** Replay records network requests in snapshots. The ones that carry metric variable data are left out, and the+ * SDK's own body scrubbing still applies to the rest. */+function withoutVariableRequests(snapshots: unknown): unknown {+ if (!Array.isArray(snapshots)) {+ return snapshots+ }+ let copy: unknown[] | null = null+ snapshots.forEach((snapshot, index) => {+ const data = snapshot?.data+ const requests = data?.plugin === NETWORK_PLUGIN ? data.payload?.requests : undefined+ if (!Array.isArray(requests) || !requests.some(holdsVariableData)) {+ return+ }+ copy ??= [...snapshots]+ const kept = requests.filter((request) => !holdsVariableData(request))+ copy[index] = { ...snapshot, data: { ...data, payload: { ...data.payload, requests: kept } } }+ })+ return copy ?? snapshots+}++function redactProperties(properties: Record<string, any> | undefined): Record<string, any> | undefined {+ if (!properties) {+ return properties+ }+ const snapshots = properties.$snapshot_data+ const filtered = withoutVariableRequests(snapshots)+ const base = filtered === snapshots ? properties : { ...properties, $snapshot_data: filtered }+ return redactValue(base, new WeakSet()) as Record<string, any>+}++export const redactMetricVariableUrls: BeforeSendFn = (event) =>+ event && {+ ...event,+ properties: redactProperties(event.properties) ?? event.properties,+ $set: redactProperties(event.$set),+ $set_once: redactProperties(event.$set_once),+ }diff --git a/frontend/src/loadPostHogJS.tsx b/frontend/src/loadPostHogJS.tsxindex bba64887e7f6..380c3681495e 100644--- a/frontend/src/loadPostHogJS.tsx+++ b/frontend/src/loadPostHogJS.tsx@@ -5,6 +5,7 @@ import { isOAuthMode } from 'lib/oauth/oauthClient'import { inStorybook, inStorybookTestRunner } from 'lib/utils/dom'import { isEmbeddedPageFrame } from 'lib/utils/embeddedPageFrame'import { getAppContext, isHobbyDeployment } from 'lib/utils/getAppContext'+import { redactMetricVariableUrls } from 'lib/utils/metricVariableUrlRedaction'import { identifyInstanceInPostHog, identifyUserInPostHog } from 'lib/utils/posthogIdentity'import { startDetachedElementTracking } from './detachedElementTracker'@@ -118,7 +119,9 @@ export function loadPostHogJS(): void {metrics: { network: true, serviceName: 'posthog-app' },// A page in a frame counts its own pageviews, so its events say so and analysis can filter them.// `register` would persist the property in storage the main window shares, so it is stamped per event.- before_send: isEmbeddedPageFrame() ? stampEmbeddedPageFrame : undefined,+ before_send: isEmbeddedPageFrame()+ ? [redactMetricVariableUrls, stampEmbeddedPageFrame]+ : redactMetricVariableUrls,loaded: (loadedInstance) => {if (loadedInstance.sessionRecording) {loadedInstance.sessionRecording._forceAllowLocalhostNetworkCapture = truediff --git a/frontend/src/scenes/dashboard/DashboardEditBar.tsx b/frontend/src/scenes/dashboard/DashboardEditBar.tsxindex c3555e197871..084f7e33e89b 100644--- a/frontend/src/scenes/dashboard/DashboardEditBar.tsx+++ b/frontend/src/scenes/dashboard/DashboardEditBar.tsx@@ -30,6 +30,7 @@ import { BreakdownFilter, NodeKind } from '~/queries/schema/schema-general'import { InsightLogicProps, IntervalType } from '~/types'import { DashboardMetricLabelFilter } from 'products/metrics/frontend/components/DashboardMetricLabelFilter'+import { DashboardMetricVariables } from 'products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables'interface DashboardEditBarProps {showDateFilter?: boolean@@ -268,6 +269,17 @@ export function DashboardEditBar({ showDateFilter = true, className }: Dashboard</div></DashboardControlSlot>)}+ {controlsEnabled && (+ <DashboardControlSlot+ control="metricVariables"+ hasValue={(effectiveEditBarFilters.metricVariables?.length ?? 0) > 0}+ className={clsx('content-end', { 'h-[61px]': hasVariables })}+ >+ <div className="flex min-h-[30px] flex-wrap items-end gap-1 border-l border-primary pl-2">+ <DashboardMetricVariables />+ </div>+ </DashboardControlSlot>+ )}</div>)}diff --git a/frontend/src/scenes/dashboard/dashboardControls.test.ts b/frontend/src/scenes/dashboard/dashboardControls.test.tsindex 21997c21aa79..f0eb97364d85 100644--- a/frontend/src/scenes/dashboard/dashboardControls.test.ts+++ b/frontend/src/scenes/dashboard/dashboardControls.test.ts@@ -18,11 +18,11 @@ describe('dashboardControls', () => {it.each([['a trends insight', trendsTile, ['dateRange', 'interval', 'properties', 'breakdown', 'testAccounts']],['a retention insight', retentionTile, ['dateRange', 'properties', 'breakdown', 'testAccounts']],- ['a metrics insight', metricsTile, ['dateRange', 'metricLabels']],+ ['a metrics insight', metricsTile, ['dateRange', 'metricLabels', 'metricVariables']],['a PromQL metrics insight',insightTile({ kind: NodeKind.MetricsQuery, clauses: [], language: 'promql', promql: 'sum(x)' }),- ['dateRange'],+ ['dateRange', 'metricVariables'],],['a SQL insight',diff --git a/frontend/src/scenes/dashboard/dashboardControls.ts b/frontend/src/scenes/dashboard/dashboardControls.tsindex fe8bfe75e3b7..e72e0679e4cf 100644--- a/frontend/src/scenes/dashboard/dashboardControls.ts+++ b/frontend/src/scenes/dashboard/dashboardControls.ts@@ -3,7 +3,14 @@ import { isNodeWithSource } from '~/queries/utils'import { DashboardTile } from '~/types'/** A filter control in the dashboard filter bar. */-export type DashboardControl = 'dateRange' | 'interval' | 'properties' | 'breakdown' | 'testAccounts' | 'metricLabels'+export type DashboardControl =+ | 'dateRange'+ | 'interval'+ | 'properties'+ | 'breakdown'+ | 'testAccounts'+ | 'metricLabels'+ | 'metricVariables'export interface DashboardControlScope {/** Insight tiles that the control changes. */@@ -19,6 +26,7 @@ const ALL_CONTROLS: DashboardControl[] = ['breakdown','testAccounts','metricLabels',+ 'metricVariables',]// Matches what each query runner's `apply_dashboard_filters` changes. A kind that is not listed keeps@@ -32,16 +40,16 @@ const CONTROLS_BY_QUERY_KIND: Partial<Record<NodeKind, DashboardControl[]>> = {[NodeKind.StickinessQuery]: ['dateRange', 'interval', 'properties', 'testAccounts'],[NodeKind.LifecycleQuery]: ['dateRange', 'interval', 'properties', 'testAccounts'],[NodeKind.CalendarHeatmapQuery]: ['dateRange', 'interval', 'properties', 'testAccounts'],- [NodeKind.MetricsQuery]: ['dateRange', 'metricLabels'],- [NodeKind.MetricsHistogramQuery]: ['dateRange', 'metricLabels'],+ [NodeKind.MetricsQuery]: ['dateRange', 'metricLabels', 'metricVariables'],+ [NodeKind.MetricsHistogramQuery]: ['dateRange', 'metricLabels', 'metricVariables'],}export function dashboardControlsForQuery(query: Node | null | undefined): DashboardControl[] {const source = isNodeWithSource(query) ? query.source : query- // Label filters are added to builder clauses only; PromQL and SQL text is not rewritten.+ // Label filters reach builder clauses only, because PromQL and SQL text is not rewritten for them. Variables reach all three languages.const metricsLanguage = source?.kind === NodeKind.MetricsQuery ? (source as MetricsQuery).language : undefinedif (metricsLanguage && metricsLanguage !== 'builder') {- return ['dateRange']+ return ['dateRange', 'metricVariables']}return (source?.kind && CONTROLS_BY_QUERY_KIND[source.kind as NodeKind]) || DEFAULT_CONTROLS}diff --git a/frontend/src/scenes/dashboard/dashboardFilterEmpty.ts b/frontend/src/scenes/dashboard/dashboardFilterEmpty.tsindex 395f679c1c1f..497c4bb381c4 100644--- a/frontend/src/scenes/dashboard/dashboardFilterEmpty.ts+++ b/frontend/src/scenes/dashboard/dashboardFilterEmpty.ts@@ -12,6 +12,7 @@ export function isDashboardFilterOverrideEmpty(filter: DashboardFilter | TileFilfilter.filterTestAccounts === undefined &&filter.compareFilter === undefined &&(filter as DashboardFilter).metricFilters === undefined &&+ (filter as DashboardFilter).metricVariables === undefined &&!(filter as TileFilters).ignoreDashboardFilters))}diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.tsindex 3f79bd4bbe11..8c2120f37c4a 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts@@ -1035,6 +1035,30 @@ describe('dashboardLogic', () => {}})+ it('keeps metric variable definitions out of refresh analytics', async () => {+ await expectLogic(logic).toFinishAllListeners()+ const refresh = jest+ .spyOn(dashboardUtils, 'getInsightWithRetry')+ .mockImplementation(async (_teamId, insight) => insight)+ const capture = jest.spyOn(posthog, 'capture').mockClear()++ try {+ await expectLogic(logic, () => {+ logic.actions.setMetricVariables([+ { name: 'env', source: 'sql', query: 'SELECT private_value', current: { values: ['a'] } },+ ])+ }).toFinishAllListeners()++ const refreshes = capture.mock.calls.filter(([event]) =>+ ['dashboard refreshed', 'dashboard insight refreshed'].includes(event)+ )+ expect(refreshes.length).toBeGreaterThan(0)+ expect(JSON.stringify(refreshes)).not.toContain('private_value')+ } finally {+ refresh.mockRestore()+ }+ })+it('keeps combined auto-preview filters after a page reload, then clears and saves them', async () => {const sevenTileDashboard = {...dashboards[5],diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsxindex 00abf7eec685..2555fcd7a4e7 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx@@ -52,6 +52,7 @@ import {dashboardViewedProperties,eventUsageLogic,sanitizeDashboard,+ filtersForAnalytics,sanitizeQuery,} from 'lib/utils/eventUsageLogic'import { objectsEqual } from 'lib/utils/objects'@@ -90,6 +91,7 @@ import {DashboardFilter,DataVisualizationNode,HogQLVariable,+ MetricsDashboardVariable,MetricsQueryFilter,NodeKind,RefreshType,@@ -155,6 +157,8 @@ import {SEARCH_PARAM_FILTERS_KEY,SEARCH_PARAM_QUERY_VARIABLES_KEY,combineDashboardFilters,+ combineSavedAndUrlFilters,+ withCompactMetricVariables,encodeURLVariables,getDashboardWidgetType,getEffectiveDateOverride,@@ -194,7 +198,7 @@ function reportDashboardTileRefreshed(was_cached: tile.is_cached,last_refreshed: insight?.last_refresh?.toString(),refresh_age: insight?.last_refresh ? now().diff(insight?.last_refresh, 'seconds') : undefined,- filters,+ filters: filtersForAnalytics(filters),variables,refresh_duration_ms: refreshDurationMs,individual_refresh: individualRefresh,@@ -392,6 +396,7 @@ export interface dashboardLogicValues {filterEditModeActive: booleanfiltersDirty: booleanfiltersOverrideForLoad: DashboardFilter+ filtersOverrideForRequests: DashboardFilterhasInvalidDashboardId: booleanhasUnsavedColorChanges: booleanhasUnsavedLayoutChanges: boolean@@ -993,6 +998,9 @@ export interface dashboardLogicActions {setMetricFilters: (metricFilters: MetricsQueryFilter[] | null) => {metricFilters: MetricsQueryFilter[] | null}+ setMetricVariables: (metricVariables: MetricsDashboardVariable[] | null) => {+ metricVariables: MetricsDashboardVariable[] | null+ }setPageVisibility: (visible: boolean) => {visible: boolean}@@ -1220,6 +1228,10 @@ export interface dashboardLogicMeta {externalFilters: DashboardFilter,currentDashboardSettings: DashboardSettings) => DashboardFilter+ filtersOverrideForRequests: (+ filtersOverrideForLoad: DashboardFilter,+ savedDashboardSettings: DashboardSettings+ ) => DashboardFiltereffectiveEditBarFilters: (externalFilters: DashboardFilter,currentDashboardSettings: DashboardSettings@@ -1570,6 +1582,7 @@ export const dashboardLogic = kea<dashboardLogicType>([changeType: DashboardFilterChangeType,properties: Record<string, string | number | boolean | null | undefined>) => ({ changeType, properties }),+ setMetricVariables: (metricVariables: MetricsDashboardVariable[] | null) => ({ metricVariables }),setExternalFilters: (filters: DashboardFilter) => ({ filters }),setDashboardSettingsDraft: (settings: DashboardSettings | null) => ({ settings }),setPreviewedDashboardSettings: (settings: DashboardSettings | null) => ({ settings }),@@ -1721,7 +1734,7 @@ export const dashboardLogic = kea<dashboardLogicType>([try {const apiUrl = values.apiUrl('force_cache',- values.filtersOverrideForLoad,+ values.filtersOverrideForRequests,values.currentDashboardVariables)// nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.@@ -1764,7 +1777,7 @@ export const dashboardLogic = kea<dashboardLogicType>([props.id,{layoutSize: values.currentLayoutSize,- filtersOverride: values.filtersOverrideForLoad,+ filtersOverride: values.filtersOverrideForRequests,variablesOverride: values.currentDashboardVariables,},// onMessage callback - handles both metadata and tiles@@ -2183,6 +2196,7 @@ export const dashboardLogic = kea<dashboardLogicType>([setFilterTestAccounts: () => false,setCompareFilter: () => false,setMetricFilters: () => false,+ setMetricVariables: () => false,overrideVariableValue: () => false,loadDashboardSuccess: () => false,loadDashboardFailure: () => false,@@ -2900,7 +2914,7 @@ export const dashboardLogic = kea<dashboardLogicType>([draft: DashboardSettings | null): DashboardSettings =>draft || {- filters: combineDashboardFilters(saved.filters, initialOverride.filters),+ filters: combineSavedAndUrlFilters(saved.filters, initialOverride.filters),variables: { ...saved.variables, ...initialOverride.variables },},],@@ -2912,7 +2926,7 @@ export const dashboardLogic = kea<dashboardLogicType>([previewed: DashboardSettings | null): DashboardSettings =>previewed || {- filters: combineDashboardFilters(saved.filters, initialOverride.filters),+ filters: combineSavedAndUrlFilters(saved.filters, initialOverride.filters),variables: { ...saved.variables, ...initialOverride.variables },},],@@ -2953,6 +2967,11 @@ export const dashboardLogic = kea<dashboardLogicType>([(externalFilters: DashboardFilter, settings: DashboardSettings) =>combineDashboardFilters(settings.filters, externalFilters),],+ filtersOverrideForRequests: [+ (s) => [s.filtersOverrideForLoad, s.savedDashboardSettings],+ (filters: DashboardFilter, saved: DashboardSettings): DashboardFilter =>+ withCompactMetricVariables(filters, saved.filters.metricVariables ?? []),+ ],effectiveEditBarFilters: [(s) => [s.externalFilters, s.currentDashboardSettings],(externalFilters: DashboardFilter, settings: DashboardSettings) => {@@ -4429,6 +4448,7 @@ export const dashboardLogic = kea<dashboardLogicType>([// Cache values before the long-running await — the logic may unmountconst { currentTeamId, effectiveRefreshFilters, settingsForRefresh, urlFilters } = values+ const savedMetricVariables = values.savedDashboardSettings.filters.metricVariables ?? []const urlVariables = settingsForRefresh.variablesconst pendingFilterChange: PendingFilterChange | undefined = cache.pendingFilterChangeconst filterChange =@@ -4453,7 +4473,7 @@ export const dashboardLogic = kea<dashboardLogicType>([uuid(),'force_blocking',undefined,- effectiveRefreshFilters,+ withCompactMetricVariables(effectiveRefreshFilters, savedMetricVariables),urlVariables,tile.filters_overrides)@@ -4560,6 +4580,7 @@ export const dashboardLogic = kea<dashboardLogicType>([// only cancels on newer invocations, not on unmount).const { currentTeamId, externalFilters, urlFilters, dashboardLoadData, lastDashboardRefresh } = valuesconst effectiveRefreshFilters = combineDashboardFilters(settingsToRefresh.filters, externalFilters)+ const savedMetricVariables = values.savedDashboardSettings.filters.metricVariables ?? []const urlVariables = settingsToRefresh.variablesconst fetchSyncInsightFunctions = sortedTilesToRefresh.map((tile) => async () => {@@ -4580,7 +4601,7 @@ export const dashboardLogic = kea<dashboardLogicType>([queryId,forceRefresh ? 'force_blocking' : 'blocking', // 'blocking' returns cached data if available, when manual refresh is triggered we want fresh resultsmethodOptions,- effectiveRefreshFilters,+ withCompactMetricVariables(effectiveRefreshFilters, savedMetricVariables),urlVariables,tile.filters_overrides)@@ -4660,7 +4681,7 @@ export const dashboardLogic = kea<dashboardLogicType>([: tilesRefreshedCount === totalTileCount && tilesErroredCount === 0 && tilesAbortedCount === 0posthog.capture(`dashboard refreshed`, {dashboard_id: dashboardId,- filters: urlFilters,+ filters: filtersForAnalytics(urlFilters),variables: urlVariables,last_refreshed: lastDashboardRefresh?.toString(),refreshAge: lastDashboardRefresh ? now().diff(lastDashboardRefresh, 'seconds') : undefined,@@ -5303,6 +5324,26 @@ export const dashboardLogic = kea<dashboardLogicType>([})}},+ setMetricVariables: ({ metricVariables }) => {+ actions.setDashboardSettingsDraft({+ ...values.currentDashboardSettings,+ filters: { ...values.currentDashboardSettings.filters, metricVariables },+ })+ actions.reportDashboardFilterChange('metric_variables', {+ metric_variable_count: metricVariables?.length ?? 0,+ metric_variable_query_count:+ metricVariables?.filter((variable) => variable.source === 'promql' || variable.source === 'sql')+ .length ?? 0,+ })++ if (values.canAutoPreview) {+ actions.refreshDashboardItems({+ action: RefreshDashboardItemsAction.Preview,+ forceRefresh: false,+ previewUnsavedFilters: true,+ })+ }+ },setExternalFilters: () => {if (values.tiles.length > 0) {actions.refreshDashboardItems({@@ -5604,6 +5645,25 @@ export const dashboardLogic = kea<dashboardLogicType>([currentLocation.hashParams,]},+ setMetricVariables: ({ metricVariables }) => {+ const { currentLocation } = router.values++ const urlFilters = parseURLFilters(currentLocation.searchParams)+ const newUrlFilters: DashboardFilter = {+ ...urlFilters,+ metricVariables,+ }++ return [+ currentLocation.pathname,+ searchParamsWithUrlFilters(+ currentLocation.searchParams,+ newUrlFilters,+ combineDashboardFilters(values.dashboard?.persisted_filters || {}, values.externalFilters)+ ),+ currentLocation.hashParams,+ ]+ },overrideVariableValue: ({ variableId, value, isNull }) => {const { currentLocation } = router.values@@ -5680,7 +5740,7 @@ export const dashboardLogic = kea<dashboardLogicType>([if (method === 'POP' && values.dashboard) {const current = values.currentDashboardSettingsconst settings: DashboardSettings = {- filters: combineDashboardFilters(+ filters: combineSavedAndUrlFilters(values.savedDashboardSettings.filters,parseURLFilters(searchParams)),diff --git a/frontend/src/scenes/dashboard/dashboardUtils.test.ts b/frontend/src/scenes/dashboard/dashboardUtils.test.tsindex 695f42b07596..18e88e7668f4 100644--- a/frontend/src/scenes/dashboard/dashboardUtils.test.ts+++ b/frontend/src/scenes/dashboard/dashboardUtils.test.ts@@ -5,7 +5,13 @@ import { ApiError } from 'lib/api-error'import { dayjs } from 'lib/dayjs'import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'-import { BreakdownFilter, DashboardFilter, HogQLVariable, QueryStatus } from '~/queries/schema/schema-general'+import {+ BreakdownFilter,+ DashboardFilter,+ HogQLVariable,+ MetricsDashboardVariable,+ QueryStatus,+} from '~/queries/schema/schema-general'import {AnyPropertyFilter,DashboardPlacement,@@ -17,6 +23,7 @@ import {} from '~/types'import {+ combineSavedAndUrlFilters,dashboardSearchParamsFromOverrides,dashboardTemplateForExport,dashboardToSaveableTemplate,@@ -67,6 +74,44 @@ describe('searchParamsWithUrlFilters', () => {expect(parseURLFilters(searchParams)).toEqual({ explicitDate: false })})+ it('keeps a saved metric variable query out of the URL, and reads it back from the saved filters', () => {+ const saved: MetricsDashboardVariable = {+ name: 'env',+ source: 'sql',+ query: 'SELECT private_value',+ current: { values: ['a'] },+ }+ const persistedFilters = { metricVariables: [saved] }+ const filters = { metricVariables: [{ ...saved, current: { values: ['b'] } }] }++ const searchParams = searchParamsWithUrlFilters({}, filters, persistedFilters)++ expect(searchParams[SEARCH_PARAM_FILTERS_KEY]).not.toContain('private_value')+ expect(combineSavedAndUrlFilters(persistedFilters, parseURLFilters(searchParams))).toEqual(filters)+ expect(+ searchParamsWithUrlFilters({}, persistedFilters, persistedFilters)[SEARCH_PARAM_FILTERS_KEY]+ ).toBeUndefined()+ expect(combineSavedAndUrlFilters({}, parseURLFilters(searchParams))).toEqual({+ metricVariables: [{ name: 'env', current: { values: ['b'] } }],+ })+ })++ it('keeps a metric variable that is not saved yet out of the URL', () => {+ const draft: MetricsDashboardVariable = {+ name: 'region',+ source: 'custom',+ customValues: ['private_option'],+ current: { values: ['private_option'] },+ }++ const searchParams = searchParamsWithUrlFilters({}, { metricVariables: [draft] }, {})++ expect(parseURLFilters(searchParams)).toEqual({+ metricVariables: [{ name: 'region', current: { values: ['private_option'] } }],+ })+ expect(searchParams[SEARCH_PARAM_FILTERS_KEY]).not.toContain('customValues')+ })+it('keeps an override that clears an external filter', () => {const searchParams = searchParamsWithUrlFilters({}, { properties: [] }, { properties: propertyFilter })diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.tsindex 117ebbed9f44..2609dee608f3 100644--- a/frontend/src/scenes/dashboard/dashboardUtils.ts+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts@@ -16,7 +16,13 @@ import { isDeterministicClientError, shouldCancelQuery } from 'lib/utils/requestimport { toParams } from 'lib/utils/url'import { isExpiredQueryStatusError, parseErrorMessage, pollForResults } from '~/queries/query'-import { DashboardFilter, HogQLVariable, QueryStatus, TileFilters } from '~/queries/schema/schema-general'+import {+ DashboardFilter,+ HogQLVariable,+ MetricsDashboardVariable,+ QueryStatus,+ TileFilters,+} from '~/queries/schema/schema-general'import {AccessControlLevel,AccessControlResourceType,@@ -614,17 +620,73 @@ export const dashboardSearchParamsFromOverrides = (* An empty override must stay in the URL when it clears a saved dashboard filter. Without that explicit* value, a reload restores the saved filter. Empty overrides on unfiltered dashboards can be removed.*/+function withoutCurrent(variable: Partial<MetricsDashboardVariable>): Partial<MetricsDashboardVariable> {+ const { current: _, ...definition } = variable+ return definition+}++/** A saved metric variable goes into requests as its name and selection, and the server puts the saved definition back.+ * A definition that is not saved yet goes in full, because the server does not know it. */+export function withCompactMetricVariables(+ filters: DashboardFilter,+ saved: MetricsDashboardVariable[]+): DashboardFilter {+ if (!filters.metricVariables) {+ return filters+ }+ return {+ ...filters,+ metricVariables: filters.metricVariables.map((variable) => {+ const savedVariable = saved.find((other) => other.name === variable.name)+ return savedVariable && equal(withoutCurrent(savedVariable), withoutCurrent(variable))+ ? ({ name: variable.name, current: variable.current } as MetricsDashboardVariable)+ : variable+ }),+ }+}++/** Saved filters with the filters from the URL on top. A metric variable that the URL names gets its saved definition. */+export function combineSavedAndUrlFilters(saved: DashboardFilter, url: DashboardFilter): DashboardFilter {+ // Until the dashboard loads, the saved definitions are unknown, so the server resolves the names.+ if (!url.metricVariables || !saved.metricVariables) {+ return combineDashboardFilters(saved, url)+ }+ const metricVariables = url.metricVariables.flatMap((variable) => {+ if (variable.source) {+ return [variable]+ }+ const savedVariable = saved.metricVariables?.find((other) => other.name === variable.name)+ return savedVariable ? [{ ...savedVariable, current: variable.current }] : []+ })+ return combineDashboardFilters(saved, { ...url, metricVariables })+}++/** Analytics captures the page URL, so metric variables go into a URL as their names and selections only. A definition+ * that is not saved yet stays in the page until the dashboard is saved. */+function withMetricVariableSelections(filters: DashboardFilter): DashboardFilter {+ if (!filters.metricVariables) {+ return filters+ }+ return {+ ...filters,+ metricVariables: filters.metricVariables.map(+ (variable) => ({ name: variable.name, current: variable.current }) as MetricsDashboardVariable+ ),+ }+}+export function searchParamsWithUrlFilters(searchParams: Record<string, any>,filters: DashboardFilter,persistedFilters: DashboardFilter = {}): Record<string, any> {const nextSearchParams = { ...searchParams }- if (!dashboardFilterOverrideChangesFilters(filters, persistedFilters)) {+ const compact = withMetricVariableSelections(filters)+ if (!dashboardFilterOverrideChangesFilters(compact, withMetricVariableSelections(persistedFilters))) {delete nextSearchParams[SEARCH_PARAM_FILTERS_KEY]return nextSearchParams}- return { ...nextSearchParams, ...encodeURLFilters(filters) }+ return { ...nextSearchParams, ...encodeURLFilters(compact) }}export function dashboardFilterOverrideChangesFilters(diff --git a/posthog/test/test_utils.py b/posthog/test/test_utils.pyindex 944d59192470..6cf12954d59f 100644--- a/posthog/test/test_utils.py+++ b/posthog/test/test_utils.py@@ -22,6 +22,7 @@ from django.utils.timezone import nowimport posthoganalyticsfrom asgiref.sync import async_to_syncfrom parameterized import parameterized+from rest_framework import serializersfrom rest_framework.request import Requestfrom posthog.exceptions import RequestParsingError, UnspecifiedCompressionFallbackParsingError@@ -1100,6 +1101,24 @@ class TestSharingOverrideProtection(SimpleTestCase):assert result == {"date_from": "-7d"}+ def test_filters_override_gets_saved_metric_variable_definitions(self):+ saved = {"name": "env", "source": "sql", "query": "SELECT 1", "current": {"values": ["a"]}}+ compact = [{"name": "env", "current": {"values": ["b"]}}, {"name": "gone", "current": {"values": ["c"]}}]+ request = self._make_request(None, query_params={"filters_override": json.dumps({"metricVariables": compact})})+ dashboard = type("Dashboard", (), {"filters": {"metricVariables": [saved]}})()++ result = filters_override_requested_by_client(request, dashboard)++ assert result["metricVariables"] == [{**saved, "current": {"values": ["b"]}}]++ def test_filters_override_rejects_a_metric_variable_name_that_is_not_a_string(self):+ override: dict[str, list[dict[str, list[str]]]] = {"metricVariables": [{"name": []}]}+ request = self._make_request(None, query_params={"filters_override": json.dumps(override)})+ dashboard = type("Dashboard", (), {"filters": {}})()++ with pytest.raises(serializers.ValidationError):+ filters_override_requested_by_client(request, dashboard)+@parameterized.expand([("access_token_auth",),diff --git a/posthog/utils.py b/posthog/utils.pyindex 46a57eb1e595..f7f40d6e19c3 100644--- a/posthog/utils.py+++ b/posthog/utils.py@@ -1871,6 +1871,28 @@ def cache_requested_by_client(request: Request) -> bool | str:return _request_has_key_set("use_cache", request)+def _with_saved_metric_variables(request_filters: dict, dashboard_filters: dict) -> dict:+ """A metric variable that the client sends as its name and selection gets its saved definition."""+ requested = request_filters.get("metricVariables")+ if not isinstance(requested, list):+ return request_filters+ saved = {+ variable["name"]: variable+ for variable in dashboard_filters.get("metricVariables") or []+ if isinstance(variable, dict) and isinstance(variable.get("name"), str)+ }+ metric_variables = []+ for variable in requested:+ if not isinstance(variable, dict) or variable.get("source"):+ metric_variables.append(variable)+ continue+ if not isinstance(variable.get("name"), str):+ raise serializers.ValidationError({"filters_override": "A metric variable name must be a string."})+ if variable["name"] in saved:+ metric_variables.append({**saved[variable["name"]], "current": variable.get("current")})+ return {**request_filters, "metricVariables": metric_variables}++def filters_override_requested_by_client(request: Request, dashboard: Optional["Dashboard"], is_shared: bool = False) -> dict:@@ -1897,6 +1919,8 @@ def filters_override_requested_by_client(except Exception:raise serializers.ValidationError({"filters_override": "Invalid JSON passed in filters_override parameter"})+ if isinstance(request_filters, dict):+ request_filters = _with_saved_metric_variables(request_filters, dashboard_filters or {})return {**dashboard_filters, **request_filters}diff --git a/products/dashboards/frontend/dashboardSettings/DashboardSettingsChangesTooltip.tsx b/products/dashboards/frontend/dashboardSettings/DashboardSettingsChangesTooltip.tsxindex 330f9ea73cf8..a78bbbab39ef 100644--- a/products/dashboards/frontend/dashboardSettings/DashboardSettingsChangesTooltip.tsx+++ b/products/dashboards/frontend/dashboardSettings/DashboardSettingsChangesTooltip.tsx@@ -55,7 +55,10 @@ function ChangeValue({ value }: { value: string[] }): JSX.Element {function renderChangeValue(change: DashboardFilterChange): JSX.Element {return (- <div className="flex flex-wrap items-center gap-1 break-words">+ // Metric variable values come from queries over customer data, so copying them captures no text.+ <div+ className={`${change.label === 'Metric variables' ? 'ph-no-capture ' : ''}flex flex-wrap items-center gap-1 break-words`}+ ><span className="min-w-0 text-muted-alt"><ChangeValue value={change.previousValue} /></span>diff --git a/products/dashboards/frontend/dashboardSettings/dashboardChanges.test.ts b/products/dashboards/frontend/dashboardSettings/dashboardChanges.test.tsindex fbc7a365dffe..83fe3f5ff098 100644--- a/products/dashboards/frontend/dashboardSettings/dashboardChanges.test.ts+++ b/products/dashboards/frontend/dashboardSettings/dashboardChanges.test.ts@@ -9,6 +9,17 @@ import {} from './dashboardChanges'describe('getDashboardFilterChanges', () => {+ it('lists a changed metric variable selection', () => {+ expect(+ getDashboardFilterChanges(+ { metricVariables: [{ name: 'job', source: 'custom', current: { values: ['api'] } }] },+ { metricVariables: [{ name: 'job', source: 'custom', current: { values: ['api', 'web'], all: true } }] }+ )+ ).toEqual([+ { label: 'Metric variables', previousValue: ['$job = api'], value: ['$job = All'], status: 'changed' },+ ])+ })+it('lists new, changed, and removed property filters with their values', () => {expect(getDashboardFilterChanges(diff --git a/products/dashboards/frontend/dashboardSettings/dashboardChanges.ts b/products/dashboards/frontend/dashboardSettings/dashboardChanges.tsindex 6b06b157ba33..5bc9bb338b3b 100644--- a/products/dashboards/frontend/dashboardSettings/dashboardChanges.ts+++ b/products/dashboards/frontend/dashboardSettings/dashboardChanges.ts@@ -333,6 +333,17 @@ export function getDashboardFilterChanges(})}+ if (!dashboardFilterValuesEqual(previousFilters.metricVariables, currentFilters.metricVariables)) {+ const previousLabels = formatMetricVariables(previousFilters)+ const currentLabels = formatMetricVariables(currentFilters)+ changes.push({+ label: 'Metric variables',+ previousValue: previousLabels,+ value: currentLabels,+ status: getChangeStatus(previousLabels.length > 0, currentLabels.length > 0),+ })+ }+return changes}@@ -348,3 +359,10 @@ function formatMetricFilters(filters: DashboardFilter): string[] {(filter) => `${filter.key} ${METRIC_FILTER_OP_LABELS[filter.op]} ${filter.value}`)}++function formatMetricVariables(filters: DashboardFilter): string[] {+ return (filters.metricVariables ?? []).map((variable) => {+ const value = variable.current?.all ? 'All' : (variable.current?.values ?? []).join(', ')+ return `$${variable.name} = ${value || 'no value'}`+ })+}diff --git a/products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables.tsx b/products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables.tsxnew file mode 100644index 000000000000..e58f9ebbf8bf--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables.tsx@@ -0,0 +1,91 @@+import { useActions, useValues } from 'kea'+import { useMemo, useState } from 'react'++import { IconGear } from '@posthog/icons'+import { LemonButton } from '@posthog/lemon-ui'++import { DashboardEventSource } from 'lib/utils/eventUsageLogic'+import { dashboardLogic } from 'scenes/dashboard/dashboardLogic'++import type { MetricsDashboardVariable } from '~/queries/schema/schema-general'++import { dashboardMetricVariablesLogic } from './dashboardMetricVariablesLogic'+import { ownValue, variablesToReload } from './metricVariables'+import { MetricVariableSelect } from './MetricVariableSelect'+import { MetricVariablesModal } from './MetricVariablesModal'++const NO_VARIABLES: MetricsDashboardVariable[] = []++export function DashboardMetricVariables(): JSX.Element | null {+ const { dashboard, dashboardEditing, effectiveEditBarFilters } = useValues(dashboardLogic)+ const { setMetricVariables, setDashboardEditing } = useActions(dashboardLogic)+ const [editorOpen, setEditorOpen] = useState(false)++ const metricVariables = effectiveEditBarFilters.metricVariables+ // A name from the URL without a saved definition, such as a deleted variable, has nothing to show.+ const variables = useMemo(+ () => metricVariables?.filter((variable) => variable.source) ?? NO_VARIABLES,+ [metricVariables]+ )+ const change = (next: MetricsDashboardVariable[]): void => {+ if (!dashboardEditing?.filters) {+ setDashboardEditing({ filters: true, layout: false }, DashboardEventSource.DashboardFilters)+ }+ setMetricVariables(next.length ? next : null)+ }+ const logic = dashboardMetricVariablesLogic({+ dashboardId: dashboard?.id ?? 'new',+ variables,+ dateFrom: effectiveEditBarFilters.date_from,+ dateTo: effectiveEditBarFilters.date_to,+ onSelectionsChange: change,+ })+ const { options, loadingNames, errors, pendingVariables } = useValues(logic)+ const { retry, select, stage } = useActions(logic)+ // Applied definitions stay staged while their options load, and the selects and the editor show them.+ const shown = pendingVariables ?? variables++ if (!dashboard) {+ return null+ }++ return (+ <div className="flex items-end gap-2 flex-wrap">+ {shown.map((variable) => (+ <MetricVariableSelect+ key={variable.name}+ variable={variable}+ options={ownValue(options, variable.name) ?? []}+ loading={loadingNames.includes(variable.name)}+ error={ownValue(errors, variable.name) ?? null}+ onRetry={() => retry(variable.name)}+ onChange={(current) => select(variable.name, current)}+ />+ ))}+ <LemonButton+ size="small"+ type="tertiary"+ icon={<IconGear />}+ tooltip={shown.length ? 'Edit variables' : undefined}+ onClick={() => setEditorOpen(true)}+ data-attr="dashboard-metric-variables-edit"+ >+ {shown.length ? null : 'Add variable'}+ </LemonButton>+ {editorOpen && (+ <MetricVariablesModal+ dashboardId={dashboard.id}+ variables={shown}+ options={options}+ dateFrom={effectiveEditBarFilters.date_from}+ dateTo={effectiveEditBarFilters.date_to}+ onClose={() => setEditorOpen(false)}+ onSave={(next) => {+ setEditorOpen(false)+ stage(next, variablesToReload(shown, next))+ }}+ />+ )}+ </div>+ )+}diff --git a/products/metrics/frontend/components/dashboardVariables/MetricVariableForm.tsx b/products/metrics/frontend/components/dashboardVariables/MetricVariableForm.tsxnew file mode 100644index 000000000000..c67807c4c8b3--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/MetricVariableForm.tsx@@ -0,0 +1,185 @@+import { useActions, useValues } from 'kea'++import { IconTrash } from '@posthog/icons'+import {+ LemonButton,+ LemonCheckbox,+ LemonInput,+ LemonInputSelect,+ LemonSegmentedButton,+ LemonTag,+ LemonTextArea,+ Spinner,+} from '@posthog/lemon-ui'++import { IconArrowDown, IconArrowUp } from 'lib/lemon-ui/icons'+import { LemonField } from 'lib/lemon-ui/LemonField'++import type { MetricsDashboardVariable, MetricsDashboardVariableSource } from '~/queries/schema/schema-general'++import { metricVariableEditorLogic } from './metricVariableEditorLogic'+import { ownValue } from './metricVariables'++const QUERY_PLACEHOLDERS: Record<'promql' | 'sql', string> = {+ promql: 'label_values(up{job=~"$job"}, instance)',+ sql: "SELECT DISTINCT attribute_value FROM posthog.metric_attributes WHERE attribute_key = 'http.route'",+}+const PREVIEW_LIMIT = 60++export function MetricVariableForm({ variable }: { variable: MetricsDashboardVariable }): JSX.Element {+ const { draft, selectedIndex, shownErrors, preview, previewError, previewLoading } =+ useValues(metricVariableEditorLogic)+ const { updateVariable, removeVariable, moveVariable } = useActions(metricVariableEditorLogic)+ const update = (patch: Partial<MetricsDashboardVariable>): void => updateVariable(selectedIndex, patch)+ const error = ownValue(shownErrors, variable.name)++ return (+ // Queries and values hold customer data, so copying them captures no text.+ <div className="ph-no-capture flex flex-col gap-4 flex-1 min-w-0">+ <div className="flex items-start gap-2">+ <h3 className="font-mono m-0 truncate flex-1 min-w-0">${variable.name}</h3>+ <LemonButton+ size="small"+ icon={<IconArrowUp />}+ tooltip="Move up"+ disabledReason={selectedIndex === 0 ? 'This is the first variable' : undefined}+ onClick={() => moveVariable(selectedIndex, -1)}+ />+ <LemonButton+ size="small"+ icon={<IconArrowDown />}+ tooltip="Move down"+ disabledReason={selectedIndex === draft.length - 1 ? 'This is the last variable' : undefined}+ onClick={() => moveVariable(selectedIndex, 1)}+ />+ <LemonButton+ size="small"+ status="danger"+ icon={<IconTrash />}+ tooltip="Remove variable"+ onClick={() => removeVariable(selectedIndex)}+ data-attr="dashboard-metric-variable-remove"+ />+ </div>+ <div className="grid grid-cols-2 gap-3">+ <LemonField.Pure label="Name" error={error?.field === 'name' ? error.message : undefined}>+ <LemonInput+ className="font-mono"+ value={variable.name}+ onChange={(name) => update({ name: name.trim() })}+ />+ </LemonField.Pure>+ <LemonField.Pure label="Label">+ <LemonInput+ value={variable.label ?? ''}+ placeholder={variable.name}+ onChange={(label) => update({ label: label || null })}+ />+ </LemonField.Pure>+ </div>+ <LemonField.Pure label="Values from">+ <LemonSegmentedButton<MetricsDashboardVariableSource>+ size="small"+ value={variable.source}+ onChange={(source) => update({ source })}+ options={[+ { value: 'custom', label: 'List' },+ { value: 'promql', label: 'PromQL query' },+ { value: 'sql', label: 'SQL query' },+ { value: 'textbox', label: 'Text input' },+ ]}+ />+ </LemonField.Pure>+ {variable.source === 'custom' && (+ <LemonField.Pure label="Values" error={error?.field === 'values' ? error.message : undefined}>+ <LemonInputSelect+ mode="multiple"+ allowCustomValues+ value={variable.customValues ?? []}+ onChange={(customValues) => update({ customValues })}+ placeholder="production, staging"+ />+ </LemonField.Pure>+ )}+ {(variable.source === 'promql' || variable.source === 'sql') && (+ <>+ <LemonField.Pure label="Query" error={error?.field === 'query' ? error.message : undefined}>+ <LemonTextArea+ className="font-mono text-xs"+ minRows={2}+ value={variable.query ?? ''}+ placeholder={QUERY_PLACEHOLDERS[variable.source]}+ onChange={(query) => update({ query })}+ />+ </LemonField.Pure>+ <LemonField.Pure+ label="Regex"+ showOptional+ error={error?.field === 'regex' ? error.message : undefined}+ >+ <LemonInput+ className="font-mono"+ value={variable.regex ?? ''}+ placeholder="/api-(.*)/"+ onChange={(regex) => update({ regex: regex || null })}+ />+ </LemonField.Pure>+ </>+ )}+ {variable.source !== 'textbox' && (+ <>+ <div className="flex gap-x-4 gap-y-2 flex-wrap items-center">+ <LemonCheckbox+ checked={!!variable.multi}+ onChange={(multi) => update({ multi: multi || null })}+ label="Allow more than one value"+ />+ <LemonCheckbox+ checked={!!variable.includeAll}+ onChange={(includeAll) => update({ includeAll: includeAll || null })}+ label="Include All"+ />+ {variable.includeAll && (+ <LemonInput+ size="small"+ className="font-mono w-48"+ value={variable.allValue ?? ''}+ placeholder=".*"+ onChange={(allValue) => update({ allValue: allValue || null })}+ />+ )}+ </div>+ {error?.field === 'allValue' && <p className="text-danger text-sm m-0">{error.message}</p>}+ {(variable.source === 'promql' || variable.source === 'sql') && (+ <div className="flex flex-col gap-1 border-t border-primary pt-3">+ <span className="flex items-center gap-2 text-sm font-semibold">+ Values preview+ {previewLoading && <Spinner textColored />}+ {preview && !previewLoading && (+ <span className="text-xs text-secondary font-normal">+ {preview.length > PREVIEW_LIMIT+ ? `First ${PREVIEW_LIMIT} of ${preview.length}`+ : `${preview.length} ${preview.length === 1 ? 'value' : 'values'}`}+ </span>+ )}+ </span>+ {previewError ? (+ <p className="text-danger text-sm m-0">{previewError}</p>+ ) : preview?.length ? (+ <div className="flex flex-wrap gap-1 max-h-28 overflow-y-auto">+ {preview.slice(0, PREVIEW_LIMIT).map((option) => (+ <LemonTag key={option} className="font-mono">+ {option}+ </LemonTag>+ ))}+ </div>+ ) : preview ? (+ <p className="text-secondary text-sm m-0">No values</p>+ ) : null}+ </div>+ )}+ </>+ )}+ </div>+ )+}diff --git a/products/metrics/frontend/components/dashboardVariables/MetricVariableSelect.tsx b/products/metrics/frontend/components/dashboardVariables/MetricVariableSelect.tsxnew file mode 100644index 000000000000..a8c369f4e963--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/MetricVariableSelect.tsx@@ -0,0 +1,160 @@+import { useEffect, useState } from 'react'++import { IconWarning } from '@posthog/icons'+import { LemonButton, LemonInput, LemonInputSelect } from '@posthog/lemon-ui'++import { LemonField } from 'lib/lemon-ui/LemonField'+import { objectsEqual } from 'lib/utils/objects'++import type { MetricsDashboardVariable, MetricsDashboardVariableSelection } from '~/queries/schema/schema-general'++import { ALL_OPTION, MAX_VALUE_LENGTH, selectionFromValues } from './metricVariables'++export function MetricVariableSelect({+ variable,+ options,+ loading,+ error,+ onChange,+ onRetry,+}: {+ variable: MetricsDashboardVariable+ options: string[]+ loading: boolean+ error: string | null+ onChange: (selection: MetricsDashboardVariableSelection | null) => void+ onRetry: () => void+}): JSX.Element {+ const label = (+ <span className="flex items-center gap-1">+ {variable.label || variable.name}+ {error && (+ <LemonButton+ size="xsmall"+ noPadding+ icon={<IconWarning className="text-warning" />}+ tooltip={<span className="ph-no-capture">{error}</span>}+ disabledReason={loading ? 'Loading values' : undefined}+ onClick={onRetry}+ data-attr="dashboard-metric-variable-retry"+ />+ )}+ </span>+ )++ return (+ // Values come from queries over customer data, so copying one captures no text.+ <div className="ph-no-capture" data-attr="dashboard-metric-variable">+ <LemonField.Pure label={label} className="gap-0">+ {variable.source === 'textbox' ? (+ <MetricVariableTextInput variable={variable} onChange={onChange} />+ ) : (+ <MetricVariableOptionsSelect+ variable={variable}+ options={options}+ loading={loading}+ error={error}+ onChange={onChange}+ onRetry={onRetry}+ />+ )}+ </LemonField.Pure>+ </div>+ )+}++function MetricVariableTextInput({+ variable,+ onChange,+}: {+ variable: MetricsDashboardVariable+ onChange: (selection: MetricsDashboardVariableSelection | null) => void+}): JSX.Element {+ const saved = variable.current?.values[0] ?? ''+ const [text, setText] = useState(saved)+ useEffect(() => setText(saved), [saved])+ // Each change refreshes every tile, so the value is applied when the user leaves the input or presses Enter.+ const commit = (): void => {+ if (text !== saved) {+ onChange(text ? { values: [text] } : null)+ }+ }++ return (+ <LemonInput+ size="small"+ className="w-40"+ value={text}+ placeholder="Enter a value"+ maxLength={MAX_VALUE_LENGTH}+ onChange={setText}+ onBlur={commit}+ onPressEnter={commit}+ />+ )+}++function MetricVariableOptionsSelect({+ variable,+ options,+ loading,+ error,+ onChange,+ onRetry,+}: {+ variable: MetricsDashboardVariable+ options: string[]+ loading: boolean+ error: string | null+ onChange: (selection: MetricsDashboardVariableSelection | null) => void+ onRetry: () => void+}): JSX.Element {+ const isAll = !!(variable.current?.all && variable.includeAll)+ const savedValues = isAll ? (variable.multi ? options : [ALL_OPTION]) : (variable.current?.values ?? [])+ // A multi-select keeps its values here while the list is open, so that each click does not refresh the tiles.+ const [pending, setPending] = useState<string[] | null>(null)+ // New options, such as after a parent changes, drop the picks that are no longer options.+ useEffect(() => setPending((current) => current && current.filter((value) => options.includes(value))), [options])+ const commit = (values: string[]): void => {+ setPending(null)+ const selection = selectionFromValues(variable, values, options)+ if (!objectsEqual(selection, variable.current ?? null)) {+ onChange(selection)+ }+ }++ return (+ <LemonInputSelect+ size="small"+ className="min-w-32 max-w-80"+ mode={variable.multi ? 'multiple' : 'single'}+ displayMode={variable.multi ? 'count' : undefined}+ bulkActions={variable.multi ? 'select-and-clear-all' : undefined}+ loading={loading}+ value={pending ?? savedValues}+ placeholder={loading ? 'Loading values' : 'Select a value'}+ options={[+ ...(variable.includeAll && !variable.multi ? [{ key: ALL_OPTION, label: 'All' }] : []),+ ...options.map((option) => ({ key: option, label: option })),+ ]}+ emptyStateComponent={+ // The list renders outside the select, so it needs its own exclusion from autocapture.+ <div className="ph-no-capture flex flex-col items-start gap-1 p-1 max-w-80">+ <p className="text-secondary italic m-0">{error ?? 'No values'}</p>+ {error && (+ <LemonButton+ size="small"+ type="secondary"+ disabledReason={loading ? 'Loading values' : undefined}+ onClick={onRetry}+ >+ Try again+ </LemonButton>+ )}+ </div>+ }+ onChange={(values) => (variable.multi ? setPending(values) : commit(values))}+ onBlur={() => pending && commit(pending)}+ />+ )+}diff --git a/products/metrics/frontend/components/dashboardVariables/MetricVariablesModal.tsx b/products/metrics/frontend/components/dashboardVariables/MetricVariablesModal.tsxnew file mode 100644index 000000000000..da36dc657abe--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/MetricVariablesModal.tsx@@ -0,0 +1,115 @@+import { BindLogic, useActions, useValues } from 'kea'++import { IconPlus, IconWarning } from '@posthog/icons'+import { LemonButton, LemonModal } from '@posthog/lemon-ui'++import type { MetricsDashboardVariable } from '~/queries/schema/schema-general'++import { metricVariableEditorLogic } from './metricVariableEditorLogic'+import { MetricVariableForm } from './MetricVariableForm'+import { ownValue } from './metricVariables'++const SOURCE_LABELS: Record<MetricsDashboardVariable['source'], string> = {+ custom: 'List',+ promql: 'PromQL',+ sql: 'SQL',+ textbox: 'Text input',+}++export function MetricVariablesModal({+ dashboardId,+ variables,+ options,+ dateFrom,+ dateTo,+ onClose,+ onSave,+}: {+ dashboardId: number | string+ variables: MetricsDashboardVariable[]+ options: Record<string, string[]>+ dateFrom?: string | null+ dateTo?: string | null+ onClose: () => void+ onSave: (variables: MetricsDashboardVariable[]) => void+}): JSX.Element {+ const logic = metricVariableEditorLogic({ dashboardId, variables, options, dateFrom, dateTo })+ const { applying, draft, selectedIndex, selectedVariable, shownErrors } = useValues(logic)+ const { addVariable, selectVariable, apply } = useActions(logic)++ return (+ <BindLogic logic={metricVariableEditorLogic} props={{ dashboardId, variables, options, dateFrom, dateTo }}>+ <LemonModal+ isOpen+ onClose={onClose}+ title="Metric variables"+ width={860}+ footer={+ <>+ <LemonButton type="secondary" onClick={onClose}>+ Cancel+ </LemonButton>+ <LemonButton+ type="primary"+ onClick={() => apply(onSave)}+ loading={applying}+ data-attr="dashboard-metric-variables-save"+ >+ Apply+ </LemonButton>+ </>+ }+ >+ {draft.length === 0 ? (+ <div className="flex items-center justify-center min-h-96">+ <LemonButton+ type="primary"+ icon={<IconPlus />}+ onClick={addVariable}+ data-attr="dashboard-metric-variable-add"+ >+ Add variable+ </LemonButton>+ </div>+ ) : (+ <div className="flex gap-4 min-h-96">+ <div className="flex flex-col gap-0.5 w-48 shrink-0 border-r border-primary pr-3">+ {draft.map((variable, index) => (+ <LemonButton+ key={index}+ size="small"+ fullWidth+ active={index === selectedIndex}+ onClick={() => selectVariable(index)}+ sideIcon={+ ownValue(shownErrors, variable.name) ? (+ <IconWarning className="text-warning" />+ ) : undefined+ }+ >+ <span className="flex flex-col items-start min-w-0">+ <span className="font-mono truncate max-w-full">${variable.name}</span>+ <span className="text-xs text-secondary font-normal">+ {SOURCE_LABELS[variable.source]}+ </span>+ </span>+ </LemonButton>+ ))}+ <LemonButton+ size="small"+ type="tertiary"+ icon={<IconPlus />}+ onClick={addVariable}+ className="mt-1"+ data-attr="dashboard-metric-variable-add"+ >+ Add variable+ </LemonButton>+ </div>+ {selectedVariable && <MetricVariableForm variable={selectedVariable} />}+ </div>+ )}+ </LemonModal>+ </BindLogic>+ )+}diff --git a/products/metrics/frontend/components/dashboardVariables/dashboardMetricVariablesLogic.test.ts b/products/metrics/frontend/components/dashboardVariables/dashboardMetricVariablesLogic.test.tsnew file mode 100644index 000000000000..fdccd093bfeb--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/dashboardMetricVariablesLogic.test.ts@@ -0,0 +1,331 @@+import { expectLogic } from 'kea-test-utils'++import type { MetricsDashboardVariable } from '~/queries/schema/schema-general'+import { initKeaTests } from '~/test/init'++import { metricsVariableOptionsCreate } from 'products/metrics/frontend/generated/api'++import { dashboardMetricVariablesLogic } from './dashboardMetricVariablesLogic'++jest.mock('products/metrics/frontend/generated/api', () => ({+ metricsVariableOptionsCreate: jest.fn(),+}))++const mockOptions = metricsVariableOptionsCreate as jest.MockedFunction<typeof metricsVariableOptionsCreate>++const variables: MetricsDashboardVariable[] = [+ { name: 'service', source: 'custom', customValues: ['api', 'worker'], current: { values: ['api'] } },+ { name: 'route', source: 'sql', query: 'SELECT route WHERE service = $service', current: { values: ['/a'] } },+]++const flush = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0))++function deferred(): { promise: Promise<{ results: string[] }>; resolve: (results: string[]) => void } {+ let resolve: (results: string[]) => void = () => {}+ const promise = new Promise<{ results: string[] }>((done) => {+ resolve = (results) => done({ results })+ })+ return { promise, resolve }+}++describe('dashboardMetricVariablesLogic', () => {+ beforeEach(() => {+ jest.clearAllMocks()+ initKeaTests()+ })++ it('keeps a selection the user makes while a background query finishes', async () => {+ const zone = deferred()+ mockOptions.mockImplementation(async (_team, body) => {+ if (body.query === 'SELECT zone') {+ return zone.promise+ }+ return { results: ['/a', '/b', '/c'] }+ })+ const withZone: MetricsDashboardVariable[] = [+ ...variables,+ { name: 'zone', source: 'sql', query: 'SELECT zone', current: { values: ['eu'] } },+ ]+ // The dashboard passes each published selection back in as props, as `DashboardMetricVariables` does.+ const onSelectionsChange = jest.fn((next: MetricsDashboardVariable[]) => {+ dashboardMetricVariablesLogic({ dashboardId: 1, variables: next, onSelectionsChange })+ })+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables: withZone, onSelectionsChange })+ logic.mount()+ await flush()+ logic.actions.select('service', { values: ['worker'] })+ await flush()+ logic.actions.select('route', { values: ['/c'] })+ await flush()++ zone.resolve(['us'])+ await expectLogic(logic).toFinishAllListeners()++ const last = onSelectionsChange.mock.calls.at(-1)?.[0] as MetricsDashboardVariable[]+ expect(last.map((variable) => variable.current)).toEqual([+ { values: ['worker'] },+ { values: ['/c'] },+ { values: ['us'] },+ ])+ })++ it('waits for a dependent that is still loading before it publishes another change', async () => {+ mockOptions.mockResolvedValue({ results: ['/a'] })+ const onSelectionsChange = jest.fn()+ const withText: MetricsDashboardVariable[] = [+ ...variables,+ { name: 'env', source: 'textbox', current: { values: ['prod'] } },+ ]+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables: withText, onSelectionsChange })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ const routes = deferred()+ mockOptions.mockReturnValue(routes.promise)+ logic.actions.select('service', { values: ['worker'] })+ await flush()+ logic.actions.select('env', { values: ['dev'] })+ await flush()+ expect(onSelectionsChange).not.toHaveBeenCalled()++ routes.resolve(['/b'])+ await expectLogic(logic).toFinishAllListeners()+ expect(onSelectionsChange).toHaveBeenCalledTimes(1)+ expect(+ onSelectionsChange.mock.calls[0][0].map((variable: MetricsDashboardVariable) => variable.current)+ ).toEqual([{ values: ['worker'] }, { values: ['/b'] }, { values: ['dev'] }])+ })++ it('loads a changed definition before it publishes it', async () => {+ mockOptions.mockResolvedValue({ results: ['/a'] })+ const onSelectionsChange = jest.fn()+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables, onSelectionsChange })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ mockOptions.mockResolvedValue({ results: ['/z'] })+ const redefined = [variables[0], { ...variables[1], query: 'SELECT other_route WHERE service = $service' }]+ logic.actions.stage(redefined, ['route'])+ await expectLogic(logic).toFinishAllListeners()++ expect(onSelectionsChange).toHaveBeenCalledTimes(1)+ expect(onSelectionsChange.mock.calls[0][0][1]).toEqual({ ...redefined[1], current: { values: ['/z'] } })+ })++ it('drops a pending selection when the dashboard discards it', async () => {+ mockOptions.mockResolvedValue({ results: ['/b'] })+ const onSelectionsChange = jest.fn((next: MetricsDashboardVariable[]) => {+ dashboardMetricVariablesLogic({ dashboardId: 1, variables: next, onSelectionsChange })+ })+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables, onSelectionsChange })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ logic.actions.select('service', { values: ['worker'] })+ await expectLogic(logic).toFinishAllListeners()++ const routes = deferred()+ mockOptions.mockReturnValue(routes.promise)+ logic.actions.select('service', { values: ['web'] })+ await flush()+ dashboardMetricVariablesLogic({ dashboardId: 1, variables, onSelectionsChange })+ routes.resolve(['/c'])+ await expectLogic(logic).toFinishAllListeners()++ const services = onSelectionsChange.mock.calls.map(([next]) => next[0].current?.values[0])+ expect(services).not.toContain('web')+ expect(logic.values.pendingVariables).toBeNull()+ })++ it('sends one request for variables with the same query and selections', async () => {+ mockOptions.mockReturnValue(new Promise(() => {}))+ const same: MetricsDashboardVariable[] = [+ { name: 'a', source: 'sql', query: 'SELECT 1' },+ { name: 'b', source: 'sql', query: 'SELECT 1' },+ ]+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables: same, onSelectionsChange: jest.fn() })+ logic.mount()+ await flush()++ expect(mockOptions).toHaveBeenCalledTimes(1)+ })++ it('sends no queued option request after the dashboard closes', async () => {+ const first = deferred()+ mockOptions.mockReturnValueOnce(first.promise).mockReturnValue(new Promise(() => {}))+ const many: MetricsDashboardVariable[] = Array.from({ length: 6 }, (_, index) => ({+ name: `v${index}`,+ source: 'sql',+ query: `SELECT ${index}`,+ }))+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables: many, onSelectionsChange: jest.fn() })+ logic.mount()+ await flush()+ logic.unmount()++ first.resolve(['a'])+ await flush()+ await flush()++ expect(mockOptions).toHaveBeenCalledTimes(4)+ })++ it('keeps loading options when the dashboard restores other selections', async () => {+ const zone = deferred()+ mockOptions.mockImplementation(async (_team, body) =>+ body.query === 'SELECT zone' ? zone.promise : { results: [] }+ )+ const withZone: MetricsDashboardVariable[] = [+ { name: 'env', source: 'textbox', current: { values: ['prod'] } },+ { name: 'zone', source: 'sql', query: 'SELECT zone' },+ ]+ const props = { dashboardId: 1, variables: withZone, onSelectionsChange: jest.fn() }+ const logic = dashboardMetricVariablesLogic(props)+ logic.mount()+ await flush()++ dashboardMetricVariablesLogic({+ ...props,+ variables: [{ ...withZone[0], current: { values: ['dev'] } }, withZone[1]],+ })+ await flush()+ zone.resolve(['eu'])+ await expectLogic(logic).toFinishAllListeners()++ expect(logic.values.options.zone).toEqual(['eu'])+ expect(logic.values.loadingNames).toEqual([])+ })++ it('publishes a reconciled selection after another change takes over the load', async () => {+ const zone = deferred()+ mockOptions.mockImplementation(async (_team, body) => {+ if (body.query === 'SELECT zone WHERE route = $route') {+ return zone.promise+ }+ return { results: body.variables?.some((other) => other.values.includes('worker')) ? ['/b'] : ['/a'] }+ })+ const chain: MetricsDashboardVariable[] = [+ ...variables,+ { name: 'zone', source: 'sql', query: 'SELECT zone WHERE route = $route', current: { values: ['eu'] } },+ { name: 'env', source: 'textbox', current: { values: ['prod'] } },+ ]+ const onSelectionsChange = jest.fn((next: MetricsDashboardVariable[]) => {+ dashboardMetricVariablesLogic({ dashboardId: 1, variables: next, onSelectionsChange })+ })+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables: chain, onSelectionsChange })+ logic.mount()+ zone.resolve(['eu'])+ await expectLogic(logic).toFinishAllListeners()++ const zoneAgain = deferred()+ mockOptions.mockImplementation(async (_team, body) => {+ if (body.query === 'SELECT zone WHERE route = $route') {+ return zoneAgain.promise+ }+ return { results: body.variables?.some((other) => other.values.includes('worker')) ? ['/b'] : ['/a'] }+ })+ logic.actions.select('service', { values: ['worker'] })+ await flush()+ logic.actions.select('env', { values: ['dev'] })+ await flush()+ zoneAgain.resolve(['eu'])+ await expectLogic(logic).toFinishAllListeners()++ const last = onSelectionsChange.mock.calls.at(-1)?.[0] as MetricsDashboardVariable[]+ expect(last.map((variable) => variable.current)).toEqual([+ { values: ['worker'] },+ { values: ['/b'] },+ { values: ['eu'] },+ { values: ['dev'] },+ ])+ })++ it('reloads the dependents of a staged definition when its parent changes', async () => {+ mockOptions.mockImplementation(async (_team, body) => ({+ results: body.variables?.some((other) => other.values.includes('worker')) ? ['/b'] : ['/a'],+ }))+ const independent: MetricsDashboardVariable[] = [+ variables[0],+ { name: 'route', source: 'sql', query: 'SELECT route', current: { values: ['/a'] } },+ ]+ const logic = dashboardMetricVariablesLogic({+ dashboardId: 1,+ variables: independent,+ onSelectionsChange: jest.fn(),+ })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ logic.actions.setPendingVariables(variables)+ mockOptions.mockClear()++ logic.actions.select('service', { values: ['worker'] })+ await expectLogic(logic).toFinishAllListeners()++ expect(mockOptions.mock.calls.map(([, body]) => body.query)).toEqual([variables[1].query])+ })++ it('runs at most four option requests at once', async () => {+ const many: MetricsDashboardVariable[] = Array.from({ length: 6 }, (_, index) => ({+ name: `v${index}`,+ source: 'sql',+ query: `SELECT ${index}`,+ }))+ mockOptions.mockReturnValue(new Promise(() => {}))+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables: many, onSelectionsChange: jest.fn() })+ logic.mount()+ await flush()++ expect(mockOptions).toHaveBeenCalledTimes(4)+ })++ it('publishes a parent selection once, with the selections of its dependents reconciled', async () => {+ mockOptions.mockResolvedValue({ results: ['/a'] })+ const onSelectionsChange = jest.fn()+ const logic = dashboardMetricVariablesLogic({ dashboardId: 1, variables, onSelectionsChange })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ mockOptions.mockResolvedValue({ results: ['/b'] })+ logic.actions.select('service', { values: ['worker'] })+ await expectLogic(logic).toFinishAllListeners()++ expect(onSelectionsChange).toHaveBeenCalledTimes(1)+ expect(+ onSelectionsChange.mock.calls[0][0].map((variable: MetricsDashboardVariable) => variable.current)+ ).toEqual([{ values: ['worker'] }, { values: ['/b'] }])+ })++ it('publishes a selection without waiting for an unrelated slow query, and does not send that query again', async () => {+ let finishZone: (value: { results: string[] }) => void = () => {}+ const zone = new Promise<{ results: string[] }>((resolve) => {+ finishZone = resolve+ })+ mockOptions.mockImplementation(async (_team, body) => {+ if (body.query?.includes('zone')) {+ return zone+ }+ return { results: body.variables?.some((other) => other.values.includes('worker')) ? ['/b'] : ['/a'] }+ })+ const onSelectionsChange = jest.fn()+ const zoneVariable: MetricsDashboardVariable = {+ name: 'zone',+ source: 'sql',+ query: 'SELECT zone',+ current: { values: ['eu'] },+ }+ const logic = dashboardMetricVariablesLogic({+ dashboardId: 1,+ variables: [...variables, zoneVariable],+ onSelectionsChange,+ })+ logic.mount()+ await new Promise((resolve) => setTimeout(resolve, 0))++ logic.actions.select('service', { values: ['worker'] })+ await new Promise((resolve) => setTimeout(resolve, 0))++ expect(onSelectionsChange).toHaveBeenCalledTimes(1)+ expect(mockOptions.mock.calls.filter(([, body]) => body.query === 'SELECT zone')).toHaveLength(1)+ finishZone({ results: ['eu'] })+ await expectLogic(logic).toFinishAllListeners()+ })+})diff --git a/products/metrics/frontend/components/dashboardVariables/dashboardMetricVariablesLogic.ts b/products/metrics/frontend/components/dashboardVariables/dashboardMetricVariablesLogic.tsnew file mode 100644index 000000000000..65e872361b64--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/dashboardMetricVariablesLogic.ts@@ -0,0 +1,367 @@+import {+ MakeLogicType,+ actions,+ afterMount,+ connect,+ kea,+ key,+ listeners,+ path,+ props,+ propsChanged,+ reducers,+} from 'kea'++import { objectsEqual } from 'lib/utils/objects'+import { teamLogic } from 'scenes/teamLogic'++import type { MetricsDashboardVariable, MetricsDashboardVariableSelection } from '~/queries/schema/schema-general'++import { metricsVariableOptionsCreate } from 'products/metrics/frontend/generated/api'++import {+ dependentsOf,+ orderVariables,+ reconcileSelection,+ selectionsForRequest,+ variableParents,+ variablesToReload,+} from './metricVariables'++export interface DashboardMetricVariablesLogicProps {+ dashboardId: number | string+ variables: MetricsDashboardVariable[]+ dateFrom?: string | null+ dateTo?: string | null+ /** Called when loaded options make a selection out of date. */+ onSelectionsChange: (variables: MetricsDashboardVariable[]) => void+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface dashboardMetricVariablesLogicValues {+ currentTeamId: number | null // teamLogic+ errors: Record<string, string>+ loadingNames: string[]+ options: Record<string, string[]>+ pendingVariables: MetricsDashboardVariable[] | null+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface dashboardMetricVariablesLogicActions {+ loadOptions: (+ names: string[] | null,+ reconcile: boolean+ ) => {+ names: string[] | null+ reconcile: boolean+ }+ retry: (name: string) => {+ name: string+ }+ select: (+ name: string,+ current: MetricsDashboardVariableSelection | null+ ) => {+ current: MetricsDashboardVariableSelection | null+ name: string+ }+ setError: (+ name: string,+ error: string | null+ ) => {+ error: string | null+ name: string+ }+ setLoading: (+ name: string,+ loading: boolean+ ) => {+ loading: boolean+ name: string+ }+ setOptions: (+ name: string,+ options: string[]+ ) => {+ name: string+ options: string[]+ }+ setPendingVariables: (variables: MetricsDashboardVariable[] | null) => {+ variables: MetricsDashboardVariable[] | null+ }+ stage: (+ variables: MetricsDashboardVariable[],+ names: string[]+ ) => {+ names: string[]+ variables: MetricsDashboardVariable[]+ }+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface dashboardMetricVariablesLogicMeta {+ key: number | string+}++export type dashboardMetricVariablesLogicType = MakeLogicType<+ dashboardMetricVariablesLogicValues,+ dashboardMetricVariablesLogicActions,+ DashboardMetricVariablesLogicProps,+ dashboardMetricVariablesLogicMeta+>++function errorMessage(error: unknown): string {+ if (error && typeof error === 'object' && 'detail' in error && typeof error.detail === 'string') {+ return error.detail+ }+ return error instanceof Error ? error.message : 'The options could not be loaded.'+}++const MAX_OPTION_REQUESTS = 4++async function acquireRequestSlot(cache: Record<string, any>): Promise<void> {+ while ((cache.runningRequests ?? 0) >= MAX_OPTION_REQUESTS) {+ await new Promise<void>((resolve) => (cache.queuedRequests ??= []).push(resolve))+ }+ cache.runningRequests = (cache.runningRequests ?? 0) + 1+}++function releaseRequestSlot(cache: Record<string, any>): void {+ cache.runningRequests -= 1+ cache.queuedRequests?.shift()?.()+}++export const dashboardMetricVariablesLogic = kea<dashboardMetricVariablesLogicType>([+ path((key) => ['products', 'metrics', 'frontend', 'components', 'dashboardMetricVariablesLogic', key]),+ props({} as DashboardMetricVariablesLogicProps),+ key((props) => props.dashboardId),+ connect(() => ({ values: [teamLogic, ['currentTeamId']] })),+ actions({+ loadOptions: (names: string[] | null, reconcile: boolean) => ({ names, reconcile }),+ setOptions: (name: string, options: string[]) => ({ name, options }),+ setError: (name: string, error: string | null) => ({ name, error }),+ setLoading: (name: string, loading: boolean) => ({ name, loading }),+ setPendingVariables: (variables: MetricsDashboardVariable[] | null) => ({ variables }),+ select: (name: string, current: MetricsDashboardVariableSelection | null) => ({ name, current }),+ stage: (variables: MetricsDashboardVariable[], names: string[]) => ({ variables, names }),+ retry: (name: string) => ({ name }),+ }),+ reducers({+ options: [+ {} as Record<string, string[]>,+ { setOptions: (state, { name, options }) => ({ ...state, [name]: options }) },+ ],+ errors: [+ {} as Record<string, string>,+ {+ setError: (state, { name, error }) => {+ const { [name]: _, ...rest } = state+ return error ? { ...rest, [name]: error } : rest+ },+ },+ ],+ loadingNames: [+ [] as string[],+ {+ setLoading: (state, { name, loading }) =>+ loading ? [...state.filter((other) => other !== name), name] : state.filter((n) => n !== name),+ },+ ],+ // Selections the dashboard does not have yet: their dependents still load, or the dashboard has not+ // rendered the published ones. The dashboard gets a change once its dependents reconcile, so tiles refresh once.+ pendingVariables: [+ null as MetricsDashboardVariable[] | null,+ { setPendingVariables: (_, { variables }) => variables },+ ],+ }),+ listeners(({ actions, values, props, cache }) => ({+ loadOptions: async ({ names, reconcile }, breakpoint) => {+ // Definitions come from the staged variables, so a change from the editor loads before tiles refresh.+ const start = values.pendingVariables ?? props.variables+ // A newer load cancels this one at its next breakpoint, so its unfinished names move to the newer load.+ const pending = new Set<string>([+ ...((cache.pending as Set<string> | undefined) ?? []),+ ...(names ?? start.map((variable) => variable.name)),+ ])+ const reconciling: Set<string> = (cache.reconciling ??= new Set())+ if (reconcile) {+ ;(names ?? start.map((variable) => variable.name)).forEach((name) => reconciling.add(name))+ }+ const shouldReconcile = reconcile || !!cache.pendingReconcile+ cache.pending = pending+ cache.pendingReconcile = shouldReconcile+ // A newer load also takes over the selections that this one reconciled but did not publish yet.+ const reconciled: Map<string, MetricsDashboardVariableSelection | null> = (cache.reconciled ??= new Map())+ let variables = start.map((variable) =>+ reconciled.has(variable.name) ? { ...variable, current: reconciled.get(variable.name) } : variable+ )++ // Each variable loads when its parents finish, and shows its options at once, so a slow+ // query delays only the variables that depend on it.+ const loads = new Map<string, Promise<void>>()+ const load = async (definition: MetricsDashboardVariable): Promise<void> => {+ const { name } = definition+ await Promise.all(variableParents(definition, start).map((parent) => loads.get(parent)))+ breakpoint()+ if (!pending.has(name) || definition.source === 'textbox') {+ reconciling.delete(name)+ return+ }+ // The parents may have changed their selections, which the request must use.+ const variable = variables.find((other) => other.name === name)!+ let options: string[]+ if (variable.source === 'custom') {+ options = [...new Set((variable.customValues ?? []).filter(Boolean))]+ actions.setLoading(name, false)+ } else {+ actions.setLoading(name, true)+ const body = {+ source: variable.source,+ query: variable.query ?? null,+ regex: variable.regex ?? null,+ variables: selectionsForRequest(variable, variables, values.options),+ // The server resolves the dashboard date filter as the tiles do.+ dateFrom: props.dateFrom ?? null,+ dateTo: props.dateTo ?? null,+ }+ // A cancelled load leaves its request running, so a newer load with the same inputs waits for it.+ const inflight: Map<string, ReturnType<typeof metricsVariableOptionsCreate>> = (cache.inflight ??=+ new Map())+ const key = JSON.stringify(body)+ let request = inflight.get(key)+ if (!request) {+ const teamId = String(values.currentTeamId)+ request = (async () => {+ // Option queries share the organization's query slots with the tiles, so few run at once.+ await acquireRequestSlot(cache)+ try {+ // A request that waited past the dashboard would take a query slot from the next page.+ if (cache.disposables.isDisposed) {+ throw new Error('The dashboard closed.')+ }+ return await metricsVariableOptionsCreate(teamId, body)+ } finally {+ releaseRequestSlot(cache)+ }+ })()+ inflight.set(key, request)+ void request.catch(() => undefined).finally(() => inflight.delete(key))+ }+ try {+ const response = await request+ breakpoint()+ options = response.results+ } catch (error) {+ breakpoint()+ actions.setLoading(name, false)+ pending.delete(name)+ reconciling.delete(name)+ // Keep the options and the selection: they are unknown now, not empty.+ actions.setError(name, errorMessage(error))+ return+ }+ actions.setLoading(name, false)+ }+ pending.delete(name)+ reconciling.delete(name)+ actions.setError(name, null)+ actions.setOptions(name, options)+ const current = reconcileSelection(variable, options)+ // Without reconcile, the dependents load for the saved selection, which the tiles use.+ if (shouldReconcile && !objectsEqual(current, variable.current ?? null)) {+ reconciled.set(name, current)+ variables = variables.map((other) => (other.name === name ? { ...other, current } : other))+ }+ }+ for (const name of orderVariables(start).order) {+ const loading = load(start.find((variable) => variable.name === name)!)+ // A newer load can cancel this listener before it waits for every load.+ void loading.catch(() => undefined)+ loads.set(name, loading)+ }+ let published = props.variables+ const publish = (): void => {+ // The first load only shows options. A saved selection changes only after the user changes something.+ if (!shouldReconcile) {+ return+ }+ // A selection the user changed during the load wins over what the load reconciled.+ const latest = values.pendingVariables ?? props.variables+ const next = latest.map((variable) => {+ const before = start.find((other) => other.name === variable.name)+ return reconciled.has(variable.name) &&+ objectsEqual(variable.current ?? null, before?.current ?? null)+ ? { ...variable, current: reconciled.get(variable.name) }+ : variable+ })+ if (!objectsEqual(next, published)) {+ published = next+ ;(cache.published ??= []).push(next)+ actions.setPendingVariables(next)+ props.onSelectionsChange(next)+ }+ }+ // A slow query that this load took over from an older one must not hold back the changed selections,+ // unless the older load was reconciling them too.+ const barrier = [...(names ?? [...loads.keys()]), ...[...pending].filter((name) => reconciling.has(name))]+ await Promise.allSettled(barrier.map((name) => loads.get(name)))+ breakpoint()+ publish()+ await Promise.allSettled(loads.values())+ breakpoint()+ cache.pending = undefined+ cache.pendingReconcile = false+ publish()+ cache.reconciled = undefined+ },+ select: ({ name, current }) => {+ const base = values.pendingVariables ?? props.variables+ const next = base.map((variable) => (variable.name === name ? { ...variable, current } : variable))+ // Staged definitions can differ from the dashboard's, and they decide what depends on the change.+ actions.stage(next, dependentsOf(base, [name]))+ },+ stage: ({ variables, names }) => {+ if (!names.length && !values.pendingVariables) {+ ;(cache.published ??= []).push(variables)+ props.onSelectionsChange(variables)+ return+ }+ actions.setPendingVariables(variables)+ actions.loadOptions(names, true)+ },+ retry: ({ name }) => {+ // The variables that depend on a failed variable kept options that its new values can change.+ actions.loadOptions([name, ...dependentsOf(values.pendingVariables ?? props.variables, [name])], true)+ },+ })),+ propsChanged(({ actions, values, props, cache }, oldProps) => {+ if (props.dateFrom !== oldProps.dateFrom || props.dateTo !== oldProps.dateTo) {+ actions.loadOptions(null, true)+ return+ }+ if (objectsEqual(props.variables, oldProps.variables)) {+ return+ }+ const published: MetricsDashboardVariable[][] = cache.published ?? []+ const ownUpdate = published.findIndex((variables) => objectsEqual(props.variables, variables))+ if (ownUpdate >= 0) {+ cache.published = published.slice(ownUpdate + 1)+ if (objectsEqual(props.variables, values.pendingVariables)) {+ actions.setPendingVariables(null)+ }+ return+ }+ // Settings from outside, such as Discard, replace the selections that are not published yet. The options that+ // are still loading stay in `cache.pending`, so the next load takes them over.+ cache.pendingReconcile = false+ cache.reconciling = undefined+ cache.reconciled = undefined+ actions.setPendingVariables(null)+ const names = variablesToReload(oldProps.variables, props.variables)+ // An empty load still cancels the older loads.+ actions.loadOptions(names, names.length > 0)+ }),+ afterMount(({ actions }) => {+ actions.loadOptions(null, false)+ }),+])diff --git a/products/metrics/frontend/components/dashboardVariables/metricVariableEditorLogic.test.ts b/products/metrics/frontend/components/dashboardVariables/metricVariableEditorLogic.test.tsnew file mode 100644index 000000000000..584cf7c9ce09--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/metricVariableEditorLogic.test.ts@@ -0,0 +1,166 @@+import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'++import { initKeaTests } from '~/test/init'++import { metricsVariableOptionsCreate } from 'products/metrics/frontend/generated/api'++import { metricVariableEditorLogic } from './metricVariableEditorLogic'++jest.mock('products/metrics/frontend/generated/api', () => ({+ metricsVariableOptionsCreate: jest.fn(),+}))++const mockOptions = metricsVariableOptionsCreate as jest.MockedFunction<typeof metricsVariableOptionsCreate>++describe('metricVariableEditorLogic', () => {+ beforeEach(() => {+ jest.clearAllMocks()+ initKeaTests()+ })++ it('previews a query edit after an edit to another field', async () => {+ mockOptions.mockResolvedValue({ results: ['a'] })+ const logic = metricVariableEditorLogic({+ dashboardId: 1,+ variables: [{ name: 'job', source: 'sql', query: 'SELECT job' }],+ options: {},+ })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ mockOptions.mockClear()++ logic.actions.updateVariable(0, { query: 'SELECT other' })+ logic.actions.updateVariable(0, { label: 'Job' })+ await expectLogic(logic).toFinishAllListeners()++ expect(mockOptions.mock.calls.map(([, body]) => body.query)).toEqual(['SELECT other'])+ })++ it('sends no preview for a query the editor rejects', async () => {+ mockOptions.mockResolvedValue({ results: ['a'] })+ const logic = metricVariableEditorLogic({+ dashboardId: 1,+ variables: [{ name: 'job', source: 'promql', query: 'up' }],+ options: {},+ })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ expect(mockOptions).not.toHaveBeenCalled()+ })++ it('previews a query that refers to another variable in the draft', async () => {+ mockOptions.mockResolvedValue({ results: ['a'] })+ const logic = metricVariableEditorLogic({+ dashboardId: 1,+ variables: [+ { name: 'job', source: 'sql', query: 'SELECT $service' },+ { name: 'service', source: 'custom', customValues: ['api'] },+ ],+ options: {},+ })+ logic.mount()+ await expectLogic(logic, () => logic.actions.loadPreview()).toFinishAllListeners()++ expect(mockOptions).toHaveBeenCalled()+ })++ it('shows a rejected preview without reporting it', async () => {+ mockOptions.mockRejectedValue({ status: 400, detail: 'Bad query [email protected]' })+ const captureException = jest.spyOn(posthog, 'captureException')+ const logic = metricVariableEditorLogic({+ dashboardId: 1,+ variables: [{ name: 'job', source: 'sql', query: 'SELECT job' }],+ options: {},+ })+ logic.mount()+ await expectLogic(logic, () => logic.actions.loadPreview()).toFinishAllListeners()++ expect(logic.values.previewError).toEqual('Bad query [email protected]')+ expect(captureException).not.toHaveBeenCalled()+ })++ it('applies only after the server accepts each regex', async () => {+ mockOptions.mockImplementation(async (_team, body) => {+ if (body.regex === '(') {+ throw { status: 400, detail: 'The variable regex cannot be used: missing )' }+ }+ return { results: [] }+ })+ const onSave = jest.fn()+ const logic = metricVariableEditorLogic({+ dashboardId: 1,+ variables: [{ name: 'job', source: 'sql', query: 'SELECT job', regex: '(' }],+ options: {},+ })+ logic.mount()++ await expectLogic(logic, () => logic.actions.apply(onSave)).toFinishAllListeners()+ expect(onSave).not.toHaveBeenCalled()+ expect(logic.values.errors.job?.message).toContain('regex cannot be used')++ logic.actions.updateVariable(0, { regex: 'a(.*)' })+ await expectLogic(logic, () => logic.actions.apply(onSave)).toFinishAllListeners()+ expect(onSave).toHaveBeenCalledTimes(1)+ })++ it('checks a regex again after a failed check, and saves nothing once the editor closes', async () => {+ mockOptions.mockRejectedValueOnce({ status: 429, detail: 'Too many requests' })+ mockOptions.mockResolvedValue({ results: [] })+ const onSave = jest.fn()+ const logic = metricVariableEditorLogic({+ dashboardId: 1,+ variables: [{ name: 'job', source: 'sql', query: 'SELECT job', regex: 'a(.*)' }],+ options: {},+ })+ logic.mount()++ await expectLogic(logic, () => logic.actions.apply(onSave)).toFinishAllListeners()+ expect(onSave).not.toHaveBeenCalled()+ await expectLogic(logic, () => logic.actions.apply(onSave)).toFinishAllListeners()+ expect(onSave).toHaveBeenCalledTimes(1)++ let pending: (value: { results: string[] }) => void = () => {}+ mockOptions.mockReturnValueOnce(new Promise((resolve) => (pending = resolve)))+ logic.actions.updateVariable(0, { regex: 'c(.*)' })+ logic.actions.apply(onSave)+ logic.actions.updateVariable(0, { regex: '(' })+ pending({ results: [] })+ await expectLogic(logic).toFinishAllListeners()+ expect(onSave).toHaveBeenCalledTimes(1)++ let finish: (value: { results: string[] }) => void = () => {}+ mockOptions.mockReturnValueOnce(new Promise((resolve) => (finish = resolve)))+ logic.actions.updateVariable(0, { regex: 'b(.*)' })+ logic.actions.apply(onSave)+ logic.unmount()+ finish({ results: [] })+ await new Promise((resolve) => setTimeout(resolve, 0))+ expect(onSave).toHaveBeenCalledTimes(1)+ })++ it('sends a preview only after the one before it finishes', async () => {+ let finish: (value: { results: string[] }) => void = () => {}+ mockOptions.mockReturnValueOnce(new Promise((resolve) => (finish = resolve)))+ mockOptions.mockResolvedValue({ results: ['b'] })+ const logic = metricVariableEditorLogic({+ dashboardId: 1,+ variables: [{ name: 'job', source: 'sql', query: 'SELECT job' }],+ options: {},+ })+ logic.mount()+ await new Promise((resolve) => setTimeout(resolve, 0))+ const sent = mockOptions.mock.calls.length++ logic.actions.loadPreview()+ logic.actions.loadPreview()+ await new Promise((resolve) => setTimeout(resolve, 0))+ expect(mockOptions).toHaveBeenCalledTimes(sent + 1)++ finish({ results: ['a'] })+ await expectLogic(logic).toFinishAllListeners()+ expect(mockOptions).toHaveBeenCalledTimes(sent + 2)+ expect(logic.values.preview).toEqual(['b'])+ })+})diff --git a/products/metrics/frontend/components/dashboardVariables/metricVariableEditorLogic.ts b/products/metrics/frontend/components/dashboardVariables/metricVariableEditorLogic.tsnew file mode 100644index 000000000000..44c424d8e730--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/metricVariableEditorLogic.ts@@ -0,0 +1,523 @@+import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'+import { loaders } from 'kea-loaders'++import { teamLogic } from 'scenes/teamLogic'++import type { MetricsDashboardVariable } from '~/queries/schema/schema-general'++import { metricsVariableOptionsCreate } from 'products/metrics/frontend/generated/api'++import {+ MAX_VALUE_LENGTH,+ VARIABLE_NAME,+ orderVariables,+ promqlReferencedNames,+ sqlReferencedNames,+ ownValue,+ reconcileSelection,+ selectionsForRequest,+} from './metricVariables'++export interface MetricVariableError {+ field: 'name' | 'values' | 'query' | 'regex' | 'allValue'+ message: string+}++export interface MetricVariableEditorLogicProps {+ dashboardId: number | string+ variables: MetricsDashboardVariable[]+ /** The options that the dashboard loaded, for the variables that the query refers to and that are set to All. */+ options: Record<string, string[]>+ /** The dashboard date filter, so the preview shows the values of the dashboard window. */+ dateFrom?: string | null+ dateTo?: string | null+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface metricVariableEditorLogicValues {+ currentTeamId: number | null // teamLogic+ applying: boolean+ draft: MetricsDashboardVariable[]+ errors: Record<string, MetricVariableError>+ preview: string[] | null+ previewError: string | null+ previewLoading: boolean+ regexErrors: Record<string, string>+ selectedIndex: number+ selectedVariable: MetricsDashboardVariable | null+ showAllErrors: boolean+ shownErrors: Record<string, MetricVariableError>+ touchedIndexes: number[]+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface metricVariableEditorLogicActions {+ addVariable: () => {+ value: true+ }+ apply: (onSave: (variables: MetricsDashboardVariable[]) => void) => {+ onSave: (variables: MetricsDashboardVariable[]) => void+ }+ clearRegexErrors: () => {+ value: true+ }+ loadPreview: (_: void) => void+ loadPreviewFailure: (+ error: string,+ errorObject?: any+ ) => {+ error: string+ errorObject?: any+ }+ loadPreviewSuccess: (+ preview: string[] | null,+ payload?: void+ ) => {+ preview: string[] | null+ payload?: void+ }+ moveVariable: (+ index: number,+ offset: 1 | -1+ ) => {+ index: number+ offset: 1 | -1+ }+ removeVariable: (index: number) => {+ index: number+ }+ schedulePreview: () => {+ value: true+ }+ selectVariable: (index: number) => {+ index: number+ }+ setApplying: (applying: boolean) => {+ applying: boolean+ }+ setPreviewError: (error: string | null) => {+ error: string | null+ }+ setRegexError: (+ regex: string,+ error: string | null+ ) => {+ error: string | null+ regex: string+ }+ updateVariable: (+ index: number,+ patch: Partial<MetricsDashboardVariable>+ ) => {+ index: number+ patch: Partial<MetricsDashboardVariable>+ }+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface metricVariableEditorLogicMeta {+ key: number | string+ __keaTypeGenInternalSelectorTypes: {+ selectedVariable: (draft: MetricsDashboardVariable[], selectedIndex: number) => MetricsDashboardVariable | null+ errors: (+ draft: MetricsDashboardVariable[],+ regexErrors: Record<string, string>+ ) => Record<string, MetricVariableError>+ shownErrors: (+ errors: Record<string, MetricVariableError>,+ draft: MetricsDashboardVariable[],+ touchedIndexes: number[],+ showAllErrors: boolean+ ) => Record<string, MetricVariableError>+ }+}++export type metricVariableEditorLogicType = MakeLogicType<+ metricVariableEditorLogicValues,+ metricVariableEditorLogicActions,+ MetricVariableEditorLogicProps,+ metricVariableEditorLogicMeta+>++function withSourceSettings(variable: MetricsDashboardVariable, previousSource: string): MetricsDashboardVariable {+ if (variable.source === previousSource || variable.source === 'promql' || variable.source === 'sql') {+ return variable+ }+ // A list and a text input have no query, so a regex left over from a query would only filter All on SQL tiles.+ if (variable.source === 'custom') {+ return { ...variable, regex: null }+ }+ // A text input has one value and no All, so a saved All would match everything with no way to clear it.+ const value = variable.current?.all ? undefined : variable.current?.values[0]+ return {+ ...variable,+ regex: null,+ multi: null,+ includeAll: null,+ allValue: null,+ current: value ? { values: [value] } : null,+ }+}++// The limits of the variable options API.+const MAX_NAME_LENGTH = 128+const MAX_QUERY_LENGTH = 10000+const MAX_CUSTOM_VALUES = 1000+// The forms that the options endpoint accepts for a PromQL query.+const PROMQL_OPTIONS_QUERY = /^\s*(label_values|label_names|metrics|query_result)\s*\(.*\)\s*$/s+// A change to one of these fields changes the values, so the preview loads again.+const PREVIEW_FIELDS = new Set(['source', 'query', 'regex', 'customValues'])++function newVariableName(variables: MetricsDashboardVariable[]): string {+ const names = new Set(variables.map((variable) => variable.name))+ let index = variables.length + 1+ while (names.has(`var${index}`)) {+ index+++ }+ return `var${index}`+}++function usesRegex(variable: MetricsDashboardVariable): boolean {+ return variable.source === 'promql' || variable.source === 'sql'+}++export function variableErrors(variables: MetricsDashboardVariable[]): Record<string, MetricVariableError> {+ // A null prototype, so a variable named `__proto__` gets its own entry.+ const errors: Record<string, MetricVariableError> = Object.create(null)+ const seen = new Set<string>()+ for (const variable of variables) {+ if (variable.name.length > MAX_NAME_LENGTH) {+ errors[variable.name] = { field: 'name', message: `Use at most ${MAX_NAME_LENGTH} characters.` }+ } else if (variable.name.startsWith('__')) {+ errors[variable.name] = {+ field: 'name',+ message: 'Names that start with two underscores are kept for built-in variables, such as $__interval.',+ }+ } else if (!VARIABLE_NAME.test(variable.name)) {+ errors[variable.name] = {+ field: 'name',+ message: 'Use letters, digits and underscores. Do not start with a digit.',+ }+ } else if (seen.has(variable.name)) {+ errors[variable.name] = { field: 'name', message: 'Another variable has this name.' }+ } else if ((variable.source === 'promql' || variable.source === 'sql') && !variable.query?.trim()) {+ errors[variable.name] = { field: 'query', message: 'Write a query that gives the values.' }+ } else if (variable.source === 'promql' && !PROMQL_OPTIONS_QUERY.test(variable.query ?? '')) {+ errors[variable.name] = {+ field: 'query',+ message: 'Use label_values(), label_names(), metrics() or query_result().',+ }+ } else if ((variable.query?.length ?? 0) > MAX_QUERY_LENGTH) {+ errors[variable.name] = { field: 'query', message: `Use at most ${MAX_QUERY_LENGTH} characters.` }+ } else if (+ (variable.source === 'promql' || variable.source === 'sql') &&+ (variable.regex?.length ?? 0) > MAX_VALUE_LENGTH+ ) {+ errors[variable.name] = { field: 'regex', message: `Use at most ${MAX_VALUE_LENGTH} characters.` }+ } else if ((variable.allValue?.length ?? 0) > MAX_VALUE_LENGTH) {+ errors[variable.name] = { field: 'allValue', message: `Use at most ${MAX_VALUE_LENGTH} characters.` }+ } else if (variable.source === 'custom' && !variable.customValues?.length) {+ errors[variable.name] = { field: 'values', message: 'Add at least one value.' }+ } else if ((variable.customValues?.length ?? 0) > MAX_CUSTOM_VALUES) {+ errors[variable.name] = { field: 'values', message: `Use at most ${MAX_CUSTOM_VALUES} values.` }+ } else if (variable.customValues?.some((value) => value.length > MAX_VALUE_LENGTH)) {+ errors[variable.name] = {+ field: 'values',+ message: `A value can have at most ${MAX_VALUE_LENGTH} characters.`,+ }+ }+ seen.add(variable.name)+ }+ const names = new Set(variables.map((variable) => variable.name))+ for (const variable of variables) {+ if (ownValue(errors, variable.name) || (variable.source !== 'promql' && variable.source !== 'sql')) {+ continue+ }+ // A label_replace replacement can use `$name` for a regex group, which the server leaves as it is.+ const referenced =+ variable.source === 'sql'+ ? sqlReferencedNames(variable.query)+ : promqlReferencedNames(variable.query, { replacements: false })+ const missing = referenced.find((name) => !names.has(name) && !name.startsWith('__') && !/^\d+$/.test(name))+ if (missing) {+ errors[variable.name] = { field: 'query', message: `$${missing} is not a variable on this dashboard.` }+ }+ }+ for (const name of orderVariables(variables).cycle) {+ if (ownValue(errors, name)) {+ continue+ }+ errors[name] = {+ field: 'query',+ message: 'The query refers to its own variable, or the variable queries refer to each other in a loop.',+ }+ }+ return errors+}++export const metricVariableEditorLogic = kea<metricVariableEditorLogicType>([+ path((key) => ['products', 'metrics', 'frontend', 'components', 'metricVariableEditorLogic', key]),+ props({} as MetricVariableEditorLogicProps),+ key((props) => props.dashboardId),+ connect(() => ({ values: [teamLogic, ['currentTeamId']] })),+ actions({+ addVariable: true,+ schedulePreview: true,+ setPreviewError: (error: string | null) => ({ error }),+ setRegexError: (regex: string, error: string | null) => ({ regex, error }),+ clearRegexErrors: true,+ setApplying: (applying: boolean) => ({ applying }),+ apply: (onSave: (variables: MetricsDashboardVariable[]) => void) => ({ onSave }),+ removeVariable: (index: number) => ({ index }),+ moveVariable: (index: number, offset: -1 | 1) => ({ index, offset }),+ updateVariable: (index: number, patch: Partial<MetricsDashboardVariable>) => ({ index, patch }),+ selectVariable: (index: number) => ({ index }),+ }),+ afterMount(({ actions }) => {+ actions.loadPreview()+ }),+ reducers(({ props }) => ({+ draft: [+ props.variables,+ {+ addVariable: (state) => [+ ...state,+ { name: newVariableName(state), source: 'custom', customValues: [] } as MetricsDashboardVariable,+ ],+ removeVariable: (state, { index }) => state.filter((_, other) => other !== index),+ moveVariable: (state, { index, offset }) => {+ const target = index + offset+ if (target < 0 || target >= state.length) {+ return state+ }+ const next = [...state]+ ;[next[index], next[target]] = [next[target], next[index]]+ return next+ },+ updateVariable: (state, { index, patch }) =>+ state.map((variable, other) =>+ other === index ? withSourceSettings({ ...variable, ...patch }, variable.source) : variable+ ),+ },+ ],+ selectedIndex: [+ 0,+ {+ selectVariable: (_, { index }) => index,+ removeVariable: (state, { index }) => (state >= index ? Math.max(state - 1, 0) : state),+ moveVariable: (state, { index, offset }) => (state === index ? state + offset : state),+ },+ ],+ preview: [+ null as string[] | null,+ {+ selectVariable: () => null,+ addVariable: () => null,+ removeVariable: () => null,+ },+ ],+ showAllErrors: [false, { apply: () => true }],+ // The server checks each regex, so its verdicts are kept by pattern.+ regexErrors: [+ {} as Record<string, string>,+ {+ setRegexError: (state, { regex, error }) => {+ const { [regex]: _, ...rest } = state+ return error ? { ...rest, [regex]: error } : rest+ },+ clearRegexErrors: () => ({}),+ },+ ],+ applying: [false, { setApplying: (_, { applying }) => applying }],+ touchedIndexes: [+ [] as number[],+ {+ updateVariable: (state, { index }) => (state.includes(index) ? state : [...state, index]),+ removeVariable: (state, { index }) =>+ state.filter((other) => other !== index).map((other) => (other > index ? other - 1 : other)),+ moveVariable: (state, { index, offset }) =>+ state.map((other) => (other === index ? index + offset : other === index + offset ? index : other)),+ },+ ],+ previewError: [+ null as string | null,+ {+ loadPreview: () => null,+ setPreviewError: (_, { error }) => error,+ loadPreviewFailure: (_, { error }) => error,+ selectVariable: () => null,+ removeVariable: () => null,+ },+ ],+ })),+ selectors({+ selectedVariable: [+ (s) => [s.draft, s.selectedIndex],+ (draft: MetricsDashboardVariable[], selectedIndex: number): MetricsDashboardVariable | null =>+ draft[selectedIndex] ?? null,+ ],+ errors: [+ (s) => [s.draft, s.regexErrors],+ (+ draft: MetricsDashboardVariable[],+ regexErrors: Record<string, string>+ ): Record<string, MetricVariableError> => {+ const errors = variableErrors(draft)+ for (const variable of draft) {+ const regexError = variable.regex ? ownValue(regexErrors, variable.regex) : undefined+ if (regexError && !ownValue(errors, variable.name) && usesRegex(variable)) {+ errors[variable.name] = { field: 'regex', message: regexError }+ }+ }+ return errors+ },+ ],+ shownErrors: [+ (s) => [s.errors, s.draft, s.touchedIndexes, s.showAllErrors],+ (+ errors: Record<string, MetricVariableError>,+ draft: MetricsDashboardVariable[],+ touchedIndexes: number[],+ showAllErrors: boolean+ ): Record<string, MetricVariableError> =>+ showAllErrors+ ? errors+ : Object.fromEntries(+ touchedIndexes+ .map((index: number) => draft[index]?.name)+ .filter((name: string | undefined): name is string => !!name && !!ownValue(errors, name))+ .map((name) => [name, errors[name]])+ ),+ ],+ }),+ listeners(({ actions, values, cache }) => ({+ addVariable: () => {+ actions.selectVariable(values.draft.length - 1)+ },+ selectVariable: () => {+ actions.loadPreview()+ },+ removeVariable: () => {+ actions.loadPreview()+ },+ apply: async ({ onSave }, breakpoint) => {+ // A failed check can be a timeout or a rate limit, so each Apply checks the patterns again.+ actions.clearRegexErrors()+ const checked = values.draft+ const firstError = (): number =>+ values.draft.findIndex((variable) => ownValue(values.errors, variable.name))+ if (firstError() === -1) {+ // The server checks a regex without running a query, so a pattern it rejects is never saved.+ const unchecked = new Set(+ values.draft+ .filter((variable) => usesRegex(variable) && variable.regex?.trim())+ .map((variable) => variable.regex!)+ .filter((regex) => !(cache.validRegexes as Set<string> | undefined)?.has(regex))+ )+ actions.setApplying(true)+ try {+ await Promise.all(+ [...unchecked].map(async (regex) => {+ try {+ await metricsVariableOptionsCreate(String(values.currentTeamId), {+ source: 'custom',+ customValues: [],+ regex,+ })+ ;(cache.validRegexes ??= new Set<string>()).add(regex)+ } catch (error) {+ const detail =+ error && typeof error === 'object' && 'detail' in error ? error.detail : null+ actions.setRegexError(+ regex,+ typeof detail === 'string' ? detail : 'The regex could not be checked. Try again.'+ )+ }+ })+ )+ } finally {+ actions.setApplying(false)+ }+ // Closing the editor or editing it while the check runs cancels the Apply.+ breakpoint()+ if (values.draft !== checked) {+ return+ }+ }+ const index = firstError()+ if (index === -1) {+ onSave(values.draft)+ } else {+ actions.selectVariable(index)+ }+ },+ updateVariable: ({ patch }) => {+ // A separate action waits, so an edit to another field does not cancel the pending preview.+ if (Object.keys(patch).some((field) => PREVIEW_FIELDS.has(field))) {+ actions.schedulePreview()+ }+ },+ schedulePreview: async (_, breakpoint) => {+ await breakpoint(600)+ actions.loadPreview()+ },+ })),+ loaders(({ actions, values, props, cache }) => ({+ preview: {+ loadPreview: async (_: void, breakpoint): Promise<string[] | null> => {+ // A cancelled fetch leaves its query running on the server, so a preview waits for the one before it.+ // A newer edit made during the wait cancels this preview.+ if (cache.previewRequest) {+ await cache.previewRequest.catch(() => undefined)+ breakpoint()+ }+ const variable = values.selectedVariable+ if (+ !variable ||+ variable.source === 'textbox' ||+ variable.source === 'custom' ||+ !variable.query?.trim() ||+ // The server would run the query before it rejects what the editor already rejects.+ ['query', 'regex'].includes(ownValue(values.errors, variable.name)?.field ?? '')+ ) {+ return null+ }+ // A parent added in this session has no selection yet, so the preview uses its first option.+ const draft = values.draft.map((other) =>+ other.current+ ? other+ : {+ ...other,+ current: reconcileSelection(+ other,+ other.source === 'custom'+ ? [...new Set((other.customValues ?? []).filter(Boolean))]+ : (ownValue(props.options, other.name) ?? [])+ ),+ }+ )+ const request = metricsVariableOptionsCreate(String(values.currentTeamId), {+ source: variable.source,+ query: variable.query ?? null,+ regex: variable.regex ?? null,+ variables: selectionsForRequest(variable, draft, props.options),+ dateFrom: props.dateFrom ?? null,+ dateTo: props.dateTo ?? null,+ })+ cache.previewRequest = request+ let response+ try {+ response = await request+ } catch (error) {+ breakpoint()+ // A rejected query echoes its text, which can hold customer data, so it is shown and not reported.+ const detail = error && typeof error === 'object' && 'detail' in error ? error.detail : null+ actions.setPreviewError(typeof detail === 'string' ? detail : 'The values could not be loaded.')+ return null+ }+ breakpoint()+ return response.results+ },+ },+ })),+])diff --git a/products/metrics/frontend/components/dashboardVariables/metricVariables.test.ts b/products/metrics/frontend/components/dashboardVariables/metricVariables.test.tsnew file mode 100644index 000000000000..230cd4ed3765--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/metricVariables.test.ts@@ -0,0 +1,142 @@+import type { MetricsDashboardVariable } from '~/queries/schema/schema-general'++import { variableErrors } from './metricVariableEditorLogic'+import {+ ALL_OPTION,+ dependentsOf,+ orderVariables,+ ownValue,+ reconcileSelection,+ selectionFromValues,+ selectionsForRequest,+ variablesToReload,+} from './metricVariables'++const custom = (name: string, values: string[]): MetricsDashboardVariable => ({+ name,+ source: 'custom',+ customValues: ['a', 'b'],+ current: { values },+})+const promql = (name: string, query: string): MetricsDashboardVariable => ({ name, source: 'promql', query })++const cluster = custom('cluster', ['a'])+const job = promql('job', 'label_values(up{cluster="$cluster"}, job)')+const instance = promql('instance', 'label_values(up{job=~"${job:regex}"}, instance)')++describe('metricVariables', () => {+ it('orders variables after the variables their queries refer to, and finds loops', () => {+ expect(orderVariables([instance, job, cluster])).toEqual({ order: ['cluster', 'job', 'instance'], cycle: [] })+ expect(orderVariables([promql('x', 'label_values(up{a="$y"}, a)'), promql('y', 'metrics($x)')]).cycle).toEqual([+ 'x',+ 'y',+ ])+ expect(orderVariables([promql('job', 'label_values(up{job="$job"}, job)')]).cycle).toEqual(['job'])+ const sqlWithComments: MetricsDashboardVariable = {+ name: 'env',+ source: 'sql',+ query: "SELECT 'prod' -- $env\n/* $env */ // $env\n# $env",+ }+ expect(orderVariables([sqlWithComments]).cycle).toEqual([])+ const escaped = [promql('env', 'label_values(up{a=~`\\$env`, b=~"\\\\$env"}, env) # $env')]+ expect(orderVariables(escaped).cycle).toEqual([])+ expect(orderVariables([promql('env', 'label_values(up{c="C:\\\\$env"}, env)')]).cycle).toEqual(['env'])+ const namedGroup = 'query_result(label_replace(up, "dst", "$env", "src", "(?P<env>.*)"))'+ expect(orderVariables([promql('env', namedGroup)]).cycle).toEqual([])+ const longQuery = `query_result(up{a="${'x'.repeat(8000)}"}${' + sum(up)'.repeat(20)})`+ const started = performance.now()+ orderVariables([promql('env', longQuery)])+ expect(performance.now() - started).toBeLessThan(200)+ expect(dependentsOf([cluster, promql('job', namedGroup.replace('$env', '$cluster'))], ['cluster'])).toEqual([+ 'job',+ ])+ const sqlWithCarriageReturn: MetricsDashboardVariable = {+ name: 'env',+ source: 'sql',+ query: 'SELECT 1 -- note\rWHERE env = $env',+ }+ expect(orderVariables([sqlWithCarriageReturn]).cycle).toEqual(['env'])+ })++ it.each([+ ['a SQL query gets the loaded options of All', 'sql', 'SELECT a WHERE zone IN $zone', ['a', 'b']],+ ['a PromQL query matches everything without them', 'promql', 'label_values(up{zone=~"$zone"}, a)', []],+ ])('%s, and only of the variables it refers to', (_name, source, text, values) => {+ const zone = { ...custom('zone', []), includeAll: true, current: { values: [], all: true } }+ const query = { name: 'q', source, query: text } as MetricsDashboardVariable+ expect(selectionsForRequest(query, [zone, cluster, query], { zone: ['a', 'b'] })).toEqual([+ { name: 'zone', values, all: true, allValue: null },+ ])+ })++ it.each([+ ['loaded options, which can be none', { zone: [] }, [{ name: 'zone', values: [], all: true, allValue: null }]],+ ['options that have not loaded', {}, []],+ ])('a SQL query sends All with %s', (_name, options, expected) => {+ const zone = { ...custom('zone', []), includeAll: true, current: { values: [], all: true } }+ const query: MetricsDashboardVariable = { name: 'q', source: 'sql', query: 'SELECT a WHERE zone IN $zone' }+ expect(selectionsForRequest(query, [zone, query], options)).toEqual(expected)+ })++ it.each([+ [+ 'a selection reloads the variables that depend on it',+ { ...cluster, current: { values: ['b'] } },+ ['job', 'instance'],+ ],+ [+ 'a new definition reloads the variable and its dependents',+ { ...cluster, customValues: ['c'] },+ ['cluster', 'job', 'instance'],+ ],+ ])('%s', (_name, changed, expected) => {+ expect(variablesToReload([cluster, job, instance], [changed, job, instance])).toEqual(expected)+ })++ it.each([+ ['keeps a value that is still an option', { values: ['b'] }, ['a', 'b'], { values: ['b'] }],+ ['replaces a value that is gone with the first option', { values: ['x'] }, ['a', 'b'], { values: ['a'] }],+ ['keeps All without values', { values: ['x'], all: true }, ['a', 'b'], { values: [], all: true }],+ ])('%s', (_name, current, options, expected) => {+ expect(reconcileSelection({ ...custom('v', []), includeAll: true, current }, options)).toEqual(expected)+ })++ it.each([+ ['picking every option means All', true, ['a', 'b'], { values: [], all: true }],+ ['picking the All option means All', false, [ALL_OPTION], { values: [], all: true }],+ ['picking some options keeps them', true, ['b'], { values: ['b'] }],+ ['picking nothing clears the selection', true, [], null],+ ])('%s', (_name, multi, values, expected) => {+ expect(selectionFromValues({ ...custom('v', []), includeAll: true, multi }, values, ['a', 'b'])).toEqual(+ expected+ )+ })++ it.each([+ ['a regex over the API limit', 'promql', 'a'.repeat(1025), 'regex'],+ ['a list, which does not use the regex', 'custom', 'a'.repeat(1025), undefined],+ ['a PromQL query that is not an options function', 'promql', undefined, 'query', 'up'],+ ['an options function in capitals', 'promql', undefined, 'query', 'LABEL_VALUES(job)'],+ ['a query that refers to a missing variable', 'promql', undefined, 'query', 'label_values(up{a="$gone"}, b)'],+ [+ 'a query with built-in and group references',+ 'promql',+ undefined,+ undefined,+ 'query_result(rate(up[$__range]))',+ ],+ ])('checks %s', (_name, source, regex, field, query = 'label_values(job)') => {+ const variable = { name: 'v', source, query, customValues: ['a'], regex }+ expect(ownValue(variableErrors([variable as MetricsDashboardVariable]), 'v')?.field).toEqual(field)+ })++ it.each(['__proto__', '__interval'])('rejects the reserved name %s', (name) => {+ const errors = variableErrors([{ name, source: 'promql', query: 'label_values(job)' }])+ expect(ownValue(errors, name)?.field).toEqual('name')+ })++ it('keeps an option that looks like the All value when the variable has no All', () => {+ const variable: MetricsDashboardVariable = { name: 'v', source: 'custom', customValues: ['$__all'] }+ expect(selectionFromValues(variable, ['$__all'], ['$__all'])).toEqual({ values: ['$__all'] })+ })+})diff --git a/products/metrics/frontend/components/dashboardVariables/metricVariables.ts b/products/metrics/frontend/components/dashboardVariables/metricVariables.tsnew file mode 100644index 000000000000..c41eb0343a4d--- /dev/null+++ b/products/metrics/frontend/components/dashboardVariables/metricVariables.ts@@ -0,0 +1,310 @@+import { objectsEqual } from 'lib/utils/objects'++import type { MetricsDashboardVariable, MetricsDashboardVariableSelection } from '~/queries/schema/schema-general'++import type { _MetricVariableSelectionApi } from 'products/metrics/frontend/generated/api.schemas'++/** The select value that stands for All. A query can return any text, so it is a character that options do not hold. */+export const ALL_OPTION = '\u0000all'+export const VARIABLE_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/+/** The API accepts selected values up to this length. */+export const MAX_VALUE_LENGTH = 1024+const REFERENCE =+ /\$\{([A-Za-z0-9_]+)(?::[A-Za-z0-9_]+)?\}|\[\[([A-Za-z0-9_]+)(?::[A-Za-z0-9_]+)?\]\]|\$([A-Za-z0-9_]+)/g++export function referencedNames(text: string | null | undefined): string[] {+ const names = new Set<string>()+ for (const match of (text ?? '').matchAll(REFERENCE)) {+ names.add(match[1] ?? match[2] ?? match[3])+ }+ return [...names]+}++function previousNonSpace(text: string, index: number): number {+ let before = index - 1+ while (before >= 0 && /\s/.test(text[before])) {+ before--+ }+ return before+}++function functionNameBefore(text: string, index: number): string {+ const end = previousNonSpace(text, index) + 1+ let start = end+ while (start > 0 && /[A-Za-z0-9_:]/.test(text[start - 1])) {+ start--+ }+ return text.slice(start, end).toLowerCase()+}++/** Follows the backend: a `#` comment holds no references, a regex-escaped `$` in a regex is a literal, and+ * `replacements: false` leaves out the replacement argument of label_replace, where `$name` can be a regex group. */+export function promqlReferencedNames(+ query: string | null | undefined,+ { replacements = true }: { replacements?: boolean } = {}+): string[] {+ const text = query ?? ''+ // The quote of the string that holds each position, or '#' inside a comment.+ const contexts: (string | null)[] = []+ // The positions inside a regex string, such as `=~"..."` or the regex argument of label_replace.+ const inRegex: boolean[] = []+ const inReplacement: boolean[] = []+ // The open brackets, each with the function name before a parenthesis and the argument index.+ const groups: { bracket: string; name: string; argument: number }[] = []+ let quote: string | null = null+ let regex = false+ let replacement = false+ for (let index = 0; index < text.length; index++) {+ const char = text[index]+ contexts[index] = quote+ inRegex[index] = regex+ inReplacement[index] = replacement+ if (quote === '#') {+ quote = char === '\n' || char === '\r' ? null : quote+ } else if (quote) {+ if (char === '\\' && quote !== '`') {+ contexts[index + 1] = quote+ inRegex[index + 1] = regex+ inReplacement[index + 1] = replacement+ index+++ } else if (char === quote) {+ quote = null+ }+ } else if (char === '(' || char === '{') {+ const name = char === '(' ? functionNameBefore(text, index) : ''+ groups.push({ bracket: char, name, argument: 0 })+ } else if (char === ')' || char === '}') {+ if (groups.at(-1)?.bracket === (char === ')' ? '(' : '{')) {+ groups.pop()+ }+ } else if (char === ',') {+ const group = groups.at(-1)+ if (group) {+ group.argument+++ }+ } else if (char === '#' || char === '"' || char === "'" || char === '`') {+ quote = char+ contexts[index] = char === '#' ? '#' : null+ const call = groups.at(-1)+ const labelReplace = call?.bracket === '(' && call.name === 'label_replace'+ const before = previousNonSpace(text, index)+ const afterMatcher =+ before > 0 && text[before] === '~' && (text[before - 1] === '=' || text[before - 1] === '!')+ regex = char !== '#' && (afterMatcher || (labelReplace && call.argument === 4))+ replacement = char !== '#' && labelReplace && call.argument === 2+ }+ }+ const names = new Set<string>()+ for (const match of text.matchAll(REFERENCE)) {+ const context = contexts[match.index] ?? null+ if (context === '#' || (!replacements && context && inReplacement[match.index])) {+ continue+ }+ if (context && inRegex[match.index]) {+ let backslashes = 0+ while (text[match.index - backslashes - 1] === '\\') {+ backslashes+++ }+ if ((context === '`' ? backslashes : Math.floor(backslashes / 2)) % 2 === 1) {+ continue+ }+ }+ names.add(match[1] ?? match[2] ?? match[3])+ }+ return [...names]+}++const SQL_REFERENCE = /\$\{([A-Za-z0-9_]+)(?::[A-Za-z0-9_]+)?\}|\$([A-Za-z0-9_]+)/y++/** Follows the backend: comments and quoted identifiers hold no references, and `[[x]]` is a nested array. */+export function sqlReferencedNames(sql: string | null | undefined): string[] {+ const text = sql ?? ''+ const names = new Set<string>()+ let index = 0+ while (index < text.length) {+ const char = text[index]+ const lineComment =+ text.startsWith('--', index) ||+ text.startsWith('//', index) ||+ (char === '#' && !/[0-9]/.test(text[index + 1] ?? ''))+ if (lineComment) {+ const end = text.slice(index).search(/[\r\n]/)+ index = end < 0 ? text.length : index + end+ } else if (text.startsWith('/*', index)) {+ const end = text.indexOf('*/', index + 2)+ index = end < 0 ? text.length : end + 2+ } else if (char === "'" || char === '"' || char === '`') {+ let end = index + 1+ while (end < text.length && text[end] !== char) {+ end += text[end] === '\\' ? 2 : 1+ }+ if (char === "'") {+ referencedNames(text.slice(index + 1, end)).forEach((name) => names.add(name))+ }+ index = end + 1+ } else {+ SQL_REFERENCE.lastIndex = index+ const match = SQL_REFERENCE.exec(text)+ const previous = text[index - 1] ?? ''+ if (match && !/[A-Za-z0-9_.$]/.test(previous)) {+ names.add(match[1] ?? match[2])+ index = SQL_REFERENCE.lastIndex+ } else {+ index += 1+ }+ }+ }+ return [...names].filter((name) => !name.startsWith('__') && !/^[0-9]+$/.test(name))+}++/** Includes the variable itself when its query refers to it, so that `orderVariables` reports the loop. */+export function variableParents(variable: MetricsDashboardVariable, variables: MetricsDashboardVariable[]): string[] {+ if (variable.source !== 'promql' && variable.source !== 'sql') {+ return []+ }+ const names = new Set(variables.map((other) => other.name))+ if (variable.source === 'sql') {+ return sqlReferencedNames(variable.query).filter((name) => names.has(name))+ }+ // The query of a variable gets no value for the variable itself, so in a replacement `$name` stays a regex group.+ const own = promqlReferencedNames(variable.query, { replacements: false }).includes(variable.name)+ return promqlReferencedNames(variable.query).filter((name) => names.has(name) && (name !== variable.name || own))+}++export function orderVariables(variables: MetricsDashboardVariable[]): { order: string[]; cycle: string[] } {+ const parents = new Map(variables.map((variable) => [variable.name, variableParents(variable, variables)]))+ const order: string[] = []+ const placed = new Set<string>()+ let progress = true+ while (progress) {+ progress = false+ for (const variable of variables) {+ if (!placed.has(variable.name) && parents.get(variable.name)!.every((parent) => placed.has(parent))) {+ order.push(variable.name)+ placed.add(variable.name)+ progress = true+ }+ }+ }+ return { order, cycle: variables.map((variable) => variable.name).filter((name) => !placed.has(name)) }+}++export function dependentsOf(variables: MetricsDashboardVariable[], names: string[]): string[] {+ // Each query is read once, so a long chain of variables does not read the same queries again for each step.+ const children = new Map<string, string[]>()+ for (const variable of variables) {+ for (const parent of variableParents(variable, variables)) {+ children.set(parent, [...(children.get(parent) ?? []), variable.name])+ }+ }+ const found = new Set<string>()+ const queue = [...names]+ while (queue.length) {+ for (const child of children.get(queue.shift()!) ?? []) {+ if (!found.has(child)) {+ found.add(child)+ queue.push(child)+ }+ }+ }+ return [...found]+}++/** The variables to load again after a change: changed definitions, and everything that depends on a change. */+export function variablesToReload(previous: MetricsDashboardVariable[], next: MetricsDashboardVariable[]): string[] {+ const before = new Map(previous.map((variable) => [variable.name, variable]))+ const redefined: string[] = []+ const changed: string[] = []+ for (const variable of next) {+ const old = before.get(variable.name)+ const { current, ...definition } = variable+ if (!old) {+ redefined.push(variable.name)+ continue+ }+ const { current: oldCurrent, ...oldDefinition } = old+ if (!objectsEqual(definition, oldDefinition)) {+ redefined.push(variable.name)+ } else if (!objectsEqual(current ?? null, oldCurrent ?? null)) {+ changed.push(variable.name)+ }+ }+ return [...new Set([...redefined, ...dependentsOf(next, [...redefined, ...changed])])]+}++/** All keeps no values, so that a high-cardinality variable does not make the dashboard URL too long. */+export function reconcileSelection(+ variable: MetricsDashboardVariable,+ options: string[]+): MetricsDashboardVariableSelection | null {+ if (variable.source === 'textbox') {+ return variable.current ?? null+ }+ if (variable.current?.all && variable.includeAll) {+ return { values: [], all: true }+ }+ const kept = (variable.current?.values ?? []).filter((value) => options.includes(value))+ if (kept.length) {+ return { values: variable.multi ? kept : kept.slice(0, 1) }+ }+ return options.length ? { values: [options[0]] } : null+}++export function selectionFromValues(+ variable: MetricsDashboardVariable,+ values: string[],+ options: string[]+): MetricsDashboardVariableSelection | null {+ if (variable.includeAll) {+ const everyOption = variable.multi && options.length > 0 && options.every((option) => values.includes(option))+ if (values.includes(ALL_OPTION) || everyOption) {+ return { values: [], all: true }+ }+ }+ const picked = variable.includeAll ? values.filter((value) => value !== ALL_OPTION) : values+ return picked.length ? { values: picked } : null+}++/** The value that the record holds for the key itself, so a variable named `constructor` does not read `Object.prototype`. */+export function ownValue<T>(record: Record<string, T>, key: string): T | undefined {+ return Object.prototype.hasOwnProperty.call(record, key) ? record[key] : undefined+}++/** The selections that the options request of `variable` sends: only of the variables its query refers to.+ * All sends the loaded options to a SQL query, which needs them. PromQL matches everything without them. */+export function selectionsForRequest(+ variable: MetricsDashboardVariable,+ variables: MetricsDashboardVariable[],+ options: Record<string, string[]>+): _MetricVariableSelectionApi[] {+ const parents = new Set(variableParents(variable, variables))+ parents.delete(variable.name)+ return (+ variables+ .filter((other) => parents.has(other.name))+ .filter((other) => other.current && (other.current.values.length || other.current.all))+ // The server reads All as the options sent, so a SQL query waits until they load.+ .filter(+ (other) =>+ !(+ variable.source === 'sql' &&+ other.current!.all &&+ other.includeAll &&+ !ownValue(options, other.name)+ )+ )+ .map((other) => {+ const all = !!(other.current!.all && other.includeAll)+ return {+ name: other.name,+ values: all+ ? variable.source === 'sql'+ ? (ownValue(options, other.name) ?? [])+ : []+ : other.current!.values,+ all,+ allValue: other.allValue ?? null,+ }+ })+ )+}diff --git a/products/metrics/frontend/queryLanguages/promqlParser.ts b/products/metrics/frontend/queryLanguages/promqlParser.tsindex d8423fa0e019..0c8b64ca9cc9 100644--- a/products/metrics/frontend/queryLanguages/promqlParser.ts+++ b/products/metrics/frontend/queryLanguages/promqlParser.ts@@ -206,10 +206,11 @@ function tokenize(input: string): Token[] {i = end + 1continue}- const variable = /^\$\{?(__rate_interval|__interval|__range)\}?/.exec(rest)+ const variable = /^\$(?:\{([A-Za-z0-9_]+)(?::[A-Za-z0-9_]+)?\}|([A-Za-z0-9_]+))/.exec(rest)if (variable) {- // Grafana fills these in; they read as "the step", which is what an omitted range means.- tokens.push({ kind: 'duration', value: `$${variable[1]}`, pos: i })+ // `$__rate_interval` and the other built-ins read as "the step", which is what an omitted range+ // means. A dashboard variable gets its value when the query runs.+ tokens.push({ kind: 'duration', value: `$${variable[1] ?? variable[2]}`, pos: i })i += variable[0].lengthcontinue}@@ -463,6 +464,10 @@ class Parser {return { type: 'number', value: Number(token.value) }}if (token.kind === 'duration') {+ if (token.value.startsWith('$')) {+ // A dashboard variable gets its value when the query runs, so the builder cannot hold it as a number.+ throw new PromQLParseError(`The variable ${token.value} cannot be used as a number`, token.pos)+ }// Durations without units are numbers; with units they are seconds, as in PromQL arithmetic.this.next()return { type: 'number', value: durationToSeconds(token.value) }diff --git a/products/metrics/frontend/queryLanguages/queryLanguages.test.ts b/products/metrics/frontend/queryLanguages/queryLanguages.test.tsindex 8f52e6c568f3..a998cc64a189 100644--- a/products/metrics/frontend/queryLanguages/queryLanguages.test.ts+++ b/products/metrics/frontend/queryLanguages/queryLanguages.test.ts@@ -350,6 +350,19 @@ const PROMQL_CASES: [string, BuilderQuery | null, 'lossless' | 'lossy'][] = [{ clauses: [clause({ metricName: 'http_requests_total', aggregation: 'rate', groupBy: [{ key: 'job' }] })] },'lossless',],+ [+ 'sum(rate(http_requests_total{job=~"$job"}[${window}]))',+ {+ clauses: [+ clause({+ metricName: 'http_requests_total',+ aggregation: 'rate',+ filters: [{ key: 'job', op: 'regex', value: '^$job$' }],+ }),+ ],+ },+ 'lossy',+ ],['sum(increase(jobs_total[1h]))',{ clauses: [clause({ metricName: 'jobs_total', aggregation: 'increase' })] },@@ -683,7 +696,7 @@ describe('metrics query languages', () => {expect(parsePromQL(printPromQL(parsed))).toEqual(parsed)})- it.each(['sum(', 'x{a=b}', 'x{a="b}', 'sum by (a (x)', '}', 'x[5m', '', 'sum(x) @ end()'])(+ it.each(['sum(', 'x{a=b}', 'x{a="b}', 'sum by (a (x)', '}', 'x[5m', '', 'sum(x) @ end()', 'sum(up) / $scale2'])('rejects %j with a position',(text) => {expect(() => parsePromQL(text)).toThrow(PromQLParseError)