From 9045b7ababbefa3a3aa67bd15236f1bafa8e16fd Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 26 Apr 2024 03:37:06 +0000 Subject: [PATCH] add in memory cache with refresh Signed-off-by: Eric --- .../data_sources/datasource/factory.test.ts | 4 ++- .../datasource_selectable.tsx | 25 ++++++++++++++++--- .../data_sources/datasource_selector/types.ts | 1 + .../datasource_services/datasource_service.ts | 11 +++++++- .../register_default_datasource.ts | 4 +-- src/plugins/data/public/plugin.ts | 15 +++++++++-- .../public/components/sidebar/index.tsx | 7 ++++-- 7 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/plugins/data/public/data_sources/datasource/factory.test.ts b/src/plugins/data/public/data_sources/datasource/factory.test.ts index 0f9ea016748f..e0d35b6ed0a6 100644 --- a/src/plugins/data/public/data_sources/datasource/factory.test.ts +++ b/src/plugins/data/public/data_sources/datasource/factory.test.ts @@ -11,17 +11,19 @@ class MockDataSource extends DataSource { private readonly indexPatterns; constructor({ + id, name, type, metadata, indexPatterns, }: { + id: string; name: string; type: string; metadata: any; indexPatterns: IndexPatternsService; }) { - super(name, type, metadata); + super({ id, name, type, metadata }); this.indexPatterns = indexPatterns; } diff --git a/src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx b/src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx index 776e8f28cca8..1d07c2e95041 100644 --- a/src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx +++ b/src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx @@ -4,7 +4,7 @@ */ import React, { useEffect, useCallback, useMemo } from 'react'; -import { EuiComboBox } from '@elastic/eui'; +import { EuiButtonIcon, EuiComboBox, EuiText, EuiToolTip } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { DataSource, DataSourceDataSet, IndexPatternOption } from '../datasource'; import { DataSourceGroup, DataSourceSelectableProps } from './types'; @@ -112,20 +112,21 @@ export const DataSourceSelectable = ({ setDataSourceOptionList, onGetDataSetError, // onGetDataSetError, Callback for handling get data set errors. Ensure it's memoized. singleSelection = { asPlainText: true }, + onRefresh, ...comboBoxProps }: DataSourceSelectableProps) => { // This effect gets data sets and prepares the datasource list for UI rendering. useEffect(() => { Promise.all( getAllDataSets( - dataSources.filter((ds) => ds.getMetadata().ui?.selector.displayDatasetsAsSource) + dataSources.filter((ds) => ds.getMetadata().ui.selector.displayDatasetsAsSource) ) ) .then((dataSetResults) => { setDataSourceOptionList( consolidateDataSourceGroups( dataSetResults as DataSourceDataSet[], - dataSources.filter((ds) => !ds.getMetadata().ui?.selector.displayDatasetsAsSource) + dataSources.filter((ds) => !ds.getMetadata().ui.selector.displayDatasetsAsSource) ) ); }) @@ -160,6 +161,24 @@ export const DataSourceSelectable = ({ onChange={handleSourceChange} singleSelection={singleSelection} isClearable={false} + prepend={ + + + + + + } /> ); }; diff --git a/src/plugins/data/public/data_sources/datasource_selector/types.ts b/src/plugins/data/public/data_sources/datasource_selector/types.ts index 0a049ea5eaec..a8c1ba6a9fa3 100644 --- a/src/plugins/data/public/data_sources/datasource_selector/types.ts +++ b/src/plugins/data/public/data_sources/datasource_selector/types.ts @@ -35,4 +35,5 @@ export interface DataSourceSelectableProps extends Pick void; + onRefresh: () => void; } diff --git a/src/plugins/data/public/data_sources/datasource_services/datasource_service.ts b/src/plugins/data/public/data_sources/datasource_services/datasource_service.ts index 8ead65633c7f..e4cef2ece4ca 100644 --- a/src/plugins/data/public/data_sources/datasource_services/datasource_service.ts +++ b/src/plugins/data/public/data_sources/datasource_services/datasource_service.ts @@ -104,14 +104,23 @@ export class DataSourceService { * Typically used to initialize or refresh the data source configurations. */ load() { + this.reset(); Object.values(this.dataSourceFetchers).forEach((fetch) => fetch()); } /** * Reloads all data source configurations by re-invoking the load method. - * Useful for refreshing the system to reflect changes such as new data source registrations. + * Used for refreshing the system to reflect changes such as new data source registrations. */ reload() { this.load(); } + + /** + * Resets all registered data sources. + */ + reset() { + this.dataSources = {}; + this.dataSourcesSubject.next(this.dataSources); + } } diff --git a/src/plugins/data/public/data_sources/register_default_datasource.ts b/src/plugins/data/public/data_sources/register_default_datasource.ts index a4198b4e8e3a..6fa6bc8aa23c 100644 --- a/src/plugins/data/public/data_sources/register_default_datasource.ts +++ b/src/plugins/data/public/data_sources/register_default_datasource.ts @@ -6,17 +6,15 @@ import { i18n } from '@osd/i18n'; import { htmlIdGenerator } from '@elastic/eui'; import { DataPublicPluginStart } from '../types'; -import { DefaultDslDataSource } from './default_datasource'; export const DEFAULT_DATASOURCE_TYPE = 'DEFAULT_INDEX_PATTERNS'; export const DEFAULT_DATASOURCE_NAME = i18n.translate('data.datasource.type.openSearchDefault', { defaultMessage: 'OpenSearch Default', }); -export const registerDefaultDatasource = (data: Omit) => { +export const registerDefaultDataSource = (data: Omit) => { // Registrations of index patterns as default data source const { dataSourceService, dataSourceFactory } = data.dataSources; - dataSourceFactory.registerDataSourceType(DEFAULT_DATASOURCE_TYPE, DefaultDslDataSource); dataSourceService.registerDataSource( dataSourceFactory.getDataSourceInstance(DEFAULT_DATASOURCE_TYPE, { id: htmlIdGenerator('local-cluster')('indices'), diff --git a/src/plugins/data/public/plugin.ts b/src/plugins/data/public/plugin.ts index 179b6c0a8c83..bc757467bd98 100644 --- a/src/plugins/data/public/plugin.ts +++ b/src/plugins/data/public/plugin.ts @@ -90,7 +90,11 @@ import { SavedObjectsClientPublicToCommon } from './index_patterns'; import { indexPatternLoad } from './index_patterns/expressions/load_index_pattern'; import { DataSourceService } from './data_sources/datasource_services'; import { DataSourceFactory } from './data_sources/datasource'; -import { registerDefaultDatasource } from './data_sources/register_default_datasource'; +import { + DEFAULT_DATASOURCE_TYPE, + registerDefaultDataSource, +} from './data_sources/register_default_datasource'; +import { DefaultDslDataSource } from './data_sources/default_datasource'; declare module '../../ui_actions/public' { export interface ActionContextMapping { @@ -218,6 +222,13 @@ export class DataPublicPlugin // Create or fetch the singleton instance const dataSourceService = DataSourceService.getInstance(); const dataSourceFactory = DataSourceFactory.getInstance(); + dataSourceFactory.registerDataSourceType(DEFAULT_DATASOURCE_TYPE, DefaultDslDataSource); + dataSourceService.registerDataSourceFetchers([ + { + type: DEFAULT_DATASOURCE_TYPE, + registerDataSources: () => registerDefaultDataSource(dataServices), + }, + ]); const dataServices = { actions: { @@ -235,7 +246,7 @@ export class DataPublicPlugin }, }; - registerDefaultDatasource(dataServices); + registerDefaultDataSource(dataServices); const SearchBar = createSearchBar({ core, diff --git a/src/plugins/data_explorer/public/components/sidebar/index.tsx b/src/plugins/data_explorer/public/components/sidebar/index.tsx index acede22c2e51..21f8d20d609a 100644 --- a/src/plugins/data_explorer/public/components/sidebar/index.tsx +++ b/src/plugins/data_explorer/public/components/sidebar/index.tsx @@ -38,8 +38,6 @@ export const Sidebar: FC = ({ children }) => { } ); - dataSources.dataSourceService.load(); - return () => { subscription.unsubscribe(); isMounted = false; @@ -100,6 +98,10 @@ export const Sidebar: FC = ({ children }) => { [toasts] ); + const memorizedReload = useCallback(() => { + dataSources.dataSourceService.reload(); + }, [dataSources.dataSourceService]); + return ( { onDataSourceSelect={handleSourceSelection} selectedSources={selectedSources} onGetDataSetError={handleGetDataSetError} + onRefresh={memorizedReload} fullWidth />