From 9086799ff2e3c1ef0c5d1871372b70f4383a7689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Oberm=C3=BCller?= Date: Fri, 26 Jul 2024 18:16:40 +0200 Subject: [PATCH] refactor(insights): use query based insight for setting metadata (#24002) --- frontend/src/scenes/insights/insightLogic.ts | 58 ++++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/frontend/src/scenes/insights/insightLogic.ts b/frontend/src/scenes/insights/insightLogic.ts index 457e10f261c3c..383bee462f721 100644 --- a/frontend/src/scenes/insights/insightLogic.ts +++ b/frontend/src/scenes/insights/insightLogic.ts @@ -1,4 +1,3 @@ -import { captureException } from '@sentry/react' import { actions, connect, events, kea, key, listeners, path, props, reducers, selectors } from 'kea' import { loaders } from 'kea-loaders' import { router } from 'kea-router' @@ -100,7 +99,11 @@ export const insightLogic = kea([ insightUpdate, callback, }), - setInsightMetadata: (metadata: Partial) => ({ metadata }), + setInsightMetadata: ( + metadataUpdate: Partial> + ) => ({ + metadataUpdate, + }), highlightSeries: (seriesIndex: number | null) => ({ seriesIndex }), }), loaders(({ actions, values, props }) => ({ @@ -142,41 +145,29 @@ export const insightLogic = kea([ dashboardsModel.actions.updateDashboardInsight(updatedInsight, removedDashboards) return updatedInsight }, - setInsightMetadata: async ({ metadata }, breakpoint) => { + setInsightMetadata: async ({ metadataUpdate }, breakpoint) => { const editMode = insightSceneLogic.isMounted() && - insightSceneLogic.values.legacyInsight === values.legacyInsight && + insightSceneLogic.values.queryBasedInsight === values.queryBasedInsight && insightSceneLogic.values.insightMode === ItemMode.Edit if (editMode) { - return { ...values.legacyInsight, ...metadata } - } - - if (metadata.filters || metadata.query) { - const error = new Error(`Will not override filters or query in setInsightMetadata`) - captureException(error) - throw error + return { ...values.legacyInsight, ...metadataUpdate } } const beforeUpdates = {} - for (const key of Object.keys(metadata)) { + for (const key of Object.keys(metadataUpdate)) { beforeUpdates[key] = values.savedInsight[key] } - const response = await api.update( - `api/projects/${teamLogic.values.currentTeamId}/insights/${values.legacyInsight.id}`, - metadata - ) + const response = await insightsApi.update(values.queryBasedInsight.id, metadataUpdate, { + writeAsQuery: values.queryBasedInsightSaving, + readAsQuery: false, + }) breakpoint() - // only update the fields that we changed - const updatedInsight = { ...values.legacyInsight } as InsightModel - for (const key of Object.keys(metadata)) { - updatedInsight[key] = response[key] - } - savedInsightsLogic.findMounted()?.actions.loadInsights() - dashboardsModel.actions.updateDashboardInsight(updatedInsight) + dashboardsModel.actions.updateDashboardInsight(response) actions.loadTags() lemonToast.success(`Updated insight`, { @@ -184,23 +175,18 @@ export const insightLogic = kea([ label: 'Undo', dataAttr: 'edit-insight-undo', action: async () => { - const response = await api.update( - `api/projects/${teamLogic.values.currentTeamId}/insights/${values.queryBasedInsight.id}`, - beforeUpdates - ) - // only update the fields that we changed - const revertedInsight = { ...values.legacyInsight } as InsightModel - for (const key of Object.keys(beforeUpdates)) { - revertedInsight[key] = response[key] - } + const response = await insightsApi.update(values.queryBasedInsight.id, beforeUpdates, { + writeAsQuery: values.queryBasedInsightSaving, + readAsQuery: false, + }) savedInsightsLogic.findMounted()?.actions.loadInsights() - dashboardsModel.actions.updateDashboardInsight(revertedInsight) - actions.setInsight(revertedInsight, { overrideFilter: false, fromPersistentApi: true }) + dashboardsModel.actions.updateDashboardInsight(response) + actions.setInsight(response, { overrideFilter: false, fromPersistentApi: true }) lemonToast.success('Insight change reverted') }, }, }) - return updatedInsight + return response }, }, ], @@ -232,7 +218,7 @@ export const insightLogic = kea([ query: clearInsightQuery ? undefined : state.query, } }, - setInsightMetadata: (state, { metadata }) => ({ ...state, ...metadata }), + setInsightMetadata: (state, { metadataUpdate }) => ({ ...state, ...metadataUpdate }), [dashboardsModel.actionTypes.updateDashboardInsight]: (state, { item, extraDashboardIds }) => { const targetDashboards = (item?.dashboards || []).concat(extraDashboardIds || []) const updateIsForThisDashboard =