Skip to content

Commit

Permalink
feat(core): Add meta field to requestInfos in TimeSeriesDataModule
Browse files Browse the repository at this point in the history
  • Loading branch information
diehbria committed Sep 6, 2022
1 parent ac5c1e5 commit 5db70c8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"fix:stylelint": "stylelint '**/*.css' --fix",
"test": "npm-run-all -p test:unit test:eslint test:stylelint test:git",
"test:integration": "npm run test:integration -ws --if-present",
"test:eslint": "eslint --ext .js,.ts,.tsx . --max-warnings=49",
"test:eslint": "eslint --ext .js,.ts,.tsx . --max-warnings=39",
"test:stylelint": "stylelint '**/*.css' --max-warnings 0",
"test:unit": "npm run test -ws --if-present",
"test:git": "git diff --exit-code",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/__mocks__/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export const createMockSiteWiseDataSource = (
{
dataStreams = [],
onRequestData = () => {},
meta,
}: {
dataStreams?: DataStream[];
onRequestData?: (props: any) => void;
meta?: DataStream["meta"];
} = { dataStreams: [], onRequestData: () => {} }
): DataSource<SiteWiseDataStreamQuery> => ({
initiateRequest: jest.fn(
Expand Down Expand Up @@ -45,6 +47,7 @@ export const createMockSiteWiseDataSource = (
id: toDataStreamId({ assetId, propertyId }),
refId,
resolution: '0',
meta,
}))
)
.flat()),
Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/data-module/TimeSeriesDataModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,3 +1273,38 @@ it('when data is requested from the viewport start to end with a buffer, include

unsubscribe();
});

it('passes meta field returned from data source into the initiate request', async () => {
const someMetaData = { someMetaData: 42 };
const dataSource = createMockSiteWiseDataSource({ dataStreams: [DATA_STREAM], meta: someMetaData });
const dataModule = new TimeSeriesDataModule(dataSource);

const END = new Date();
const START = new Date(END.getTime() - HOUR_IN_MS);

const timeSeriesCallback = jest.fn();
dataModule.subscribeToDataStreams(
{
queries: [DATA_STREAM_QUERY],
request: {
viewport: { start: START, end: END },
settings: { fetchFromStartToEnd: true, refreshRate: MINUTE_IN_MS, requestBuffer: 0 },
},
},
timeSeriesCallback
);

await flushPromises();

expect(dataSource.initiateRequest).toBeCalledWith(
expect.objectContaining({
query: DATA_STREAM_QUERY,
}),
[
expect.objectContaining({
id: DATA_STREAM_INFO.id,
meta: someMetaData,
}),
]
);
});
3 changes: 2 additions & 1 deletion packages/core/src/data-module/TimeSeriesDataModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ export class TimeSeriesDataModule<Query extends DataStreamQuery> {
const requiredStreams = requestedStreams.filter(isRequestedDataStream);

const requests = requiredStreams
.map(({ resolution, id, cacheSettings }) =>
.map(({ resolution, id, cacheSettings, meta }) =>
getRequestInformations({
request,
meta,
store: this.dataCache.getState(),
start: viewportStartDate(viewport),
end: viewportEndDate(viewport),
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/data-module/data-cache/caching/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CacheSettings, DataStreamsStore, DataStreamStore, TTLDurationMapping }
import { getExpiredCacheIntervals } from './expiredCacheIntervals';
import { TimeSeriesDataRequestSettings, TimeSeriesDataRequest } from '../requestTypes';
import { pointBisector } from '../../../common/dataFilters';
import { RequestInformationAndRange } from '../../types';
import { RequestInformation, RequestInformationAndRange } from '../../types';

export const unexpiredCacheIntervals = (
streamStore: DataStreamStore,
Expand Down Expand Up @@ -119,6 +119,7 @@ export const getRequestInformations = ({
store,
dataStreamId,
start,
meta,
end,
resolution,
cacheSettings,
Expand All @@ -128,6 +129,7 @@ export const getRequestInformations = ({
dataStreamId: string;
start: Date;
end: Date;
meta?: RequestInformation['meta'];
resolution: string;
cacheSettings: CacheSettings;
}): RequestInformationAndRange[] => {
Expand All @@ -151,6 +153,7 @@ export const getRequestInformations = ({
start: rangeStart,
end: rangeEnd,
id: dataStreamId,
meta,
resolution,
fetchFromStartToEnd,
}));
Expand All @@ -170,6 +173,7 @@ export const getRequestInformations = ({
requestInformations.push({
start,
end,
meta,
id: dataStreamId,
resolution,
fetchMostRecentBeforeEnd: true,
Expand All @@ -190,6 +194,7 @@ export const getRequestInformations = ({
requestInformations.unshift({
start,
end,
meta,
id: dataStreamId,
resolution,
fetchMostRecentBeforeStart: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/data-module/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type RequestInformation = {
fetchMostRecentBeforeStart?: boolean;
fetchMostRecentBeforeEnd?: boolean;
fetchFromStartToEnd?: boolean;
// Mechanism to associate some information about how the request should be made
meta?: Record<string, string | number | boolean>;
};
export type RequestInformationAndRange = RequestInformation & { start: Date; end: Date };

Expand Down

0 comments on commit 5db70c8

Please sign in to comment.