Skip to content

Commit

Permalink
add in memory cache with refresh
Browse files Browse the repository at this point in the history
Signed-off-by: Eric <menwe@amazon.com>
  • Loading branch information
mengweieric committed Apr 28, 2024
1 parent 564c820 commit c1feb10
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ class MockDataSource extends DataSource<any, any, any, any, any> {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
)
);
})
Expand Down Expand Up @@ -160,6 +161,24 @@ export const DataSourceSelectable = ({
onChange={handleSourceChange}
singleSelection={singleSelection}
isClearable={false}
prepend={
<EuiText size="s">
<EuiToolTip
position="top"
content={i18n.translate('data.datasource.selector.refreshDataSources', {
defaultMessage: 'Refresh data source selector',
})}
display="block"
>
<EuiButtonIcon
size="s"
onClick={onRefresh}
iconType="refresh"
aria-label="sourceRefresh"
/>
</EuiToolTip>
</EuiText>
}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export interface DataSourceSelectableProps extends Pick<EuiComboBoxProps<unknown
dataSourceOptionList: DataSourceGroup[];
selectedSources: DataSourceOption[];
setDataSourceOptionList: (dataSourceList: DataSourceGroup[]) => void;
onRefresh: () => void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataPublicPluginStart, 'ui'>) => {
export const registerDefaultDataSource = (data: Omit<DataPublicPluginStart, 'ui'>) => {
// 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'),
Expand Down
15 changes: 13 additions & 2 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: {
Expand All @@ -235,7 +246,7 @@ export class DataPublicPlugin
},
};

registerDefaultDatasource(dataServices);
registerDefaultDataSource(dataServices);

const SearchBar = createSearchBar({
core,
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/data_explorer/public/components/sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export const Sidebar: FC = ({ children }) => {
}
);

dataSources.dataSourceService.load();

return () => {
subscription.unsubscribe();
isMounted = false;
Expand Down Expand Up @@ -100,6 +98,10 @@ export const Sidebar: FC = ({ children }) => {
[toasts]
);

const memorizedReload = useCallback(() => {
dataSources.dataSourceService.reload();
}, [dataSources.dataSourceService]);

return (
<EuiPageSideBar className="deSidebar" sticky>
<EuiSplitPanel.Outer
Expand All @@ -121,6 +123,7 @@ export const Sidebar: FC = ({ children }) => {
onDataSourceSelect={handleSourceSelection}
selectedSources={selectedSources}
onGetDataSetError={handleGetDataSetError}
onRefresh={memorizedReload}
fullWidth
/>
</EuiSplitPanel.Inner>
Expand Down

0 comments on commit c1feb10

Please sign in to comment.