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

fix(react-components): improve asset mapping cache mechanism and refactoring to stabilize rule threshold styling and switching #4652

Merged
merged 33 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7ea39ca
refactoring to use caching and use treeindex instead of numeric range…
danpriori Jun 14, 2024
246970f
use the asset mappings per model cache to generate the cache per asse…
danpriori Jun 20, 2024
61d5aa4
add cache for node 3d when loading reveal 3d resources to speed up th…
danpriori Jun 24, 2024
71a3dd7
some wip refactoring on cache functions
danpriori Jun 25, 2024
4b848b3
minor refactoring
danpriori Jun 25, 2024
8eb42fa
refactoring and adding cache for assets for no mappings to skip reque…
danpriori Jun 26, 2024
f46889c
cleanup
danpriori Jun 26, 2024
5232873
separate caches for asset and node ids
danpriori Jun 26, 2024
f7baf3b
initial test for triggering callback to switch on off rule base styli…
danpriori Jun 27, 2024
5466801
Merge remote-tracking branch 'origin/master' into danpriori/rule-base…
danpriori Jun 28, 2024
2c1857c
add loading spinner and some ui changes
danpriori Jun 28, 2024
67a8b9d
split into different useEffects hooks to let render the spinner sooner
danpriori Jun 28, 2024
06309dd
dont use spinner on the reset button
danpriori Jun 28, 2024
c48d6f6
cleanup
danpriori Jul 1, 2024
78673b9
lint and remove unused function
danpriori Jul 1, 2024
9a5413e
missed call to show up outputs panel
danpriori Jul 2, 2024
01d9e41
changes from cr
danpriori Jul 3, 2024
fa47fce
changes from cr - splitting out selector component into smaller pieces
danpriori Jul 4, 2024
b04a961
move extract asset id from mapped to the general hooks folder
danpriori Jul 4, 2024
6820e25
move _amountOfAssetIdsChunks to a private readonly parameter
danpriori Jul 4, 2024
511c495
use the correct map key for asset ids
danpriori Jul 4, 2024
06c1f4b
refactoring from cr and using the resource context provider to link s…
danpriori Jul 8, 2024
f0282f2
refactoring and splitting asset mapping caches into smaller classes
danpriori Jul 9, 2024
f4f9d23
lint
danpriori Jul 9, 2024
a123bf9
turn splitChunkInCacheNode3D to private
danpriori Jul 10, 2024
c46f3cb
more refactoring - cr
danpriori Jul 10, 2024
818296a
lint
danpriori Jul 10, 2024
306ca38
removing not used rule based callback
danpriori Jul 10, 2024
2e9ec23
populate the both caches
danpriori Jul 10, 2024
58a592e
move hooks into the hooks sub folder
danpriori Jul 10, 2024
fa90456
Merge remote-tracking branch 'origin/master' into danpriori/rule-base…
danpriori Jul 11, 2024
44c7891
update import for Reveal3DResourcesInfoContext
danpriori Jul 11, 2024
63bf233
minor refactoring to force spinner more stable - still not fully but …
danpriori Jul 11, 2024
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
danpriori marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { type ReactElement, type ReactNode, createContext, useContext, useMemo }
import { type CadModelOptions } from '../Reveal3DResources/types';
import {
type AssetMapping,
AssetMappingCache,
AssetMappingAndNode3DCache,
type NodeAssetMappingResult
} from './AssetMappingCache';
} from './AssetMappingAndNode3DCache';
import { type UseQueryResult, useQuery } from '@tanstack/react-query';
import { type CogniteInternalId } from '@cognite/sdk';
import { useSDK } from '../RevealCanvas/SDKProvider';
Expand All @@ -17,31 +17,80 @@ import { type ModelRevisionId, type ModelRevisionAssetNodesResult } from './type
import { fetchAncestorNodesForTreeIndex } from './requests';
import { type AnyIntersection } from '@cognite/reveal';

