: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(dashboards): pin dashboard refresh "X out of Y" denominator (#66280)
Vasco de Krijgerc9a530be2fb2
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.tsindex 09d39506fd7a..f8fe2d6b9635 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts@@ -1065,6 +1065,9 @@ describe('dashboardLogic', () => {timer: null,},},+ // Y is captured up front when the batch is enrolled, so it stays fixed+ // for the whole cycle rather than tracking the still-populating map.+ refreshTilesTotal: 2,refreshMetrics: {completed: 0,total: 2,@@ -1100,6 +1103,78 @@ describe('dashboardLogic', () => {})})+ it('pins the "X out of Y" denominator when a tile aborts mid-cycle and keeps siblings tracked', async () => {+ const dashboard = dashboards[5]+ const insight1 = dashboard.tiles[0].insight!+ const insight2 = dashboard.tiles[1].insight!++ // Hold both insight fetches in flight so we can observe a mid-cycle abort while the batch is live.+ const gates: Record<string, { barrier: Promise<void>; release: () => void }> = {}+ for (const shortId of [insight1.short_id, insight2.short_id]) {+ let release!: () => void+ const barrier = new Promise<void>((resolve): void => {+ release = resolve+ })+ gates[shortId] = { barrier, release }+ }++ const realGetInsightWithRetry =+ jest.requireActual<typeof dashboardUtils>('./dashboardUtils').getInsightWithRetry++ const getInsightWithRetrySpy = jest+ .spyOn(dashboardUtils, 'getInsightWithRetry')+ .mockImplementation(+ async (+ ...args: Parameters<typeof realGetInsightWithRetry>+ ): ReturnType<typeof realGetInsightWithRetry> => {+ await gates[args[1].short_id].barrier+ return realGetInsightWithRetry(...args)+ }+ )+ const cancelQuerySpy = jest.spyOn(api.insights, 'cancelQuery').mockResolvedValue(undefined as any)++ const poll = async (cond: () => boolean, message: string): Promise<void> => {+ const deadline = Date.now() + 5000+ while (!cond()) {+ if (Date.now() > deadline) {+ throw new Error(message)+ }+ await new Promise((r) => setTimeout(r, 0))+ }+ }++ try {+ // forceRefresh: true so both tiles enter the refresh loop+ const refreshDone = expectLogic(logic, () => {+ logic.actions.triggerDashboardRefresh()+ }).toFinishAllListeners()++ // Both tiles enrolled up front and in flight: Y is the fixed batch size, X is 0.+ await poll(+ () => getInsightWithRetrySpy.mock.calls.length >= 2,+ 'Timed out waiting for insight fetches to start'+ )+ expect(logic.values.refreshMetrics).toEqual({ completed: 0, total: 2 })++ // One tile's query aborts mid-cycle (e.g. a 504). Y must stay pinned at 2 — the pre-fix+ // selector derived Y from the live map, so it collapsed as the map shrank — and only the+ // aborted tile leaves the status map, so the sibling still in flight keeps being counted+ // (a whole-map wipe would drop it and overstate X as "done").+ logic.actions.abortQuery({ queryId: 'q1', queryStartTime: 0, shortId: insight1.short_id })+ expect(logic.values.refreshStatus).not.toHaveProperty(insight1.short_id)+ expect(logic.values.refreshStatus[insight2.short_id]?.loading).toBe(true)+ expect(logic.values.refreshMetrics).toEqual({ completed: 1, total: 2 })++ gates[insight1.short_id].release()+ gates[insight2.short_id].release()+ await refreshDone+ } finally {+ Object.values(gates).forEach(({ release }) => release())+ getInsightWithRetrySpy.mockRestore()+ cancelQuerySpy.mockRestore()+ }+ })+it('save during in-flight dashboard refresh does not abort insight fetches', async () => {const dashboard = dashboards[5]const insight1 = dashboard.tiles[0].insight!diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsxindex 5249f76216a8..befaf3299083 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx@@ -322,7 +322,9 @@ export const dashboardLogic = kea<dashboardLogicType>([queued,}),setRefreshError: (shortId: InsightShortId, error?: Error) => ({ shortId, error }),- abortQuery: (payload: { queryId: string; queryStartTime: number }) => payload,+ /** Number of insights enrolled in the current refresh cycle, captured up front. */+ setRefreshTilesTotal: (total: number) => ({ total }),+ abortQuery: (payload: { queryId: string; queryStartTime: number; shortId: InsightShortId }) => payload,abortAnyRunningQuery: true,cancelDashboardRefresh: true,@@ -1078,10 +1080,26 @@ export const dashboardLogic = kea<dashboardLogicType>([[shortId]: { errored: true, error, timer: state[shortId]?.timer || null },}),refreshDashboardItems: () => ({}),- abortQuery: () => ({}),+ // Drop only the aborted tile so sibling tiles still in flight stay tracked; wiping the+ // whole map here would make them count as completed and overstate "X out of Y".+ abortQuery: (state, { shortId }) => {+ const { [shortId]: _aborted, ...rest } = state+ return rest+ },cancelDashboardRefresh: () => ({}),},],+ // Denominator for "X out of Y", pinned up front so Y stays fixed while tiles enroll one by one.+ // null = no batch pinned → selector falls back to the live map size (single-insight refreshes).+ // Reset on cycle boundaries only, never on the per-tile abortQuery, so an aborted tile can't shrink Y.+ refreshTilesTotal: [+ null as number | null,+ {+ setRefreshTilesTotal: (_, { total }) => total,+ refreshDashboardItems: () => null,+ cancelDashboardRefresh: () => null,+ },+ ],columns: [null as number | null,{@@ -1861,11 +1879,13 @@ export const dashboardLogic = kea<dashboardLogicType>([},],refreshMetrics: [- (s) => [s.refreshStatus],- (refreshStatus) => {- const total = Object.keys(refreshStatus).length ?? 0+ (s) => [s.refreshStatus, s.refreshTilesTotal],+ (refreshStatus, refreshTilesTotal) => {+ const inFlight = Object.values(refreshStatus).filter((s) => s.loading || s.queued).length+ // Pinned batch size keeps Y fixed for the cycle; fall back to the live map for single-insight refreshes.+ const total = refreshTilesTotal ?? Object.keys(refreshStatus).lengthreturn {- completed: total - (Object.values(refreshStatus).filter((s) => s.loading || s.queued).length ?? 0),+ completed: total - inFlight,total,}},@@ -2519,6 +2539,8 @@ export const dashboardLogic = kea<dashboardLogicType>([await breakpoint()actions.resetIntermittentFilters()+ // Pin the progress denominator to the batch size before enrolling the insights+ actions.setRefreshTilesTotal(tilesStaleCount)// Set refresh status for all insightsactions.setRefreshStatuses(sortedTilesToRefresh.map((tile) => tile.insight.short_id),@@ -2589,7 +2611,7 @@ export const dashboardLogic = kea<dashboardLogicType>([} catch (e: any) {if (shouldCancelQuery(e)) {console.warn(`Insight refresh cancelled for ${insight.short_id} due to abort signal:`, e)- actions.abortQuery({ queryId, queryStartTime })+ actions.abortQuery({ queryId, queryStartTime, shortId: insight.short_id })tilesAbortedCount++} else {actions.setRefreshError(insight.short_id, e)