Skip to content

Commit

Permalink
[Discover 2.0] Updating fetch functions to include local cluster (ope…
Browse files Browse the repository at this point in the history
…nsearch-project#7542)

* Update datasources fetch function to include local cluster
* Check for duplicates when fetching external datasources (in the case local cluster is added as a datasource)
* Clean up types in DataSetNavigator so items are displayed properly


---------

Signed-off-by: Sean Li <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Qxisylolo committed Aug 1, 2024
1 parent caa0069 commit 688566e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7542.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Fix tables not displaying in navigator and add local cluster to datasources ([#7542](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7542))
59 changes: 33 additions & 26 deletions src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ interface DataSetNavigatorState {
externalDataSources: SimpleDataSource[];
currentDataSourceRef?: SimpleDataSource;
currentDataSet?: SimpleDataSet;
cachedDatabases: any[];
cachedDatabases: SimpleObject[];
cachedTables: SimpleObject[];
}

interface SelectedDataSetState extends SimpleDataSet {
database?: any | undefined;
database?: SimpleObject | undefined;
}

export const DataSetNavigator = (props: DataSetNavigatorProps) => {
Expand Down Expand Up @@ -285,7 +285,10 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {
if (status === DirectQueryLoadingStatus.SUCCESS) {
setNavigatorState((prevState) => ({
...prevState,
cachedDatabases: dataSourceCache.databases,
cachedDatabases: dataSourceCache.databases.map((database) => ({
id: database.name,
title: database.name,
})),
}));
} else if (
status === DirectQueryLoadingStatus.CANCELED ||
Expand All @@ -297,7 +300,7 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {
}, [databasesLoadStatus, selectedDataSetState?.dataSourceRef]);

const handleSelectExternalDataSource = useCallback(
async (dataSource) => {
async (dataSource: SimpleDataSource) => {
if (dataSource && dataSource.type === SIMPLE_DATA_SOURCE_TYPES.EXTERNAL) {
const dataSourceCache = CatalogCacheManager.getOrCreateDataSource(
dataSource.name,
Expand All @@ -315,13 +318,15 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {
} else if (dataSourceCache.status === CachedDataSourceStatus.Updated) {
setNavigatorState((prevState) => ({
...prevState,
cachedDatabases: dataSourceCache.databases,
cachedDatabases: dataSourceCache.databases.map((database) => ({
id: database.name,
title: database.name,
})),
}));
}
setSelectedDataSetState((prevState) => ({
...prevState,
...prevState!,
dataSourceRef: dataSource,
isExternal: true,
}));
}
},
Expand All @@ -330,13 +335,13 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {

// Start loading tables for selected database
const handleSelectExternalDatabase = useCallback(
(externalDatabase: SimpleDataSource) => {
if (selectedDataSetState?.dataSourceRef && externalDatabase) {
(externalDatabase: SimpleObject) => {
if (selectedDataSetState?.dataSourceRef && externalDatabase && externalDatabase.title) {
let databaseCache: CachedDatabase;
try {
databaseCache = CatalogCacheManager.getDatabase(
selectedDataSetState.dataSourceRef.name,
externalDatabase.name,
externalDatabase.title,
selectedDataSetState.dataSourceRef.id
);
} catch (error) {
Expand All @@ -349,7 +354,7 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {
) {
startLoadingTables({
dataSourceName: selectedDataSetState.dataSourceRef.name,
databaseName: externalDatabase.name,
databaseName: externalDatabase.title,
dataSourceMDSId: selectedDataSetState.dataSourceRef.id,
});
} else if (databaseCache.status === CachedDataSourceStatus.Updated) {
Expand Down Expand Up @@ -378,7 +383,7 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {
try {
databaseCache = CatalogCacheManager.getDatabase(
selectedDataSetState.dataSourceRef.name,
selectedDataSetState.database,
selectedDataSetState.database.title!,
selectedDataSetState.dataSourceRef.id
);
} catch (error) {
Expand Down Expand Up @@ -549,10 +554,10 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {
: 'Databases',
items: [
...navigatorState.cachedDatabases.map((db) => ({
name: db.name,
name: db.title,
onClick: async () => {
setSelectedDataSetState((prevState) => ({
...prevState,
...prevState!,
database: db,
}));
await handleSelectExternalDatabase(db);
Expand Down Expand Up @@ -654,24 +659,26 @@ export const DataSetNavigator = (props: DataSetNavigatorProps) => {
createDatabasesPanel(),
{
id: 6,
title: selectedDataSetState?.database ? selectedDataSetState.database.name : 'Tables',
title: selectedDataSetState?.database ? selectedDataSetState.database.title : 'Tables',
items: [
...navigatorState.cachedTables.map((table) => ({
name: table.name,
name: table.title,
onClick: async () => {
const tableObject = {
const tableObject: SimpleDataSet = {
...selectedDataSetState,
id: `${selectedDataSetState?.dataSourceRef!.name}.${
selectedDataSetState?.database.name
}.${table.name}`,
selectedDataSetState?.database?.title
}.${table.title}`,
title: `${selectedDataSetState?.dataSourceRef!.name}.${
selectedDataSetState?.database.name
}.${table.name}`,
dataSourceRef: {
id: selectedDataSetState?.dataSourceRef!.id,
name: selectedDataSetState?.dataSourceRef!.name,
type: selectedDataSetState?.dataSourceRef!.type,
},
selectedDataSetState?.database?.title
}.${table.title}`,
...(selectedDataSetState?.dataSourceRef && {
dataSourceRef: {
id: selectedDataSetState?.dataSourceRef!.id,
name: selectedDataSetState?.dataSourceRef!.name,
type: selectedDataSetState?.dataSourceRef!.type,
} as SimpleDataSource,
}),
type: SIMPLE_DATA_SET_TYPES.TEMPORARY_ASYNC,
};
await handleSelectedDataSet(tableObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export const fetchDataSources = async (client: SavedObjectsClientContract) => {
type: 'data-source',
perPage: 10000,
});
return resp.savedObjects.map((savedObject) => ({
id: savedObject.id,
name: savedObject.attributes.title,
type: 'data-source',
})) as SimpleDataSource[];
const dataSources: SimpleDataSource[] = [{ id: '', name: 'Local Cluster', type: 'data-source' }];
return dataSources.concat([
...(resp.savedObjects.map((savedObject) => ({
id: savedObject.id,
name: savedObject.attributes.title,
type: 'data-source',
})) as SimpleDataSource[]),
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { HttpStart } from 'opensearch-dashboards/public';
import { DatasourceDetails } from '../types';

export const fetchIfExternalDataSourcesEnabled = async (http: HttpStart) => {
try {
Expand All @@ -19,8 +20,8 @@ export const fetchExternalDataSources = async (http: HttpStart, connectedCluster
connectedClusters.map(async (cluster) => {
const dataSources = await http.get(`/api/dataconnections/dataSourceMDSId=${cluster}`);
return dataSources
.filter((dataSource) => dataSource.connector === 'S3GLUE')
.map((dataSource) => ({
.filter((dataSource: DatasourceDetails) => dataSource.connector === 'S3GLUE')
.map((dataSource: DatasourceDetails) => ({
name: dataSource.name,
status: dataSource.status,
dataSourceRef: cluster,
Expand All @@ -29,5 +30,17 @@ export const fetchExternalDataSources = async (http: HttpStart, connectedCluster
);

const flattenedResults = results.flat();
return flattenedResults;
const uniqueResults = Array.from(
flattenedResults
.reduce((map, ds) => {
const key = `${ds.name}-${ds.status}`;
if (!map.has(key) || ds.dataSourceRef === '') {
map.set(key, ds);
}
return map;
}, new Map<string, any>())
.values()
);

return uniqueResults;
};

0 comments on commit 688566e

Please sign in to comment.