Skip to content

Commit

Permalink
refactor variable names
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 committed Oct 5, 2023
1 parent f188b5f commit d56bc1d
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class DataSource<
return this.type;
}

getMetadata(): DataSourceMetaData {
getMetadata() {
return this.metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('DataSourceSelectable', () => {
selectedSources={selectedSourcesMock}
onDataSourceSelect={setSelectedSourcesMock}
setDataSourceOptionList={setDataSourceOptionListMock}
onFetchDataSetError={onFetchDataSetErrorMock}
onGetDataSetError={onFetchDataSetErrorMock}
/>
);
});
Expand All @@ -55,7 +55,7 @@ describe('DataSourceSelectable', () => {
selectedSources={selectedSourcesMock}
onDataSourceSelect={setSelectedSourcesMock}
setDataSourceOptionList={setDataSourceOptionListMock}
onFetchDataSetError={onFetchDataSetErrorMock}
onGetDataSetError={onFetchDataSetErrorMock}
/>
);
});
Expand All @@ -74,7 +74,7 @@ describe('DataSourceSelectable', () => {
selectedSources={selectedSourcesMock}
onDataSourceSelect={setSelectedSourcesMock}
setDataSourceOptionList={setDataSourceOptionListMock}
onFetchDataSetError={onFetchDataSetErrorMock}
onGetDataSetError={onFetchDataSetErrorMock}
/>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,18 @@ const DATASOURCE_TYPE_DISPLAY_NAME_MAP: Record<DataSourceTypeKey, string> = {

type DataSetType = ISourceDataSet['data_sets'][number];

interface DataSetWithSource {
ds: GenericDataSource;
data_sets: DataSetType[];
}

// Fetches data sets for a given datasource and returns it along with the source.
const fetchDataSetWithSource = async (ds: GenericDataSource): Promise<DataSetWithSource> => {
// Get data sets for a given datasource and returns it along with the source.
const getDataSetWithSource = async (ds: GenericDataSource): Promise<ISourceDataSet> => {
const dataSet = await ds.getDataSet();
return {
ds,
data_sets: dataSet,
};
};

// Map through all data sources and fetch their respective data sets.
const fetchDataSets = (dataSources: GenericDataSource[]) =>
dataSources.map((ds) => fetchDataSetWithSource(ds));
// Map through all data sources and get their respective data sets.
const getDataSets = (dataSources: GenericDataSource[]) =>
dataSources.map((ds) => getDataSetWithSource(ds));

const isIndexPatterns = (dataSet: DataSetType): dataSet is IndexPatternOption => {
if (typeof dataSet === 'string') return false;
Expand Down Expand Up @@ -67,7 +62,7 @@ const getSourceOptions = (dataSource: DataSourceType, dataSet: DataSetType) => {
};
};

// Convert fetched data sets into a structured format suitable for selector rendering.
// Convert data sets into a structured format suitable for selector rendering.
const getSourceList = (allDataSets: ISourceDataSet[]) => {
const finalList = [] as DataSourceGroup[];
allDataSets.forEach((curDataSet) => {
Expand Down Expand Up @@ -100,17 +95,17 @@ export const DataSourceSelectable = ({
selectedSources, // current selected datasource in the form of [{ label: xxx, value: xxx }]
onDataSourceSelect,
setDataSourceOptionList,
onFetchDataSetError, // onFetchDataSetError, Callback for handling data set fetch errors. Ensure it's memoized.
onGetDataSetError, // onGetDataSetError, Callback for handling get data set errors. Ensure it's memoized.
singleSelection = true,
}: DataSourceSelectableProps) => {
// This effect fetches data sets and prepares the datasource list for UI rendering.
// This effect gets data sets and prepares the datasource list for UI rendering.
useEffect(() => {
Promise.all(fetchDataSets(dataSources))
Promise.all(getDataSets(dataSources))
.then((results) => {
setDataSourceOptionList(getSourceList(results));
})
.catch((e) => onFetchDataSetError(e));
}, [dataSources, setDataSourceOptionList, onFetchDataSetError]);
.catch((e) => onGetDataSetError(e));
}, [dataSources, setDataSourceOptionList, onGetDataSetError]);

const handleSourceChange = (selectedOptions: any) => onDataSourceSelect(selectedOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { EuiComboBoxOptionOption, EuiComboBoxSingleSelectionShape } from '@elastic/eui';
import { EuiComboBoxSingleSelectionShape } from '@elastic/eui';
import { GenericDataSource } from '../datasource_services';

export interface DataSourceGroup {
Expand All @@ -18,13 +18,11 @@ export interface DataSourceOption {
ds: GenericDataSource;
}

export type DataSourceOptionType = EuiComboBoxOptionOption<string>;

export interface DataSourceSelectableProps {
dataSources: GenericDataSource[];
onDataSourceSelect: (dataSourceOption: DataSourceOption[]) => void;
singleSelection?: boolean | EuiComboBoxSingleSelectionShape;
onFetchDataSetError: (error: Error) => void;
onGetDataSetError: (error: Error) => void;
dataSourceOptionList: DataSourceGroup[];
selectedSources: DataSourceOption[];
setDataSourceOptionList: (dataSourceList: DataSourceGroup[]) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe('DataSourceService', () => {
const ds1 = new MockDataSource(mockConfig1);
const ds2 = new MockDataSource(mockConfig2);
service.registerMultipleDataSources([ds1, ds2]);
const filters = { names: ['test_datasource1'] };
const retrievedDataSources = service.getDataSources(filters);
const filter = { names: ['test_datasource1'] };
const retrievedDataSources = service.getDataSources(filter);
expect(retrievedDataSources).toHaveProperty('test_datasource1');
expect(retrievedDataSources).not.toHaveProperty('test_datasource2');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

import { BehaviorSubject } from 'rxjs';
import {
IDataSourceFilters,
IDataSourceRegisterationResult,
DataSourceRegisterationError,
DataSourceRegistrationError,
GenericDataSource,
IDataSourceFilter,
IDataSourceRegistrationResult,
} from './types';

export class DataSourceService {
private static dataSourceService: DataSourceService;
// A record to store all registered data sources, using the data source name as the key.
private dataSources: Record<string, GenericDataSource> = {};
private _dataSourcesSubject: BehaviorSubject<Record<string, GenericDataSource>>;
private dataSourcesSubject: BehaviorSubject<Record<string, GenericDataSource>>;

private constructor() {
this._dataSourcesSubject = new BehaviorSubject(this.dataSources);
this.dataSourcesSubject = new BehaviorSubject(this.dataSources);
}

static getInstance(): DataSourceService {
Expand All @@ -36,7 +36,7 @@ export class DataSourceService {
*/
async registerMultipleDataSources(
datasources: GenericDataSource[]
): Promise<IDataSourceRegisterationResult[]> {
): Promise<IDataSourceRegistrationResult[]> {
return Promise.all(datasources.map((ds) => this.registerDataSource(ds)));
}

Expand All @@ -46,46 +46,41 @@ export class DataSourceService {
*
* @param ds - The data source to be registered.
* @returns A registration result indicating success or failure.
* @throws {DataSourceRegisterationError} Throws an error if a data source with the same name already exists.
* @throws {DataSourceRegistrationError} Throws an error if a data source with the same name already exists.
*/
async registerDataSource(ds: GenericDataSource): Promise<IDataSourceRegisterationResult> {
async registerDataSource(ds: GenericDataSource): Promise<IDataSourceRegistrationResult> {
const dsName = ds.getName();
if (dsName in this.dataSources) {
throw new DataSourceRegisterationError(
throw new DataSourceRegistrationError(
`Unable to register datasource ${dsName}, error: datasource name exists.`
);
} else {
this.dataSources = {
...this.dataSources,
[dsName]: ds,
};
this._dataSourcesSubject.next(this.dataSources);
return { success: true, info: '' } as IDataSourceRegisterationResult;
this.dataSources[dsName] = ds;
this.dataSourcesSubject.next(this.dataSources);
return { success: true, info: '' } as IDataSourceRegistrationResult;
}
}

public get dataSources$() {
return this._dataSourcesSubject.asObservable();
return this.dataSourcesSubject.asObservable();
}

/**
* Retrieve the registered data sources based on provided filters.
* If no filters are provided, all registered data sources are returned.
*
* @param filters - An optional object with filter criteria (e.g., names of data sources).
* @param filter - An optional object with filter criteria (e.g., names of data sources).
* @returns A record of filtered data sources.
*/
getDataSources(filters?: IDataSourceFilters): Record<string, GenericDataSource> {
if (!Array.isArray(filters?.names) || filters.names.length === 0) return this.dataSources;
getDataSources(filter?: IDataSourceFilter): Record<string, GenericDataSource> {
if (!filter || !Array.isArray(filter.names) || filter.names.length === 0)
return this.dataSources;

return filters.names.reduce<Record<string, GenericDataSource>>(
(filteredDataSources, dsName) => {
if (dsName in this.dataSources) {
filteredDataSources[dsName] = this.dataSources[dsName];
}
return filteredDataSources;
},
{} as Record<string, GenericDataSource>
);
return filter.names.reduce<Record<string, GenericDataSource>>((filteredDataSources, dsName) => {
if (dsName in this.dataSources) {
filteredDataSources[dsName] = this.dataSources[dsName];
}
return filteredDataSources;
}, {} as Record<string, GenericDataSource>);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

export { DataSourceService } from './datasource_service';
export {
IDataSourceFilters,
IDataSourceRegisterationResult,
DataSourceRegisterationError,
IDataSourceFilter,
IDataSourceRegistrationResult,
DataSourceRegistrationError,
DataSourceType,
GenericDataSource,
} from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import {
} from '../datasource';
import { DataSourceService } from './datasource_service';

export interface IDataSourceFilters {
export interface IDataSourceFilter {
names: string[];
}

export interface IDataSourceRegisterationResult {
export interface IDataSourceRegistrationResult {
success: boolean;
info: string;
}

export class DataSourceRegisterationError extends Error {
export class DataSourceRegistrationError extends Error {
success: boolean;
info: string;
constructor(message: string) {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,11 @@ export {
DataSourceConfig,
} from './data_sources/datasource';
export {
DataSourceRegisterationError,
DataSourceRegistrationError,
DataSourceService,
DataSourceType,
IDataSourceFilters,
IDataSourceRegisterationResult,
IDataSourceFilter,
IDataSourceRegistrationResult,
} from './data_sources/datasource_services';
export {
DataSourceSelectable,
Expand Down
22 changes: 14 additions & 8 deletions src/plugins/data_explorer/public/components/sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { FC, useEffect, useState, useCallback } from 'react';
import { EuiSplitPanel, EuiPageSideBar } from '@elastic/eui';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { DataExplorerServices } from '../../types';
import { useTypedDispatch, useTypedSelector, setIndexPattern } from '../../utils/state_management';
import { EuiPageSideBar, EuiSplitPanel } from '@elastic/eui';
import { i18n } from '@osd/i18n';
import React, { FC, useCallback, useEffect, useState } from 'react';
import { DataSourceGroup, DataSourceSelectable, DataSourceType } from '../../../../data/public';
import { DataSourceOption } from '../../../../data/public/';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { DataExplorerServices } from '../../types';
import { setIndexPattern, useTypedDispatch, useTypedSelector } from '../../utils/state_management';

export const Sidebar: FC = ({ children }) => {
const { indexPattern: indexPatternId } = useTypedSelector((state) => state.metadata);
Expand Down Expand Up @@ -73,9 +74,14 @@ export const Sidebar: FC = ({ children }) => {
dispatch(setIndexPattern(selectedDataSources[0].value));
};

const handleDataSetFetchError = useCallback(
const handleGetDataSetError = useCallback(
() => (error: Error) => {
toasts.addError(error, { title: `Data set fetching error: ${error}` });
toasts.addError(error, {
title:
i18n.translate('dataExplorer.sidebar.failedToGetDataSetErrorDescription', {
defaultMessage: 'Failed to get data set: ',
}) + (error.message || error.name),
});
},
[toasts]
);
Expand All @@ -90,7 +96,7 @@ export const Sidebar: FC = ({ children }) => {
setDataSourceOptionList={setDataSourceOptionList}
onDataSourceSelect={handleSourceSelection}
selectedSources={selectedSources}
onFetchDataSetError={handleDataSetFetchError}
onGetDataSetError={handleGetDataSetError}
/>
</EuiSplitPanel.Inner>
<EuiSplitPanel.Inner paddingSize="none" color="subdued" className="eui-yScroll">
Expand Down

0 comments on commit d56bc1d

Please sign in to comment.