From 9a79ec94f13c2a4faefd27c0b2f1a7e0898b5236 Mon Sep 17 00:00:00 2001 From: Saurav Date: Tue, 23 Jul 2024 11:10:37 -0400 Subject: [PATCH] add no query support for timeseries --- etc/client.api.report.api.md | 4 +- .../client.api/src/timeseries/timeseries.ts | 4 +- .../client/src/createTimeseriesProperty.ts | 10 ++--- packages/client/src/object/timeseries.test.ts | 39 +++++++++++++++++++ .../src/stubs/timeseriesRequests.ts | 3 ++ 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/etc/client.api.report.api.md b/etc/client.api.report.api.md index cab4e57b4..25e1ff9b0 100644 --- a/etc/client.api.report.api.md +++ b/etc/client.api.report.api.md @@ -719,9 +719,9 @@ export interface TimeSeriesPoint { // @public (undocumented) export interface TimeSeriesProperty { // (undocumented) - asyncIterPoints(query: TimeSeriesQuery): AsyncGenerator>; + asyncIterPoints(query?: TimeSeriesQuery): AsyncGenerator>; // (undocumented) - getAllPoints(query: TimeSeriesQuery): Promise>>; + getAllPoints(query?: TimeSeriesQuery): Promise>>; // (undocumented) getFirstPoint(): Promise>; // (undocumented) diff --git a/packages/client.api/src/timeseries/timeseries.ts b/packages/client.api/src/timeseries/timeseries.ts index d48104fad..be8d2ffb5 100644 --- a/packages/client.api/src/timeseries/timeseries.ts +++ b/packages/client.api/src/timeseries/timeseries.ts @@ -70,6 +70,6 @@ export interface TimeSeriesPoint { export interface TimeSeriesProperty { getFirstPoint(): Promise>; getLastPoint(): Promise>; - getAllPoints(query: TimeSeriesQuery): Promise>>; - asyncIterPoints(query: TimeSeriesQuery): AsyncGenerator>; + getAllPoints(query?: TimeSeriesQuery): Promise>>; + asyncIterPoints(query?: TimeSeriesQuery): AsyncGenerator>; } diff --git a/packages/client/src/createTimeseriesProperty.ts b/packages/client/src/createTimeseriesProperty.ts index 20db65a8c..66e9d6e30 100644 --- a/packages/client/src/createTimeseriesProperty.ts +++ b/packages/client/src/createTimeseriesProperty.ts @@ -60,7 +60,7 @@ export function createTimeseriesProperty( propertyName, ) as Promise>; }, - async getAllPoints(query: TimeSeriesQuery) { + async getAllPoints(query?: TimeSeriesQuery) { return getAllTimeSeriesPoints( client, objectApiName, @@ -70,7 +70,7 @@ export function createTimeseriesProperty( ); }, - asyncIterPoints(query: TimeSeriesQuery) { + asyncIterPoints(query?: TimeSeriesQuery) { return iterateTimeSeriesPoints( client, objectApiName, @@ -87,7 +87,7 @@ async function getAllTimeSeriesPoints( objectApiName: string, primaryKey: any, propertyName: string, - body: TimeSeriesQuery, + body?: TimeSeriesQuery, ): Promise>> { const allPoints: Array> = []; @@ -113,7 +113,7 @@ async function* iterateTimeSeriesPoints( objectApiName: string, primaryKey: any, propertyName: string, - body: TimeSeriesQuery, + body?: TimeSeriesQuery, ): AsyncGenerator, any, unknown> { const utf8decoder = new TextDecoder("utf-8"); @@ -124,7 +124,7 @@ async function* iterateTimeSeriesPoints( objectApiName, primaryKey, propertyName, - { range: getTimeRange(body) }, + body ? { range: getTimeRange(body) } : {}, ); const reader = streamPointsIterator.stream().getReader(); diff --git a/packages/client/src/object/timeseries.test.ts b/packages/client/src/object/timeseries.test.ts index a7a034121..844167726 100644 --- a/packages/client/src/object/timeseries.test.ts +++ b/packages/client/src/object/timeseries.test.ts @@ -109,6 +109,23 @@ describe("Timeseries", () => { }, { time: "2014-04-14", value: 30 }]); }); + it("getAll points with no query works", async () => { + const employee = await client(Employee).fetchOne(50030); + expect(employee.$primaryKey).toEqual(50030); + const points = await employee.employeeStatus?.getAllPoints(); + expect(points).toBeDefined(); + expect(points!).toEqual([ + { time: "2012-02-12", value: 10 }, + { time: "2012-02-12", value: 10 }, + { + time: "2013-03-13", + value: 20, + }, + { time: "2014-04-14", value: 30 }, + { time: "2014-04-14", value: 30 }, + ]); + }); + it("async iter points with absolute range works", async () => { const employee = await client(Employee).fetchOne(50030); expect(employee.$primaryKey).toEqual(50030); @@ -127,4 +144,26 @@ describe("Timeseries", () => { value: 20, }, { time: "2014-04-14", value: 30 }]); }); + + it("async iter points with no query", async () => { + const employee = await client(Employee).fetchOne(50030); + expect(employee.$primaryKey).toEqual(50030); + const pointsIter = employee.employeeStatus?.asyncIterPoints(); + + const points: TimeSeriesPoint[] = []; + for await (const point of pointsIter!) { + points.push(point); + } + expect(points).toBeDefined(); + expect(points!).toEqual([ + { time: "2012-02-12", value: 10 }, + { time: "2012-02-12", value: 10 }, + { + time: "2013-03-13", + value: 20, + }, + { time: "2014-04-14", value: 30 }, + { time: "2014-04-14", value: 30 }, + ]); + }); }); diff --git a/packages/shared.test/src/stubs/timeseriesRequests.ts b/packages/shared.test/src/stubs/timeseriesRequests.ts index f3ac0920c..16ebd1e52 100644 --- a/packages/shared.test/src/stubs/timeseriesRequests.ts +++ b/packages/shared.test/src/stubs/timeseriesRequests.ts @@ -55,6 +55,8 @@ const noBodyRequest: StreamTimeSeriesPointsRequest = { range: { type: "absolute" }, }; +const noRangeRequest: StreamTimeSeriesPointsRequest = {}; + const fromBodyRequest: StreamTimeSeriesPointsRequest = { range: { type: "relative", @@ -121,6 +123,7 @@ export const streamPointsRequestHandlers: Record< StreamTimeSeriesPointsResponse > = { [stableStringify(noBodyRequest)]: streamPointsNoBody, + [stableStringify(noRangeRequest)]: streamPointsNoBody, [stableStringify(rangeBodyRequest)]: streamPointsRange, [stableStringify(fromBodyRequest)]: streamPointsFrom, [stableStringify(afterBodyRequest)]: streamPointsAfter,