: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
fix(metrics): show metric variables in the dashboard review preview (#120431)
Frank Hamand48090dd74b11
diff --git a/frontend/src/scenes/dashboard/DashboardFilters.test.tsx b/frontend/src/scenes/dashboard/DashboardFilters.test.tsxnew file mode 100644index 000000000000..306282f8dddb--- /dev/null+++ b/frontend/src/scenes/dashboard/DashboardFilters.test.tsx@@ -0,0 +1,66 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import { BindLogic } from 'kea'++import { FEATURE_FLAGS } from 'lib/constants'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'++import { initKeaTests } from '~/test/init'+import { AccessControlLevel, DashboardPlacement, DashboardType } from '~/types'++import { DashboardFilterBar } from './DashboardFilters'+import { dashboardLogic } from './dashboardLogic'++const FILTERS = {+ metricVariables: [{ name: 'service', label: 'Service', source: 'custom', customValues: ['api', 'web'] }],+}++const DASHBOARD = {+ id: 7,+ name: 'HTTP server',+ pinned: false,+ tiles: [],+ tags: [],+ created_at: '2020-01-01T00:00:00Z',+ created_by: null,+ last_accessed_at: '2020-01-01T00:00:00Z',+ is_shared: false,+ deleted: false,+ creation_mode: 'default',+ user_access_level: AccessControlLevel.Viewer,+ filters: FILTERS,+ persisted_filters: FILTERS,+ variables: {},+} as unknown as DashboardType++describe('DashboardFilterBar', () => {+ beforeEach(() => {+ initKeaTests()+ featureFlagLogic.mount()+ featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.METRICS_DASHBOARD_CONTROLS], {+ [FEATURE_FLAGS.METRICS_DASHBOARD_CONTROLS]: true,+ })+ })++ afterEach(() => {+ cleanup()+ })++ it.each([+ [DashboardPlacement.Dashboard, true],+ [DashboardPlacement.Builtin, false],+ ])('shows the metric variables in the %s placement, with the editor %s', (placement, editable) => {+ const props = { id: DASHBOARD.id, dashboard: DASHBOARD, placement }+ dashboardLogic(props).mount()++ render(+ <BindLogic logic={dashboardLogic} props={props}>+ <DashboardFilterBar />+ </BindLogic>+ )++ expect(screen.getByText('Service')).toBeInTheDocument()+ expect(!!document.querySelector('[data-attr="dashboard-metric-variables-edit"]')).toBe(editable)+ })+})diff --git a/frontend/src/scenes/dashboard/DashboardFilters.tsx b/frontend/src/scenes/dashboard/DashboardFilters.tsxindex 8866a5ae766f..f7e7948d488b 100644--- a/frontend/src/scenes/dashboard/DashboardFilters.tsx+++ b/frontend/src/scenes/dashboard/DashboardFilters.tsx@@ -4,11 +4,13 @@ import { useValues } from 'kea'import { IconEllipsis } from '@posthog/icons'import { LemonButton, LemonMenu } from '@posthog/lemon-ui'+import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'import { urls } from 'scenes/urls'import { DashboardPlacement } from '~/types'import { DashboardUnsavedChangesIndicator } from 'products/dashboards/frontend/dashboardSettings/DashboardUnsavedChangesIndicator'+import { DashboardMetricVariables } from 'products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables'import { DashboardEditBar } from './DashboardEditBar'import { dashboardLogic } from './dashboardLogic'@@ -19,7 +21,8 @@ interface DashboardFilterBarProps {}export function DashboardFilterBar({ backTo }: DashboardFilterBarProps): JSX.Element {- const { placement, dashboard, hasVariables } = useValues(dashboardLogic)+ const { placement, dashboard, hasVariables, effectiveEditBarFilters } = useValues(dashboardLogic)+ const controlsEnabled = useFeatureFlag('METRICS_DASHBOARD_CONTROLS')return (<div className="@container/dashboard-filters flex min-w-0 flex-1 flex-col gap-2"><div className="flex flex-wrap gap-x-2 gap-y-2 justify-between items-start">@@ -34,6 +37,11 @@ export function DashboardFilterBar({ backTo }: DashboardFilterBarProps): JSX.EleDashboardPlacement.Builtin,].includes(placement) &&dashboard && <DashboardEditBar />}+ {placement === DashboardPlacement.Builtin &&+ controlsEnabled &&+ !!effectiveEditBarFilters.metricVariables?.length && (+ <DashboardMetricVariables editable={false} />+ )}{placement === DashboardPlacement.Dashboard && <DashboardUnsavedChangesIndicator />}</div></div>diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.tsindex 36d974100fce..b263f1d8de10 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts@@ -19,7 +19,7 @@ import { DashboardEventSource, eventUsageLogic } from 'lib/utils/eventUsageLogicimport { addInsightToDashboardLogic } from 'scenes/dashboard/addInsightToDashboardModalLogic'import { parseDashboardId } from 'scenes/dashboard/Dashboard'import { dashboardInsightColorsModalLogic } from 'scenes/dashboard/dashboardInsightColorsModalLogic'-import { DashboardLoadAction, dashboardLogic } from 'scenes/dashboard/dashboardLogic'+import { DashboardLoadAction, dashboardLogic, RefreshDashboardItemsAction } from 'scenes/dashboard/dashboardLogic'import * as dashboardUtils from 'scenes/dashboard/dashboardUtils'import { liveDashboardIds } from 'scenes/dashboard/liveDashboardIds'import * as widgetFetchUtils from 'scenes/dashboard/widgetFetchUtils'@@ -1059,6 +1059,28 @@ describe('dashboardLogic', () => {}})+ it('refreshes a large builtin dashboard on a variable change, since it has no Preview button', async () => {+ const autoPreviewLimit = jest.replaceProperty(dashboardUtils, 'AUTO_PREVIEW_TILE_LIMIT', 0)+ logic.unmount()+ logic = dashboardLogic({ id: 5, dashboard: dashboards[5], placement: DashboardPlacement.Builtin })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ try {+ await expectLogic(logic, () => {+ logic.actions.setMetricVariables([{ name: 'env', source: 'custom', current: { values: ['a'] } }])+ }).toDispatchActions([+ logic.actionCreators.refreshDashboardItems({+ action: RefreshDashboardItemsAction.Preview,+ forceRefresh: false,+ previewUnsavedFilters: true,+ }),+ ])+ } finally {+ autoPreviewLimit.restore()+ }+ })+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 af2f185117d2..3bb1db6084e3 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx@@ -5293,7 +5293,8 @@ export const dashboardLogic = kea<dashboardLogicType>([.length ?? 0,})- if (values.canAutoPreview) {+ // The builtin placement has no Preview button, so a selection there always applies.+ if (values.canAutoPreview || values.placement === DashboardPlacement.Builtin) {actions.refreshDashboardItems({action: RefreshDashboardItemsAction.Preview,forceRefresh: false,diff --git a/products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables.tsx b/products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables.tsxindex e58f9ebbf8bf..a38aac72fe46 100644--- a/products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables.tsx+++ b/products/metrics/frontend/components/dashboardVariables/DashboardMetricVariables.tsx@@ -16,7 +16,7 @@ import { MetricVariablesModal } from './MetricVariablesModal'const NO_VARIABLES: MetricsDashboardVariable[] = []-export function DashboardMetricVariables(): JSX.Element | null {+export function DashboardMetricVariables({ editable = true }: { editable?: boolean }): JSX.Element | null {const { dashboard, dashboardEditing, effectiveEditBarFilters } = useValues(dashboardLogic)const { setMetricVariables, setDashboardEditing } = useActions(dashboardLogic)const [editorOpen, setEditorOpen] = useState(false)@@ -62,16 +62,18 @@ export function DashboardMetricVariables(): JSX.Element | null {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>+ {editable && (+ <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 && (<MetricVariablesModaldashboardId={dashboard.id}