From 29b23a775acae75c65172e68e4b502e5238f863f Mon Sep 17 00:00:00 2001 From: Norbert Nader Date: Fri, 7 Jan 2022 12:22:15 -0800 Subject: [PATCH] feat: request data in descending order (#30) Co-authored-by: Norbert Nader --- .../testing/testing-ground/testing-ground.tsx | 3 ++- .../data-cache/dataReducer.spec.ts | 21 ++++++++----------- .../src/data-module/data-cache/dataReducer.ts | 5 ++++- .../client/getAggregatedPropertyDataPoints.ts | 2 +- .../client/getHistoricalPropertyDataPoints.ts | 2 +- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/components/src/testing/testing-ground/testing-ground.tsx b/packages/components/src/testing/testing-ground/testing-ground.tsx index 2e8bb40b2..7423222b5 100755 --- a/packages/components/src/testing/testing-ground/testing-ground.tsx +++ b/packages/components/src/testing/testing-ground/testing-ground.tsx @@ -121,12 +121,13 @@ export class TestingGround { +
diff --git a/packages/core/src/data-module/data-cache/dataReducer.spec.ts b/packages/core/src/data-module/data-cache/dataReducer.spec.ts index 6cee88acf..98ca2a1fe 100755 --- a/packages/core/src/data-module/data-cache/dataReducer.spec.ts +++ b/packages/core/src/data-module/data-cache/dataReducer.spec.ts @@ -399,21 +399,18 @@ it('merges data into existing data cache', () => { }, }, }; - const DATA_POINTS_ADDED: DataPoint[] = [ - { - x: new Date(2002, 0, 0).getTime(), - y: 600, - }, - { - x: new Date(2002, 6, 0).getTime(), - y: 400, - }, - ]; + + const NEWER_DATA_POINT_1 = { x: new Date(2002, 6, 0).getTime(), y: 400 }; + const OLDER_DATA_POINT_2 = { x: new Date(2002, 0, 0).getTime(), y: 600 }; + const dataStream = { name: 'some name', id: ID, aggregates: { - [SECOND_IN_MS]: DATA_POINTS_ADDED, + [SECOND_IN_MS]: [ + NEWER_DATA_POINT_1, + OLDER_DATA_POINT_2 + ], }, data: [], resolution: SECOND_IN_MS, @@ -431,7 +428,7 @@ it('merges data into existing data cache', () => { error: undefined, dataCache: { intervals: [[DATE_ONE, DATE_FOUR]], - items: [[...DATA_POINTS_ONE, ...DATA_POINTS_ADDED, ...DATA_POINTS_TWO]], + items: [[...DATA_POINTS_ONE, OLDER_DATA_POINT_2, NEWER_DATA_POINT_1, ...DATA_POINTS_TWO]], }, requestCache: expect.objectContaining({ intervals: [[DATE_ONE, DATE_FOUR]], diff --git a/packages/core/src/data-module/data-cache/dataReducer.ts b/packages/core/src/data-module/data-cache/dataReducer.ts index aa80a8d37..cdc6d47c1 100755 --- a/packages/core/src/data-module/data-cache/dataReducer.ts +++ b/packages/core/src/data-module/data-cache/dataReducer.ts @@ -62,10 +62,13 @@ export const dataReducer: Reducer = ( // TODO: clean this to one single source of truth cache const requestCache = streamStore != null ? streamStore.requestCache : EMPTY_CACHE; + // We always want data in ascending order in the cache + const sortedData = getDataPoints(data, data.resolution).sort((a, b) => a.x - b.x); + const updatedDataCache = addToDataPointCache({ start: first, end: last, - data: getDataPoints(data, data.resolution), + data: sortedData, cache: (streamStore && streamStore.dataCache) || EMPTY_CACHE, }); diff --git a/packages/core/src/data-sources/site-wise/client/getAggregatedPropertyDataPoints.ts b/packages/core/src/data-sources/site-wise/client/getAggregatedPropertyDataPoints.ts index 1246cc299..fc4edf449 100644 --- a/packages/core/src/data-sources/site-wise/client/getAggregatedPropertyDataPoints.ts +++ b/packages/core/src/data-sources/site-wise/client/getAggregatedPropertyDataPoints.ts @@ -47,7 +47,7 @@ const getAggregatedPropertyDataPointsForProperty = ({ resolution, aggregateTypes, maxResults, - timeOrdering: TimeOrdering.ASCENDING, + timeOrdering: TimeOrdering.DESCENDING, nextToken: prevToken, }) ) diff --git a/packages/core/src/data-sources/site-wise/client/getHistoricalPropertyDataPoints.ts b/packages/core/src/data-sources/site-wise/client/getHistoricalPropertyDataPoints.ts index 8b8007e57..913cdfd5a 100644 --- a/packages/core/src/data-sources/site-wise/client/getHistoricalPropertyDataPoints.ts +++ b/packages/core/src/data-sources/site-wise/client/getHistoricalPropertyDataPoints.ts @@ -35,7 +35,7 @@ const getHistoricalPropertyDataPointsForProperty = ({ startDate: start, endDate: end, maxResults, - timeOrdering: TimeOrdering.ASCENDING, + timeOrdering: TimeOrdering.DESCENDING, nextToken: prevToken, }) )