diff --git a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts index 80c553249a0..56584e7525e 100644 --- a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts +++ b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts @@ -18,7 +18,8 @@ import { } from './types'; import { chunk, maxBy } from 'lodash'; import assert from 'assert'; -import { isValidAssetMapping, modelRevisionNodesAssetsToKey, modelRevisionToKey } from './utils'; +import { isValidAssetMapping } from './utils'; +import { modelRevisionNodesAssetToKey, createModelRevisionKey } from './idAndKeyTranslation'; import { type ModelWithAssetMappings } from './AssetMappingAndNode3DCacheProvider'; import { AssetMappingPerAssetIdCache } from './AssetMappingPerAssetIdCache'; import { AssetMappingPerNodeIdCache } from './AssetMappingPerNodeIdCache'; @@ -149,7 +150,7 @@ export class AssetMappingAndNode3DCache { } assetMappingsPerModel.forEach(async (modelMapping) => { modelMapping.assetMappings.forEach(async (item) => { - const key = modelRevisionNodesAssetsToKey(modelId, revisionId, [item.assetId]); + const key = modelRevisionNodesAssetToKey(modelId, revisionId, item.assetId); await this.assetIdsToAssetMappingCache.setAssetMappingsCacheItem(key, item); }); }); @@ -159,7 +160,7 @@ export class AssetMappingAndNode3DCache { modelId: ModelId, revisionId: RevisionId ): Promise { - const key = modelRevisionToKey(modelId, revisionId); + const key = createModelRevisionKey(modelId, revisionId); const cachedResult = await this.modelToAssetMappingsCache.getModelToAssetMappingCacheItems(key); if (cachedResult !== undefined) { @@ -180,7 +181,7 @@ export class AssetMappingAndNode3DCache { await Promise.all( currentChunk.map(async (id) => { - const key = modelRevisionNodesAssetsToKey(modelId, revisionId, [id]); + const key = modelRevisionNodesAssetToKey(modelId, revisionId, id); const cachedResult = await this.getItemCacheResult(type, key); if (cachedResult !== undefined) { chunkInCache.push(...cachedResult); @@ -235,18 +236,22 @@ export class AssetMappingAndNode3DCache { .autoPagingToArray({ limit: Infinity }); assetMapping3D.forEach(async (item) => { - const keyAssetId: ModelAssetIdKey = modelRevisionNodesAssetsToKey(modelId, revisionId, [ + const keyAssetId: ModelAssetIdKey = modelRevisionNodesAssetToKey( + modelId, + revisionId, item.assetId - ]); - const keyNodeId: ModelTreeIndexKey = modelRevisionNodesAssetsToKey(modelId, revisionId, [ + ); + const keyNodeId: ModelTreeIndexKey = modelRevisionNodesAssetToKey( + modelId, + revisionId, item.nodeId - ]); + ); await this.assetIdsToAssetMappingCache.setAssetMappingsCacheItem(keyAssetId, item); await this.nodeIdsToAssetMappingCache.setAssetMappingsCacheItem(keyNodeId, item); }); currentChunk.forEach(async (id) => { - const key = modelRevisionNodesAssetsToKey(modelId, revisionId, [id]); + const key = modelRevisionNodesAssetToKey(modelId, revisionId, id); const cachedResult = await this.getItemCacheResult(filterType, key); if (cachedResult === undefined) { diff --git a/react-components/src/components/CacheProvider/AssetMappingPerModelCache.ts b/react-components/src/components/CacheProvider/AssetMappingPerModelCache.ts index 2e6b0565920..8329234ee8b 100644 --- a/react-components/src/components/CacheProvider/AssetMappingPerModelCache.ts +++ b/react-components/src/components/CacheProvider/AssetMappingPerModelCache.ts @@ -4,7 +4,8 @@ import { type CogniteClient, type AssetMapping3D } from '@cognite/sdk/dist/src'; import { type ModelId, type RevisionId, type ModelRevisionKey } from './types'; import { type AssetMapping } from './AssetMappingAndNode3DCache'; -import { isValidAssetMapping, modelRevisionToKey } from './utils'; +import { isValidAssetMapping } from './utils'; +import { createModelRevisionKey } from './idAndKeyTranslation'; export class AssetMappingPerModelCache { private readonly _sdk: CogniteClient; @@ -32,7 +33,7 @@ export class AssetMappingPerModelCache { modelId: ModelId, revisionId: RevisionId ): Promise { - const key = modelRevisionToKey(modelId, revisionId); + const key = createModelRevisionKey(modelId, revisionId); const assetMappings = this.fetchAssetMappingsForModel(modelId, revisionId); this.setModelToAssetMappingCacheItems(key, assetMappings); diff --git a/react-components/src/components/CacheProvider/Node3DPerNodeIdCache.ts b/react-components/src/components/CacheProvider/Node3DPerNodeIdCache.ts index 9d8c95e5913..cc7b7c085f6 100644 --- a/react-components/src/components/CacheProvider/Node3DPerNodeIdCache.ts +++ b/react-components/src/components/CacheProvider/Node3DPerNodeIdCache.ts @@ -8,7 +8,7 @@ import { type RevisionId, type ModelTreeIndexKey } from './types'; -import { modelRevisionNodesAssetsToKey } from './utils'; +import { modelRevisionNodesAssetToKey } from './idAndKeyTranslation'; import { fetchNodesForNodeIds } from './requests'; export class Node3DPerNodeIdCache { @@ -30,7 +30,7 @@ export class Node3DPerNodeIdCache { await Promise.all( currentChunk.map(async (id) => { - const key = modelRevisionNodesAssetsToKey(modelId, revisionId, [id]); + const key = modelRevisionNodesAssetToKey(modelId, revisionId, id); const cachedResult = await this.getNodeIdToNode3DCacheItem(key); if (cachedResult !== undefined) { chunkInCache.push(cachedResult); @@ -50,7 +50,7 @@ export class Node3DPerNodeIdCache { ): Promise { const node3Ds = await this.getNodesForNodeIds(modelId, revisionId, nodeIds ?? []); node3Ds.forEach((node) => { - const key = modelRevisionNodesAssetsToKey(modelId, revisionId, [node.id]); + const key = modelRevisionNodesAssetToKey(modelId, revisionId, node.id); this.setNodeIdToNode3DCacheItem(key, Promise.resolve(node)); }); } diff --git a/react-components/src/components/CacheProvider/PointCloudAnnotationCache.ts b/react-components/src/components/CacheProvider/PointCloudAnnotationCache.ts index c6e72b51564..a07c695f466 100644 --- a/react-components/src/components/CacheProvider/PointCloudAnnotationCache.ts +++ b/react-components/src/components/CacheProvider/PointCloudAnnotationCache.ts @@ -10,9 +10,10 @@ import { type AnnotationId } from './types'; import { type CogniteClient, type Asset, type AnnotationFilterProps } from '@cognite/sdk'; -import { getAssetIdOrExternalIdFromPointCloudAnnotation, modelRevisionToKey } from './utils'; +import { getAssetIdOrExternalIdFromPointCloudAnnotation } from './utils'; import { fetchPointCloudAnnotationAssets } from './AnnotationModelUtils'; import assert from 'assert'; +import { createModelRevisionKey } from './idAndKeyTranslation'; export class PointCloudAnnotationCache { private readonly _sdk: CogniteClient; @@ -34,7 +35,7 @@ export class PointCloudAnnotationCache { modelId: ModelId, revisionId: RevisionId ): Promise> { - const key = modelRevisionToKey(modelId, revisionId); + const key = createModelRevisionKey(modelId, revisionId); const cachedResult = this._modelToAnnotationAssetMappings.get(key); if (cachedResult !== undefined) { @@ -51,7 +52,7 @@ export class PointCloudAnnotationCache { modelId: ModelId, revisionId: RevisionId ): Promise { - const key = modelRevisionToKey(modelId, revisionId); + const key = createModelRevisionKey(modelId, revisionId); const cachedResult = this._modelToAnnotationMappings.get(key); if (cachedResult !== undefined) { diff --git a/react-components/src/components/CacheProvider/idAndKeyTranslation.ts b/react-components/src/components/CacheProvider/idAndKeyTranslation.ts index 6df9932fe51..27aaaad8113 100644 --- a/react-components/src/components/CacheProvider/idAndKeyTranslation.ts +++ b/react-components/src/components/CacheProvider/idAndKeyTranslation.ts @@ -9,7 +9,8 @@ import { type ModelRevisionKey, type TreeIndex, type RevisionId, - type ModelId + type ModelId, + type ModelAssetIdKey } from './types'; import { split } from 'lodash'; @@ -34,3 +35,11 @@ export function createModelTreeIndexKey( export function createFdmKey(id: DmsUniqueIdentifier): FdmKey { return `${id.space}/${id.externalId}`; } + +export function modelRevisionNodesAssetToKey( + modelId: ModelId, + revisionId: RevisionId, + id: number +): ModelAssetIdKey { + return `${modelId}/${revisionId}/${id}`; +} diff --git a/react-components/src/components/CacheProvider/utils.ts b/react-components/src/components/CacheProvider/utils.ts index 09790bd94b2..36495f21c5c 100644 --- a/react-components/src/components/CacheProvider/utils.ts +++ b/react-components/src/components/CacheProvider/utils.ts @@ -6,43 +6,8 @@ import { type AnnotationsCogniteAnnotationTypesImagesAssetLink, type AnnotationModel, type AnnotationsBoundingVolume, - type CogniteInternalId, type AssetMapping3D } from '@cognite/sdk'; -import { - type ModelRevisionId, - type ModelAssetIdKey, - type ModelId, - type ModelRevisionKey, - type RevisionId, - type ModelTreeIndexKey -} from './types'; - -export function modelRevisionToKey(modelId: ModelId, revisionId: RevisionId): ModelRevisionKey { - return `${modelId}/${revisionId}`; -} - -export function modelRevisionKeyToModelRevision(key: ModelRevisionKey): [ModelId, RevisionId] { - const [modelId, revisionId] = key.split('/'); - - return [Number(modelId), Number(revisionId)]; -} - -export function modelRevisionNodesAssetsToKey( - modelId: ModelId, - revisionId: RevisionId, - ids: number[] -): ModelTreeIndexKey { - const idsSerialized = ids.reduce((a, b) => a + b, 0); - return `${modelId}/${revisionId}/${idsSerialized}`; -} - -export function modelRevisionAssetIdsToKey( - modelRevisionId: ModelRevisionId, - assetId: CogniteInternalId -): ModelAssetIdKey { - return `${modelRevisionId.modelId}/${modelRevisionId.revisionId}/${assetId}`; -} export function getAssetIdOrExternalIdFromPointCloudAnnotation( annotation: AnnotationModel diff --git a/react-components/src/data-providers/legacy-fdm-provider/fdmEdgesToCadConnections.ts b/react-components/src/data-providers/legacy-fdm-provider/fdmEdgesToCadConnections.ts index ce93b4b2646..a258ca80ac5 100644 --- a/react-components/src/data-providers/legacy-fdm-provider/fdmEdgesToCadConnections.ts +++ b/react-components/src/data-providers/legacy-fdm-provider/fdmEdgesToCadConnections.ts @@ -12,13 +12,13 @@ import { } from '../../components/CacheProvider/types'; import { type DmsUniqueIdentifier, type EdgeItem } from '../FdmSDK'; import { type InModel3dEdgeProperties } from './dataModels'; -import { - modelRevisionKeyToModelRevision, - modelRevisionToKey -} from '../../components/CacheProvider/utils'; import { executeParallel } from '../../utilities/executeParallel'; import { isDefined } from '../../utilities/isDefined'; import { chunk } from 'lodash'; +import { + createModelRevisionKey, + revisionKeyToIds +} from '../../components/CacheProvider/idAndKeyTranslation'; const MAX_PARALLEL_QUERIES = 2; @@ -56,7 +56,7 @@ function createModelToNodeIdConnectionsMap( ): Map { return edges.reduce((connectionMap, edge) => { const modelId = Number(edge.endNode.externalId); - const revisionKey = modelRevisionToKey(modelId, edge.properties.revisionId); + const revisionKey = createModelRevisionKey(modelId, edge.properties.revisionId); const connectionObject = { nodeId: edge.properties.revisionNodeId, @@ -80,7 +80,7 @@ async function getTreeIndexConnectionsForNodeIdConnections( connectionList: NodeIdConnection[], cogniteClient: CogniteClient ): Promise { - const [modelId, revisionId] = modelRevisionKeyToModelRevision(modelRevisionKey); + const [modelId, revisionId] = revisionKeyToIds(modelRevisionKey); const connectionChunks = chunk(connectionList, 1000); const connectionResult: FdmCadConnection[] = []; diff --git a/react-components/src/data-providers/utils/mergeQueryResult.ts b/react-components/src/data-providers/utils/mergeQueryResult.ts index 41a8f348bf7..6a213370322 100644 --- a/react-components/src/data-providers/utils/mergeQueryResult.ts +++ b/react-components/src/data-providers/utils/mergeQueryResult.ts @@ -3,7 +3,7 @@ */ export function mergeQueryResults>(dst: T, src: T): T { - [...Object.keys(src)].forEach((key0) => { + Object.keys(src).forEach((key0) => { if (!(key0 in dst)) { Object.assign(dst, key0, []); } diff --git a/react-components/src/query/useSearchMappedEquipmentFDM.tsx b/react-components/src/query/useSearchMappedEquipmentFDM.tsx index bd38fbc2e28..4faf73b391a 100644 --- a/react-components/src/query/useSearchMappedEquipmentFDM.tsx +++ b/react-components/src/query/useSearchMappedEquipmentFDM.tsx @@ -15,6 +15,7 @@ import { type AddModelOptions } from '@cognite/reveal'; import { isEqual, uniq, chunk } from 'lodash'; import { type Fdm3dDataProvider } from '../data-providers/Fdm3dDataProvider'; import { removeEmptyProperties } from '../utilities/removeEmptyProperties'; +import { isDefined } from '../utilities/isDefined'; export type InstancesWithView = { view: Source; instances: NodeItem[] }; @@ -202,19 +203,22 @@ async function createSourcesFromViews( }) ); - return viewsToSearch.map((view) => { - const version = viewToVersionMap.get(`${view.space}/${view.externalId}`); - if (version === undefined) { - throw Error( - `Could not find version for view with space/externalId ${view.space}/${view.externalId}` - ); - } - return { - ...view, - type: 'view' as const, - version - }; - }); + return viewsToSearch + .map((view) => { + const version = viewToVersionMap.get(`${view.space}/${view.externalId}`); + if (version === undefined) { + console.error( + `Could not find version for view with space/externalId ${view.space}/${view.externalId}` + ); + return undefined; + } + return { + ...view, + type: 'view' as const, + version + }; + }) + .filter(isDefined); } catch (e) { console.error('Error when fetching sources from views', e); throw e; diff --git a/react-components/src/utilities/convertAssetMetadataToLowerCase.ts b/react-components/src/utilities/convertAssetMetadataToLowerCase.ts index 8d9074bd801..64897516486 100644 --- a/react-components/src/utilities/convertAssetMetadataToLowerCase.ts +++ b/react-components/src/utilities/convertAssetMetadataToLowerCase.ts @@ -7,7 +7,7 @@ export function convertAssetMetadataKeysToLowerCase(asset: Asset): Asset { return { ...asset, metadata: Object.fromEntries( - [...Object.entries(asset.metadata ?? {})].map( + Object.entries(asset.metadata ?? {}).map( ([key, value]) => [key.toLowerCase(), value] as const ) )