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 26, 2024
1 parent 88f22c4 commit 9045b7a
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(

Check warning on line 120 in src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx#L120

Added line #L120 was not covered by tests
getAllDataSets(
dataSources.filter((ds) => ds.getMetadata().ui?.selector.displayDatasetsAsSource)
dataSources.filter((ds) => ds.getMetadata().ui.selector.displayDatasetsAsSource)

Check warning on line 122 in src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx#L122

Added line #L122 was not covered by tests
)
)
.then((dataSetResults) => {
setDataSourceOptionList(

Check warning on line 126 in src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx#L126

Added line #L126 was not covered by tests
consolidateDataSourceGroups(
dataSetResults as DataSourceDataSet[],
dataSources.filter((ds) => !ds.getMetadata().ui?.selector.displayDatasetsAsSource)
dataSources.filter((ds) => !ds.getMetadata().ui.selector.displayDatasetsAsSource)

Check warning on line 129 in src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/data_sources/datasource_selector/datasource_selectable.tsx#L129

Added line #L129 was not covered by tests
)
);
})
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());

Check warning on line 108 in src/plugins/data/public/data_sources/datasource_services/datasource_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/data_sources/datasource_services/datasource_service.ts#L107-L108

Added lines #L107 - L108 were not covered by tests
}

/**
* 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();

Check warning on line 116 in src/plugins/data/public/data_sources/datasource_services/datasource_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/data_sources/datasource_services/datasource_service.ts#L116

Added line #L116 was not covered by tests
}

/**
* Resets all registered data sources.
*/
reset() {
this.dataSources = {};
this.dataSourcesSubject.next(this.dataSources);

Check warning on line 124 in src/plugins/data/public/data_sources/datasource_services/datasource_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/data_sources/datasource_services/datasource_service.ts#L123-L124

Added lines #L123 - L124 were not covered by tests
}
}
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([

Check warning on line 226 in src/plugins/data/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/plugin.ts#L225-L226

Added lines #L225 - L226 were not covered by tests
{
type: DEFAULT_DATASOURCE_TYPE,
registerDataSources: () => registerDefaultDataSource(dataServices),

Check warning on line 229 in src/plugins/data/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/plugin.ts#L229

Added line #L229 was not covered by tests
},
]);

const dataServices = {
actions: {
Expand All @@ -235,7 +246,7 @@ export class DataPublicPlugin
},
};

registerDefaultDatasource(dataServices);
registerDefaultDataSource(dataServices);

Check warning on line 249 in src/plugins/data/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/plugin.ts#L249

Added line #L249 was not covered by tests

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 9045b7a

Please sign in to comment.