: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): queue recalculation when a shared metric is attached during a run (#122757)
Rodrigo Iloro0a6319fcf1b2
diff --git a/frontend/src/scenes/experiments/experimentLogic.test.ts b/frontend/src/scenes/experiments/experimentLogic.test.tsindex c25e3093305b..f4db3da14100 100644--- a/frontend/src/scenes/experiments/experimentLogic.test.ts+++ b/frontend/src/scenes/experiments/experimentLogic.test.ts@@ -388,36 +388,70 @@ describe('experimentLogic', () => {.toFinishAllListeners()})- it('addSharedMetricsToExperiment reuses the window (metric_config_change)', async () => {- // A keyed logic with a real experiment id so loadExperiment returns the launched experiment- // (the default unkeyed logic resolves experimentId to "new" and loads a draft). Mock every- // endpoint the follow-up refresh touches so its async work settles before unmount.- useMocks({- get: { '/api/projects/:team/experiments/:id': experiment },- post: {- '/api/environments/:team/query': () => [- 200,- { cache_key: 'cache_key', query_status: experimentMetricResultsSuccessJson.query_status },- ],- },- })- jest.spyOn(api, 'update').mockResolvedValue(experiment)- const keyed = experimentLogic({ experimentId: experiment.id })- keyed.mount()- keyed.actions.setExperiment(experiment)-- await expectLogic(keyed, () => {- keyed.actions.addSharedMetricsToExperiment([1], { type: 'primary' })- })- .toDispatchActions([- (action) =>- action.type === keyed.actionTypes.refreshExperimentResults &&- action.payload.triggeredBy === 'metric_config_change',- ])- .toFinishAllListeners()+ const attachedSharedMetric = {+ id: 7,+ experiment: experiment.id as number,+ saved_metric: 1,+ name: 'Attached shared metric',+ query: experiment.metrics[0],+ metadata: { type: 'primary' },+ created_at: '2024-01-01T00:00:00Z',+ } as unknown as ExperimentSavedMetric+ const keptSharedMetric = { ...attachedSharedMetric, id: 8, saved_metric: 2 } as ExperimentSavedMetric- keyed.unmount()- })+ it.each([+ [+ 'addSharedMetricsToExperiment',+ [attachedSharedMetric],+ [],+ (keyed: ReturnType<typeof experimentLogic>): void =>+ keyed.actions.addSharedMetricsToExperiment([1], { type: 'primary' }),+ ],+ [+ 'removeSharedMetricFromExperiment',+ [keptSharedMetric],+ [attachedSharedMetric, keptSharedMetric],+ (keyed: ReturnType<typeof experimentLogic>): void => keyed.actions.removeSharedMetricFromExperiment(1),+ ],+ ])(+ '%s applies the save response, clears positional results and reuses the window (metric_config_change)',+ async (_name, savedMetricsAfter, savedMetricsBefore, dispatch) => {+ useMocks({+ post: {+ '/api/environments/:team/query': () => [+ 200,+ { cache_key: 'cache_key', query_status: experimentMetricResultsSuccessJson.query_status },+ ],+ },+ })+ const before = { ...experiment, version: 4, saved_metrics: savedMetricsBefore } as Experiment+ const response = { ...experiment, version: 5, saved_metrics: savedMetricsAfter } as Experiment+ jest.spyOn(api, 'update').mockResolvedValue(response)+ const keyed = experimentLogic({ experimentId: experiment.id })+ keyed.mount()+ keyed.actions.setUnmodifiedExperiment(before)+ keyed.actions.setExperiment(before)++ await expectLogic(keyed, () => dispatch(keyed))+ .toFinishAllListeners()+ .toNotHaveDispatchedActions(['loadExperiment'])+ .toDispatchActions([+ (action) =>+ action.type ===+ experimentMetricsLogic({ experiment }).actionTypes.setPrimaryMetricsResults &&+ action.payload.results.length === 0,+ (action) =>+ action.type === keyed.actionTypes.refreshExperimentResults &&+ action.payload.forceRefresh === false &&+ action.payload.triggeredBy === 'metric_config_change',+ ])++ expect(keyed.values.experiment.saved_metrics).toEqual(savedMetricsAfter)+ expect(keyed.values.unmodifiedExperiment?.version).toEqual(5)++ keyed.unmount()+ }+ )})describe('currentRefresh tracking', () => {diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsxindex 5e7647edc7ec..e896f1e9487b 100644--- a/frontend/src/scenes/experiments/experimentLogic.tsx+++ b/frontend/src/scenes/experiments/experimentLogic.tsx@@ -2138,25 +2138,42 @@ export const experimentLogic = kea<experimentLogicType>([})const combinedMetricsIds = [...existingMetricsIds, ...newMetricsIds]+ let response: Experimenttry {// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use experimentsPartialUpdate() from 'products/experiments/frontend/generated/api' instead.- await api.update(`api/projects/${values.currentProjectId}/experiments/${values.experimentId}`, {- ...toConcurrencyPayload(values.unmodifiedExperiment),- saved_metrics_ids: combinedMetricsIds,- update_feature_flag_params: false,- })+ response = await api.update(+ `api/projects/${values.currentProjectId}/experiments/${values.experimentId}`,+ {+ ...toConcurrencyPayload(values.unmodifiedExperiment),+ saved_metrics_ids: combinedMetricsIds,+ update_feature_flag_params: false,+ }+ )} catch (error: any) {if (isExperimentConflictError(error)) {lemonToast.error(error.data?.detail ||'This experiment was changed while you were editing it. Review the latest changes and try again.')- } else {- throw error+ actions.loadExperiment({ triggeredBy: 'metric_config_change' })+ return}+ throw error}- actions.loadExperiment({ triggeredBy: 'metric_config_change' })+ /** A reload would unmount the metrics view and drop the recalculation trigger, so apply the response. */+ actions.setUnmodifiedExperiment(structuredClone(response))+ actions.setExperiment(response)++ const metricsLogic = experimentMetricsLogic({ experiment: values.experiment })+ metricsLogic.actions.setPrimaryMetricsResults([])+ metricsLogic.actions.setPrimaryMetricsResultsErrors([])+ metricsLogic.actions.setSecondaryMetricsResults([])+ metricsLogic.actions.setSecondaryMetricsResultsErrors([])++ if (isLaunched(response)) {+ actions.refreshExperimentResults(false, 'metric_config_change')+ }},duplicateSharedMetricAsInlineMetric: ({ isSecondary, newUuid }) => {// Listeners run after reducers, so the copy is only there if the shared metric was actually found@@ -2181,27 +2198,44 @@ export const experimentLogic = kea<experimentLogicType>([(m) => !('isSharedMetric' in m && m.isSharedMetric && m.sharedMetricId === sharedMetricId))+ let response: Experimenttry {// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use experimentsPartialUpdate() from 'products/experiments/frontend/generated/api' instead.- await api.update(`api/projects/${values.currentProjectId}/experiments/${values.experimentId}`, {- ...toConcurrencyPayload(values.unmodifiedExperiment),- saved_metrics_ids: sharedMetricsIds,- metrics: cleanedMetrics,- metrics_secondary: cleanedMetricsSecondary,- update_feature_flag_params: false,- })+ response = await api.update(+ `api/projects/${values.currentProjectId}/experiments/${values.experimentId}`,+ {+ ...toConcurrencyPayload(values.unmodifiedExperiment),+ saved_metrics_ids: sharedMetricsIds,+ metrics: cleanedMetrics,+ metrics_secondary: cleanedMetricsSecondary,+ update_feature_flag_params: false,+ }+ )} catch (error: any) {if (isExperimentConflictError(error)) {lemonToast.error(error.data?.detail ||'This experiment was changed while you were editing it. Review the latest changes and try again.')- } else {- throw error+ actions.loadExperiment({ triggeredBy: 'metric_config_change' })+ return}+ throw error}- actions.loadExperiment({ triggeredBy: 'metric_config_change' })+ /** A reload would unmount the metrics view and drop the recalculation trigger, so apply the response. */+ actions.setUnmodifiedExperiment(structuredClone(response))+ actions.setExperiment(response)++ const metricsLogic = experimentMetricsLogic({ experiment: values.experiment })+ metricsLogic.actions.setPrimaryMetricsResults([])+ metricsLogic.actions.setPrimaryMetricsResultsErrors([])+ metricsLogic.actions.setSecondaryMetricsResults([])+ metricsLogic.actions.setSecondaryMetricsResultsErrors([])++ if (isLaunched(response)) {+ actions.refreshExperimentResults(false, 'metric_config_change')+ }},createExperimentDashboard: async () => {actions.setIsCreatingExperimentDashboard(true)