: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(experiments): show metrics for completed experiments in recalc flow (#64329)
Anders11a38ab9d80b
diff --git a/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts b/frontend/src/scenes/experiments/experimentMetricsLogic.test.tsindex 91df2eb040d8..c98972fb1c0e 100644--- a/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts+++ b/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts@@ -124,27 +124,41 @@ describe('experimentMetricsLogic', () => {})describe('loadLatestRecalculation', () => {- it.each([- // Draft: never launched (no start_date, no status).- ['draft', { ...EXPERIMENT, status: undefined, start_date: null, end_date: null } as Experiment],- // Stopped/completed: launched then ended (has an end_date).- ['stopped', { ...EXPERIMENT, status: undefined, end_date: '2025-01-01T00:00:00Z' } as Experiment],- ])('does not fetch latest on mount for a %s experiment', async (_label, experiment) => {+ it('does not fetch latest on mount for a draft experiment', async () => {+ const draft = { ...EXPERIMENT, status: undefined, start_date: null, end_date: null } as Experimentconst latestMock = jest.fn(() => [200, completedRecalculation])useMocks({get: {'/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': latestMock,},})- logic = experimentMetricsLogic({ experiment })+ logic = experimentMetricsLogic({ experiment: draft })logic.mount()await expectLogic(logic).toDispatchActions(['loadLatestRecalculation'])- // Gated before the request: a non-running experiment has nothing to recalculate.+ // Gated before the request: a draft has nothing to recalculate yet.expect(latestMock).not.toHaveBeenCalled()expect(logic.values.currentRecalculation).toBeNull()})+ it('fetches latest on mount for a stopped experiment so completed results still display', async () => {+ // Stopped/completed: launched then ended (has an end_date).+ const stopped = { ...EXPERIMENT, status: undefined, end_date: '2025-01-01T00:00:00Z' } as Experiment+ const latestMock = jest.fn(() => [200, completedRecalculation])+ useMocks({+ get: {+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': latestMock,+ },+ })+ logic = experimentMetricsLogic({ experiment: stopped })+ logic.mount()++ await expectLogic(logic).toDispatchActions(['setCurrentRecalculation', 'setPrimaryMetricsResults'])+ // A stopped experiment must still load its final results — otherwise the table loads forever.+ expect(latestMock).toHaveBeenCalled()+ expect(logic.values.primaryMetricsResults[0]).toEqual(primaryResult)+ })+it('loads the latest completed recalculation and maps results by metric position', async () => {useMocks({get: {@@ -307,18 +321,14 @@ describe('experimentMetricsLogic', () => {})describe('triggerRecalculation', () => {- it.each([- // Draft: never launched (no start_date, no status).- ['draft', { ...EXPERIMENT, status: undefined, start_date: null, end_date: null } as Experiment],- // Stopped/completed: launched then ended (has an end_date).- ['stopped', { ...EXPERIMENT, status: undefined, end_date: '2025-01-01T00:00:00Z' } as Experiment],- ])('does not create a recalculation for a %s experiment', async (_label, experiment) => {+ it('does not create a recalculation for a draft experiment', async () => {+ const draft = { ...EXPERIMENT, status: undefined, start_date: null, end_date: null } as Experimentconst createMock = jest.fn(() => [201, pendingRecalculation])useMocks({get: { '/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': () => [404, {}] },post: { '/api/projects/:team_id/experiments/:id/metrics_recalculation/': createMock },})- logic = experimentMetricsLogic({ experiment })+ logic = experimentMetricsLogic({ experiment: draft })logic.mount()await expectLogic(logic, () => {@@ -326,6 +336,22 @@ describe('experimentMetricsLogic', () => {}).toNotHaveDispatchedActions(['pollRecalculation', 'setCurrentRecalculation'])expect(createMock).not.toHaveBeenCalled()})++ it('creates a recalculation for a stopped experiment to compute its final results', async () => {+ const stopped = { ...EXPERIMENT, status: undefined, end_date: '2025-01-01T00:00:00Z' } as Experiment+ const createMock = jest.fn(() => [201, pendingRecalculation])+ useMocks({+ get: { '/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': () => [404, {}] },+ post: { '/api/projects/:team_id/experiments/:id/metrics_recalculation/': createMock },+ })+ logic = experimentMetricsLogic({ experiment: stopped })+ logic.mount()++ await expectLogic(logic, () => {+ logic.actions.triggerRecalculation()+ }).toDispatchActions(['setCurrentRecalculation'])+ expect(createMock).toHaveBeenCalled()+ })})describe('loading + progress selectors', () => {diff --git a/frontend/src/scenes/experiments/experimentMetricsLogic.ts b/frontend/src/scenes/experiments/experimentMetricsLogic.tsindex f6b4a6c8d854..e5edbb4cd8d1 100644--- a/frontend/src/scenes/experiments/experimentMetricsLogic.ts+++ b/frontend/src/scenes/experiments/experimentMetricsLogic.ts@@ -19,7 +19,7 @@ import {import type { ExperimentMetricsRecalculationApi } from 'products/experiments/frontend/generated/api.schemas'import type { experimentMetricsLogicType } from './experimentMetricsLogicType'-import { hasEnded, isLaunched } from './experimentsLogic'+import { isLaunched } from './experimentsLogic'type ExperimentSavedMetric = {metadata: {@@ -199,10 +199,12 @@ export const experimentMetricsLogic = kea<experimentMetricsLogicType>([const flagEnabled = (): boolean => !!values.featureFlags[FEATURE_FLAGS.EXPERIMENTS_METRICS_RECALCULATION]/**- * Recalculations only make sense for a running experiment: a draft has no results to fetch, and a- * stopped one is frozen. Gates both the latest-fetch and triggering a new run.+ * Recalculations only make sense once an experiment has launched: a draft has no results to fetch.+ * A stopped experiment still has final results to compute and display, so it is included here — this+ * mirrors the backend, which only rejects recalculation for drafts (`is_launched`). Gates both the+ * latest-fetch and triggering a new run.*/- const experimentIsRunning = (): boolean => isLaunched(props.experiment) && !hasEnded(props.experiment)+ const experimentIsLaunched = (): boolean => isLaunched(props.experiment)/*** some local helpers for the listeners closure@@ -266,8 +268,8 @@ export const experimentMetricsLogic = kea<experimentMetricsLogicType>([actions.setRecalculationLoading(false)return}- // Don't fetch for draft or stopped experiments — there's nothing to recalculate.- if (!experimentIsRunning()) {+ // Don't fetch for draft experiments — there's nothing to recalculate yet.+ if (!experimentIsLaunched()) {actions.setRecalculationLoading(false)return}@@ -324,10 +326,10 @@ export const experimentMetricsLogic = kea<experimentMetricsLogicType>([return}/**- * Don't recalculate draft or stopped experiments; a config-change edit on either shouldn't- * kick off a run.+ * Don't recalculate draft experiments; a config-change edit on a draft shouldn't kick off a+ * run. Stopped experiments are allowed — they still need their final results computed.*/- if (!experimentIsRunning()) {+ if (!experimentIsLaunched()) {return}/**