Skip to content

Commit

Permalink
optimize the code
Browse files Browse the repository at this point in the history
Signed-off-by: yubonluo <[email protected]>
  • Loading branch information
yubonluo committed Nov 11, 2024
1 parent 0db2dd8 commit a86a3f7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/import/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface SavedObjectsImportMissingReferencesError {
*/
export interface SavedObjectsImportMissingDataSourceError {
type: 'missing_data_source';
dataSourceName: string;
dataSource: string;
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/core/server/saved_objects/import/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,7 @@ const parseJSONSpec = (spec: string) => {

export function findReferenceDataSourceForObject(
savedObject: SavedObject,
savedObjects: Map<
string,
SavedObject<{
title?: string | undefined;
}>
>,
savedObjects: Map<string, SavedObject>,
visited = new Set<string>()
): SavedObjectReference | null {
const { references } = savedObject;
Expand Down
8 changes: 5 additions & 3 deletions src/core/server/saved_objects/import/validate_data_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { fetchNestedDependencies } from '../export/inject_nested_depdendencies';
import { SavedObject, SavedObjectsBaseOptions, SavedObjectsClientContract } from '../types';
import { SavedObjectsImportError } from './types';
import { findReferenceDataSourceForObject } from './utils';
Expand Down Expand Up @@ -38,11 +39,12 @@ export async function validateDataSources(
return response?.saved_objects?.map((ds) => ds.id) ?? [];
});

const filteredObjectsMap = new Map(savedObjects.map((so) => [so.id, so]));
const nestedDependencies = await fetchNestedDependencies(filteredObjects, savedObjectsClient);
const nestedObjectsMap = new Map(nestedDependencies.objects.map((object) => [object.id, object]));

for (const object of filteredObjects) {
const { id, type, attributes } = object;
const referenceDS = findReferenceDataSourceForObject(object, filteredObjectsMap);
const referenceDS = findReferenceDataSourceForObject(object, nestedObjectsMap);
if (referenceDS && !assignedDataSourcesInTargetWorkspace.includes(referenceDS.id)) {
errorMap[`${type}:${id}`] = {
id,
Expand All @@ -51,7 +53,7 @@ export async function validateDataSources(
meta: { title: attributes?.title },
error: {
type: 'missing_data_source',
dataSourceName: referenceDS.name,
dataSource: referenceDS.id,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ const getErrorMessage = ({ error }: SavedObjectsImportError) => {
return unsupportedTypeErrorMessage;
} else if (error.type === 'missing_data_source') {
return i18n.translate('savedObjectsManagement.objectsTable.copyResult.missingDataSourceError', {
defaultMessage: 'Missing data source: {dsName}.',
values: { dsName: error.dataSourceName },
defaultMessage: 'Missing data source: {ds}',
values: { ds: error.dataSource },
});
} else if (error.type === 'missing_references') {
return i18n.translate('savedObjectsManagement.objectsTable.copyResult.missingReferencesError', {
Expand Down

0 comments on commit a86a3f7

Please sign in to comment.