export type AssetMappingCacheContent = {
cache: AssetMappingCache;
export type AssetMappingAndNode3DCacheContent = {
cache: AssetMappingAndNode3DCache;
};

export type ModelWithAssetMappings = {
model: CadModelOptions;
assetMappings: AssetMapping[];
};

const AssetMappingCacheContext = createContext<AssetMappingCacheContent | undefined>(undefined);
const AssetMappingAndNode3DCacheContext = createContext<
AssetMappingAndNode3DCacheContent | undefined
>(undefined);

const useAssetMappingCache = (): AssetMappingCache => {
const content = useContext(AssetMappingCacheContext);
const useAssetMappingAndNode3DCache = (): AssetMappingAndNode3DCache => {
const content = useContext(AssetMappingAndNode3DCacheContext);

if (content === undefined) {
throw Error('Must use useAssetMappingCache inside a AssetMappingCacheContext');
throw Error('Must use useAssetMappingAndNode3DCache inside a AssetMappingCacheContext');
}

return content.cache;
};

export const useGenerateNode3DCache = (
cadModelOptions: CadModelOptions[],
assetMappings: ModelWithAssetMappings[] | undefined
): void => {
const assetMappingAndNode3DCache = useAssetMappingAndNode3DCache();

useMemo(() => {
cadModelOptions.forEach(async ({ modelId, revisionId }) => {
const assetMapping = assetMappings?.filter(
(item) => item.model.modelId === modelId && item.model.revisionId === revisionId
);
const nodeIdsFromAssetMappings = assetMapping?.flatMap((item) =>
item.assetMappings.map((mapping) => mapping.nodeId)
);

if (nodeIdsFromAssetMappings === undefined || nodeIdsFromAssetMappings.length === 0) return;

await assetMappingAndNode3DCache.generateNode3DCachePerItem(
modelId,
revisionId,
nodeIdsFromAssetMappings
);
});
}, [cadModelOptions, assetMappings]);
};

export const useGenerateAssetMappingCachePerItemFromModelCache = (
cadModelOptions: CadModelOptions[],
assetMappings: ModelWithAssetMappings[] | undefined
): void => {
const assetMappingAndNode3DCache = useAssetMappingAndNode3DCache();
useMemo(() => {
cadModelOptions.forEach(async ({ modelId, revisionId }) => {
const assetMapping = assetMappings?.filter(
(item) => item.model.modelId === modelId && item.model.revisionId === revisionId
);
if (assetMapping !== undefined && assetMapping.length > 0) {
await assetMappingAndNode3DCache.generateAssetMappingsCachePerItemFromModelCache(
modelId,
revisionId,
assetMapping
);
}
});
}, [cadModelOptions, assetMappings]);
};

export const useAssetMappedNodesForRevisions = (
cadModels: CadModelOptions[]
): UseQueryResult<ModelWithAssetMappings[]> => {
const assetMappingCache = useAssetMappingCache();
const assetMappingAndNode3DCache = useAssetMappingAndNode3DCache();

return useQuery({
queryKey: [
Expand All @@ -53,7 +102,7 @@ export const useAssetMappedNodesForRevisions = (
queryFn: async () => {
const fetchPromises = cadModels.map(
async (model) =>
await assetMappingCache
await assetMappingAndNode3DCache
.getAssetMappingsForModel(model.modelId, model.revisionId)
.then((assetMappings) => ({ model, assetMappings }))
);
Expand All @@ -68,7 +117,7 @@ export const useNodesForAssets = (
models: ModelRevisionId[],
assetIds: CogniteInternalId[]
): UseQueryResult<ModelRevisionAssetNodesResult[]> => {
const assetMappingCache = useAssetMappingCache();
const assetMappingAndNode3DCache = useAssetMappingAndNode3DCache();

return useQuery({
queryKey: [
Expand All @@ -80,7 +129,7 @@ export const useNodesForAssets = (
],
queryFn: async () => {
const modelAndNodeMapPromises = models.map(async (model) => {
const nodeMap = await assetMappingCache.getNodesForAssetIds(
const nodeMap = await assetMappingAndNode3DCache.getNodesForAssetIds(
model.modelId,
model.revisionId,
assetIds
Expand All @@ -98,7 +147,7 @@ export const useNodesForAssets = (
export const useAssetMappingForTreeIndex = (
intersection: AnyIntersection | undefined
): UseQueryResult<NodeAssetMappingResult> => {
const assetMappingCache = useAssetMappingCache();
const assetMappingAndNode3DCache = useAssetMappingAndNode3DCache();
const cdfClient = useSDK();

const isCadModel = intersection?.type === 'cad';
Expand Down Expand Up @@ -130,7 +179,7 @@ export const useAssetMappingForTreeIndex = (
cdfClient
);

return await assetMappingCache.getAssetMappingsForLowestAncestor(
return await assetMappingAndNode3DCache.getAssetMappingsForLowestAncestor(
modelId,
revisionId,
ancestors
Expand All @@ -140,13 +189,17 @@ export const useAssetMappingForTreeIndex = (
});
};

export function AssetMappingCacheProvider({ children }: { children?: ReactNode }): ReactElement {
export function AssetMappingAndNode3DCacheProvider({
children
}: {
children?: ReactNode;
}): ReactElement {
const cdfClient = useSDK();
const revealKeepAliveData = useRevealKeepAlive();

const fdmCache = useMemo(() => {
const cache =
revealKeepAliveData?.assetMappingCache.current ?? new AssetMappingCache(cdfClient);
revealKeepAliveData?.assetMappingCache.current ?? new AssetMappingAndNode3DCache(cdfClient);

const isRevealKeepAliveContextProvided = revealKeepAliveData !== undefined;
if (isRevealKeepAliveContextProvided) {
Expand All @@ -157,8 +210,8 @@ export function AssetMappingCacheProvider({ children }: { children?: ReactNode }
}, [cdfClient]);

return (
<AssetMappingCacheContext.Provider value={{ cache: fdmCache }}>
<AssetMappingAndNode3DCacheContext.Provider value={{ cache: fdmCache }}>
{children}
</AssetMappingCacheContext.Provider>
</AssetMappingAndNode3DCacheContext.Provider>
);
}
203 changes: 0 additions & 203 deletions react-components/src/components/CacheProvider/AssetMappingCache.ts
danpriori marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

Loading
Loading