Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multi Datasource] Unify getDefaultDataSourceId and export #6843

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/6843.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
refactor:
- Unify getDefaultDataSourceId and export ([#6843](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6843))
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export const NO_DATASOURCES_CONNECTED_MESSAGE = 'No data sources connected yet.'
export const CONNECT_DATASOURCES_MESSAGE = 'Connect your data sources to get started.';
export const NO_COMPATIBLE_DATASOURCES_MESSAGE = 'No compatible data sources are available.';
export const ADD_COMPATIBLE_DATASOURCES_MESSAGE = 'Add a compatible data source.';

export const DEFAULT_DATA_SOURCE_UI_SETTINGS_ID = 'defaultDataSource';
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
handleNoAvailableDataSourceError,
generateComponentId,
getDataSourceSelection,
getDefaultDataSourceId,
} from '../utils';
import { SavedObject } from '../../../../../core/public';
import { DataSourceAttributes } from '../../types';
Expand Down Expand Up @@ -132,7 +133,7 @@ export class DataSourceAggregatedView extends React.Component<
this.setState({
...this.state,
allDataSourcesIdToTitleMap,
defaultDataSource: this.props.uiSettings?.get('defaultDataSource', null) ?? null,
defaultDataSource: getDefaultDataSourceId(this.props.uiSettings) ?? null,
showEmptyState: allDataSourcesIdToTitleMap.size === 0,
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
handleNoAvailableDataSourceError,
generateComponentId,
getDataSourceSelection,
getDefaultDataSourceId,
} from '../utils';
import { DataSourceBaseState } from '../data_source_menu/types';
import { DataSourceErrorMenu } from '../data_source_error_menu';
Expand Down Expand Up @@ -76,7 +77,7 @@ export class DataSourceMultiSelectable extends React.Component<
async componentDidMount() {
this._isMounted = true;
try {
const defaultDataSource = this.props.uiSettings?.get('defaultDataSource', null) ?? null;
const defaultDataSource = getDefaultDataSourceId(this.props.uiSettings) ?? null;
let selectedOptions: SelectedDataSourceOption[] = [];
const fetchedDataSources = await getDataSourcesWithFields(this.props.savedObjectsClient, [
'id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
handleNoAvailableDataSourceError,
generateComponentId,
getDataSourceSelection,
getDefaultDataSourceId,
} from '../utils';
import { LocalCluster } from '../data_source_selector/data_source_selector';
import { SavedObject } from '../../../../../core/public';
Expand Down Expand Up @@ -209,7 +210,7 @@ export class DataSourceSelectable extends React.Component<
return;
}

const defaultDataSource = this.props.uiSettings?.get('defaultDataSource', null) ?? null;
const defaultDataSource = getDefaultDataSourceId(this.props.uiSettings) ?? null;

if (this.props.selectedOption?.length) {
this.handleSelectedOption(dataSourceOptions, defaultDataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getFilteredDataSources,
generateComponentId,
getDataSourceSelection,
getDefaultDataSourceId,
} from '../utils';
import { DataSourceAttributes } from '../../types';
import { DataSourceItem } from '../data_source_item';
Expand Down Expand Up @@ -164,7 +165,7 @@ export class DataSourceSelector extends React.Component<
return;
}

const defaultDataSource = this.props.uiSettings?.get('defaultDataSource', null) ?? null;
const defaultDataSource = getDefaultDataSourceId(this.props.uiSettings) ?? null;
// 5.1 Empty default option, [], just want to show placeholder
if (this.props.defaultOption?.length === 0) {
this.setState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import {
} from '../../../../opensearch_dashboards_react/public';
import { DataSourceManagementContext, DataSourceTableItem, ToastMessageItem } from '../../types';
import { CreateButton } from '../create_button';
import { deleteMultipleDataSources, getDataSources, setFirstDataSourceAsDefault } from '../utils';
import {
deleteMultipleDataSources,
getDataSources,
setFirstDataSourceAsDefault,
getDefaultDataSourceId,
} from '../utils';
import { LoadingMask } from '../loading_mask';

/* Table config */
Expand Down Expand Up @@ -149,7 +154,7 @@ export const DataSourceTable = ({ history }: RouteComponentProps) => {
<EuiButtonEmpty size="xs" {...reactRouterNavigate(history, `${index.id}`)}>
{name}
</EuiButtonEmpty>
{index.id === uiSettings.get('defaultDataSource', null) ? (
{index.id === getDefaultDataSourceId(uiSettings) ? (
<EuiBadge iconType="starFilled" iconSide="left">
Default
</EuiBadge>
Expand Down Expand Up @@ -251,7 +256,7 @@ export const DataSourceTable = ({ history }: RouteComponentProps) => {
const setDefaultDataSource = async () => {
try {
for (const dataSource of selectedDataSources) {
if (uiSettings.get('defaultDataSource') === dataSource.id) {
if (getDefaultDataSourceId(uiSettings) === dataSource.id) {
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
handleDataSourceFetchError,
generateComponentId,
getDataSourceSelection,
getDefaultDataSourceId,
} from '../utils';
import { DataSourceDropDownHeader } from '../drop_down_header';
import { DataSourceItem } from '../data_source_item';
Expand Down Expand Up @@ -71,7 +72,7 @@ export class DataSourceView extends React.Component<DataSourceViewProps, DataSou
const option = selectedOption[0];
const optionId = option.id;

const defaultDataSource = this.props.uiSettings?.get('defaultDataSource', null) ?? null;
const defaultDataSource = getDefaultDataSourceId(this.props.uiSettings) ?? null;
if (optionId === '' && !this.props.hideLocalCluster) {
this.setState({
selectedOption: [LocalCluster],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import {
testConnection,
updateDataSourceById,
setFirstDataSourceAsDefault,
getDefaultDataSourceId,
} from '../utils';
import { getEditBreadcrumbs } from '../breadcrumbs';
import { EditDataSourceForm } from './components/edit_form/edit_data_source_form';
import { LoadingMask } from '../loading_mask';
import { AuthType, DataSourceAttributes } from '../../types';
import { DEFAULT_DATA_SOURCE_UI_SETTINGS_ID } from '../constants';

const defaultDataSource: DataSourceAttributes = {
title: '',
Expand Down Expand Up @@ -86,10 +88,10 @@ export const EditDataSource: React.FunctionComponent<RouteComponentProps<{ id: s
};

const handleSetDefault = async () => {
await uiSettings.set('defaultDataSource', dataSourceID);
await uiSettings.set(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID, dataSourceID);
};

const isDefaultDataSource = uiSettings.get('defaultDataSource', null) === dataSourceID;
const isDefaultDataSource = getDefaultDataSourceId(uiSettings) === dataSourceID;

/* Handle submit - create data source*/
const handleSubmit = async (attributes: DataSourceAttributes) => {
Expand Down Expand Up @@ -126,7 +128,7 @@ export const EditDataSource: React.FunctionComponent<RouteComponentProps<{ id: s

const setDefaultDataSource = async () => {
try {
if (uiSettings.get('defaultDataSource') === dataSourceID) {
if (getDefaultDataSourceId(uiSettings) === dataSourceID) {
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, true);
}
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
handleNoAvailableDataSourceError,
getDataSourceSelection,
setDataSourceSelection,
getDefaultDataSourceId,
getDefaultDataSourceId$,
} from './utils';
import { coreMock, notificationServiceMock } from '../../../../core/public/mocks';
import {
Expand Down Expand Up @@ -59,6 +61,7 @@ import {
DataSourceSelectionService,
defaultDataSourceSelection,
} from '../service/data_source_selection_service';
import { Observable, of } from 'rxjs';

const { savedObjects } = coreMock.createStart();
const { uiSettings } = coreMock.createStart();
Expand Down Expand Up @@ -658,4 +661,33 @@ describe('DataSourceManagement: Utils.ts', () => {
expect(result).toEqual(dataSourceSelection);
});
});
describe('getDefaultDataSourceId', () => {
it('should return null if uiSettings is not passed', () => {
mockUiSettingsCalls(uiSettings, 'get', 'id-1');
const result = getDefaultDataSourceId();
expect(result).toEqual(null);
});

it('should return string value normally', () => {
mockUiSettingsCalls(uiSettings, 'get', 'id-1');
const result = getDefaultDataSourceId(uiSettings);
expect(result).toEqual('id-1');
});
});

describe('getDefaultDataSourceId$', () => {
it('should return null if uiSettings is not passed', () => {
mockUiSettingsCalls(uiSettings, 'get', 'id-1');
const result = getDefaultDataSourceId$();
expect(result).toEqual(null);
});

it('should return observable value normally', () => {
const id$ = of('id-1');
mockUiSettingsCalls(uiSettings, 'get$', id$);
const result$ = getDefaultDataSourceId$(uiSettings);
expect(result$).toBeInstanceOf(Observable);
expect(result$).toEqual(id$);
});
});
});
17 changes: 14 additions & 3 deletions src/plugins/data_source_management/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
CONNECT_DATASOURCES_MESSAGE,
NO_COMPATIBLE_DATASOURCES_MESSAGE,
NO_DATASOURCES_CONNECTED_MESSAGE,
DEFAULT_DATA_SOURCE_UI_SETTINGS_ID,
} from './constants';
import {
DataSourceSelectionService,
Expand Down Expand Up @@ -78,7 +79,7 @@ export async function handleSetDefaultDatasource(
savedObjectsClient: SavedObjectsClientContract,
uiSettings: IUiSettingsClient
) {
if (uiSettings.get('defaultDataSource', null) === null) {
if (getDefaultDataSourceId(uiSettings) === null) {
return await setFirstDataSourceAsDefault(savedObjectsClient, uiSettings, false);
}
}
Expand All @@ -89,12 +90,12 @@ export async function setFirstDataSourceAsDefault(
exists: boolean
) {
if (exists) {
uiSettings.remove('defaultDataSource');
uiSettings.remove(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID);
}
const listOfDataSources: DataSourceTableItem[] = await getDataSources(savedObjectsClient);
if (Array.isArray(listOfDataSources) && listOfDataSources.length >= 1) {
const datasourceId = listOfDataSources[0].id;
return await uiSettings.set('defaultDataSource', datasourceId);
return await uiSettings.set(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID, datasourceId);
}
}

Expand Down Expand Up @@ -135,6 +136,16 @@ export function getFilteredDataSources(
.sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase()));
}

export function getDefaultDataSourceId(uiSettings?: IUiSettingsClient) {
if (!uiSettings) return null;
return uiSettings.get<string | null>(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID, null);
}

export function getDefaultDataSourceId$(uiSettings?: IUiSettingsClient) {
if (!uiSettings) return null;
return uiSettings.get$<string | null>(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID, null);
}

export function getDefaultDataSource(
dataSourcesOptions: DataSourceOption[],
LocalCluster: DataSourceOption,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_source_management/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export {
createDataSourceMenu,
} from './components/data_source_menu';
export { DataSourceSelectionService } from './service/data_source_selection_service';
export { getDefaultDataSourceId, getDefaultDataSourceId$ } from './components/utils';
2 changes: 1 addition & 1 deletion src/plugins/data_source_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export const mockErrorResponseForSavedObjectsCalls = (

export const mockUiSettingsCalls = (
uiSettings: IUiSettingsClient,
uiSettingsMethodName: 'get' | 'set',
uiSettingsMethodName: 'get' | 'set' | 'get$',
response: any
) => {
(uiSettings[uiSettingsMethodName] as jest.Mock).mockReturnValue(response);
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data_source_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
setHideLocalCluster,
setUiSettings,
setDataSourceSelection,
getDefaultDataSourceId,
getDefaultDataSourceId$,
} from './components/utils';
import { DataSourceSelectionService } from './service/data_source_selection_service';

Expand All @@ -40,6 +42,8 @@ export interface DataSourceManagementPluginSetup {
ui: {
DataSourceSelector: React.ComponentType<DataSourceSelectorProps>;
getDataSourceMenu: <T>() => React.ComponentType<DataSourceMenuProps<T>>;
getDefaultDataSourceId: typeof getDefaultDataSourceId;
getDefaultDataSourceId$: typeof getDefaultDataSourceId$;
raintygao marked this conversation as resolved.
Show resolved Hide resolved
};
dataSourceSelection: DataSourceSelectionService;
}
Expand Down Expand Up @@ -123,6 +127,8 @@ export class DataSourceManagementPlugin
DataSourceSelector: createDataSourceSelector(uiSettings, dataSource),
getDataSourceMenu: <T>() => createDataSourceMenu<T>(),
},
getDefaultDataSourceId,
getDefaultDataSourceId$,
};
}

Expand Down
Loading