: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(data-warehouse): attach dashboard when saving SQL insight from dashboard flow (#63244)
George Munyoro7601522cdd2d
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.tsindex 3bc65844c334..e18cc2c22198 100644--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts@@ -2,6 +2,7 @@ import { router } from 'kea-router'import { expectLogic, partial } from 'kea-test-utils'import { databaseTableListLogic } from 'scenes/data-management/database/databaseTableListLogic'+import { insightsApi } from 'scenes/insights/utils/api'import { sceneLogic } from 'scenes/sceneLogic'import { teamLogic } from 'scenes/teamLogic'import { urls } from 'scenes/urls'@@ -1480,4 +1481,97 @@ describe('sqlEditorLogic', () => {expect(model.pushEditOperations).not.toHaveBeenCalled()})})++ describe('attaching the dashboard when saving from a dashboard flow', () => {+ const DASHBOARD_ID = 99++ afterEach(() => {+ jest.restoreAllMocks()+ })++ it.each([+ { name: 'passes dashboards to insightsApi.create when a dashboardId is set', dashboardId: DASHBOARD_ID },+ { name: 'does not pass dashboards to insightsApi.create when no dashboardId is set', dashboardId: null },+ ])('$name', async ({ dashboardId }) => {+ const createSpy = jest.spyOn(insightsApi, 'create').mockResolvedValue(MOCK_INSIGHT)++ logic = sqlEditorLogic({+ tabId: TAB_ID,+ monaco: createMockMonaco(),+ editor: createMockEditor(),+ })+ logic.mount()++ logic.actions.createTab('SELECT count() FROM events')+ await expectLogic(logic).toDispatchActions(['createTab', 'updateTab'])+ if (dashboardId !== null) {+ logic.actions.setDashboardId(dashboardId)+ }++ logic.actions.saveAsInsightSubmit('My SQL insight')+ await expectLogic(logic).toFinishAllListeners()++ expect(createSpy).toHaveBeenCalledTimes(1)+ const createPayload = createSpy.mock.calls[0][0]+ expect(createPayload).toMatchObject({ name: 'My SQL insight', saved: true })+ if (dashboardId !== null) {+ expect(createPayload.dashboards).toEqual([dashboardId])+ } else {+ expect(createPayload).not.toHaveProperty('dashboards')+ }+ })++ // The update path unions the target dashboard with the insight's existing links, read+ // from both dashboard_tiles (preferred) and the legacy dashboards field, deduped.+ it.each([+ {+ name: 'merges the dashboard with existing legacy dashboards links',+ dashboards: [7],+ dashboardTiles: [],+ expected: [7, DASHBOARD_ID],+ },+ {+ name: 'merges the dashboard with existing dashboard_tiles links',+ dashboards: [],+ dashboardTiles: [{ id: 1, dashboard_id: 7, deleted: null }],+ expected: [7, DASHBOARD_ID],+ },+ {+ name: 'does not duplicate a dashboard the insight is already linked to',+ dashboards: [],+ dashboardTiles: [{ id: 1, dashboard_id: DASHBOARD_ID, deleted: null }],+ expected: [DASHBOARD_ID],+ },+ ])('$name', async ({ dashboards, dashboardTiles, expected }) => {+ const updateSpy = jest.spyOn(insightsApi, 'update').mockResolvedValue(MOCK_INSIGHT)++ logic = sqlEditorLogic({+ tabId: TAB_ID,+ monaco: createMockMonaco(),+ editor: createMockEditor(),+ })+ logic.mount()+ editorRootLogic = editorSceneLogic({ tabId: TAB_ID })+ editorRootLogic.mount()++ const insightOnDashboards = {+ ...MOCK_INSIGHT,+ dashboards,+ dashboard_tiles: dashboardTiles,+ } as QueryBasedInsightModel+ logic.actions.editInsight(MOCK_INSIGHT_QUERY.source.query, insightOnDashboards)+ await expectLogic(logic)+ .toDispatchActions(['createTab', 'updateTab'])+ .toMatchValues({ editingInsight: partial({ short_id: MOCK_INSIGHT_SHORT_ID }) })++ logic.actions.setDashboardId(DASHBOARD_ID)+ logic.actions.updateInsight()+ await expectLogic(logic).toFinishAllListeners()++ expect(updateSpy).toHaveBeenCalledTimes(1)+ const [, updatePayload] = updateSpy.mock.calls[0]+ // Order-independent: only the set of linked dashboards matters.+ expect([...(updatePayload.dashboards ?? [])].sort()).toEqual([...expected].sort())+ })+ })})diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsxindex eba9da02d08f..4d8e49033726 100644--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx@@ -1566,6 +1566,7 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([name,query: sourceQueryToSave,saved: true,+ ...(dashboardId ? { dashboards: [dashboardId] } : {}),})const logic = insightLogic({dashboardItemId: insight.short_id,@@ -1683,6 +1684,17 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([query: currentVisualizationQuery,}+ // When saving from a dashboard flow, attach the tile server-side without+ // dropping the insight's existing dashboard links.+ const dashboardId = values.dashboardId+ if (dashboardId) {+ const existingDashboardIds = [+ ...(values.editingInsight.dashboard_tiles?.map((tile) => tile.dashboard_id) ?? []),+ ...(values.editingInsight.dashboards ?? []),+ ]+ insightRequest.dashboards = Array.from(new Set([...existingDashboardIds, dashboardId]))+ }+let savedInsight: QueryBasedInsightModeltry {savedInsight = await insightsApi.update(values.editingInsight.id, insightRequest)@@ -1715,7 +1727,6 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([})}- const dashboardId = values.dashboardIdif (dashboardId) {dashboardsModel.findMounted()?.actions.updateDashboardInsight(savedInsight)dashboardLogic.findMounted({ id: dashboardId })?.actions.loadDashboard({