Skip to content

Commit

Permalink
renaming
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 46d0b35 commit 9005834
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/plugins/data/public/data_sources/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export { DataSource } from './datasource';
export {
IDataSourceMetadata,
DataSourceDataSet,
DataSetWithDataSource,
IDataSetParams,
IDataSourceQueryParams,
IDataSourceQueryResult,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/data_sources/datasource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface IDataSourceGroup {
name: string;
}

export interface DataSourceDataSet<T = []> {
export interface DataSetWithDataSource<T = []> {
ds: DataSource;
list: T;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ export interface IDataSourceUISelector {
export interface IDataSourceUISettings {
selector: IDataSourceUISelector;
label: string; // the display name of data source
typeGroup: string; // the group to which the data source belongs
groupType: string; // the group to which the data source belongs
typeLabel: string; // the display name of data source type
displayOrder?: number; // the order in which the data source should be displayed in selector
description?: string; // short description of your database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import React, { useEffect, useCallback, useMemo } from 'react';
import { EuiButtonIcon, EuiComboBox, EuiText, EuiToolTip } from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { DataSource, DataSourceDataSet, IndexPatternOption } from '../datasource';
import { DataSourceGroup, DataSourceSelectableProps } from './types';
import { DataSource, DataSetWithDataSource, IndexPatternOption } from '../datasource';
import { DataSourceGroup, DataSourceOption, DataSourceSelectableProps } from './types';

// Get Index patterns for local cluster.
// Asynchronously retrieves and formats dataset from a given data source.
const getAndFormatDataSetFromDataSource = async (
ds: DataSource
): Promise<DataSourceDataSet<IndexPatternOption[]>> => {
): Promise<DataSetWithDataSource<IndexPatternOption[]>> => {
const { dataSets } = await ds.getDataSet();
return { ds, list: dataSets } as DataSourceDataSet<IndexPatternOption[]>;
return { ds, list: dataSets } as DataSetWithDataSource<IndexPatternOption[]>;

Check warning on line 17 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#L16-L17

Added lines #L16 - L17 were not covered by tests
};

// Map through all data sources and get their respective data sets.
Expand All @@ -28,11 +28,11 @@ export const isIndexPatterns = (dataSet: unknown) =>
!!(dataSet as any).title &&
!!(dataSet as any).id;

// Mapping function for datasets to get the option format for the combo box from the dataSource and dataSet.
// Mapping function to get the option format for the combo box from the dataSource and dataSet.
const mapToOption = (
dataSource: DataSource,
dataSet: DataSourceDataSet | undefined = undefined
) => {
dataSet: DataSetWithDataSource | undefined = undefined
): DataSourceOption => {
const baseOption = {

Check warning on line 36 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#L36

Added line #L36 was not covered by tests
type: dataSource.getType(),
name: dataSource.getName(),
Expand All @@ -41,9 +41,9 @@ const mapToOption = (
if (dataSet && 'title' in dataSet && 'id' in dataSet && isIndexPatterns(dataSet)) {
return {
...baseOption,
label: dataSet.title,
value: dataSet.id,
key: dataSet.id,
label: dataSet.title as string,
value: dataSet.id as string,
key: dataSet.id as string,
};
}
return {
Expand All @@ -55,9 +55,13 @@ const mapToOption = (
};

// Function to add or update groups in a reduction process
const addOrUpdateGroup = (acc: DataSourceGroup[], dataSource: DataSource, option) => {
const addOrUpdateGroup = (
existingGroups: DataSourceGroup[],
dataSource: DataSource,
option: DataSourceOption
) => {
const metadata = dataSource.getMetadata();
const groupType = metadata.ui.typeGroup;
const groupType = metadata.ui.groupType;

Check warning on line 64 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#L63-L64

Added lines #L63 - L64 were not covered by tests
let groupName =
metadata.ui.typeLabel ||
i18n.translate('dataExplorer.dataSourceSelector.defaultGroupTitle', {
Expand All @@ -70,34 +74,34 @@ const addOrUpdateGroup = (acc: DataSourceGroup[], dataSource: DataSource, option
});
}

const group = acc.find((g: DataSourceGroup) => g.typeGroup === groupType);
if (group) {
if (!group.options.some((opt) => opt.key === option.key)) {
group.options.push(option);
}
const group = existingGroups.find((g: DataSourceGroup) => g.id === groupType);

Check warning on line 77 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#L77

Added line #L77 was not covered by tests
if (group && !group.options.some((opt) => opt.key === option.key)) {
group.options.push(option);

Check warning on line 79 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#L79

Added line #L79 was not covered by tests
} else {
acc.push({
typeGroup: groupType,
existingGroups.push({

Check warning on line 81 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#L81

Added line #L81 was not covered by tests
groupType,
label: groupName,
options: [option],
id: metadata.ui.typeGroup, // id for each group
id: metadata.ui.groupType, // id for each group
});
}
return acc;
};

const consolidateDataSourceGroups = (dataSets: DataSourceDataSet[], dataSources: DataSource[]) => {
return [...dataSets, ...dataSources].reduce((acc, item) => {
const consolidateDataSourceGroups = (
dataSets: DataSetWithDataSource[],
dataSources: DataSource[]
) => {
return [...dataSets, ...dataSources].reduce((dsGroup, item) => {

Check warning on line 94 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#L94

Added line #L94 was not covered by tests
if ('list' in item && item.ds) {
// Confirm item is a DataSet
const options = item.list.map((dataset) => mapToOption(item.ds, dataset));
options.forEach((option) => addOrUpdateGroup(acc, item.ds, option));
options.forEach((option) => addOrUpdateGroup(dsGroup, item.ds, option));

Check warning on line 98 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#L97-L98

Added lines #L97 - L98 were not covered by tests
} else {
// Handle DataSource directly
const option = mapToOption(item as InstanceType<typeof DataSource>);
addOrUpdateGroup(acc, item as InstanceType<typeof DataSource>, option);
addOrUpdateGroup(dsGroup, item as InstanceType<typeof DataSource>, option);

Check warning on line 102 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#L101-L102

Added lines #L101 - L102 were not covered by tests
}
return acc;
return dsGroup;

Check warning on line 104 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#L104

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

Expand Down Expand Up @@ -125,7 +129,7 @@ export const DataSourceSelectable = ({
.then((dataSetResults) => {
setDataSourceOptionList(

Check warning on line 130 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#L130

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

Check warning on line 133 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#L133

Added line #L133 was not covered by tests
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export interface DataSourceGroup {
label: string;
id: string;
options: DataSourceOption[];
typeGroup: string;
groupType: string;
}

export interface DataSourceOption {
key: string;
id: string;
name: string;
label: string;
value: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const registerDefaultDataSource = (data: Omit<DataPublicPluginStart, 'ui'
ui: {
label: 'Index patterns', // display name of your data source,
typeLabel: 'Index patterns', // display name of your data source type,
typeGroup: 'DEFAULT_INDEX_PATTERNS',
groupType: 'DEFAULT_INDEX_PATTERNS',
selector: {
displayDatasetsAsSource: true, // when true, selector UI will render data sets with source by calling getDataSets()
},
Expand Down

0 comments on commit 9005834

Please sign in to comment.