Skip to content

Commit

Permalink
Add multi-property query support (#105)
Browse files Browse the repository at this point in the history
Co-authored-by: Xinyi Xu <xinyxu@amazon.com>
  • Loading branch information
sheilaXu and Xinyi Xu authored Sep 14, 2022
1 parent a0668e5 commit 9020c2f
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('getPropertyValueHistoryByComponentType', () => {
componentName: 'comp-1',
propertyName: 'prop-1',
};
const streamIdComponents3: TwinMakerDataStreamIdComponent = {
workspaceId: 'ws-1',
entityId: 'entity-2',
componentName: 'comp-1',
propertyName: 'prop-2',
};
const mockEntityRef1 = {
propertyName: streamIdComponents1.propertyName,
externalIdProperty: {
Expand All @@ -34,6 +40,12 @@ describe('getPropertyValueHistoryByComponentType', () => {
},
};
const mockEntityRef3 = {
propertyName: streamIdComponents3.propertyName,
externalIdProperty: {
extid: 'ext-id-2',
},
};
const mockEntityRef4 = {
propertyName: streamIdComponents1.propertyName,
externalIdProperty: {
extid: 'ext-id-3',
Expand All @@ -57,6 +69,13 @@ describe('getPropertyValueHistoryByComponentType', () => {
end,
meta: { componentTypeId: 'comp-type-1' },
},
{
id: toDataStreamId(streamIdComponents3),
resolution: '0',
start,
end,
meta: { componentTypeId: 'comp-type-1' },
},
];
const tmClient = new IoTTwinMakerClient({});
const twinMakerMetadataModule = new TwinMakerMetadataModule('workspace-id', tmClient);
Expand Down Expand Up @@ -105,6 +124,11 @@ describe('getPropertyValueHistoryByComponentType', () => {
isExternalId: false,
},
},
'prop-2': {
definition: {
isExternalId: false,
},
},
},
},
'comp-2': {
Expand Down Expand Up @@ -150,7 +174,7 @@ describe('getPropertyValueHistoryByComponentType', () => {
],
},
{
entityPropertyReference: mockEntityRef3,
entityPropertyReference: mockEntityRef4,
values: [
{
value: {
Expand Down Expand Up @@ -187,6 +211,17 @@ describe('getPropertyValueHistoryByComponentType', () => {
},
],
},
{
entityPropertyReference: mockEntityRef3,
values: [
{
value: {
integerValue: 44,
},
time: new Date(2022, 1, 4).toISOString(),
},
],
},
],
};

Expand Down Expand Up @@ -282,13 +317,14 @@ describe('getPropertyValueHistoryByComponentType', () => {
requestInformations: [
{ ...mockRequestInfos[0], fetchFromStartToEnd: true },
{ ...mockRequestInfos[1], fetchFromStartToEnd: true },
{ ...mockRequestInfos[2], fetchFromStartToEnd: true },
],
client: tmClient,
});

await flushPromises();

expect(onSuccess).toBeCalledTimes(3);
expect(onSuccess).toBeCalledTimes(4);
expect(onSuccess).toHaveBeenNthCalledWith(
1,
[
Expand Down Expand Up @@ -355,6 +391,28 @@ describe('getPropertyValueHistoryByComponentType', () => {
start,
end
);
expect(onSuccess).toHaveBeenNthCalledWith(
4,
[
expect.objectContaining({
id: mockRequestInfos[2].id,
data: [
{
x: new Date(2022, 1, 4).getTime(),
y: 44,
},
],
meta: {
entityId: streamIdComponents3.entityId,
componentName: streamIdComponents3.componentName,
propertyName: streamIdComponents3.propertyName,
},
}),
],
{ ...mockRequestInfos[2], fetchFromStartToEnd: true },
start,
end
);
});

it('should trigger onSuccess with correct dataStream response for fetchMostRecentBeforeEnd case', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { toDataPoint, isDefined, toDataStream, toDataType } from '../utils/value
export const getPropertyValueHistoryByComponentTypeRequest = async ({
workspaceId,
componentTypeId,
propertyName,
propertyNames,
requestInformations,
entities,
onSuccess,
Expand All @@ -20,7 +20,7 @@ export const getPropertyValueHistoryByComponentTypeRequest = async ({
}: {
workspaceId: string;
componentTypeId: string;
propertyName: string;
propertyNames: string[];
requestInformations: Record<string, RequestInformationAndRange>;
entities: GetEntityResponse[];
onError: ErrorCallback;
Expand Down Expand Up @@ -51,7 +51,7 @@ export const getPropertyValueHistoryByComponentTypeRequest = async ({
new GetPropertyValueHistoryCommand({
workspaceId,
componentTypeId,
selectedProperties: [propertyName],
selectedProperties: propertyNames,
orderByTime: fetchMostRecent ? 'DESCENDING' : 'ASCENDING',
startTime: start.toISOString(),
endTime: end.toISOString(),
Expand All @@ -70,7 +70,8 @@ export const getPropertyValueHistoryByComponentTypeRequest = async ({
Object.values(components || {}).forEach((comp) => {
if (
comp.componentTypeId === componentTypeId &&
values.entityPropertyReference?.propertyName === propertyName &&
values.entityPropertyReference?.propertyName &&
propertyNames.includes(values.entityPropertyReference?.propertyName) &&
values.entityPropertyReference.externalIdProperty &&
comp.componentName
) {
Expand All @@ -97,8 +98,9 @@ export const getPropertyValueHistoryByComponentTypeRequest = async ({
return isMatch;
});
const entityId = matchingEntity?.entityId;
const propertyName = values.entityPropertyReference.propertyName;

if (!entityId || isEmpty(matchingComponentName)) return;
if (!entityId || isEmpty(matchingComponentName) || !propertyName) return;

const streamId = toDataStreamId({
workspaceId,
Expand Down Expand Up @@ -147,7 +149,7 @@ export const getPropertyValueHistoryByComponentTypeRequest = async ({
getPropertyValueHistoryByComponentTypeRequest({
workspaceId,
componentTypeId,
propertyName,
propertyNames,
requestInformations,
entities,
onError,
Expand Down Expand Up @@ -188,12 +190,12 @@ export const getPropertyValueHistoryByComponentType = async ({
}): Promise<void> => {
// Group same component type API call into one
const requestInputs: {
params: { componentTypeId: string; propertyName: string; workspaceId: string };
params: { componentTypeId: string; workspaceId: string };
propertyNames: string[];
requestInfos: Record<string, RequestInformationAndRange>;
entities: GetEntityResponse[];
}[] = [];

// TODO: May bundle the requests for the same component type, but different properties into the same call
requestInformations.forEach(async (info) => {
const { workspaceId, propertyName } = fromDataStreamId(info.id);
const componentTypeId =
Expand All @@ -219,13 +221,16 @@ export const getPropertyValueHistoryByComponentType = async ({

if (inputIndex >= 0) {
requestInputs[inputIndex].requestInfos[info.id] = info;
if (!requestInputs[inputIndex].propertyNames.includes(propertyName)) {
requestInputs[inputIndex].propertyNames.push(propertyName);
}
} else {
requestInputs.push({
params: {
componentTypeId: componentTypeId,
propertyName: propertyName,
workspaceId: workspaceId,
},
propertyNames: [propertyName],
requestInfos: { [info.id]: info },
entities: [],
});
Expand All @@ -240,7 +245,7 @@ export const getPropertyValueHistoryByComponentType = async ({
return getPropertyValueHistoryByComponentTypeRequest({
workspaceId: input.params.workspaceId,
componentTypeId: input.params.componentTypeId,
propertyName: input.params.propertyName,
propertyNames: input.propertyNames,
requestInformations: input.requestInfos,
entities,
onSuccess,
Expand Down
Loading

0 comments on commit 9020c2f

Please sign in to comment.