From 4c61a11e7824b3dd3390e24fb6fb4caabc01516e Mon Sep 17 00:00:00 2001 From: Tom Owers Date: Mon, 15 Jul 2024 16:59:01 +0100 Subject: [PATCH] fix(data-warehouse): Auto reload source settings scene (#23713) --- .../settings/dataWarehouseSettingsLogic.ts | 68 ----------------- .../settings/source/Schemas.tsx | 4 +- .../dataWarehouseSourceSettingsLogic.ts | 75 ++++++++++++++++++- 3 files changed, 74 insertions(+), 73 deletions(-) diff --git a/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsLogic.ts b/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsLogic.ts index d34ff71b94041..608ec821867ed 100644 --- a/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsLogic.ts +++ b/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsLogic.ts @@ -35,8 +35,6 @@ export const dataWarehouseSettingsLogic = kea([ actions({ deleteSource: (source: ExternalDataStripeSource) => ({ source }), reloadSource: (source: ExternalDataStripeSource) => ({ source }), - reloadSchema: (schema: ExternalDataSourceSchema) => ({ schema }), - resyncSchema: (schema: ExternalDataSourceSchema) => ({ schema }), sourceLoadingFinished: (source: ExternalDataStripeSource) => ({ source }), schemaLoadingFinished: (schema: ExternalDataSourceSchema) => ({ schema }), abortAnyRunningQuery: true, @@ -128,14 +126,6 @@ export const dataWarehouseSettingsLogic = kea([ schemaReloadingById: [ {} as Record, { - reloadSchema: (state, { schema }) => ({ - ...state, - [schema.id]: true, - }), - resyncSchema: (state, { schema }) => ({ - ...state, - [schema.id]: true, - }), schemaLoadingFinished: (state, { schema }) => ({ ...state, [schema.id]: false, @@ -235,64 +225,6 @@ export const dataWarehouseSettingsLogic = kea([ } actions.sourceLoadingFinished(source) }, - reloadSchema: async ({ schema }) => { - // Optimistic UI updates before sending updates to the backend - const clonedSources = JSON.parse( - JSON.stringify(values.dataWarehouseSources?.results ?? []) - ) as ExternalDataStripeSource[] - const sourceIndex = clonedSources.findIndex((n) => n.schemas.find((m) => m.id === schema.id)) - const schemaIndex = clonedSources[sourceIndex].schemas.findIndex((n) => n.id === schema.id) - clonedSources[sourceIndex].status = 'Running' - clonedSources[sourceIndex].schemas[schemaIndex].status = 'Running' - - actions.loadSourcesSuccess({ - ...values.dataWarehouseSources, - results: clonedSources, - }) - - try { - await api.externalDataSchemas.reload(schema.id) - actions.schemaLoadingFinished(schema) - actions.loadSources(null) - - posthog.capture('schema reloaded', { sourceType: clonedSources[sourceIndex].source_type }) - } catch (e: any) { - if (e.message) { - lemonToast.error(e.message) - } else { - lemonToast.error('Cant reload schema at this time') - } - } - }, - // Complete refresh - resyncSchema: async ({ schema }) => { - const clonedSources = JSON.parse( - JSON.stringify(values.dataWarehouseSources?.results ?? []) - ) as ExternalDataStripeSource[] - const sourceIndex = clonedSources.findIndex((n) => n.schemas.find((m) => m.id === schema.id)) - const schemaIndex = clonedSources[sourceIndex].schemas.findIndex((n) => n.id === schema.id) - clonedSources[sourceIndex].status = 'Running' - clonedSources[sourceIndex].schemas[schemaIndex].status = 'Running' - - actions.loadSourcesSuccess({ - ...values.dataWarehouseSources, - results: clonedSources, - }) - - try { - await api.externalDataSchemas.resync(schema.id) - actions.schemaLoadingFinished(schema) - actions.loadSources(null) - - posthog.capture('schema resynced', { sourceType: clonedSources[sourceIndex].source_type }) - } catch (e: any) { - if (e.message) { - lemonToast.error(e.message) - } else { - lemonToast.error('Cant refresh schema at this time') - } - } - }, abortAnyRunningQuery: () => { if (cache.abortController) { cache.abortController.abort() diff --git a/frontend/src/scenes/data-warehouse/settings/source/Schemas.tsx b/frontend/src/scenes/data-warehouse/settings/source/Schemas.tsx index f106fb91bff2c..c30f5c95f09e5 100644 --- a/frontend/src/scenes/data-warehouse/settings/source/Schemas.tsx +++ b/frontend/src/scenes/data-warehouse/settings/source/Schemas.tsx @@ -46,8 +46,7 @@ const StatusTagSetting = { } export const SchemaTable = ({ schemas, isLoading }: SchemaTableProps): JSX.Element => { - const { reloadSchema, resyncSchema } = useActions(dataWarehouseSettingsLogic) - const { updateSchema } = useActions(dataWarehouseSourceSettingsLogic) + const { updateSchema, reloadSchema, resyncSchema } = useActions(dataWarehouseSourceSettingsLogic) const { schemaReloadingById } = useValues(dataWarehouseSettingsLogic) return ( @@ -55,6 +54,7 @@ export const SchemaTable = ({ schemas, isLoading }: SchemaTableProps): JSX.Eleme ([ path(['scenes', 'data-warehouse', 'settings', 'source', 'dataWarehouseSourceSettingsLogic']), props({} as DataWarehouseSourceSettingsLogicProps), @@ -33,8 +37,10 @@ export const dataWarehouseSourceSettingsLogic = kea ({ tab }), setParentSettingsTab: (tab: DataWarehouseSettingsTab) => ({ tab }), setSourceId: (id: string) => ({ id }), + reloadSchema: (schema: ExternalDataSourceSchema) => ({ schema }), + resyncSchema: (schema: ExternalDataSourceSchema) => ({ schema }), }), - loaders(({ values }) => ({ + loaders(({ actions, values }) => ({ source: [ null as ExternalDataStripeSource | null, { @@ -42,10 +48,15 @@ export const dataWarehouseSourceSettingsLogic = kea { + // Optimistic UI updates before sending updates to the backend + const clonedSource = JSON.parse(JSON.stringify(values.source)) as ExternalDataStripeSource + const schemaIndex = clonedSource.schemas.findIndex((n) => n.id === schema.id) + clonedSource.schemas[schemaIndex] = schema + actions.loadSourceSuccess(clonedSource) + const updatedSchema = await api.externalDataSchemas.update(schema.id, schema) const source = values.source - const schemaIndex = source?.schemas.findIndex((n) => n.id === schema.id) if (schemaIndex !== undefined) { source!.schemas[schemaIndex] = updatedSchema } @@ -105,6 +116,64 @@ export const dataWarehouseSourceSettingsLogic = kea ({ + loadSourceSuccess: () => { + clearTimeout(cache.refreshTimeout) + + cache.refreshTimeout = setTimeout(() => { + actions.loadSource() + }, REFRESH_INTERVAL) + }, + loadSourceFailure: () => { + clearTimeout(cache.refreshTimeout) + + cache.refreshTimeout = setTimeout(() => { + actions.loadSource() + }, REFRESH_INTERVAL) + }, + reloadSchema: async ({ schema }) => { + // Optimistic UI updates before sending updates to the backend + const clonedSource = JSON.parse(JSON.stringify(values.source)) as ExternalDataStripeSource + const schemaIndex = clonedSource.schemas.findIndex((n) => n.id === schema.id) + clonedSource.status = 'Running' + clonedSource.schemas[schemaIndex].status = 'Running' + + actions.loadSourceSuccess(clonedSource) + + try { + await api.externalDataSchemas.reload(schema.id) + + posthog.capture('schema reloaded', { sourceType: clonedSource.source_type }) + } catch (e: any) { + if (e.message) { + lemonToast.error(e.message) + } else { + lemonToast.error('Cant reload schema at this time') + } + } + }, + resyncSchema: async ({ schema }) => { + // Optimistic UI updates before sending updates to the backend + const clonedSource = JSON.parse(JSON.stringify(values.source)) as ExternalDataStripeSource + const schemaIndex = clonedSource.schemas.findIndex((n) => n.id === schema.id) + clonedSource.status = 'Running' + clonedSource.schemas[schemaIndex].status = 'Running' + + actions.loadSourceSuccess(clonedSource) + + try { + await api.externalDataSchemas.resync(schema.id) + + posthog.capture('schema resynced', { sourceType: clonedSource.source_type }) + } catch (e: any) { + if (e.message) { + lemonToast.error(e.message) + } else { + lemonToast.error('Cant refresh schema at this time') + } + } + }, + })), urlToAction(({ actions, values }) => ({ '/data-warehouse/settings/:parentTab/:id': ({ parentTab, id }) => { if (id) {