From 1d1acb0113223a940aabd59780501b257e29e47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= Date: Mon, 16 Sep 2024 16:07:54 +0200 Subject: [PATCH 1/6] chore(react-components): small refactoring --- .../data-providers/utils/mergeQueryResult.ts | 2 +- .../src/query/useSearchMappedEquipmentFDM.tsx | 30 +++++++++++-------- .../convertAssetMetadataToLowerCase.ts | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) 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 ) ) From 5dfbaadb5e95642a0952aaa245e175aa27ac25f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= Date: Mon, 16 Sep 2024 16:08:16 +0200 Subject: [PATCH 2/6] chore: centralize model/revision key translation --- .../CacheProvider/AssetMappingAndNode3DCache.ts | 5 +++-- .../CacheProvider/AssetMappingPerModelCache.ts | 5 +++-- .../CacheProvider/PointCloudAnnotationCache.ts | 7 ++++--- .../src/components/CacheProvider/utils.ts | 11 ----------- .../legacy-fdm-provider/fdmEdgesToCadConnections.ts | 12 ++++++------ 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts index 80c553249a0..7b0933a2fa4 100644 --- a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts +++ b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts @@ -18,12 +18,13 @@ import { } from './types'; import { chunk, maxBy } from 'lodash'; import assert from 'assert'; -import { isValidAssetMapping, modelRevisionNodesAssetsToKey, modelRevisionToKey } from './utils'; +import { isValidAssetMapping, modelRevisionNodesAssetsToKey } from './utils'; import { type ModelWithAssetMappings } from './AssetMappingAndNode3DCacheProvider'; import { AssetMappingPerAssetIdCache } from './AssetMappingPerAssetIdCache'; import { AssetMappingPerNodeIdCache } from './AssetMappingPerNodeIdCache'; import { Node3DPerNodeIdCache } from './Node3DPerNodeIdCache'; import { AssetMappingPerModelCache } from './AssetMappingPerModelCache'; +import { createModelRevisionKey } from './idAndKeyTranslation'; export type NodeAssetMappingResult = { node?: Node3D; mappings: AssetMapping[] }; @@ -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) { 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/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/utils.ts b/react-components/src/components/CacheProvider/utils.ts index 09790bd94b2..dfd4c28ac0c 100644 --- a/react-components/src/components/CacheProvider/utils.ts +++ b/react-components/src/components/CacheProvider/utils.ts @@ -13,21 +13,10 @@ 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, 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[] = []; From b0e0f38a72920929a258e126d3c2c71f043f3fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= Date: Mon, 16 Sep 2024 16:12:59 +0200 Subject: [PATCH 3/6] chore: refactor and centralize even more --- .../AssetMappingAndNode3DCache.ts | 20 +++++++++------- .../CacheProvider/Node3DPerNodeIdCache.ts | 6 ++--- .../CacheProvider/idAndKeyTranslation.ts | 11 ++++++++- .../src/components/CacheProvider/utils.ts | 24 ------------------- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts index 7b0933a2fa4..9043d351583 100644 --- a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts +++ b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts @@ -18,7 +18,7 @@ import { } from './types'; import { chunk, maxBy } from 'lodash'; import assert from 'assert'; -import { isValidAssetMapping, modelRevisionNodesAssetsToKey } from './utils'; +import { isValidAssetMapping, modelRevisionNodesAssetToKey } from './idAndKeyTranslation'; import { type ModelWithAssetMappings } from './AssetMappingAndNode3DCacheProvider'; import { AssetMappingPerAssetIdCache } from './AssetMappingPerAssetIdCache'; import { AssetMappingPerNodeIdCache } from './AssetMappingPerNodeIdCache'; @@ -150,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); }); }); @@ -181,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); @@ -236,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/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/idAndKeyTranslation.ts b/react-components/src/components/CacheProvider/idAndKeyTranslation.ts index 6df9932fe51..140ecb81179 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, + 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 dfd4c28ac0c..36495f21c5c 100644 --- a/react-components/src/components/CacheProvider/utils.ts +++ b/react-components/src/components/CacheProvider/utils.ts @@ -6,32 +6,8 @@ import { type AnnotationsCogniteAnnotationTypesImagesAssetLink, type AnnotationModel, type AnnotationsBoundingVolume, - type CogniteInternalId, type AssetMapping3D } from '@cognite/sdk'; -import { - type ModelRevisionId, - type ModelAssetIdKey, - type ModelId, - type RevisionId, - type ModelTreeIndexKey -} from './types'; - -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 From acb50c34845e69ae9da3ee6064b329453ffcf5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= Date: Mon, 16 Sep 2024 16:18:44 +0200 Subject: [PATCH 4/6] chore: bump react-components version --- react-components/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-components/package.json b/react-components/package.json index 2a5f41575d6..f1bb0589e29 100644 --- a/react-components/package.json +++ b/react-components/package.json @@ -1,6 +1,6 @@ { "name": "@cognite/reveal-react-components", - "version": "0.58.4", + "version": "0.59.0", "exports": { ".": { "import": "./dist/index.js", From a05a57114263570822b526bd080cb0c4405547e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= Date: Mon, 16 Sep 2024 16:23:40 +0200 Subject: [PATCH 5/6] chore: lint fix --- .../components/CacheProvider/AssetMappingAndNode3DCache.ts | 4 ++-- .../src/components/CacheProvider/idAndKeyTranslation.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts index 9043d351583..56584e7525e 100644 --- a/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts +++ b/react-components/src/components/CacheProvider/AssetMappingAndNode3DCache.ts @@ -18,13 +18,13 @@ import { } from './types'; import { chunk, maxBy } from 'lodash'; import assert from 'assert'; -import { isValidAssetMapping, modelRevisionNodesAssetToKey } from './idAndKeyTranslation'; +import { isValidAssetMapping } from './utils'; +import { modelRevisionNodesAssetToKey, createModelRevisionKey } from './idAndKeyTranslation'; import { type ModelWithAssetMappings } from './AssetMappingAndNode3DCacheProvider'; import { AssetMappingPerAssetIdCache } from './AssetMappingPerAssetIdCache'; import { AssetMappingPerNodeIdCache } from './AssetMappingPerNodeIdCache'; import { Node3DPerNodeIdCache } from './Node3DPerNodeIdCache'; import { AssetMappingPerModelCache } from './AssetMappingPerModelCache'; -import { createModelRevisionKey } from './idAndKeyTranslation'; export type NodeAssetMappingResult = { node?: Node3D; mappings: AssetMapping[] }; diff --git a/react-components/src/components/CacheProvider/idAndKeyTranslation.ts b/react-components/src/components/CacheProvider/idAndKeyTranslation.ts index 140ecb81179..27aaaad8113 100644 --- a/react-components/src/components/CacheProvider/idAndKeyTranslation.ts +++ b/react-components/src/components/CacheProvider/idAndKeyTranslation.ts @@ -10,7 +10,7 @@ import { type TreeIndex, type RevisionId, type ModelId, - ModelAssetIdKey + type ModelAssetIdKey } from './types'; import { split } from 'lodash'; From 31c8656e0882893a2f3384b0b9bad36a98a5543d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Flatval?= Date: Mon, 16 Sep 2024 16:26:42 +0200 Subject: [PATCH 6/6] Revert "chore: bump react-components version" This reverts commit acb50c34845e69ae9da3ee6064b329453ffcf5a7. --- react-components/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-components/package.json b/react-components/package.json index f1bb0589e29..2a5f41575d6 100644 --- a/react-components/package.json +++ b/react-components/package.json @@ -1,6 +1,6 @@ { "name": "@cognite/reveal-react-components", - "version": "0.59.0", + "version": "0.58.4", "exports": { ".": { "import": "./dist/index.js",