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

refactor(react-components): minor refactors #4760

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
Expand All @@ -159,7 +160,7 @@ export class AssetMappingAndNode3DCache {
modelId: ModelId,
revisionId: RevisionId
): Promise<AssetMapping[]> {
const key = modelRevisionToKey(modelId, revisionId);
const key = createModelRevisionKey(modelId, revisionId);
const cachedResult = await this.modelToAssetMappingsCache.getModelToAssetMappingCacheItems(key);

if (cachedResult !== undefined) {
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,7 +33,7 @@ export class AssetMappingPerModelCache {
modelId: ModelId,
revisionId: RevisionId
): Promise<AssetMapping[]> {
const key = modelRevisionToKey(modelId, revisionId);
const key = createModelRevisionKey(modelId, revisionId);
const assetMappings = this.fetchAssetMappingsForModel(modelId, revisionId);

this.setModelToAssetMappingCacheItems(key, assetMappings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -50,7 +50,7 @@ export class Node3DPerNodeIdCache {
): Promise<void> {
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));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +35,7 @@ export class PointCloudAnnotationCache {
modelId: ModelId,
revisionId: RevisionId
): Promise<Map<AnnotationId, Asset>> {
const key = modelRevisionToKey(modelId, revisionId);
const key = createModelRevisionKey(modelId, revisionId);
const cachedResult = this._modelToAnnotationAssetMappings.get(key);

if (cachedResult !== undefined) {
Expand All @@ -51,7 +52,7 @@ export class PointCloudAnnotationCache {
modelId: ModelId,
revisionId: RevisionId
): Promise<PointCloudAnnotationModel[]> {
const key = modelRevisionToKey(modelId, revisionId);
const key = createModelRevisionKey(modelId, revisionId);
const cachedResult = this._modelToAnnotationMappings.get(key);

if (cachedResult !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
type ModelRevisionKey,
type TreeIndex,
type RevisionId,
type ModelId
type ModelId,
type ModelAssetIdKey
} from './types';

import { split } from 'lodash';
Expand All @@ -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}`;
}
35 changes: 0 additions & 35 deletions react-components/src/components/CacheProvider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,7 +56,7 @@ function createModelToNodeIdConnectionsMap(
): Map<ModelRevisionKey, NodeIdConnection[]> {
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,
Expand All @@ -80,7 +80,7 @@ async function getTreeIndexConnectionsForNodeIdConnections(
connectionList: NodeIdConnection[],
cogniteClient: CogniteClient
): Promise<FdmCadConnection[]> {
const [modelId, revisionId] = modelRevisionKeyToModelRevision(modelRevisionKey);
const [modelId, revisionId] = revisionKeyToIds(modelRevisionKey);
const connectionChunks = chunk(connectionList, 1000);

const connectionResult: FdmCadConnection[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

export function mergeQueryResults<T extends Record<string, unknown[]>>(dst: T, src: T): T {
[...Object.keys(src)].forEach((key0) => {
Object.keys(src).forEach((key0) => {
if (!(key0 in dst)) {
Object.assign(dst, key0, []);
}
Expand Down
30 changes: 17 additions & 13 deletions react-components/src/query/useSearchMappedEquipmentFDM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[] };

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Expand Down
Loading