: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): clear metric loading indicators on network error (#112895)
Rodrigo Iloro81b497e1df06
diff --git a/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts b/frontend/src/scenes/experiments/experimentMetricsLogic.test.tsindex bebc1ba23673..f7fd5b4fe70a 100644--- a/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts+++ b/frontend/src/scenes/experiments/experimentMetricsLogic.test.ts@@ -713,6 +713,125 @@ describe('experimentMetricsLogic', () => {expect(createMock).toHaveBeenCalled()})+ it('clears the recalculating marks when the create request is rejected', async () => {+ // The marks are set before the POST so the shown values read as refreshing; a rejected POST+ // (a 429 from the refresh window, or any error) never reaches the poll that would clear them.+ useMocks({+ get: {+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': () => [+ 200,+ completedRecalculation,+ ],+ },+ post: {+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/': () => [+ 429,+ {+ code: 'recalculation_rate_limited',+ detail: 'Metrics were recalculated less than 5 minutes ago.',+ },+ ],+ },+ })+ mountLogic()+ await expectLogic(logic).toDispatchActions(['setCurrentRecalculation'])+ expect(logic.values.primaryMetricsResults[0]).toEqual(primaryResult)++ await expectLogic(logic, () => {+ logic.actions.triggerRecalculation('manual')+ })+ .toDispatchActions(['triggerRecalculation', 'setRecalculatingMetricUuids'])+ .toFinishAllListeners()++ expect(logic.values.recalculatingMetricUuids).toEqual([])+ expect(logic.values.isRecalculating).toBe(false)+ expect(lemonToast.error).toHaveBeenCalledWith('Metrics were recalculated less than 5 minutes ago.')+ })++ it('keeps the marks of a run already being polled when a retry request is rejected', async () => {+ // The latest read found an active run and marked the shown metrics; the first poll has not landed+ // yet, so the retry button is enabled. A rejected retry must not strip the polled run's marks.+ useMocks({+ get: {+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': () => [+ 200,+ { ...completedRecalculation, active_run: { id: 'recalc-2', status: 'in_progress' } },+ ],+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/:recalc_id/': () => [+ 200,+ { ...pendingRecalculation, id: 'recalc-2', status: 'in_progress' },+ ],+ },+ post: {+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/': () => [+ 429,+ {+ code: 'recalculation_rate_limited',+ detail: 'Metrics were recalculated less than 5 minutes ago.',+ },+ ],+ },+ })+ // Real timers: the poll's first tick is two seconds out, and the by-id mock stays in progress with+ // no results, so a tick that lands keeps the marks either way.+ mountLogic()+ await expectLogic(logic).toDispatchActions(['setCurrentRecalculation', 'pollRecalculation'])+ expect(logic.values.recalculatingMetricUuids).toContain(PRIMARY_METRIC_UUID)++ // Wait for the catch block's dispatches rather than all listeners: the active run's poll loop+ // keeps a listener in flight for as long as the run stays in progress.+ await expectLogic(logic, () => {+ logic.actions.triggerRecalculation('manual_retry')+ }).toDispatchActions([+ 'triggerRecalculation',+ 'setRecalculatingMetricUuids',+ 'setRecalculationLoading',+ 'setRecalculationLoading',+ 'setRecalculatingMetricUuids',+ ])++ expect(logic.values.recalculatingMetricUuids).toContain(PRIMARY_METRIC_UUID)+ expect(lemonToast.error).toHaveBeenCalledWith('Metrics were recalculated less than 5 minutes ago.')+ })++ it('does not restore marks a completed poll already cleared while the retry request was pending', async () => {+ let releasePost: () => void = () => {}+ const postGate = new Promise<void>((resolve) => {+ releasePost = resolve+ })+ useMocks({+ get: {+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/latest/': () => [+ 200,+ { ...completedRecalculation, active_run: { id: 'recalc-2', status: 'in_progress' } },+ ],+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/:recalc_id/': () => [+ 200,+ completedRecalculation2,+ ],+ },+ post: {+ '/api/projects/:team_id/experiments/:id/metrics_recalculation/': async () => {+ await postGate+ return [429, { code: 'recalculation_rate_limited', detail: 'Too soon.' }]+ },+ },+ })+ mountLogic()+ await expectLogic(logic).toDispatchActions(['setCurrentRecalculation', 'pollRecalculation'])+ expect(logic.values.recalculatingMetricUuids).toContain(PRIMARY_METRIC_UUID)++ logic.actions.triggerRecalculation('manual_retry')+ // The first poll tick lands two seconds in, finds the run completed, and clears its marks.+ await expectLogic(logic).toDispatchActions(['setCurrentRecalculation'])+ expect(logic.values.currentRecalculation?.id).toBe('recalc-2')+ expect(logic.values.recalculatingMetricUuids).toEqual([])++ releasePost()+ await expectLogic(logic).toDispatchActions(['setRecalculationLoading', 'setRecalculatingMetricUuids'])+ expect(logic.values.recalculatingMetricUuids).toEqual([])+ })+describe('queuing', () => {it('queues instead of posting when a run is active', async () => {const createMock = jest.fn(() => [201, pendingRecalculation])diff --git a/frontend/src/scenes/experiments/experimentMetricsLogic.ts b/frontend/src/scenes/experiments/experimentMetricsLogic.tsindex c2be3252ba20..fd849967a4ea 100644--- a/frontend/src/scenes/experiments/experimentMetricsLogic.ts+++ b/frontend/src/scenes/experiments/experimentMetricsLogic.ts@@ -772,6 +772,9 @@ export const experimentMetricsLogic = kea<experimentMetricsLogicType>([* they keep their value and show a loading tag until the new result streams in. Cold runs* have nothing prior, so nothing to mark.*/+ // Marks that already belong to a run being polled (set by loadLatestRecalculation when it+ // found an active run) must survive a rejected create, so keep them for the catch block.+ const previousRecalculatingMetricUuids = new Set(values.recalculatingMetricUuids)if (trigger !== 'cold_run') {actions.setRecalculatingMetricUuids(metricUuidsToMarkRecalculating(@@ -863,9 +866,18 @@ export const experimentMetricsLogic = kea<experimentMetricsLogicType>([}} catch (error: any) {/**- * Re-enable the reload button: the run never started, so nothing else will clear loading.+ * re enable the reload button.*/actions.setRecalculationLoading(false)+ /**+ * clears the metric badges and the metric group loading indicator this create set,+ * and keeps the marks of a run that is already being polled. The intersection, not the+ * snapshot: a poll that landed while the request was pending has already cleared its+ * metrics, and restoring them would leave them on with nothing left to clear them.+ */+ actions.setRecalculatingMetricUuids(+ values.recalculatingMetricUuids.filter((uuid) => previousRecalculatingMetricUuids.has(uuid))+ )lemonToast.error(error?.detail || 'Failed to trigger metrics recalculation')} finally {cache.createInFlight = false