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(insights): stop dashboard date override from overwriting saved insight (#72359)

Matt Pua7cea227d12bd
diff --git a/frontend/src/scenes/insights/insightLogic.test.ts b/frontend/src/scenes/insights/insightLogic.test.ts
index 722b1719626a..8b91c2cf498d 100644
--- a/frontend/src/scenes/insights/insightLogic.test.ts
+++ b/frontend/src/scenes/insights/insightLogic.test.ts
@@ -1169,4 +1169,56 @@ describe('insightLogic', () => {
})
})
})
+
+ describe('updateDashboardInsight query merge', () => {
+ const canonicalQuery = {
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [{ kind: NodeKind.EventsNode, event: '$pageview', math: BaseMathType.TotalCount }],
+ dateRange: { date_from: 'all' },
+ },
+ }
+ const dashboardOverriddenQuery = {
+ ...canonicalQuery,
+ source: {
+ ...canonicalQuery.source,
+ dateRange: { date_from: '-14d' },
+ },
+ }
+
+ beforeEach(() => {
+ logic = insightLogic({
+ dashboardItemId: Insight42,
+ dashboardId: MOCK_DASHBOARD_ID,
+ cachedInsight: insightModelWith({ query: canonicalQuery }),
+ })
+ logic.mount()
+ })
+
+ it('does not overwrite the canonical query on a dashboard-scoped refresh', async () => {
+ dashboardsModel.actions.updateDashboardInsight(
+ insightModelWith({
+ query: dashboardOverriddenQuery,
+ dashboards: [MOCK_DASHBOARD_ID],
+ dashboard_tiles: [{ dashboard_id: MOCK_DASHBOARD_ID }],
+ }),
+ undefined,
+ MOCK_DASHBOARD_ID
+ )
+
+ await expectLogic(logic).toMatchValues({ insight: partial({ query: canonicalQuery }) })
+ })
+
+ it('applies the query from a non-scoped update', async () => {
+ dashboardsModel.actions.updateDashboardInsight(
+ insightModelWith({
+ query: dashboardOverriddenQuery,
+ dashboards: [MOCK_DASHBOARD_ID],
+ })
+ )
+
+ await expectLogic(logic).toMatchValues({ insight: partial({ query: dashboardOverriddenQuery }) })
+ })
+ })
})
diff --git a/frontend/src/scenes/insights/insightLogic.tsx b/frontend/src/scenes/insights/insightLogic.tsx
index 533ca2d3cce2..93ee294b6c07 100644
--- a/frontend/src/scenes/insights/insightLogic.tsx
+++ b/frontend/src/scenes/insights/insightLogic.tsx
@@ -765,6 +765,12 @@ export const insightLogic: LogicWrapper<insightLogicType> = kea<insightLogicType
props.dashboardId &&
targetDashboards.includes(props.dashboardId)
if (updateIsForThisDashboard) {
+ // Dashboard refreshes return a view-only query with the dashboard filters applied.
+ // Keep that query out of the canonical insight state that the editor persists.
+ if (sourceDashboardId != null) {
+ const { query: _query, result: _result, ...insightWithoutDashboardOverrides } = insight
+ return { ...state, ...insightWithoutDashboardOverrides }
+ }
return { ...state, ...insight }
}
return state