Skip to content

: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.

fix(dashboards): release the refresh block when the tiles are stale (#102793)

posthog[bot]a047448ab449
diff --git a/frontend/src/scenes/dashboard/DashboardReloadAction.test.tsx b/frontend/src/scenes/dashboard/DashboardReloadAction.test.tsx
new file mode 100644
index 000000000000..4931a2fe2bc4
--- /dev/null
+++ b/frontend/src/scenes/dashboard/DashboardReloadAction.test.tsx
@@ -0,0 +1,113 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, render } from '@testing-library/react'
+import { BindLogic } from 'kea'
+import { expectLogic } from 'kea-test-utils'
+
+import { dayjs } from 'lib/dayjs'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+import { AccessControlLevel, DashboardTile, DashboardType, InsightModel } from '~/types'
+
+import { dashboardLogic } from './dashboardLogic'
+import { DashboardReloadAction } from './DashboardReloadAction'
+import * as dashboardUtils from './dashboardUtils'
+
+const REFRESH_BUTTON_SELECTOR = '[data-attr="dashboard-items-action-refresh"]'
+// The "up to date" check icon is what contradicted a stale age, and its clip path is the only marker it leaves
+const CHECK_ICON_SELECTOR = 'clipPath[id="icon/check__a"]'
+
+function makeDashboard(tileLastRefresh: string): DashboardType {
+ const insight = {
+ id: 1,
+ short_id: 'insight1',
+ name: 'An insight',
+ last_refresh: tileLastRefresh,
+ query: null,
+ result: [],
+ filters: {},
+ } as unknown as InsightModel
+
+ return {
+ id: 5,
+ name: 'Test dashboard',
+ pinned: false,
+ tiles: [{ id: 1, color: null, layouts: {}, insight } as unknown as DashboardTile],
+ tags: [],
+ created_at: '2020-01-01T00:00:00Z',
+ last_accessed_at: '2020-01-01T00:00:00Z',
+ is_shared: false,
+ deleted: false,
+ creation_mode: 'default',
+ user_access_level: AccessControlLevel.Editor,
+ filters: {},
+ variables: {},
+ } as unknown as DashboardType
+}
+
+describe('DashboardReloadAction', () => {
+ let getInsightWithRetrySpy: jest.SpyInstance
+
+ beforeEach(() => {
+ useMocks({
+ post: {
+ '/api/environments/:team_id/query/': () => [200, { results: [] }],
+ },
+ })
+ initKeaTests()
+ // The tile refresh on load fails, so the tile keeps its old `last_refresh`
+ getInsightWithRetrySpy = jest
+ .spyOn(dashboardUtils, 'getInsightWithRetry')
+ .mockRejectedValue(new Error('Queries are a little too busy right now.'))
+ })
+
+ afterEach(() => {
+ getInsightWithRetrySpy.mockRestore()
+ cleanup()
+ })
+
+ async function renderAction(tileLastRefresh: string): Promise<ReturnType<typeof dashboardLogic.build>> {
+ const dashboard = makeDashboard(tileLastRefresh)
+ const props = { id: dashboard.id, dashboard }
+ const logic = dashboardLogic(props)
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ // A teammate's refresh writes the shared `Dashboard.last_refresh`, which starts the block window
+ await expectLogic(logic, () => {
+ logic.actions.updateDashboardLastRefresh(dayjs())
+ }).toFinishAllListeners()
+
+ render(
+ <BindLogic logic={dashboardLogic} props={props}>
+ <DashboardReloadAction />
+ </BindLogic>
+ )
+
+ return logic
+ }
+
+ it.each([
+ {
+ scenario: 'stale tiles release the block',
+ tileLastRefresh: () => dayjs().subtract(7, 'hours'),
+ ariaDisabled: 'false',
+ showsCheckIcon: false,
+ },
+ {
+ scenario: 'fresh tiles keep the block',
+ tileLastRefresh: () => dayjs(),
+ ariaDisabled: 'true',
+ showsCheckIcon: true,
+ },
+ ])('$scenario', async ({ tileLastRefresh, ariaDisabled, showsCheckIcon }) => {
+ const logic = await renderAction(tileLastRefresh().toISOString())
+
+ expect(logic.values.itemsLoading).toBe(false)
+ expect(document.querySelector(REFRESH_BUTTON_SELECTOR)).toHaveAttribute('aria-disabled', ariaDisabled)
+ expect(!!document.querySelector(CHECK_ICON_SELECTOR)).toBe(showsCheckIcon)
+
+ logic.unmount()
+ })
+})
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
index 2ff231177fa5..8a3f3bab26e2 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
@@ -2642,6 +2642,7 @@ describe('dashboardLogic', () => {
expect(logic.values.oldestRefreshed?.toISOString()).toEqual(dayjs(staleIso).toISOString())
expect(logic.values.effectiveLastRefresh?.toISOString()).toEqual(dayjs(staleIso).toISOString())
+ expect(logic.values.blockRefresh).toBe(false)
})
it('persisting the last refresh keeps refreshed results instead of an earlier snapshot', async () => {
@@ -2671,7 +2672,22 @@ describe('dashboardLogic', () => {
})
describe('insight refresh', () => {
- it('allows another manual dashboard refresh after five minutes', () => {
+ it('allows another manual dashboard refresh after five minutes', async () => {
+ await expectLogic(logic).toFinishAllListeners()
+ for (const tile of logic.values.dashboard!.tiles) {
+ const insight = tile.insight
+ if (!insight) {
+ continue
+ }
+ await expectLogic(logic, () => {
+ dashboardsModel.actions.updateDashboardInsight(
+ { ...insight, last_refresh: now().toISOString(), query: insight.query ?? null },
+ undefined,
+ 5
+ )
+ }).toFinishAllListeners()
+ }
+
const recentRefresh = now().subtract(4, 'minutes')
logic.actions.updateDashboardLastRefresh(recentRefresh)
expect(logic.values.blockRefresh).toBe(true)
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index ab2400572380..bc6640025d91 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -138,6 +138,7 @@ import {
DASHBOARD_MIN_REFRESH_INTERVAL_MINUTES,
IS_TEST_MODE,
AUTO_PREVIEW_TILE_LIMIT,
+ isEffectiveRefreshStale,
SEARCH_PARAM_FILTERS_KEY,
SEARCH_PARAM_QUERY_VARIABLES_KEY,
combineDashboardFilters,
@@ -1209,6 +1210,7 @@ export interface dashboardLogicMeta {
nextAllowedDashboardRefresh: (lastDashboardRefresh: Dayjs | null) => Dayjs | null
blockRefresh: (
nextAllowedDashboardRefresh: Dayjs | null,
+ effectiveLastRefresh: Dayjs | null,
placement: DashboardPlacement,
pageVisibility: boolean
) => boolean
@@ -3086,13 +3088,14 @@ export const dashboardLogic = kea<dashboardLogicType>([
],
blockRefresh: [
// page visibility is only here to trigger a recompute when the page is hidden/shown
- (s) => [s.nextAllowedDashboardRefresh, s.placement, s.pageVisibility],
- (nextAllowedDashboardRefresh: Dayjs, placement: DashboardPlacement) => {
+ (s) => [s.nextAllowedDashboardRefresh, s.effectiveLastRefresh, s.placement, s.pageVisibility],
+ (nextAllowedDashboardRefresh: Dayjs, effectiveLastRefresh: Dayjs | null, placement: DashboardPlacement) => {
return (
!(placement === DashboardPlacement.FeatureFlag) &&
!(placement === DashboardPlacement.Group) &&
!!nextAllowedDashboardRefresh &&
- nextAllowedDashboardRefresh?.isAfter(now())
+ nextAllowedDashboardRefresh?.isAfter(now()) &&
+ !isEffectiveRefreshStale(effectiveLastRefresh)
)
},
],
diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.ts
index 486e74304205..910ee6190ed2 100644
--- a/frontend/src/scenes/dashboard/dashboardUtils.ts
+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts
@@ -208,6 +208,15 @@ export function shouldSharedDashboardAutoForceForStaleTime(effectiveLastRefresh:
return ageMinutes !== null && ageMinutes >= SHARED_DASHBOARD_AUTO_FORCE_IF_STALE_MINUTES
}
+/**
+ * `Dashboard.last_refresh` is shared by all viewers, so one person's refresh can start a block
+ * window for everybody while the tiles keep showing old data. In that state the block must give way.
+ */
+export function isEffectiveRefreshStale(effectiveLastRefresh: Dayjs | null): boolean {
+ const ageMinutes = staleAgeMinutes(effectiveLastRefresh)
+ return ageMinutes !== null && ageMinutes >= DASHBOARD_MIN_REFRESH_INTERVAL_MINUTES
+}
+
// Helper function for exponential backoff
const wait = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))