diff --git a/packages/client.api/src/index.ts b/packages/client.api/src/index.ts index ef5385120..55baab566 100644 --- a/packages/client.api/src/index.ts +++ b/packages/client.api/src/index.ts @@ -108,6 +108,10 @@ export type { } from "./OsdkObjectFrom.js"; export type { OsdkObjectPrimaryKeyType } from "./OsdkObjectPrimaryKeyType.js"; export type { PageResult } from "./PageResult.js"; +export type { + TimeSeriesPoint, + TimeSeriesProperty, +} from "./timeseries/timeseries.js"; export type { LinkedType, LinkNames } from "./util/LinkUtils.js"; export type { NOOP } from "./util/NOOP.js"; diff --git a/packages/client.api/src/mapping/PropertyValueMapping.ts b/packages/client.api/src/mapping/PropertyValueMapping.ts index eb19cf0a8..8d0bb4b5b 100644 --- a/packages/client.api/src/mapping/PropertyValueMapping.ts +++ b/packages/client.api/src/mapping/PropertyValueMapping.ts @@ -15,6 +15,7 @@ */ import type { Attachment, AttachmentUpload } from "../object/Attachment.js"; +import type { TimeSeriesProperty } from "../timeseries/timeseries.js"; /** * Map from the PropertyDefinition type to the typescript type that we return @@ -36,8 +37,8 @@ export interface PropertyValueWireToClient { string: string; timestamp: string; - numericTimeseries: unknown; - stringTimeseries: unknown; + numericTimeseries: TimeSeriesProperty; + stringTimeseries: TimeSeriesProperty; } /** @@ -60,6 +61,6 @@ export interface PropertyValueClientToWire { string: string; timestamp: string; - numericTimeseries: unknown; - stringTimeseries: unknown; + numericTimeseries: TimeSeriesProperty; + stringTimeseries: TimeSeriesProperty; } diff --git a/packages/client.api/src/timeseries/timeseries.ts b/packages/client.api/src/timeseries/timeseries.ts new file mode 100644 index 000000000..5c957ca6c --- /dev/null +++ b/packages/client.api/src/timeseries/timeseries.ts @@ -0,0 +1,43 @@ +/* + * Copyright 2024 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export interface TimeSeriesQuery { + when: "BEFORE" | "AFTER"; + value: number; + unit: TimeseriesDurationUnits; +} + +export type TimeseriesDurationUnits = + | "YEARS" + | "MONTHS" + | "WEEKS" + | "DAYS" + | "HOURS" + | "MINUTES" + | "SECONDS" + | "MILLISECONDS"; + +export interface TimeSeriesPoint { + time: string; + value: T; +} + +export interface TimeSeriesProperty { + getFirstPoint(): Promise>; + getLastPoint(): Promise>; + getAllPoints(query: TimeSeriesQuery): Promise>>; + asyncIterPoints(query: TimeSeriesQuery): AsyncGenerator>; +} diff --git a/packages/client/src/createTimeseriesProperty.ts b/packages/client/src/createTimeseriesProperty.ts new file mode 100644 index 000000000..1cb52a663 --- /dev/null +++ b/packages/client/src/createTimeseriesProperty.ts @@ -0,0 +1,109 @@ +/* + * Copyright 2024 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { + Attachment, + TimeSeriesPoint, + TimeSeriesProperty, +} from "@osdk/client.api"; +import type { + StreamTimeSeriesPointsRequest} from "@osdk/internal.foundry"; +import { + Ontologies, + OntologiesV2 +} from "@osdk/internal.foundry"; +import type { MinimalClient } from "./MinimalClientContext.js"; + +export function createTimeseriesProperty( + client: MinimalClient, + objectApiName: string, + primaryKey: any, + propertyName: string, + body: StreamTimeSeriesPointsRequest, +): TimeSeriesProperty { + return { + async getFirstPoint() { + return OntologiesV2.OntologyObjectsV2.getFirstPoint( + client, + client.ontologyRid, + objectApiName, + primaryKey, + propertyName, + ) as Promise>; + }, + async getLastPoint() { + return OntologiesV2.OntologyObjectsV2.getLastPoint( + client, + client.ontologyRid, + objectApiName, + primaryKey, + propertyName, + ) as Promise>; + }, + async getAllPoints() { + return OntologiesV2.OntologyObjectsV2.streamPoints( + client, + client.ontologyRid, + objectApiName, + primaryKey, + propertyName, + body, + ); + }, + + asyncIterPoints() { + return OntologiesV2.OntologyObjectsV2.streamPoints( + client, + client.ontologyRid, + objectApiName, + primaryKey, + propertyName, + body, + ); + }, + }; +} + +async function getAllTimeSeriesPoints( + client: MinimalClient, + objectApiName: string, + primaryKey: any, + propertyName: string, + body: StreamTimeSeriesPointsRequest, +): Promise>> { + const streamPointsIterator = await OntologiesV2.OntologyObjectsV2 + .streamPoints( + client, + client.ontologyRid, + objectApiName, + primaryKey, + propertyName, + body, + ); + + const allPoints: Array> = []; + + const reader = streamPointsIterator.getReader(); + for await ( + const point of parseStreamedResponse(iterateReadableStream(reader)) + ) { + allPoints.push({ + time: point.time, + value: point.value as T, + }); + } + return allPoints; +} diff --git a/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts b/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts index b933df382..f5d30584e 100644 --- a/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts +++ b/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts @@ -17,6 +17,7 @@ import type { Osdk } from "@osdk/client.api"; import type { OntologyObjectV2 } from "@osdk/internal.foundry"; import { createAttachmentFromRid } from "../../createAttachmentFromRid.js"; +import { createTimeseriesProperty } from "../../createTimeseriesProperty.js"; import type { MinimalClient } from "../../MinimalClientContext.js"; import type { FetchedObjectTypeDefinition } from "../../ontology/OntologyProvider.js"; import { createClientCache } from "../Cache.js"; @@ -112,6 +113,21 @@ export function createOsdkObject< } return createAttachmentFromRid(client, rawValue.rid); } + if ( + propDef.type === "numericTimeseries" + || propDef.type === "stringTimeseries" + ) { + return createTimeseriesProperty< + (typeof propDef)["type"] extends "numericTimeseries" ? number + : string + >( + client, + objectDef.apiName, + objectDef.primaryKeyApiName, + p as string, + undefined as any, + ); + } } return rawValue; }