Skip to content

Commit

Permalink
feat: support fetchMostRecentBeforeStart (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
boweihan authored Mar 29, 2022
1 parent 1fad859 commit c44b7b8
Show file tree
Hide file tree
Showing 22 changed files with 1,398 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,41 @@ describe('handles gestures', () => {

before(() => {
cy.intercept('/properties/history?*', (req) => {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
})
);
if (new Date(req.query.startDate).getUTCFullYear() === 1899) {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(new Date(req.query.endDate).getTime() - SECOND_IN_MS),
endDate: new Date(req.query.endDate),
})
);
} else {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
})
);
}
});

cy.intercept('/properties/aggregates?*', (req) => {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
if (new Date(req.query.startDate).getUTCFullYear() === 1899) {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(new Date(req.query.endDate).getTime() - 60 * SECOND_IN_MS),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
} else {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
}
});

cy.intercept(`/assets/${assetId}`, (req) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ describe('line chart', () => {

before(() => {
cy.intercept('/properties/aggregates?*', (req) => {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
if (new Date(req.query.startDate).getUTCFullYear() === 1899) {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(new Date(req.query.endDate).getTime() - 60 * SECOND_IN_MS),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
} else {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
}
});

cy.intercept(`/assets/${assetId}`, (req) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { renderChart } from '../../testing/renderChart';
import { mockLatestValueResponse } from '../../testing/mocks/mockGetAggregatedOrRawResponse';
import { mockGetAssetSummary } from '../../testing/mocks/mockGetAssetSummaries';
import { COMPARISON_OPERATOR } from '@synchro-charts/core';

const SECOND_IN_MS = 1000;

Expand All @@ -27,7 +28,7 @@ describe('status grid', () => {
chartType: 'iot-status-grid',
settings: { resolution: '0' },
viewport: { duration: '1m' },
annotations: { y: [{ color: '#FF0000', comparisonOperator: 'GT', value: 25 }] },
annotations: { y: [{ color: '#FF0000', comparisonOperator: COMPARISON_OPERATOR.GREATER_THAN, value: 25 }] },
});

cy.wait(SECOND_IN_MS * 2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { renderChart } from '../../testing/renderChart';
import { mockGetAggregatedOrRawResponse } from '../../testing/mocks/mockGetAggregatedOrRawResponse';
import { mockGetAssetSummary } from '../../testing/mocks/mockGetAssetSummaries';
import { COMPARISON_OPERATOR } from '@synchro-charts/core';

const SECOND_IN_MS = 1000;

Expand All @@ -14,13 +15,23 @@ describe('status timeline', () => {

before(() => {
cy.intercept('/properties/history?*', (req) => {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
if (new Date(req.query.startDate).getUTCFullYear() === 1899) {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(new Date(req.query.endDate).getTime() - SECOND_IN_MS),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
} else {
req.reply(
mockGetAggregatedOrRawResponse({
startDate: new Date(req.query.startDate),
endDate: new Date(req.query.endDate),
resolution: req.query.resolution as string,
})
);
}
});

cy.intercept(`/assets/${assetId}`, (req) => {
Expand All @@ -32,7 +43,7 @@ describe('status timeline', () => {
renderChart({
chartType: 'iot-status-timeline',
settings: { resolution: '0' },
annotations: { y: [{ color: '#FF0000', comparisonOperator: 'GT', value: 26 }] },
annotations: { y: [{ color: '#FF0000', comparisonOperator: COMPARISON_OPERATOR.GREATER_THAN, value: 26 }] },
});

cy.wait(SECOND_IN_MS * 2);
Expand Down
41 changes: 25 additions & 16 deletions packages/core/src/__mocks__/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
DataSourceRequest,
DataStream,
RequestInformationAndRange,
SiteWiseDataStreamQuery
} from "../data-module/types";
import { toDataStreamId } from "../common/dataStreamId";
import { toId } from "@iot-app-kit/source-iotsitewise";
SiteWiseDataStreamQuery,
} from '../data-module/types';
import { toDataStreamId } from '../common/dataStreamId';

// A simple mock data source, which will always immediately return a successful response of your choosing.
export const createMockSiteWiseDataSource = (
Expand All @@ -19,24 +18,34 @@ export const createMockSiteWiseDataSource = (
} = { dataStreams: [], onRequestData: () => {} }
): DataSource<SiteWiseDataStreamQuery> => ({
name: 'site-wise',
initiateRequest: jest.fn(({ query, request, onSuccess = () => {} }: DataSourceRequest<SiteWiseDataStreamQuery>, requestInformations: RequestInformationAndRange[]) => {
query.assets.forEach(({ assetId, properties }) =>
properties.forEach(({ propertyId }) => {
const correspondingRequestInfo = requestInformations.find(({ id }) => `${ assetId }---${propertyId}` === id);
if (correspondingRequestInfo) {
onRequestData({ assetId, propertyId, request });
onSuccess(dataStreams, 'fetchFromStartToEnd', correspondingRequestInfo.start, correspondingRequestInfo.end);
}
})
);
}),
initiateRequest: jest.fn(
(
{ query, request, onSuccess = () => {} }: DataSourceRequest<SiteWiseDataStreamQuery>,
requestInformations: RequestInformationAndRange[]
) => {
query.assets.forEach(({ assetId, properties }) =>
properties.forEach(({ propertyId }) => {
const correspondingRequestInfo = requestInformations.find(({ id }) => `${assetId}---${propertyId}` === id);
if (correspondingRequestInfo) {
onRequestData({ assetId, propertyId, request });
onSuccess(
dataStreams,
correspondingRequestInfo,
correspondingRequestInfo.start,
correspondingRequestInfo.end
);
}
})
);
}
),
getRequestsFromQuery: ({ query }) =>
query.assets
.map(({ assetId, properties }) =>
properties.map(({ propertyId, refId }) => ({
id: toDataStreamId({ assetId, propertyId }),
refId,
resolution: '0'
resolution: '0',
}))
)
.flat(),
Expand Down
44 changes: 27 additions & 17 deletions packages/core/src/data-module/IotAppKitDataModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ describe('initial request', () => {
query: DATA_STREAM_QUERY,
request: { viewport: { start: START, end: END }, settings: { fetchFromStartToEnd: true } },
}),
[{ id: DATA_STREAM.id, resolution: DATA_STREAM.resolution.toString(), start: START, end: END }]
[
expect.objectContaining({
id: DATA_STREAM.id,
resolution: DATA_STREAM.resolution.toString(),
start: START,
end: END,
}),
]
);
});
});
Expand Down Expand Up @@ -395,6 +402,7 @@ it('subscribes to multiple queries on the same data source', () => {

const request: TimeSeriesDataRequest = {
viewport: { start: START, end: END },
settings: { fetchFromStartToEnd: true },
};

const dataModule = new IotAppKitDataModule();
Expand Down Expand Up @@ -530,6 +538,7 @@ it('subscribes to multiple data streams on multiple data sources', () => {

const request: TimeSeriesDataRequest = {
viewport: { start: START, end: END },
settings: { fetchFromStartToEnd: true },
};

const dataModule = new IotAppKitDataModule();
Expand Down Expand Up @@ -810,12 +819,12 @@ describe('caching', () => {
request: { viewport: { start: START_1, end: END_1 }, settings: { fetchFromStartToEnd: true } },
}),
[
{
expect.objectContaining({
id: DATA_STREAM.id,
resolution: DATA_STREAM.resolution.toString(),
start: START_1,
end: END_1,
},
}),
]
);

Expand All @@ -833,18 +842,18 @@ describe('caching', () => {
request: { viewport: { start: START_2, end: END_2 }, settings: { fetchFromStartToEnd: true } },
}),
[
{
expect.objectContaining({
id: DATA_STREAM.id,
resolution: DATA_STREAM.resolution.toString(),
start: START_2,
end: START_1,
},
{
}),
expect.objectContaining({
id: DATA_STREAM.id,
resolution: DATA_STREAM.resolution.toString(),
start: END_1,
end: END_2,
},
}),
]
);
});
Expand Down Expand Up @@ -874,21 +883,21 @@ describe('caching', () => {
(dataSource.initiateRequest as Mock).mockClear();

update({
request: { viewport: { start: START_2, end: END_2 } },
request: { viewport: { start: START_2, end: END_2 }, settings: { fetchFromStartToEnd: true } },
});

expect(dataSource.initiateRequest).toBeCalledWith(
expect.objectContaining({
query: DATA_STREAM_QUERY,
request: { viewport: { start: START_2, end: END_2 } },
request: { viewport: { start: START_2, end: END_2 }, settings: { fetchFromStartToEnd: true } },
}),
[
{
expect.objectContaining({
id: DATA_STREAM_INFO.id,
resolution: DATA_STREAM_INFO.resolution.toString(),
start: START_2,
end: END_2,
},
}),
]
);
});
Expand Down Expand Up @@ -927,13 +936,13 @@ describe('caching', () => {
},
}),
[
{
expect.objectContaining({
id: DATA_STREAM_INFO.id,
resolution: DATA_STREAM_INFO.resolution.toString(),
// 1 minute time advancement invalidates 3 minutes of cache by default, which is 2 minutes from END_1
start: new Date(END.getTime() - 2 * MINUTE_IN_MS),
end: END,
},
}),
]
);
});
Expand Down Expand Up @@ -1101,7 +1110,7 @@ describe('request scheduler', () => {
queries: [DATA_STREAM_QUERY],
request: {
viewport: { start: START, end: END },
settings: { refreshRate: SECOND_IN_MS * 0.1 },
settings: { fetchFromStartToEnd: true, refreshRate: SECOND_IN_MS * 0.1 },
},
},
timeSeriesCallback
Expand Down Expand Up @@ -1276,7 +1285,7 @@ describe('request scheduler', () => {
const { update } = dataModule.subscribeToDataStreams(
{
queries: [DATA_STREAM_QUERY],
request: { viewport: { duration: SECOND_IN_MS } },
request: { viewport: { duration: SECOND_IN_MS }, settings: { fetchFromStartToEnd: true } },
},
timeSeriesCallback
);
Expand All @@ -1287,7 +1296,7 @@ describe('request scheduler', () => {
update({
request: {
viewport: { start: START, end: END },
settings: { refreshRate: SECOND_IN_MS * 0.1 },
settings: { refreshRate: SECOND_IN_MS * 0.1, fetchFromStartToEnd: true },
},
});
timeSeriesCallback.mockClear();
Expand All @@ -1313,7 +1322,7 @@ it('when data is requested from the viewport start to end with a buffer, include
const { unsubscribe } = dataModule.subscribeToDataStreams(
{
queries: [DATA_STREAM_QUERY],
request: { viewport: { start, end }, settings: { requestBuffer } },
request: { viewport: { start, end }, settings: { requestBuffer, fetchFromStartToEnd: true } },
},
timeSeriesCallback
);
Expand All @@ -1323,6 +1332,7 @@ it('when data is requested from the viewport start to end with a buffer, include
request: {
settings: {
requestBuffer,
fetchFromStartToEnd: true,
},
viewport: {
start,
Expand Down
Loading

0 comments on commit c44b7b8

Please sign in to comment.