: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
feat(dashboards): wait to load tile groups until a viewer opens them (#117069)
Paul D'Ambrac8ca80e7baa3
diff --git a/frontend/src/scenes/dashboard/Dashboard.tsx b/frontend/src/scenes/dashboard/Dashboard.tsxindex 054ce844c783..a72c750687f4 100644--- a/frontend/src/scenes/dashboard/Dashboard.tsx+++ b/frontend/src/scenes/dashboard/Dashboard.tsx@@ -132,7 +132,7 @@ function DashboardScene({} = useValues(dashboardLogic)const { layoutZoom } = useValues(dashboardLogic)const { currentTeamId } = useValues(teamLogic)- const { reportDashboardViewed, abortAnyRunningQuery, retryDashboardLoad, setLayoutZoom } =+ const { reportDashboardViewed, cancelDashboardRefresh, retryDashboardLoad, setLayoutZoom } =useActions(dashboardLogic)const { addInsightToDashboardModalVisible } = useValues(addInsightToDashboardLogic)const { hideAddInsightToDashboardModal } = useActions(addInsightToDashboardLogic)@@ -156,8 +156,8 @@ function DashboardScene({useOnMountEffect(() => {reportDashboardViewed()- // request cancellation of any running queries when this component is no longer in the dom- return () => abortAnyRunningQuery()+ // request cancellation of any running insight, widget and tile group loads when this component is no longer in the dom+ return () => cancelDashboardRefresh()})// `error404` only becomes true once a load has settled as a 404, so pending loads fall through to the empty/loading statediff --git a/frontend/src/scenes/dashboard/DashboardItems.test.tsx b/frontend/src/scenes/dashboard/DashboardItems.test.tsxindex 4ae390823ab1..f627d759e2c3 100644--- a/frontend/src/scenes/dashboard/DashboardItems.test.tsx+++ b/frontend/src/scenes/dashboard/DashboardItems.test.tsx@@ -384,6 +384,23 @@ describe('DashboardItems', () => {expect(setDashboardEditing).toHaveBeenCalledTimes(editable ? 1 : 0)})+ it.each([+ ['queued', true, true, false],+ ['loading', true, false, true],+ ['idle', false, false, false],+ ])('with the dashboard load %s, tells the card not to load: %s', (_state, doNotLoad, queued, loading) => {+ const defaultValues = mockedUseValues.getMockImplementation()!+ mockedUseValues.mockImplementation((logic) =>+ logic === dashboardLogic+ ? { ...defaultValues(logic), isRefreshingQueued: () => queued, isRefreshing: () => loading }+ : defaultValues(logic)+ )++ render(<DashboardItems />)++ expect(mockInsightCard).toHaveBeenLastCalledWith(expect.objectContaining({ doNotLoad }))+ })+it.each([['tight', '8,8'],['condensed', '12,12'],diff --git a/frontend/src/scenes/dashboard/DashboardItems.tsx b/frontend/src/scenes/dashboard/DashboardItems.tsxindex d62ac182fcb7..1a0474629a2f 100644--- a/frontend/src/scenes/dashboard/DashboardItems.tsx+++ b/frontend/src/scenes/dashboard/DashboardItems.tsx@@ -633,6 +633,8 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsPinsight={insight}loadingQueued={loadingQueued}loading={loading}+ // The dashboard load queue owns this tile, so the card must not start its own query.+ doNotLoad={loading || loadingQueued}apiErrored={apiErrored}apiError={apiError}queryId={insight.query_status?.id}diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.tsindex e2ca9c1c22ea..37dcd718d974 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts@@ -812,6 +812,515 @@ describe('dashboardLogic', () => {expect(logic.values.layouts.sm?.map((item) => item.w)).toEqual([6, 6, 6])})++ describe('loading closed groups', () => {+ let refreshInsight: jest.SpiedFunction<typeof dashboardUtils.getInsightWithRetry>+ let fetchRunWidgetsMock: jest.SpiedFunction<typeof widgetFetchUtils.fetchRunWidgets>++ const loads = (shortId: InsightShortId): Parameters<typeof dashboardUtils.getInsightWithRetry>[] =>+ refreshInsight.mock.calls.filter((call) => call[1].short_id === shortId)++ const toggle = async (): Promise<void> => {+ await expectLogic(logic, () => logic.actions.toggleTileGroup('target')).toFinishAllListeners()+ }++ const hangUntilAborted = (signal: AbortSignal | undefined): Promise<never> =>+ new Promise((_, reject) =>+ signal?.addEventListener('abort', () => reject(new DOMException('Aborted', 'AbortError')))+ )++ beforeEach(() => {+ refreshInsight = jest+ .spyOn(dashboardUtils, 'getInsightWithRetry')+ .mockImplementation((_teamId, insight) => Promise.resolve(insight))+ fetchRunWidgetsMock = jest+ .spyOn(widgetFetchUtils, 'fetchRunWidgets')+ .mockResolvedValue([+ { tile_id: WIDGET_TILE.id, widget_type: 'error_tracking_list', result: {}, error: null },+ ])+ })++ afterEach(() => {+ refreshInsight.mockRestore()+ fetchRunWidgetsMock.mockRestore()+ })++ it.each([+ {+ name: 'a manual refresh',+ refresh: () => logic.actions.triggerDashboardRefresh(),+ mode: 'force_blocking',+ dateFrom: undefined,+ },+ // The preview has not finished, so settingsForRefresh still holds the saved filters.+ {+ name: 'a filter preview',+ refresh: () => logic.actions.setDates('-7d', null),+ mode: 'blocking',+ dateFrom: '-7d',+ },+ ])('loads a closed group on open with the settings of $name', async ({ refresh, mode, dateFrom }) => {+ groupTiles({ target: { collapsed: true } })+ await mountDashboard()+ const visible = dashboards[5].tiles[1].insight!.short_id+ refreshInsight.mockImplementation((...args) =>+ args[1].short_id === visible ? hangUntilAborted(args[5]?.signal) : Promise.resolve(args[1])+ )++ refresh()+ await expectLogic(logic).toDispatchActions(['setWaitingTileRefresh'])+ expect(loads(target)).toHaveLength(0)++ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['clearWaitingTileRefresh'])++ const [[, , , , refreshMode, , filters]] = loads(target)+ expect(refreshMode).toBe(mode)+ expect(filters?.date_from).toBe(dateFrom)+ expect(logic.values.waitingTileRefresh).toBeNull()+ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ })++ it('settles a filter preview when every insight sits in a closed group', async () => {+ groupTiles({ target: { collapsed: true }, other: { collapsed: true } })+ await mountDashboard()++ await expectLogic(logic, () => logic.actions.setDates('-7d', null)).toFinishAllListeners()++ expect(loads(target)).toHaveLength(0)+ expect(logic.values.previewedDashboardSettings?.filters.date_from).toBe('-7d')+ expect(logic.values.breakdownValuesIncomplete).toBe(true)+ })++ it.each([+ {+ name: 'its first load failed',+ holdBack: async (): Promise<void> => {+ refreshInsight.mockRejectedValueOnce(new Error('Queries are busy'))+ await toggle()+ await toggle()+ },+ expectedLoads: ['blocking', 'blocking'],+ },+ {+ name: 'the viewer cancelled a refresh',+ holdBack: async (): Promise<void> => {+ refreshInsight.mockImplementationOnce((...args) => hangUntilAborted(args[5]?.signal))+ logic.actions.triggerDashboardRefresh()+ await expectLogic(logic).toDispatchActions(['setRefreshStatus'])+ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ },+ expectedLoads: ['force_blocking'],+ },+ ])('loads a closed group on open after $name', async ({ holdBack, expectedLoads }) => {+ groupTiles({ target: { collapsed: true } })+ await mountDashboard()+ await holdBack()+ expect(logic.values.waitingTileRefresh?.tileIds).toEqual([dashboards[5].tiles[0].id])++ await toggle()++ expect(loads(target).map((call) => call[4])).toEqual(expectedLoads)+ expect(logic.values.waitingTileRefresh).toBeNull()+ })++ it('loads a closed group that a highlight link opens', async () => {+ groupTiles({ target: { collapsed: true } })+ await mountDashboard()+ expect(loads(target)).toHaveLength(0)++ await highlight(target)++ expect(loads(target)).toHaveLength(1)+ expect(logic.values.waitingTileRefresh).toBeNull()+ })++ it.each([+ { name: 'the viewer closes and opens it again', open: () => logic.actions.toggleTileGroup('target') },+ {+ name: 'a highlight link opens it',+ open: () => router.actions.push('/dashboard/5', { highlightInsightId: target }),+ },+ ])('loads each tile of a group once when $name during its load', async ({ open }) => {+ groupTiles({ target: { collapsed: true } })+ dashboards[5].tiles.push({ ...WIDGET_TILE, group_key: 'target' })+ await mountDashboard()+ refreshInsight.mockImplementation((...args) => hangUntilAborted(args[5]?.signal))+ fetchRunWidgetsMock.mockImplementation((_projectId, _dashboardId, _tileIds, options) =>+ hangUntilAborted(options?.signal)+ )++ open()+ await expectLogic(logic).toDispatchActions(['refreshTileGroup', 'setWidgetRefreshStatuses'])+ logic.actions.toggleTileGroup('target')+ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['refreshTileGroup'])++ expect(logic.values.collapsedTileGroupKeys).toEqual([])+ expect(loads(target)).toHaveLength(1)+ expect(fetchRunWidgetsMock).toHaveBeenCalledTimes(1)+ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ })++ it('loads a widget in a closed group on open', async () => {+ groupTiles({ target: { collapsed: true } })+ dashboards[5].tiles.push({ ...WIDGET_TILE, group_key: 'target' })+ await mountDashboard()+ expect(fetchRunWidgetsMock).not.toHaveBeenCalled()++ await toggle()++ expect(logic.values.widgetResultsByTileId[WIDGET_TILE.id]?.result).toEqual({})+ expect(logic.values.widgetRefreshStatus[WIDGET_TILE.id]?.error).toBeNull()+ })++ it('a filter preview neither aborts nor repeats the widget load of an opened group', async () => {+ groupTiles({ target: { collapsed: true } })+ dashboards[5].tiles.push({ ...WIDGET_TILE, group_key: 'target' })+ await mountDashboard()+ let finishWidgetLoad: () => void = () => {}+ fetchRunWidgetsMock.mockImplementationOnce(+ (_projectId, _dashboardId, _tileIds, options) =>+ new Promise((resolve, reject) => {+ options?.signal?.addEventListener('abort', () =>+ reject(new DOMException('Aborted', 'AbortError'))+ )+ finishWidgetLoad = () =>+ resolve([+ {+ tile_id: WIDGET_TILE.id,+ widget_type: 'error_tracking_list',+ result: {},+ error: null,+ },+ ])+ })+ )+ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['setWidgetRefreshStatuses'])++ logic.actions.setDates('-7d', null)+ await expectLogic(logic).toDispatchActions(['abortAnyRunningQuery', 'setPreviewedDashboardSettings'])+ finishWidgetLoad()+ await expectLogic(logic).toFinishAllListeners()++ expect(fetchRunWidgetsMock).toHaveBeenCalledTimes(1)+ expect(logic.values.widgetResultsByTileId[WIDGET_TILE.id]?.result).toEqual({})+ expect(logic.values.widgetRefreshStatus[WIDGET_TILE.id]?.error).toBeNull()+ })++ it.each([+ { name: 'cancels the refresh', stop: () => logic.actions.cancelDashboardRefresh(), loading: false },+ { name: 'refreshes again', stop: () => logic.actions.triggerDashboardRefresh(), loading: true },+ ])('stops a widget load without an error when the viewer $name', async ({ stop, loading }) => {+ groupTiles({})+ dashboards[5].tiles.push(WIDGET_TILE)+ await mountDashboard()+ fetchRunWidgetsMock.mockImplementation((_projectId, _dashboardId, _tileIds, options) =>+ hangUntilAborted(options?.signal)+ )+ logic.actions.refreshDashboardWidgets({ tileIds: [WIDGET_TILE.id], forceRefresh: true })+ await expectLogic(logic).toDispatchActions(['setWidgetRefreshStatuses'])++ await expectLogic(logic, stop).toDispatchActions(['stopWidgetRefreshes'])++ expect(logic.values.widgetRefreshStatus[WIDGET_TILE.id]).toMatchObject({ loading, error: null })+ fetchRunWidgetsMock.mockResolvedValue([])+ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ })++ it('adds an opened group to the "X out of Y" count of a running refresh', async () => {+ groupTiles({ target: { collapsed: true } })+ await mountDashboard()+ const visible = dashboards[5].tiles[1].insight!.short_id+ refreshInsight.mockImplementation((...args) =>+ args[1].short_id === visible ? hangUntilAborted(args[5]?.signal) : Promise.resolve(args[1])+ )+ logic.actions.triggerDashboardRefresh()+ await expectLogic(logic).toDispatchActions(['setWaitingTileRefresh'])+ expect(logic.values.refreshMetrics).toEqual({ completed: 0, total: 1 })++ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['clearWaitingTileRefresh'])++ expect(logic.values.refreshMetrics).toEqual({ completed: 1, total: 2 })+ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ })++ it('keeps a group waiting when the viewer cancels its load, and loads it on the next open', async () => {+ groupTiles({ target: { collapsed: true } })+ await mountDashboard()+ refreshInsight.mockImplementationOnce((...args) => hangUntilAborted(args[5]?.signal))+ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['setRefreshStatus'])++ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ expect(logic.values.waitingTileRefresh?.tileIds).toEqual([dashboards[5].tiles[0].id])+ await toggle()+ await toggle()++ expect(loads(target)).toHaveLength(2)+ expect(logic.values.waitingTileRefresh).toBeNull()+ })++ it.each([+ { name: 'insight tiles', widgetsPerTile: 0 },+ { name: 'insight and widget tiles', widgetsPerTile: 1 },+ ])(+ 'runs at most four loads at once when the viewer opens several groups during a refresh of $name',+ async ({ widgetsPerTile }) => {+ const groupKeys = ['a', 'b', 'c']+ const insightTile = dashboards[5].tiles[1]+ const tile = (id: number, groupKey?: string): DashboardTile => ({+ ...insightTile,+ id,+ group_key: groupKey,+ insight: { ...insightTile.insight!, id, short_id: `${id}` as InsightShortId },+ })+ const widgetTile = (id: number, groupKey?: string): DashboardTile => ({+ ...WIDGET_TILE,+ id,+ group_key: groupKey,+ })+ const tilesIn = (groupKey: string | undefined, offset: number): DashboardTile[] =>+ Array.from({ length: 4 }, (_, index) => [+ tile(offset + index, groupKey),+ ...(widgetsPerTile ? [widgetTile(offset + 50 + index, groupKey)] : []),+ ]).flat()+ dashboards[5] = {+ ...dashboards[5],+ customization: {+ groups: Object.fromEntries(groupKeys.map((groupKey) => [groupKey, { collapsed: true }])),+ },+ tiles: [+ ...tilesIn(undefined, 1000),+ ...groupKeys.flatMap((groupKey, index) => tilesIn(groupKey, 2000 + index * 100)),+ ],+ }+ await mountDashboard()++ let running = 0+ let maxRunning = 0+ const finishers: (() => void)[] = []+ const track = <T>(signal: AbortSignal | undefined, value: T): Promise<T> =>+ new Promise((resolve, reject) => {+ let settled = false+ const settleWith = (finish: () => void) => (): void => {+ if (!settled) {+ settled = true+ running--+ finish()+ }+ }+ running+++ maxRunning = Math.max(maxRunning, running)+ signal?.addEventListener(+ 'abort',+ settleWith(() => reject(new DOMException('Aborted', 'AbortError')))+ )+ finishers.push(settleWith(() => resolve(value)))+ })+ refreshInsight.mockImplementation((...args) => track(args[5]?.signal, args[1]))+ fetchRunWidgetsMock.mockImplementation((_projectId, _dashboardId, tileIds, options) =>+ track(+ options?.signal,+ tileIds.map((tileId) => ({+ tile_id: tileId,+ widget_type: 'error_tracking_list' as const,+ result: {},+ error: null,+ }))+ )+ )+ refreshInsight.mockClear()++ logic.actions.triggerDashboardRefresh()+ groupKeys.forEach((groupKey) => logic.actions.toggleTileGroup(groupKey))+ for (let round = 0; round < 100 && (finishers.length > 0 || round < 2); round++) {+ finishers.shift()?.()+ await new Promise((resolve) => setTimeout(resolve, 0))+ }+ await expectLogic(logic).toFinishAllListeners()++ expect(maxRunning).toBe(4)+ expect(new Set(refreshInsight.mock.calls.map((call) => call[1].short_id)).size).toBe(16)+ expect(logic.values.waitingTileRefresh).toBeNull()+ }+ )++ it('keeps an opened group waiting when the viewer cancels a filter preview that took over its load', async () => {+ groupTiles({ target: { collapsed: true } })+ await mountDashboard()+ refreshInsight.mockImplementation((...args) => hangUntilAborted(args[5]?.signal))+ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['setRefreshStatus'])+ logic.actions.setDates('-7d', null)+ await expectLogic(logic).toDispatchActions(['abortAnyRunningQuery'])++ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ expect(logic.values.waitingTileRefresh?.tileIds).toEqual([dashboards[5].tiles[0].id])+ refreshInsight.mockImplementation((_teamId, insight) => Promise.resolve(insight))+ await toggle()+ await toggle()++ expect(loads(target)).toHaveLength(3)+ expect(logic.values.waitingTileRefresh).toBeNull()+ })++ it('drops a group load that a filter change supersedes while every group is closed', async () => {+ groupTiles({ target: { collapsed: true }, other: { collapsed: true } })+ await mountDashboard()+ let finishOldLoad: () => void = () => {}+ refreshInsight.mockImplementationOnce(+ (...args) =>+ new Promise((resolve, reject) => {+ args[5]?.signal?.addEventListener('abort', () =>+ reject(new DOMException('Aborted', 'AbortError'))+ )+ finishOldLoad = () => resolve({ ...args[1], result: ['old filters'] })+ })+ )+ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['setRefreshStatus'])+ logic.actions.toggleTileGroup('target')++ logic.actions.setDates('-7d', null)+ finishOldLoad()+ await expectLogic(logic).toFinishAllListeners()++ expect(logic.values.insightTiles[0].insight?.result).not.toEqual(['old filters'])+ expect(logic.values.waitingTileRefresh).toMatchObject({+ tileIds: expect.arrayContaining([dashboards[5].tiles[0].id]),+ settings: { filters: { date_from: '-7d' } },+ })+ await toggle()+ expect(loads(target).map((call) => call[6]?.date_from)).toEqual([undefined, '-7d'])+ })++ it('loads an open group again after an update refresh aborts its forced load', async () => {+ groupTiles({ target: { collapsed: true } })+ dashboards[5].tiles[0].insight!.cache_target_age = now().add(1, 'hour').toISOString()+ dashboards[5].tiles[1].insight!.cache_target_age = now().subtract(1, 'hour').toISOString()+ await mountDashboard()+ await expectLogic(logic, () => logic.actions.triggerDashboardRefresh()).toFinishAllListeners()+ refreshInsight.mockImplementationOnce((...args) => hangUntilAborted(args[5]?.signal))+ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['refreshTileGroup', 'setRefreshStatus'])++ await expectLogic(logic, () =>+ logic.actions.refreshDashboardItems({ action: DashboardLoadAction.Update })+ )+ .toDispatchActions(['refreshTileGroup'])+ .toFinishAllListeners()++ expect(loads(target).map((call) => call[4])).toEqual(['force_blocking', 'force_blocking'])+ expect(logic.values.waitingTileRefresh).toBeNull()+ })++ it.each([+ { cancelPoint: 'after the group dispatch', expectedLoads: 2 },+ { cancelPoint: 'before the group dispatch', expectedLoads: 1 },+ ])(+ 'keeps an open group queued during an update refresh, and a cancel $cancelPoint stops its reload',+ async ({ cancelPoint, expectedLoads }) => {+ groupTiles({ target: { collapsed: true } })+ dashboards[5].tiles[0].insight!.cache_target_age = now().add(1, 'hour').toISOString()+ dashboards[5].tiles[1].insight!.cache_target_age = now().subtract(1, 'hour').toISOString()+ await mountDashboard()+ await expectLogic(logic, () => logic.actions.triggerDashboardRefresh()).toFinishAllListeners()+ refreshInsight.mockImplementation((...args) => hangUntilAborted(args[5]?.signal))+ logic.actions.toggleTileGroup('target')+ await expectLogic(logic).toDispatchActions(['refreshTileGroup', 'setRefreshStatus'])++ if (cancelPoint === 'after the group dispatch') {+ await expectLogic(logic, () =>+ logic.actions.refreshDashboardItems({ action: DashboardLoadAction.Update })+ ).toDispatchActions(['refreshTileGroup'])+ expect(logic.values.refreshStatus[target]).toMatchObject({ queued: true })++ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh()).toFinishAllListeners()+ } else {+ await expectLogic(logic, () =>+ logic.actions.refreshDashboardItems({ action: DashboardLoadAction.Update })+ ).toDispatchActions(['abortAnyRunningQuery'])++ await expectLogic(logic, () => logic.actions.cancelDashboardRefresh())+ .delay(5)+ .toFinishAllListeners()+ .toNotHaveDispatchedActions(['refreshTileGroup'])+ expect(Object.values(logic.values.refreshStatus).filter((status) => status.queued)).toEqual([])+ }++ expect(loads(target)).toHaveLength(expectedLoads)+ expect(logic.values.waitingTileRefresh?.tileIds).toContain(dashboards[5].tiles[0].id)+ }+ )++ it('an update refresh with nothing stale keeps a manual refresh running', async () => {+ groupTiles({ target: { collapsed: true } })+ dashboards[5].tiles[1].insight!.cache_target_age = now().add(1, 'hour').toISOString()+ await mountDashboard()+ const visible = dashboards[5].tiles[1].insight!.short_id+ let finishForcedLoad: () => void = () => {}+ refreshInsight.mockImplementation(+ (...args) =>+ new Promise((resolve, reject) => {+ args[5]?.signal?.addEventListener('abort', () =>+ reject(new DOMException('Aborted', 'AbortError'))+ )+ finishForcedLoad = () => resolve({ ...args[1], result: ['forced'] })+ })+ )+ logic.actions.triggerDashboardRefresh()+ await expectLogic(logic).toDispatchActions(['setRefreshStatus'])++ await expectLogic(logic, () => {+ logic.actions.refreshDashboardItems({ action: DashboardLoadAction.Update })+ finishForcedLoad()+ })+ .toFinishAllListeners()+ .toNotHaveDispatchedActions(['abortQuery'])++ expect(loads(visible).map((call) => call[4])).toEqual(['force_blocking'])+ expect(logic.values.insightTiles[1].insight?.result).toEqual(['forced'])+ })++ it.each([+ { group: 'closed', collapsed: true, counts: false },+ { group: 'open', collapsed: false, counts: true },+ ])(+ 'counts a stale tile in a $group group toward refreshing the dashboard: $counts',+ async ({ collapsed, counts }) => {+ const blockRefresh = !counts+ const forcesRefresh = counts+ groupTiles({ target: { collapsed } })+ await mountDashboard()+ const [staleTile, freshTile] = logic.values.insightTiles+ await expectLogic(logic, () => {+ for (const [tile, lastRefresh] of [+ [staleTile, now().subtract(1, 'day')],+ [freshTile, now()],+ ] as const) {+ dashboardsModel.actions.updateDashboardInsight(+ { ...tile.insight!, last_refresh: lastRefresh.toISOString() },+ undefined,+ 5+ )+ }+ logic.actions.updateDashboardLastRefresh(now())+ }).toFinishAllListeners()++ expect(logic.values.blockRefresh).toBe(blockRefresh)+ logic.actions.forceRefreshIfStale()+ await Promise.resolve()+ if (forcesRefresh) {+ await expectLogic(logic).toDispatchActions(['triggerDashboardRefresh'])+ } else {+ await expectLogic(logic).toNotHaveDispatchedActions(['triggerDashboardRefresh'])+ }+ }+ )+ })})describe('tile layouts', () => {diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsxindex c05c219d9bc0..4b3fdb1bdda8 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx@@ -34,7 +34,7 @@ import {type WidgetIssueMetadataDelta,} from '@posthog/products-dashboards/frontend/widgets/error_tracking/applyWidgetIssueMetadataChange'-import api, { ApiMethodOptions, getJSONOrNull } from 'lib/api'+import api, { getJSONOrNull } from 'lib/api'import { ApiError, isAccessDeniedError } from 'lib/api-error'import { DataColorTheme } from 'lib/colors'import { OrganizationMembershipLevel } from 'lib/constants'@@ -184,7 +184,7 @@ import {layoutsByTile,parseURLFilters,parseURLVariables,- runWithLimit,+ TaskQueue,searchParamsWithUrlFilters,shouldSharedDashboardAutoForceForStaleTime,} from './dashboardUtils'@@ -229,6 +229,147 @@ function reportDashboardTileRefreshed(})}+const TILE_REFRESH_CONCURRENCY = 4++type InsightTileRefreshOutcome = 'refreshed' | 'cached' | 'errored' | 'aborted'++interface InsightTileRefresh {+ actions: Pick<dashboardLogicType['actions'], 'setRefreshStatus' | 'setRefreshError' | 'abortQuery'>+ dashboardId: number+ currentTeamId: number | null+ filters: DashboardFilter+ variables: Record<string, HogQLVariable>+ urlFilters: DashboardFilter+ forceRefresh: boolean+ signal: AbortSignal+ queue: TaskQueue+}++/** One queue for every insight and widget load, so a dashboard never sends more than the cap at the same time. */+function tileLoadQueue(cache: Record<string, any>): TaskQueue {+ return (cache.tileLoadQueue ??= new TaskQueue(TILE_REFRESH_CONCURRENCY))+}++async function refreshInsightTile(+ tile: DashboardTile & { insight: InsightModel },+ {+ actions,+ dashboardId,+ currentTeamId,+ filters,+ variables,+ urlFilters,+ forceRefresh,+ signal,+ queue,+ }: InsightTileRefresh+): Promise<InsightTileRefreshOutcome> {+ const insight = tile.insight+ const queryId = uuid()+ let queryStartTime = performance.now()++ try {+ const refreshedInsight = await queue.run(() => {+ queryStartTime = performance.now()+ actions.setRefreshStatus(insight.short_id, true, true)+ return getInsightWithRetry(+ currentTeamId,+ insight,+ dashboardId,+ queryId,+ forceRefresh ? 'force_blocking' : 'blocking', // 'blocking' returns cached data if available, when manual refresh is triggered we want fresh results+ { signal },+ filters,+ variables,+ tile.filters_overrides+ )+ }, signal)++ const queryError = refreshedInsight ? getInsightQueryError(refreshedInsight) : null+ if (!refreshedInsight || isRefreshRejectionStub(refreshedInsight) || queryError) {+ actions.setRefreshError(insight.short_id, queryError?.code ? queryError : undefined)+ return 'errored'+ }+ dashboardsModel.actions.updateDashboardInsight(refreshedInsight, undefined, dashboardId)+ actions.setRefreshStatus(insight.short_id)+ reportDashboardTileRefreshed(+ dashboardId,+ tile,+ urlFilters,+ variables,+ Math.floor(performance.now() - queryStartTime),+ false+ )+ return refreshedInsight.is_cached ? 'cached' : 'refreshed'+ } catch (e: any) {+ if (shouldCancelQuery(e)) {+ console.warn(`Insight refresh cancelled for ${insight.short_id} due to abort signal:`, e)+ actions.abortQuery({ queryId, queryStartTime, shortId: insight.short_id })+ return 'aborted'+ }+ actions.setRefreshError(insight.short_id, e)+ return 'errored'+ }+}++function abortWidgetLoads(cache: Record<string, any>): void {+ cache.widgetAbortController?.abort()+ cache.widgetAbortController = undefined+}++function abortGroupLoads(cache: Record<string, any>): void {+ cache.groupAbortController?.abort()+ cache.groupAbortController = undefined+}++/** Tiles in closed groups that a refresh skipped, and how that refresh would have loaded them. */+interface WaitingTileRefresh {+ tileIds: number[]+ forceRefresh: boolean+ settings: DashboardSettings+}++/** Tiles in closed groups that a refresh skips wait for their group to open, alongside tiles an earlier refresh skipped. */+function nextWaitingTileRefresh({+ previous,+ tiles,+ collapsedTileIds,+ candidateTileIds,+ forceRefresh,+ settings,+}: {+ previous: WaitingTileRefresh | null+ tiles: DashboardTile[]+ collapsedTileIds: Set<number>+ candidateTileIds: number[]+ forceRefresh: boolean+ settings: DashboardSettings+}): WaitingTileRefresh {+ const groupedTileIds = new Set(tiles.filter((tile) => tile.group_key).map((tile) => tile.id))+ // This refresh aborts a running group load, and a viewer can cancel this refresh too,+ // so a tile keeps waiting until a load of it succeeds.+ const previouslyWaiting = previous?.tileIds.filter((id) => groupedTileIds.has(id)) ?? []+ return {+ tileIds: [...new Set([...previouslyWaiting, ...candidateTileIds.filter((id) => collapsedTileIds.has(id))])],+ // A refresh of only stale tiles must not let a group that a manual refresh skipped show older data.+ forceRefresh: forceRefresh || (previouslyWaiting.length > 0 && !!previous?.forceRefresh),+ settings,+ }+}++/** Open groups that still hold tiles an earlier refresh skipped. */+function openTileGroupsWaiting(+ tiles: DashboardTile[],+ collapsedTileIds: Set<number>,+ waiting: WaitingTileRefresh | null+): Set<string> {+ return new Set(+ tiles+ .filter((tile) => tile.group_key && !collapsedTileIds.has(tile.id) && waiting?.tileIds.includes(tile.id))+ .map((tile) => tile.group_key as string)+ )+}+export interface DashboardLogicProps {id: numberdashboard?: DashboardType@@ -501,6 +642,7 @@ export interface dashboardLogicValues {cancellingPreview: booleanchangedFilterCount: numbercollapsedTileGroupKeys: string[]+ collapsedTileIds: Set<number>columns: number | nullcontainerWidth: number | nullcurrentDashboardSettings: DashboardSettings@@ -619,6 +761,7 @@ export interface dashboardLogicValues {} | nullurlVariables: Record<string, HogQLVariable>variablesDirty: boolean+ waitingTileRefresh: WaitingTileRefresh | nullwidgetRefreshStatus: Record<number,{@@ -699,6 +842,9 @@ export interface dashboardLogicActions {clearInitialDashboardSettingsOverride: () => {value: true}+ clearWaitingTileRefresh: (tileIds: number[]) => {+ tileIds: number[]+ }closeTileModal: () => {value: true}@@ -934,6 +1080,9 @@ export interface dashboardLogicActions {forceRefresh?: boolean | undefinedtileIds: number[]}+ refreshTileGroup: (groupKey: string) => {+ groupKey: string+ }removeTile: (tile: DashboardTile) => {tile: DashboardTile}@@ -1218,6 +1367,9 @@ export interface dashboardLogicActions {properties: Partial<Pick<DashboardTile, 'color' | 'group_key' | 'layouts' | 'show_description'>>tileId: number}+ setWaitingTileRefresh: (waiting: WaitingTileRefresh) => {+ waiting: WaitingTileRefresh+ }setWidgetRefreshStatuses: (tileIds: number[],loading: boolean,@@ -1233,6 +1385,9 @@ export interface dashboardLogicActions {stageLayouts: (layouts: ResponsiveLayouts) => {layouts: Partial<Record<string, Layout>>}+ stopWidgetRefreshes: (tileIds: number[]) => {+ tileIds: number[]+ }tilePlacementsSaved: (placements: Record<number, TilePlacement>) => {placements: Record<number, TilePlacement>}@@ -1488,6 +1643,7 @@ export interface dashboardLogicMeta {tileGroupChoices: TileGroupChoices,revealedTileGroup: RevealedTileGroup | null) => string[]+ collapsedTileIds: (tiles: DashboardTile[], collapsedTileGroupKeys: string[]) => Set<number>insightTiles: (tiles: DashboardTile[]) => DashboardTile[]textTiles: (tiles: DashboardTile[]) => DashboardTile[]dashboardControlScopes: (insightTiles: DashboardTile[]) => Record<DashboardControl, DashboardControlScope>@@ -1500,7 +1656,7 @@ export interface dashboardLogicMeta {isRefreshingQueued: (refreshStatus: Record<string, RefreshStatus>) => (id: string) => booleanisRefreshing: (refreshStatus: Record<string, RefreshStatus>) => (id: string) => booleanhighlightedInsightId: (searchParams: Record<string, any>) => any- sortedDates: (insightTiles: DashboardTile[]) => Dayjs[]+ sortedDates: (insightTiles: DashboardTile[], collapsedTileIds: Set<number>) => Dayjs[]oldestRefreshed: (sortedDates: Dayjs[], pageVisibility: boolean) => Dayjs | nulleffectiveLastRefresh: (lastDashboardRefresh: Dayjs | null, oldestRefreshed: Dayjs | null) => Dayjs | nullnextAllowedDashboardRefresh: (lastDashboardRefresh: Dayjs | null) => Dayjs | null@@ -1529,6 +1685,7 @@ export interface dashboardLogicMeta {>,refreshEligibilityTick: number,placement: DashboardPlacement,+ collapsedTileIds: Set<number>,pageVisibility: boolean) => booleancanEditDashboard: (dashboard: DashboardType | null) => boolean@@ -1591,7 +1748,11 @@ export interface dashboardLogicMeta {getTheme: (themeId: number | string | null | undefined) => DataColorTheme | null // dataThemeLogic) => DataColorTheme | nullautoBreakdownColorsEnabled: (featureFlags: FeatureFlagsSet) => boolean- breakdownValuesIncomplete: (itemsLoading: boolean, insightTiles: DashboardTile[]) => boolean+ breakdownValuesIncomplete: (+ itemsLoading: boolean,+ insightTiles: DashboardTile[],+ waitingTileRefresh: WaitingTileRefresh | null+ ) => booleaneffectiveBreakdownColors: (temporaryBreakdownColors: BreakdownColorConfig[],dashboard: DashboardType | null,@@ -1761,6 +1922,9 @@ export const dashboardLogic = kea<dashboardLogicType>([}) => payload,refreshDashboardWidgets: (payload: { tileIds: number[]; forceRefresh?: boolean }) => payload,toggleTileGroup: (groupKey: string) => ({ groupKey }),+ refreshTileGroup: (groupKey: string) => ({ groupKey }),+ setWaitingTileRefresh: (waiting: WaitingTileRefresh) => ({ waiting }),+ clearWaitingTileRefresh: (tileIds: number[]) => ({ tileIds }),setRevealedTileGroup: (revealedTileGroup: RevealedTileGroup | null) => ({ revealedTileGroup }),/** A band grid reports its own layout, which joins the other bands into one dashboard layout. */updateTileGroupSectionLayout: (layouts: ResponsiveLayouts) => ({ layouts }),@@ -1786,6 +1950,7 @@ export const dashboardLogic = kea<dashboardLogicType>([loading,error,}),+ stopWidgetRefreshes: (tileIds: number[]) => ({ tileIds }),addWidgetTiles: (payload: {dashboardId: numberwidgets: { widgetType: string; config: Record<string, unknown> }[]@@ -2809,6 +2974,17 @@ export const dashboardLogic = kea<dashboardLogicType>([cancelDashboardRefresh: () => ({}),},],+ // Kept when a viewer cancels a refresh, so a group opened later still loads with that refresh's settings.+ waitingTileRefresh: [+ null as WaitingTileRefresh | null,+ {+ setWaitingTileRefresh: (_, { waiting }) => (waiting.tileIds.length > 0 ? waiting : null),+ clearWaitingTileRefresh: (state, { tileIds }) => {+ const remaining = state?.tileIds.filter((tileId) => !tileIds.includes(tileId)) ?? []+ return state && remaining.length > 0 ? { ...state, tileIds: remaining } : null+ },+ },+ ],// 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.@@ -3107,6 +3283,11 @@ export const dashboardLogic = kea<dashboardLogicType>([}return next},+ // A stopped load fetched nothing, so the tile keeps its last fetch time.+ stopWidgetRefreshes: (state, { tileIds }) => ({+ ...state,+ ...Object.fromEntries(tileIds.map((tileId) => [tileId, { ...state[tileId], loading: false }])),+ }),},],refreshEligibilityTick: [0, { recheckRefreshEligibility: (state) => state + 1 }],@@ -3570,6 +3751,15 @@ export const dashboardLogic = kea<dashboardLogicType>([}).filter((groupKey) => groupKey !== revealedTileGroup?.groupKey): [],],+ collapsedTileIds: [+ (s) => [s.tiles, s.collapsedTileGroupKeys],+ (tiles: DashboardTile[], collapsedTileGroupKeys: string[]): Set<number> =>+ new Set(+ tiles+ .filter((tile) => tile.group_key && collapsedTileGroupKeys.includes(tile.group_key))+ .map((tile) => tile.id)+ ),+ ],insightTiles: [(s) => [s.tiles],(tiles: DashboardTile[]) => tiles.filter((t) => !!t.insight).filter((i) => !i.insight?.deleted),@@ -3608,14 +3798,17 @@ export const dashboardLogic = kea<dashboardLogicType>([() => [router.selectors.searchParams],(searchParams: Record<string, any>) => searchParams.highlightInsightId,],+ // A refresh skips tiles in closed groups, so their age must not make the dashboard look stale:+ // on a shared dashboard that would force a full refresh on every load.sortedDates: [- (s) => [s.insightTiles],- (insightTiles: DashboardTile[]): Dayjs[] => {+ (s) => [s.insightTiles, s.collapsedTileIds],+ (insightTiles: DashboardTile[], collapsedTileIds: Set<number>): Dayjs[] => {if (!insightTiles || !insightTiles.length) {return []}const validDates = insightTiles+ .filter((tile) => !collapsedTileIds.has(tile.id)).map((i) => dayjs(i.insight?.last_refresh)).filter((date) => date.isValid())return sortDayJsDates(validDates)@@ -3685,6 +3878,7 @@ export const dashboardLogic = kea<dashboardLogicType>([s.widgetRefreshStatus,s.refreshEligibilityTick,s.placement,+ s.collapsedTileIds,s.pageVisibility,],(@@ -3693,7 +3887,8 @@ export const dashboardLogic = kea<dashboardLogicType>([widgetTiles: DashboardTile[],widgetRefreshStatus: Record<number, { loading?: boolean; error?: string | null; fetchedAt?: number }>,_refreshEligibilityTick: number,- placement: DashboardPlacement+ placement: DashboardPlacement,+ collapsedTileIds: Set<number>) => {return (!(placement === DashboardPlacement.FeatureFlag) &&@@ -3703,7 +3898,7 @@ export const dashboardLogic = kea<dashboardLogicType>([!isEffectiveRefreshStale(effectiveLastRefresh) &&!widgetTiles.some((tile) => {const status = widgetRefreshStatus[tile.id]- return !status?.error && isWidgetStale(status)+ return !collapsedTileIds.has(tile.id) && !status?.error && isWidgetStale(status)}))},@@ -3913,10 +4108,17 @@ export const dashboardLogic = kea<dashboardLogicType>([// itemsLoading alone can't stand in for completeness: a refresh that errors or is// aborted before the insight ever got results leaves the tile with result: null after// loading settles, hiding its breakdown values from the sharing check.+ // A tile in a closed group still holds results from before the last refresh, so its values can be stale too.breakdownValuesIncomplete: [- (s) => [s.itemsLoading, s.insightTiles],- (itemsLoading: boolean, insightTiles: DashboardTile[] | null): boolean =>- itemsLoading || hasUnresolvedBreakdownTiles(insightTiles),+ (s) => [s.itemsLoading, s.insightTiles, s.waitingTileRefresh],+ (+ itemsLoading: boolean,+ insightTiles: DashboardTile[] | null,+ waitingTileRefresh: WaitingTileRefresh | null+ ): boolean =>+ itemsLoading ||+ hasUnresolvedBreakdownTiles(insightTiles) ||+ !!insightTiles?.some((tile) => waitingTileRefresh?.tileIds.includes(tile.id)),],// Persisted colors with unsaved edits merged over them, plus auto-assigned colors for// breakdown values shared by multiple tiles. This is both what tiles render and what a@@ -4091,6 +4293,8 @@ export const dashboardLogic = kea<dashboardLogicType>([beforeUnmount: () => {cache.pendingFilterChange = undefinedcache.widgetTileRefreshScheduler?.cancelAll()+ abortWidgetLoads(cache)+ abortGroupLoads(cache)actions.abortAnyRunningQuery()},})),@@ -4138,6 +4342,7 @@ export const dashboardLogic = kea<dashboardLogicType>([} else if (insightId !== values.revealedTileGroup?.insightId) {// Once per link, so a later URL change does not reopen a group the viewer closed.actions.setRevealedTileGroup({ insightId, groupKey })+ actions.refreshTileGroup(groupKey)}},updateTileGroupSectionLayout: ({ layouts }) => {@@ -4858,10 +5063,14 @@ export const dashboardLogic = kea<dashboardLogicType>([},/** Triggered from dashboard refresh button, when user refreshes entire dashboard */triggerDashboardRefresh: () => {+ // The refresh loads every open widget again, and an older load must not land over the new one.+ abortWidgetLoads(cache)actions.resetInterval()actions.refreshDashboardItems({ action: RefreshDashboardItemsAction.Refresh, forceRefresh: true })if (values.placement !== DashboardPlacement.Export && values.placement !== DashboardPlacement.Public) {- const widgetTileIds = values.widgetTiles.map((tile) => tile.id)+ const widgetTileIds = values.widgetTiles+ .filter((tile) => !values.collapsedTileIds.has(tile.id))+ .map((tile) => tile.id)if (widgetTileIds.length > 0) {actions.refreshDashboardWidgets({ tileIds: widgetTileIds, forceRefresh: true })}@@ -4951,6 +5160,9 @@ export const dashboardLogic = kea<dashboardLogicType>([}},refreshDashboardItems: async ({ action, forceRefresh, previewUnsavedFilters }, breakpoint) => {+ cache.disposables.dispose('openGroupReload')+ abortGroupLoads(cache)+ const groupSignal = (cache.groupAbortController = new AbortController()).signalconst dashboardRefreshStartTime = performance.now()const settingsToRefresh = previewUnsavedFilters? values.currentDashboardSettings@@ -4967,13 +5179,18 @@ export const dashboardLogic = kea<dashboardLogicType>([isInitialLoad && !values.canAutoPreview && values.dashboardSettingsState === 'unsavedChanges'const dashboardId: number = props.id+ const { collapsedTileIds } = valuesconst allInsightTiles = values.insightTiles || []const totalTileCount = allInsightTiles.lengthif (filterChange) {- filterChange.remainingInsightIds = new Set(allInsightTiles.map((tile) => tile.insight!.short_id))+ filterChange.remainingInsightIds = new Set(+ allInsightTiles+ .filter((tile) => !collapsedTileIds.has(tile.id))+ .map((tile) => tile.insight!.short_id)+ )}- const sortedTilesToRefresh = allInsightTiles+ const staleTiles = allInsightTiles// sort tiles so we poll them in the exact order they are computed on the backend.sort((a, b) => (a.order ?? 0) - (b.order ?? 0)).filter((t): t is DashboardTile & { insight: InsightModel } => !!t.insight)@@ -4985,6 +5202,18 @@ export const dashboardLogic = kea<dashboardLogicType>([!t.insight.cache_target_age ||dayjs(t.insight.cache_target_age).isBefore(dayjs()))+ const sortedTilesToRefresh = staleTiles.filter((tile) => !collapsedTileIds.has(tile.id))+ const widgetTileIds = values.widgetTiles.map((tile) => tile.id)+ const openWidgetTileIds = widgetTileIds.filter((id) => !collapsedTileIds.has(id))+ const waiting = nextWaitingTileRefresh({+ previous: values.waitingTileRefresh,+ tiles: values.tiles,+ collapsedTileIds,+ candidateTileIds: [...staleTiles.map((tile) => tile.id), ...widgetTileIds],+ forceRefresh: !!forceRefresh,+ settings: settingsToRefresh,+ })+ actions.setWaitingTileRefresh(waiting)const tilesStaleCount = sortedTilesToRefresh.lengthlet tilesRefreshedCount = 0@@ -5008,8 +5237,23 @@ export const dashboardLogic = kea<dashboardLogicType>([actions.abortAnyRunningQuery()cache.abortController = new AbortController()- const methodOptions: ApiMethodOptions = { signal: cache.abortController.signal }+ }+ const openGroupsWaiting = (): Set<string> =>+ openTileGroupsWaiting(values.tiles, values.collapsedTileIds, values.waitingTileRefresh)+ if (openGroupsWaiting().size > 0) {+ // The aborted group load clears its queued status a few microtasks later, and that status blocks a reload.+ cache.disposables.add(() => {+ const timerId = setTimeout(() => {+ if (!groupSignal.aborted) {+ openGroupsWaiting().forEach((groupKey) => actions.refreshTileGroup(groupKey))+ }+ }, 0)+ return () => clearTimeout(timerId)+ }, 'openGroupReload')+ }++ if (sortedTilesToRefresh.length > 0) {// Cache values used during and after the long-running fetch, since the logic// may be unmounted by the time the awaits complete (kea's no-arg breakpoint()// only cancels on newer invocations, not on unmount).@@ -5018,66 +5262,36 @@ export const dashboardLogic = kea<dashboardLogicType>([const savedMetricVariables = values.savedDashboardSettings.filters.metricVariables ?? []const urlVariables = settingsToRefresh.variables+ const tileRefresh: InsightTileRefresh = {+ actions,+ dashboardId,+ currentTeamId,+ filters: withCompactMetricVariables(effectiveRefreshFilters, savedMetricVariables),+ variables: urlVariables,+ urlFilters,+ forceRefresh: !!forceRefresh,+ signal: cache.abortController.signal,+ queue: tileLoadQueue(cache),+ }+ const loadingHere = (cache.refreshingTileIds = new Set(sortedTilesToRefresh.map((tile) => tile.id)))const fetchSyncInsightFunctions = sortedTilesToRefresh.map((tile) => async () => {- const insight = tile.insight- const queryId = uuid()- const queryStartTime = performance.now()- const dashboardId: number = props.id-- // Set insight as refreshing- actions.setRefreshStatus(insight.short_id, true, true)-- try {- const insightRefreshStartTime = performance.now()- const refreshedInsight = await getInsightWithRetry(- currentTeamId,- insight,- dashboardId,- queryId,- forceRefresh ? 'force_blocking' : 'blocking', // 'blocking' returns cached data if available, when manual refresh is triggered we want fresh results- methodOptions,- withCompactMetricVariables(effectiveRefreshFilters, savedMetricVariables),- urlVariables,- tile.filters_overrides- )-- const queryError = refreshedInsight ? getInsightQueryError(refreshedInsight) : null- if (refreshedInsight && !isRefreshRejectionStub(refreshedInsight) && !queryError) {- dashboardsModel.actions.updateDashboardInsight(refreshedInsight, undefined, dashboardId)- actions.setRefreshStatus(insight.short_id)- tilesRefreshedCount++- filterChange?.remainingInsightIds.delete(insight.short_id)- if (refreshedInsight.is_cached) {- tilesRefreshedCachedCount++- }- reportDashboardTileRefreshed(- dashboardId,- tile,- urlFilters,- urlVariables,- Math.floor(performance.now() - insightRefreshStartTime),- false- )- } else {- actions.setRefreshError(insight.short_id, queryError?.code ? queryError : undefined)- filterChange?.remainingInsightIds.add(insight.short_id)- tilesErroredCount++- }- } catch (e: any) {- filterChange?.remainingInsightIds.add(insight.short_id)- if (shouldCancelQuery(e)) {- console.warn(`Insight refresh cancelled for ${insight.short_id} due to abort signal:`, e)- actions.abortQuery({ queryId, queryStartTime, shortId: insight.short_id })- tilesAbortedCount++- } else {- actions.setRefreshError(insight.short_id, e)- tilesErroredCount+++ const outcome = await refreshInsightTile(tile, tileRefresh)+ loadingHere.delete(tile.id)+ if (outcome === 'refreshed' || outcome === 'cached') {+ if (waiting.tileIds.includes(tile.id)) {+ actions.clearWaitingTileRefresh([tile.id])}+ tilesRefreshedCount+++ tilesRefreshedCachedCount += outcome === 'cached' ? 1 : 0+ filterChange?.remainingInsightIds.delete(tile.insight.short_id)+ } else {+ filterChange?.remainingInsightIds.add(tile.insight.short_id)+ tilesAbortedCount += outcome === 'aborted' ? 1 : 0+ tilesErroredCount += outcome === 'errored' ? 1 : 0}})- // Execute the fetches with concurrency limit of 4- await runWithLimit(fetchSyncInsightFunctions, 4)+ await Promise.all(fetchSyncInsightFunctions.map((fetchInsight) => fetchInsight()))breakpoint()// REFRESH DONE: all insights have been refreshed@@ -5125,6 +5339,7 @@ export const dashboardLogic = kea<dashboardLogicType>([refresh_duration_ms: refreshDurationMs,total_tile_count: totalTileCount,tiles_stale_count: tilesStaleCount,+ tiles_deferred_count: staleTiles.length - tilesStaleCount,tiles_refreshed_count: tilesRefreshedCount,tiles_errored_count: tilesErroredCount,tiles_aborted_count: tilesAbortedCount,@@ -5139,15 +5354,16 @@ export const dashboardLogic = kea<dashboardLogicType>([if (filterChangeIsCurrent && allInsightsSucceeded) {cache.pendingFilterChange = undefined}+ }- if (- (previewUnsavedFilters || initialUrlOverridesArePreviewed) &&- (tilesErroredCount > 0 || tilesAbortedCount > 0)- ) {- actions.previewDashboardChangesFailure()- } else if (previewUnsavedFilters || initialUrlOverridesArePreviewed) {- actions.setPreviewedDashboardSettings(settingsToRefresh)- }+ // Also when every insight sits in a closed group, so the preview settles and later refreshes use it.+ if (+ (previewUnsavedFilters || initialUrlOverridesArePreviewed) &&+ (tilesErroredCount > 0 || tilesAbortedCount > 0)+ ) {+ actions.previewDashboardChangesFailure()+ } else if (previewUnsavedFilters || initialUrlOverridesArePreviewed) {+ actions.setPreviewedDashboardSettings(settingsToRefresh)}if (@@ -5160,11 +5376,8 @@ export const dashboardLogic = kea<dashboardLogicType>([actions.forceRefreshIfStale()}- if (values.placement !== DashboardPlacement.Export && values.placement !== DashboardPlacement.Public) {- const widgetTileIds = values.widgetTiles.map((tile) => tile.id)- if (widgetTileIds.length > 0) {- actions.refreshDashboardWidgets({ tileIds: widgetTileIds, forceRefresh: !!forceRefresh })- }+ if (openWidgetTileIds.length > 0) {+ actions.refreshDashboardWidgets({ tileIds: openWidgetTileIds, forceRefresh: !!forceRefresh })}},toggleTileGroup: ({ groupKey }) => {@@ -5175,40 +5388,117 @@ export const dashboardLogic = kea<dashboardLogicType>([tile_count: values.tiles.filter((tile) => tile.group_key === groupKey).length,})dashboardTileGroupChoicesLogic.actions.setTileGroupCollapsed(props.id, groupKey, collapse)+ if (!collapse) {+ actions.refreshTileGroup(groupKey)+ }+ },+ refreshTileGroup: async ({ groupKey }) => {+ const { waitingTileRefresh: waiting, refreshStatus, widgetRefreshStatus } = values+ // A tile leaves the waiting list only once it loads, so one still loading must not load again.+ const tiles = values.tiles.filter(+ (tile) =>+ tile.group_key === groupKey &&+ waiting?.tileIds.includes(tile.id) &&+ !cache.refreshingTileIds?.has(tile.id) &&+ !(tile.insight+ ? refreshStatus[tile.insight.short_id]?.queued+ : widgetRefreshStatus[tile.id]?.loading)+ )+ if (!waiting || tiles.length === 0) {+ return+ }++ const widgetTileIds = tiles.filter((tile) => tile.widget).map((tile) => tile.id)+ if (widgetTileIds.length > 0) {+ actions.refreshDashboardWidgets({ tileIds: widgetTileIds, forceRefresh: waiting.forceRefresh })+ }++ const insightTiles = tiles.filter(+ (tile): tile is DashboardTile & { insight: InsightModel } => !!tile.insight+ )+ if (insightTiles.length === 0) {+ return+ }+ // Grow "X out of Y", so the tiles that already loaded still count as done.+ actions.setRefreshTilesTotal(+ (values.refreshTilesTotal ?? Object.keys(refreshStatus).length) ++ insightTiles.filter((tile) => !(tile.insight.short_id in refreshStatus)).length+ )+ insightTiles.forEach((tile) => actions.setRefreshStatus(tile.insight.short_id, false, true))++ cache.groupAbortController ??= new AbortController()+ const tileRefresh: InsightTileRefresh = {+ actions,+ dashboardId: props.id,+ currentTeamId: values.currentTeamId,+ filters: withCompactMetricVariables(+ combineDashboardFilters(waiting.settings.filters, values.externalFilters),+ values.savedDashboardSettings.filters.metricVariables ?? []+ ),+ variables: waiting.settings.variables,+ urlFilters: values.urlFilters,+ forceRefresh: waiting.forceRefresh,+ signal: cache.groupAbortController.signal,+ queue: tileLoadQueue(cache),+ }+ await Promise.all(+ insightTiles.map(async (tile) => {+ const outcome = await refreshInsightTile(tile, tileRefresh)+ if (outcome === 'refreshed' || outcome === 'cached') {+ actions.clearWaitingTileRefresh([tile.id])+ }+ })+ )},refreshDashboardWidgets: async ({ tileIds, forceRefresh }, breakpoint) => {if (values.placement === DashboardPlacement.Export || values.placement === DashboardPlacement.Public) {return}+ const loadingTileIds = forceRefresh+ ? []+ : tileIds.filter((tileId) => values.widgetRefreshStatus[tileId]?.loading)const staleTileIds = tileIds.filter((tileId) => {if (forceRefresh) {return true}const fetchedAt = values.widgetRefreshStatus[tileId]?.fetchedAtreturn (- !!values.widgetRefreshStatus[tileId]?.error ||- !fetchedAt ||- Date.now() - fetchedAt >= WIDGET_CLIENT_TTL_MS+ !loadingTileIds.includes(tileId) &&+ (!!values.widgetRefreshStatus[tileId]?.error ||+ !fetchedAt ||+ Date.now() - fetchedAt >= WIDGET_CLIENT_TTL_MS))})+ actions.clearWaitingTileRefresh(+ tileIds.filter((tileId) => !staleTileIds.includes(tileId) && !loadingTileIds.includes(tileId))+ )if (staleTileIds.length === 0) {return}+ // Not the insight queries' controller: every insight refresh aborts that one, and an aborted widget load+ // would show an error rather than its results. Only a cancel or a manual refresh stops widget loads.+ cache.widgetAbortController ??= new AbortController()+ const { signal } = cache.widgetAbortController+ cache.widgetLoadSignals ??= new Map<number, AbortSignal>()+ staleTileIds.forEach((tileId) => cache.widgetLoadSignals.set(tileId, signal))actions.setWidgetRefreshStatuses(staleTileIds, true)await breakpoint()const projectId = String(values.currentTeamId)const dashboardId = props.id+ const queue = tileLoadQueue(cache)const fetchFunctions = chunkTileIds(staleTileIds, 4).map((chunk) => async () => {try {- const results = await fetchRunWidgets(projectId, dashboardId, chunk, {- signal: cache.abortController?.signal,- })+ const results = await queue.run(+ () => fetchRunWidgets(projectId, dashboardId, chunk, { signal }),+ signal+ )const resultsByTileId = Object.fromEntries(results.map((result) => [result.tile_id, result]))actions.setWidgetRunResults(resultsByTileId)+ actions.clearWaitingTileRefresh(results.filter((result) => !result.error).map((r) => r.tile_id))for (const result of results) {actions.setWidgetRefreshStatuses([result.tile_id],@@ -5217,11 +5507,18 @@ export const dashboardLogic = kea<dashboardLogicType>([)}} catch {+ if (signal.aborted) {+ // After a manual refresh a newer load owns the tile, and its status must stay with that load.+ actions.stopWidgetRefreshes(+ chunk.filter((tileId) => cache.widgetLoadSignals.get(tileId) === signal)+ )+ return+ }actions.setWidgetRefreshStatuses(chunk, false, DASHBOARD_WIDGET_FETCH_ERROR_MESSAGE)}})- await runWithLimit(fetchFunctions, 4)+ await Promise.all(fetchFunctions.map((fetchWidgets) => fetchWidgets()))},addWidgetTiles: async ({ dashboardId, widgets }) => {if (widgets.length === 0) {@@ -5617,6 +5914,8 @@ export const dashboardLogic = kea<dashboardLogicType>([},cancelDashboardRefresh: () => {actions.abortAnyRunningQuery()+ abortWidgetLoads(cache)+ abortGroupLoads(cache)},abortQuery: async ({ queryId, queryStartTime }) => {const { currentTeamId, dashboardLoadData } = valuesdiff --git a/frontend/src/scenes/dashboard/dashboardUtils.test.ts b/frontend/src/scenes/dashboard/dashboardUtils.test.tsindex 18e88e7668f4..4b4f9d45daa1 100644--- a/frontend/src/scenes/dashboard/dashboardUtils.test.ts+++ b/frontend/src/scenes/dashboard/dashboardUtils.test.ts@@ -37,6 +37,7 @@ import {SEARCH_PARAM_FILTERS_KEY,SEARCH_PARAM_QUERY_VARIABLES_KEY,shouldSharedDashboardAutoForceForStaleTime,+ TaskQueue,} from './dashboardUtils'describe('searchParamsWithUrlFilters', () => {@@ -612,6 +613,34 @@ describe('getInsightWithRetry', () => {})})+describe('TaskQueue', () => {+ it.each([+ { name: 'before it joins the queue', abortBeforeRun: true },+ { name: 'while it waits', abortBeforeRun: false },+ ])('drops a task whose signal aborts $name and starts the next one', async ({ abortBeforeRun }) => {+ const queue = new TaskQueue(1)+ const started: string[] = []+ let finishFirst: () => void = () => {}+ const first = queue.run(() => {+ started.push('first')+ return new Promise<void>((resolve) => (finishFirst = resolve))+ })+ const controller = new AbortController()+ if (abortBeforeRun) {+ controller.abort()+ }+ const dropped = queue.run(async () => void started.push('dropped'), controller.signal)+ const last = queue.run(async () => void started.push('last'))+ controller.abort()++ await expect(dropped).rejects.toMatchObject({ name: 'AbortError' })+ expect(started).toEqual(['first'])+ finishFirst()+ await Promise.all([first, last])+ expect(started).toEqual(['first', 'last'])+ })+})+describe('shouldSharedDashboardAutoForceForStaleTime', () => {it.each<[string, dayjs.Dayjs | null, boolean]>([['last refresh is null', null, false],diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.tsindex 2609dee608f3..e58ffa5462f4 100644--- a/frontend/src/scenes/dashboard/dashboardUtils.ts+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts@@ -296,6 +296,49 @@ export async function runWithLimit<T>(tasks: (() => Promise<T>)[], limit: numberreturn results}+/**+ * Runs tasks in first-in, first-out order with at most `limit` tasks at the same time.+ * A task whose signal aborts while it waits does not start, and its promise rejects with an AbortError.+ */+export class TaskQueue {+ private running = 0+ private readonly waiting: { start: () => void; drop: () => void }[] = []++ constructor(private readonly limit: number) {}++ run<T>(task: () => Promise<T>, signal?: AbortSignal): Promise<T> {+ return new Promise<T>((resolve, reject) => {+ if (signal?.aborted) {+ reject(new DOMException('Aborted', 'AbortError'))+ return+ }+ const entry = {+ start: (): void => {+ signal?.removeEventListener('abort', entry.drop)+ this.running+++ new Promise<T>((resolveTask) => resolveTask(task())).then(resolve, reject).finally(() => {+ this.running--+ this.startNext()+ })+ },+ drop: (): void => {+ this.waiting.splice(this.waiting.indexOf(entry), 1)+ reject(new DOMException('Aborted', 'AbortError'))+ },+ }+ signal?.addEventListener('abort', entry.drop, { once: true })+ this.waiting.push(entry)+ this.startNext()+ })+ }++ private startNext(): void {+ while (this.running < this.limit && this.waiting.length > 0) {+ this.waiting.shift()!.start()+ }+ }+}+export const layoutsByTile = (layouts: ResponsiveLayouts): Record<string, Record<DashboardLayoutSize, TileLayout>> => {const itemLayouts: Record<string, Record<DashboardLayoutSize, TileLayout>> = {}diff --git a/products/dashboards/backend/widget_specs/openapi.py b/products/dashboards/backend/widget_specs/openapi.pyindex ccdc5f0210ec..025bb1f88285 100644--- a/products/dashboards/backend/widget_specs/openapi.py+++ b/products/dashboards/backend/widget_specs/openapi.py@@ -58,7 +58,8 @@ class DashboardTileGroupSerializer(serializers.Serializer):required=False,help_text=("Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their "- "browser keeps that choice and it wins over this default."+ "browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer "+ "opens it."),)color = serializers.ChoiceField(diff --git a/products/dashboards/frontend/generated/api.schemas.ts b/products/dashboards/frontend/generated/api.schemas.tsindex 86030a340db8..35a7c9b90b12 100644--- a/products/dashboards/frontend/generated/api.schemas.ts+++ b/products/dashboards/frontend/generated/api.schemas.ts@@ -317,7 +317,7 @@ export interface DashboardTileGroupApi {* @maxLength 200*/title?: string- /** Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. */+ /** Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it. */collapsed?: boolean/** Color of a dot before the group heading. Null or omitted shows no dot.*diff --git a/products/dashboards/frontend/generated/api.zod.ts b/products/dashboards/frontend/generated/api.zod.tsindex 14a5c3593c2d..0b982a0bd74e 100644--- a/products/dashboards/frontend/generated/api.zod.ts+++ b/products/dashboards/frontend/generated/api.zod.ts@@ -222,7 +222,7 @@ export const DashboardsCreateBody = /* @__PURE__ */ zod.boolean().optional().describe(- 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.'+ 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.'),color: zod.union([@@ -355,7 +355,7 @@ export const DashboardsUpdateBody = /* @__PURE__ */ zod.boolean().optional().describe(- 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.'+ 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.'),color: zod.union([@@ -613,7 +613,7 @@ export const DashboardsPartialUpdateBody = /* @__PURE__ */ zod.boolean().optional().describe(- 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.'+ 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.'),color: zod.union([@@ -2146,7 +2146,7 @@ export const DashboardsCreateFromTemplateJsonCreateBody = /* @__PURE__ */ zod.boolean().optional().describe(- 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.'+ 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.'),color: zod.union([@@ -2286,7 +2286,7 @@ export const DashboardsCreateUnlistedDashboardCreateBody = /* @__PURE__ */ zod.boolean().optional().describe(- 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.'+ 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.'),color: zod.union([diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex d75b7561da91..db9fe751e5ff 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -28259,7 +28259,7 @@ export namespace Schemas {* @maxLength 200*/title?: string;- /** Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. */+ /** Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it. */collapsed?: boolean;/** Color of a dot before the group heading. Null or omitted shows no dot.*diff --git a/services/mcp/src/generated/dashboards/api.ts b/services/mcp/src/generated/dashboards/api.tsindex 69d02fb776e8..8bbb8b8b15af 100644--- a/services/mcp/src/generated/dashboards/api.ts+++ b/services/mcp/src/generated/dashboards/api.ts@@ -199,7 +199,7 @@ export const DashboardsCreateBody = () => zod.boolean().optional().describe(- 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.'+ 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.'),color: zod.union([@@ -413,7 +413,7 @@ export const DashboardsPartialUpdateBody = () => zod.boolean().optional().describe(- 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.'+ 'Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.'),color: zod.union([diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-create.jsonindex 8bb35cff1ade..8e311ef4b404 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-create.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-create.json@@ -99,7 +99,7 @@"additionalProperties": {"properties": {"collapsed": {- "description": "Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.",+ "description": "Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.","type": "boolean"},"color": {diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-update.jsonindex 0e691360914e..0bcd741b4c2c 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-update.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/dashboard-update.json@@ -129,7 +129,7 @@"additionalProperties": {"properties": {"collapsed": {- "description": "Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default.",+ "description": "Whether the group starts closed. Defaults to false. When a viewer opens or closes the group, their browser keeps that choice and it wins over this default. A closed group loads its tiles when a viewer opens it.","type": "boolean"},"color": {