Skip to content

Commit

Permalink
fix(composer): improve load sub model latency
Browse files Browse the repository at this point in the history
  • Loading branch information
sheilaXu committed Nov 28, 2023
1 parent aee749d commit 23ad9ad
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
2 changes: 2 additions & 0 deletions examples/react-app/src/components/SceneViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { sceneId, componentTypeQueries, entityQueries, dataBindingTemplate } fro
import './SceneViewer.scss';

const sceneLoader = dataSource.s3SceneLoader(sceneId);
const sceneMetadataModule = dataSource.sceneMetadataModule(sceneId);

const queries = [
...componentTypeQueries.map((q) => dataSource.query.timeSeriesData(q)),
Expand All @@ -30,6 +31,7 @@ const SceneViewer: FC<SceneViewerProps> = ({ onSelectionChanged, onWidgetClick }
},
}}
onSelectionChanged={onSelectionChanged}
sceneMetadataModule={sceneMetadataModule}
onWidgetClick={onWidgetClick}
queries={queries}
dataBindingTemplate={dataBindingTemplate}
Expand Down
1 change: 1 addition & 0 deletions packages/scene-composer/src/common/entityModelConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const componentTypeToId: Record<KnownComponentType, string> = {
};
export const DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME = 'isVisualOf';
export const DEFAULT_PARENT_RELATIONSHIP_NAME = 'isChildOf';
export const SUB_MODEL_REF_PARENT_RELATIONSHIP_NAME = 'parentRef';
export const DEFAULT_NODE_COMPONENT_NAME = 'Node';
export const SCENE_ROOT_ENTITY_ID = 'SCENES_EntityId';
export const SCENE_ROOT_ENTITY_NAME = '$SCENES';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('SceneLayers', () => {
expect(processQueries as jest.Mock).toBeCalledWith(
[
expect.stringContaining("AND e.entityId = 'layer1'"),
expect.stringContaining(`MATCH (binding)<-[r2:${DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME}]-(entity)-[r]->(e)`),
expect.stringContaining(`MATCH (binding)<-[r2]-(entity)-[r]->(e)`),
],
expect.anything(),
);
Expand Down
9 changes: 6 additions & 3 deletions packages/scene-composer/src/components/SceneLayers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { KnownSceneProperty } from '../interfaces';
import {
DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME,
DEFAULT_LAYER_RELATIONSHIP_NAME,
SUB_MODEL_REF_PARENT_RELATIONSHIP_NAME,
} from '../common/entityModelConstants';

export const SceneLayers: React.FC = () => {
Expand All @@ -34,12 +35,14 @@ export const SceneLayers: React.FC = () => {
MATCH (entity)-[r]->(e)
WHERE r.relationshipName = '${DEFAULT_LAYER_RELATIONSHIP_NAME}'
AND e.entityId = '${layerId}'`,
// Get entityBinding for the nodes in the layer
// Get entityBinding and subModel parentRef for the nodes in the layer
`SELECT entity, r2, binding
FROM EntityGraph
MATCH (binding)<-[r2:${DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME}]-(entity)-[r]->(e)
MATCH (binding)<-[r2]-(entity)-[r]->(e)
WHERE r.relationshipName = '${DEFAULT_LAYER_RELATIONSHIP_NAME}'
AND e.entityId = '${layerId}'`,
AND e.entityId = '${layerId}'
AND (r2.relationshipName = '${DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME}'
OR r2.relationshipName = '${SUB_MODEL_REF_PARENT_RELATIONSHIP_NAME}')`,
],
(node) => (node.properties.layerIds = [...(node.properties.layerIds ?? []), layerId!]),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME,
DEFAULT_PARENT_RELATIONSHIP_NAME,
NODE_COMPONENT_TYPE_ID,
SUB_MODEL_REF_PARENT_RELATIONSHIP_NAME,
componentTypeToId,
} from '../../common/entityModelConstants';
import { ISubModelRefComponent, KnownComponentType } from '../../interfaces';

import { isValidSceneNodeEntity, processQueries } from './processQueries';
import { SubModelRefComponentProperty } from './subModelRefComponent';

jest.mock('../mathUtils', () => ({
generateUUID: jest.fn(() => 'random-uuid'),
Expand All @@ -31,10 +31,8 @@ describe('isValidSceneNodeEntity', () => {

describe('processQueries', () => {
const executeQuery = jest.fn();
const getSceneEntity = jest.fn();
const mockMetadataModule: Partial<TwinMakerSceneMetadataModule> = {
kgModule: { executeQuery },
getSceneEntity,
};

const mockRows = [
Expand Down Expand Up @@ -160,6 +158,11 @@ describe('processQueries', () => {
},
],
},
{
relationshipName: SUB_MODEL_REF_PARENT_RELATIONSHIP_NAME,
sourceEntityId: 'id1',
targetEntityId: parentRef,
},
],
},
{
Expand All @@ -177,21 +180,6 @@ describe('processQueries', () => {
},
];
executeQuery.mockResolvedValue({ rows: rows });
getSceneEntity.mockResolvedValue({
components: {
SubModelRef: {
properties: {
[SubModelRefComponentProperty.ParentRef]: {
value: {
relationshipValue: {
targetEntityId: parentRef,
},
},
},
},
},
},
});

const nodes = await processQueries(['random query']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME,
DEFAULT_PARENT_RELATIONSHIP_NAME,
NODE_COMPONENT_TYPE_ID,
SUB_MODEL_REF_PARENT_RELATIONSHIP_NAME,
} from '../../common/entityModelConstants';
import { IEntityBindingComponentInternal, ISceneNodeInternal, ISubModelRefComponentInternal } from '../../store';
import { generateUUID } from '../mathUtils';
import { findComponentByType } from '../nodeUtils';

import { parseNode } from './nodeComponent';
import { SubModelRefComponentProperty } from './subModelRefComponent';

export const isValidSceneNodeEntity = (entity: DocumentType): boolean => {
return !!(entity && entity['entityId'] && entity['components'] && entity['components'].length > 0);
Expand Down Expand Up @@ -62,6 +62,15 @@ export const processQueries = async (
if (relationship && relationship['sourceEntityId'] == entityId) {
if (relationship['relationshipName'] === DEFAULT_PARENT_RELATIONSHIP_NAME) {
sceneNodes[entityId].parentRef = relationship['targetEntityId'];
} else if (relationship['relationshipName'] === SUB_MODEL_REF_PARENT_RELATIONSHIP_NAME) {
sceneNodes[entityId].parentRef = relationship['targetEntityId'];

const subModelComp = findComponentByType(sceneNodes[entityId], KnownComponentType.SubModelRef) as
| ISubModelRefComponentInternal
| undefined;
if (subModelComp && !subModelComp.parentRef) {
subModelComp.parentRef = relationship['targetEntityId'];
}
} else if (relationship['relationshipName'] === DEFAULT_ENTITY_BINDING_RELATIONSHIP_NAME) {
const entityBinding: IEntityBindingComponentInternal = {
ref: generateUUID(),
Expand All @@ -82,19 +91,6 @@ export const processQueries = async (
for (const node of Object.values(sceneNodes)) {
postProcessNode?.(node);

// Fetch parentRef for SubModelRef node
const subModelComp = findComponentByType(node, KnownComponentType.SubModelRef) as
| ISubModelRefComponentInternal
| undefined;
if (subModelComp && !subModelComp.parentRef) {
const nodeEntity = await sceneMetadataModule.getSceneEntity({ entityId: node.ref });
subModelComp.parentRef =
nodeEntity.components?.[KnownComponentType.SubModelRef].properties?.[
SubModelRefComponentProperty.ParentRef
].value?.relationshipValue?.targetEntityId;
node.parentRef = subModelComp.parentRef;
}

if (node.parentRef && !sceneNodes[node.parentRef]) {
node.parentRef = undefined;
}
Expand Down

0 comments on commit 23ad9ad

Please sign in to comment.