From 5db70c85e51dd6a44b2ab89049a952da9140c990 Mon Sep 17 00:00:00 2001 From: Brian Diehr Date: Tue, 6 Sep 2022 09:48:55 -0700 Subject: [PATCH] feat(core): Add meta field to requestInfos in TimeSeriesDataModule --- package.json | 2 +- packages/core/src/__mocks__/data-source.ts | 3 ++ .../data-module/TimeSeriesDataModule.spec.ts | 35 +++++++++++++++++++ .../src/data-module/TimeSeriesDataModule.ts | 3 +- .../data-module/data-cache/caching/caching.ts | 7 +++- packages/core/src/data-module/types.ts | 2 ++ 6 files changed, 49 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3250b2df4..479c83d27 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/core/src/__mocks__/data-source.ts b/packages/core/src/__mocks__/data-source.ts index d2e4fc5fe..4c49bcca3 100644 --- a/packages/core/src/__mocks__/data-source.ts +++ b/packages/core/src/__mocks__/data-source.ts @@ -12,9 +12,11 @@ export const createMockSiteWiseDataSource = ( { dataStreams = [], onRequestData = () => {}, + meta, }: { dataStreams?: DataStream[]; onRequestData?: (props: any) => void; + meta?: DataStream["meta"]; } = { dataStreams: [], onRequestData: () => {} } ): DataSource => ({ initiateRequest: jest.fn( @@ -45,6 +47,7 @@ export const createMockSiteWiseDataSource = ( id: toDataStreamId({ assetId, propertyId }), refId, resolution: '0', + meta, })) ) .flat()), diff --git a/packages/core/src/data-module/TimeSeriesDataModule.spec.ts b/packages/core/src/data-module/TimeSeriesDataModule.spec.ts index 6df6171e9..80b91853e 100644 --- a/packages/core/src/data-module/TimeSeriesDataModule.spec.ts +++ b/packages/core/src/data-module/TimeSeriesDataModule.spec.ts @@ -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, + }), + ] + ); +}); diff --git a/packages/core/src/data-module/TimeSeriesDataModule.ts b/packages/core/src/data-module/TimeSeriesDataModule.ts index 4426b336d..4ac82c757 100644 --- a/packages/core/src/data-module/TimeSeriesDataModule.ts +++ b/packages/core/src/data-module/TimeSeriesDataModule.ts @@ -86,9 +86,10 @@ export class TimeSeriesDataModule { 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), diff --git a/packages/core/src/data-module/data-cache/caching/caching.ts b/packages/core/src/data-module/data-cache/caching/caching.ts index 66be16f86..0d0958f69 100755 --- a/packages/core/src/data-module/data-cache/caching/caching.ts +++ b/packages/core/src/data-module/data-cache/caching/caching.ts @@ -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, @@ -119,6 +119,7 @@ export const getRequestInformations = ({ store, dataStreamId, start, + meta, end, resolution, cacheSettings, @@ -128,6 +129,7 @@ export const getRequestInformations = ({ dataStreamId: string; start: Date; end: Date; + meta?: RequestInformation['meta']; resolution: string; cacheSettings: CacheSettings; }): RequestInformationAndRange[] => { @@ -151,6 +153,7 @@ export const getRequestInformations = ({ start: rangeStart, end: rangeEnd, id: dataStreamId, + meta, resolution, fetchFromStartToEnd, })); @@ -170,6 +173,7 @@ export const getRequestInformations = ({ requestInformations.push({ start, end, + meta, id: dataStreamId, resolution, fetchMostRecentBeforeEnd: true, @@ -190,6 +194,7 @@ export const getRequestInformations = ({ requestInformations.unshift({ start, end, + meta, id: dataStreamId, resolution, fetchMostRecentBeforeStart: true, diff --git a/packages/core/src/data-module/types.ts b/packages/core/src/data-module/types.ts index 019dbecb3..a4f53de51 100644 --- a/packages/core/src/data-module/types.ts +++ b/packages/core/src/data-module/types.ts @@ -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; }; export type RequestInformationAndRange = RequestInformation & { start: Date; end: Date };