Skip to content

Commit

Permalink
add no query support for timeseries
Browse files Browse the repository at this point in the history
  • Loading branch information
ssanjay1 committed Jul 23, 2024
1 parent 8cff4f0 commit 9a79ec9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
4 changes: 2 additions & 2 deletions etc/client.api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,9 @@ export interface TimeSeriesPoint<T extends string | number> {
// @public (undocumented)
export interface TimeSeriesProperty<T extends number | string> {
// (undocumented)
asyncIterPoints(query: TimeSeriesQuery): AsyncGenerator<TimeSeriesPoint<T>>;
asyncIterPoints(query?: TimeSeriesQuery): AsyncGenerator<TimeSeriesPoint<T>>;
// (undocumented)
getAllPoints(query: TimeSeriesQuery): Promise<Array<TimeSeriesPoint<T>>>;
getAllPoints(query?: TimeSeriesQuery): Promise<Array<TimeSeriesPoint<T>>>;
// (undocumented)
getFirstPoint(): Promise<TimeSeriesPoint<T>>;
// (undocumented)
Expand Down
4 changes: 2 additions & 2 deletions packages/client.api/src/timeseries/timeseries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ export interface TimeSeriesPoint<T extends string | number> {
export interface TimeSeriesProperty<T extends number | string> {
getFirstPoint(): Promise<TimeSeriesPoint<T>>;
getLastPoint(): Promise<TimeSeriesPoint<T>>;
getAllPoints(query: TimeSeriesQuery): Promise<Array<TimeSeriesPoint<T>>>;
asyncIterPoints(query: TimeSeriesQuery): AsyncGenerator<TimeSeriesPoint<T>>;
getAllPoints(query?: TimeSeriesQuery): Promise<Array<TimeSeriesPoint<T>>>;
asyncIterPoints(query?: TimeSeriesQuery): AsyncGenerator<TimeSeriesPoint<T>>;
}
10 changes: 5 additions & 5 deletions packages/client/src/createTimeseriesProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function createTimeseriesProperty<T extends number | string>(
propertyName,
) as Promise<TimeSeriesPoint<T>>;
},
async getAllPoints(query: TimeSeriesQuery) {
async getAllPoints(query?: TimeSeriesQuery) {
return getAllTimeSeriesPoints(
client,
objectApiName,
Expand All @@ -70,7 +70,7 @@ export function createTimeseriesProperty<T extends number | string>(
);
},

asyncIterPoints(query: TimeSeriesQuery) {
asyncIterPoints(query?: TimeSeriesQuery) {
return iterateTimeSeriesPoints(
client,
objectApiName,
Expand All @@ -87,7 +87,7 @@ async function getAllTimeSeriesPoints<T extends string | number>(
objectApiName: string,
primaryKey: any,
propertyName: string,
body: TimeSeriesQuery,
body?: TimeSeriesQuery,
): Promise<Array<TimeSeriesPoint<T>>> {
const allPoints: Array<TimeSeriesPoint<T>> = [];

Expand All @@ -113,7 +113,7 @@ async function* iterateTimeSeriesPoints<T extends string | number>(
objectApiName: string,
primaryKey: any,
propertyName: string,
body: TimeSeriesQuery,
body?: TimeSeriesQuery,
): AsyncGenerator<TimeSeriesPoint<T>, any, unknown> {
const utf8decoder = new TextDecoder("utf-8");

Expand All @@ -124,7 +124,7 @@ async function* iterateTimeSeriesPoints<T extends string | number>(
objectApiName,
primaryKey,
propertyName,
{ range: getTimeRange(body) },
body ? { range: getTimeRange(body) } : {},
);

const reader = streamPointsIterator.stream().getReader();
Expand Down
39 changes: 39 additions & 0 deletions packages/client/src/object/timeseries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<string>[] = [];
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 },
]);
});
});
3 changes: 3 additions & 0 deletions packages/shared.test/src/stubs/timeseriesRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const noBodyRequest: StreamTimeSeriesPointsRequest = {
range: { type: "absolute" },
};

const noRangeRequest: StreamTimeSeriesPointsRequest = {};

const fromBodyRequest: StreamTimeSeriesPointsRequest = {
range: {
type: "relative",
Expand Down Expand Up @@ -121,6 +123,7 @@ export const streamPointsRequestHandlers: Record<
StreamTimeSeriesPointsResponse
> = {
[stableStringify(noBodyRequest)]: streamPointsNoBody,
[stableStringify(noRangeRequest)]: streamPointsNoBody,
[stableStringify(rangeBodyRequest)]: streamPointsRange,
[stableStringify(fromBodyRequest)]: streamPointsFrom,
[stableStringify(afterBodyRequest)]: streamPointsAfter,
Expand Down

0 comments on commit 9a79ec9

Please sign in to comment.