Skip to content

Commit

Permalink
start timeseries work
Browse files Browse the repository at this point in the history
  • Loading branch information
ssanjay1 committed Jul 2, 2024
1 parent bfdb123 commit 26c668e
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/client.api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
9 changes: 5 additions & 4 deletions packages/client.api/src/mapping/PropertyValueMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,8 +37,8 @@ export interface PropertyValueWireToClient {
string: string;
timestamp: string;

numericTimeseries: unknown;
stringTimeseries: unknown;
numericTimeseries: TimeSeriesProperty<number>;
stringTimeseries: TimeSeriesProperty<string>;
}

/**
Expand All @@ -60,6 +61,6 @@ export interface PropertyValueClientToWire {
string: string;
timestamp: string;

numericTimeseries: unknown;
stringTimeseries: unknown;
numericTimeseries: TimeSeriesProperty<number>;
stringTimeseries: TimeSeriesProperty<string>;
}
43 changes: 43 additions & 0 deletions packages/client.api/src/timeseries/timeseries.ts
Original file line number Diff line number Diff line change
@@ -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<T extends string | number> {
time: string;
value: T;
}

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>>;
}
109 changes: 109 additions & 0 deletions packages/client/src/createTimeseriesProperty.ts
Original file line number Diff line number Diff line change
@@ -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<T extends number | string>(
client: MinimalClient,
objectApiName: string,
primaryKey: any,
propertyName: string,
body: StreamTimeSeriesPointsRequest,
): TimeSeriesProperty<T> {
return {
async getFirstPoint() {
return OntologiesV2.OntologyObjectsV2.getFirstPoint(
client,
client.ontologyRid,
objectApiName,
primaryKey,
propertyName,
) as Promise<TimeSeriesPoint<T>>;
},
async getLastPoint() {
return OntologiesV2.OntologyObjectsV2.getLastPoint(
client,
client.ontologyRid,
objectApiName,
primaryKey,
propertyName,
) as Promise<TimeSeriesPoint<T>>;
},
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<T extends string | number>(
client: MinimalClient,
objectApiName: string,
primaryKey: any,
propertyName: string,
body: StreamTimeSeriesPointsRequest,
): Promise<Array<TimeSeriesPoint<T>>> {
const streamPointsIterator = await OntologiesV2.OntologyObjectsV2
.streamPoints(
client,
client.ontologyRid,
objectApiName,
primaryKey,
propertyName,
body,
);

const allPoints: Array<TimeSeriesPoint<T>> = [];

const reader = streamPointsIterator.getReader();
for await (
const point of parseStreamedResponse(iterateReadableStream(reader))
) {
allPoints.push({
time: point.time,
value: point.value as T,
});
}
return allPoints;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 26c668e

Please sign in to comment.