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

[workspace]add 2 step loading in 'Associate data sources' modal #8999

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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/8999.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Add two-steps loading for associating data sources ([#8999](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8999))
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import { IntlProvider } from 'react-intl';

import { DataSourceConnectionType } from '../../../common/types';
import { DataSourceConnectionType, DataSourceConnection } from '../../../common/types';
import { chromeServiceMock, coreMock } from '../../../../../core/public/mocks';
import * as utilsExports from '../../utils';

Expand All @@ -23,42 +23,63 @@ const setupAssociationDataSourceModal = ({
}: Partial<AssociationDataSourceModalProps> = {}) => {
const coreServices = coreMock.createStart();
jest.spyOn(utilsExports, 'getDataSourcesList').mockResolvedValue([]);
jest.spyOn(utilsExports, 'fetchDataSourceConnections').mockResolvedValue([
{
id: 'ds1',
name: 'Data Source 1',
connectionType: DataSourceConnectionType.OpenSearchConnection,
type: 'OpenSearch',
relatedConnections: [

jest.spyOn(utilsExports, 'getOpenSearchAndDataConnections').mockResolvedValue({
openSearchConnections: [
{
id: 'ds1',
name: 'Data Source 1',
type: 'OpenSearch',
connectionType: 0,
Qxisylolo marked this conversation as resolved.
Show resolved Hide resolved
relatedConnections: [],
},
],
dataConnections: [
{
id: 'dqs1',
name: 'Data Connection 1',
connectionType: DataSourceConnectionType.DataConnection,
type: 'AWS Security Lake',
},
],
});

jest
.spyOn(utilsExports, 'updateFullFillRelatedConnections')
.mockImplementationOnce(
(
openSearchConnections: DataSourceConnection[],
directQueryConnections: DataSourceConnection[]
): DataSourceConnection[] => [
{
id: 'ds1-dqc1',
name: 'dqc1',
parentId: 'ds1',
connectionType: DataSourceConnectionType.DirectQueryConnection,
type: 'Amazon S3',
id: 'ds1',
name: 'Data Source 1',
type: 'OpenSearch',
connectionType: 0,
relatedConnections: [
{
id: 'ds1-dqc1',
name: 'dqc1',
type: 'Amazon S3',
connectionType: 1,
Qxisylolo marked this conversation as resolved.
Show resolved Hide resolved
parentId: 'ds1',
},
],
},
],
},
]
);

jest.spyOn(utilsExports, 'fetchDirectQueryConnections').mockResolvedValue([
{
id: 'ds1-dqc1',
name: 'dqc1',
parentId: 'ds1',
connectionType: DataSourceConnectionType.DirectQueryConnection,
type: 'Amazon S3',
},
{
id: 'ds2',
name: 'Data Source 2',
connectionType: DataSourceConnectionType.OpenSearchConnection,
type: 'OpenSearch',
},
{
id: 'dqs1',
name: 'Data Connection 1',
connectionType: DataSourceConnectionType.DataConnection,
type: 'AWS Security Lake',
connectionType: 1,
description: 'direct_query_connections_1',
parentId: 'ds1',
},
]);

const { logos } = chromeServiceMock.createStartContract();
render(
<IntlProvider locale="en">
Expand Down Expand Up @@ -115,10 +136,7 @@ describe('AssociationDataSourceModal', () => {
'Add data sources that will be available in the workspace. If a selected data source has related Direct Query data sources, they will also be available in the workspace.'
)
).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByRole('option', { name: 'Data Source 1' })).toBeInTheDocument();
expect(screen.getByRole('option', { name: 'Data Source 2' })).toBeInTheDocument();
});
await waitFor(() => expect(screen.getByText('Data Source 1')).toBeInTheDocument());
});

it('should display direct query connections after opensearch connection selected', async () => {
Expand All @@ -127,6 +145,7 @@ describe('AssociationDataSourceModal', () => {
});
expect(screen.getByText('Associate direct query data sources')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByText('Data Source 1')).toBeInTheDocument();
expect(screen.queryByRole('option', { name: 'dqc1' })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('option', { name: 'Data Source 1' }));
expect(screen.getByRole('option', { name: 'dqc1' })).toBeInTheDocument();
Expand All @@ -135,17 +154,14 @@ describe('AssociationDataSourceModal', () => {

it('should hide associated connections', async () => {
setupAssociationDataSourceModal({
excludedConnectionIds: ['ds2'],
excludedConnectionIds: ['ds1'],
});
expect(
screen.getByText(
'Add data sources that will be available in the workspace. If a selected data source has related Direct Query data sources, they will also be available in the workspace.'
)
).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByRole('option', { name: 'Data Source 1' })).toBeInTheDocument();
expect(screen.queryByRole('option', { name: 'Data Source 2' })).not.toBeInTheDocument();
});
expect(screen.queryByRole('option', { name: 'Data Source 1' })).not.toBeInTheDocument();
});

it('should call handleAssignDataSourceConnections with opensearch connections after assigned', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import {
import { FormattedMessage } from 'react-intl';
import { i18n } from '@osd/i18n';

import { getDataSourcesList, fetchDataSourceConnections } from '../../utils';
import {
getDataSourcesList,
getOpenSearchAndDataConnections,
fetchDirectQueryConnections,
updateFullFillRelatedConnections,
} from '../../utils';
import { DataSourceConnection, DataSourceConnectionType } from '../../../common/types';
import { HttpStart, NotificationsStart, SavedObjectsStart } from '../../../../../core/public';
import { AssociationDataSourceModalMode } from '../../../common/constants';
Expand Down Expand Up @@ -88,55 +93,62 @@ const convertConnectionToOption = ({
connection,
selectedConnectionIds,
logos,
isRelatedConnectionsLoaded,
}: {
connection: DataSourceConnection;
selectedConnectionIds: string[];
logos: Logos;
}) => ({
label: connection.name,
key: connection.id,
description: connection.description,
append:
connection.relatedConnections && connection.relatedConnections.length > 0 ? (
isRelatedConnectionsLoaded: boolean;
}) => {
return {
label: connection.name,
key: connection.id,
description: connection.description,
append: isRelatedConnectionsLoaded ? (
connection.relatedConnections && connection.relatedConnections.length > 0 ? (
<EuiBadge>
{i18n.translate('workspace.form.selectDataSource.optionBadge', {
defaultMessage: '+ {relatedConnections} related',
values: {
relatedConnections: connection.relatedConnections.length,
},
})}
</EuiBadge>
) : undefined
) : (
<EuiBadge>
{i18n.translate('workspace.form.selectDataSource.optionBadge', {
defaultMessage: '+ {relatedConnections} related',
values: {
relatedConnections: connection.relatedConnections.length,
},
{i18n.translate('workspace.form.selectDataSource.loading', {
defaultMessage: 'Loading...',
})}
</EuiBadge>
) : undefined,
disabled: connection.connectionType === DataSourceConnectionType.DirectQueryConnection,
checked:
connection.connectionType !== DataSourceConnectionType.DirectQueryConnection &&
selectedConnectionIds.includes(connection.id)
? ('on' as const)
: undefined,
prepend:
connection.connectionType === DataSourceConnectionType.DirectQueryConnection ? (
<>
<div style={{ width: 16 }} />
<ConnectionIcon connection={connection} logos={logos} />
</>
) : (
<ConnectionIcon connection={connection} logos={logos} />
),
parentId: connection.parentId,
});

disabled: connection.connectionType === DataSourceConnectionType.DirectQueryConnection,
checked:
connection.connectionType !== DataSourceConnectionType.DirectQueryConnection &&
selectedConnectionIds.includes(connection.id)
? ('on' as const)
: undefined,
prepend: <ConnectionIcon connection={connection} logos={logos} />,

parentId: connection.parentId,
};
};

const convertConnectionsToOptions = ({
connections,
showDirectQueryConnections,
selectedConnectionIds,
excludedConnectionIds,
logos,
isRelatedConnectionsLoaded,
}: {
connections: DataSourceConnection[];
excludedConnectionIds: string[];
showDirectQueryConnections: boolean;
selectedConnectionIds: string[];
logos: Logos;
isRelatedConnectionsLoaded: boolean;
}) => {
return connections
.flatMap((connection) => {
Expand All @@ -154,18 +166,28 @@ const convertConnectionsToOptions = ({
return [];
}

if (showDirectQueryConnections) {
if (!connection.relatedConnections || connection.relatedConnections.length === 0) {
return [];
if (connection.connectionType === DataSourceConnectionType.OpenSearchConnection) {
if (showDirectQueryConnections) {
if (!connection.relatedConnections || connection.relatedConnections.length === 0) {
return [connection];
Qxisylolo marked this conversation as resolved.
Show resolved Hide resolved
}
return [
connection,
...(selectedConnectionIds.includes(connection.id) ? connection.relatedConnections : []),
];
}
return [
connection,
...(selectedConnectionIds.includes(connection.id) ? connection.relatedConnections : []),
];
}

return [connection];
})
.map((connection) => convertConnectionToOption({ connection, selectedConnectionIds, logos }));
.map((connection) =>
convertConnectionToOption({
connection,
selectedConnectionIds,
logos,
isRelatedConnectionsLoaded,
})
);
};

export interface AssociationDataSourceModalProps {
Expand Down Expand Up @@ -199,6 +221,7 @@ export const AssociationDataSourceModalContent = ({
}: AssociationDataSourceModalProps) => {
const [allConnections, setAllConnections] = useState<DataSourceConnection[]>([]);
const [selectedConnectionIds, setSelectedConnectionIds] = useState<string[]>([]);
const [isRelatedConnectionsLoaded, setIsRelatedConnectionsLoaded] = useState(false);
const [options, setOptions] = useState<DataSourceModalOption[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isSaving, setIsSaving] = useState(false);
Expand Down Expand Up @@ -230,27 +253,56 @@ export const AssociationDataSourceModalContent = ({

useEffect(() => {
setIsLoading(true);
setIsRelatedConnectionsLoaded(false);
Qxisylolo marked this conversation as resolved.
Show resolved Hide resolved
getDataSourcesList(savedObjects.client, ['*'])
.then((dataSourcesList) => fetchDataSourceConnections(dataSourcesList, http, notifications))
.then((connections) => {
setAllConnections(connections);
.then((dataSourcesList) => {
return getOpenSearchAndDataConnections(dataSourcesList, notifications).then(
({ openSearchConnections, dataConnections }) => {
setAllConnections([...openSearchConnections, ...dataConnections]);
return { openSearchConnections, dataConnections, dataSourcesList };
}
);
})
.then(({ openSearchConnections, dataConnections, dataSourcesList }) => {
fetchDirectQueryConnections(dataSourcesList, http, notifications).then(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'm thinking of do we need to have a loading status for direct query connections for each data source. Since load direct query connections may take long time, but if we separate to each data source, it would be more faster.

Copy link
Contributor Author

@Qxisylolo Qxisylolo Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice, updated and new screenshot is also updated

(directQueryConnections) => {
const updatedOpenSearchConnections = updateFullFillRelatedConnections(
Qxisylolo marked this conversation as resolved.
Show resolved Hide resolved
openSearchConnections,
directQueryConnections
);

setAllConnections([...updatedOpenSearchConnections, ...dataConnections]);
setIsRelatedConnectionsLoaded(true); // related connections are completely loaded
}
);
})
.finally(() => {
setIsLoading(false);
});
}, [savedObjects.client, http, notifications, mode]);
}, [savedObjects.client, notifications, http]);

useEffect(() => {
setOptions(
convertConnectionsToOptions({
connections: allConnections,
excludedConnectionIds,
selectedConnectionIds,
showDirectQueryConnections: mode === AssociationDataSourceModalMode.DirectQueryConnections,
logos,
})
);
}, [allConnections, excludedConnectionIds, selectedConnectionIds, mode, logos]);
if (allConnections.length > 0) {
setOptions(
convertConnectionsToOptions({
connections: allConnections,
excludedConnectionIds,
selectedConnectionIds,
showDirectQueryConnections:
mode === AssociationDataSourceModalMode.DirectQueryConnections,
logos,
isRelatedConnectionsLoaded,
})
);
}
}, [
excludedConnectionIds,
selectedConnectionIds,
mode,
allConnections,
logos,
isRelatedConnectionsLoaded,
]);

return (
<>
Expand Down
Loading
Loading