Skip to content

Commit

Permalink
Merge pull request #75 from palantir/ssanjay/addCount
Browse files Browse the repository at this point in the history
Use object set in aggregations
  • Loading branch information
ssanjay1 authored Feb 15, 2024
2 parents 26fc687 + 2c718a8 commit b2177c1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@ import { expectType } from "ts-expect";
export async function fetchAggregationForEmployeesGroupedThin(
clientCtx: ClientContext<Ontology>,
) {
const result = await aggregateOrThrow(clientCtx, Ontology.objects.Employee, {
select: {
locationCity: "approximateDistinct",
locationName: "approximateDistinct",
employeeNumber: ["avg", "max", "min"],
const result = await aggregateOrThrow(
clientCtx,
Ontology.objects.Employee,
{
type: "base",
objectType: "Employee",
},
groupBy: {
locationType: "exact",
{
select: {
locationCity: "approximateDistinct",
locationName: "approximateDistinct",
employeeNumber: ["avg", "max", "min"],
},
groupBy: {
locationType: "exact",
},
},
});
);

result[0].values.employeeNumber.avg;
result[0].group.locationType;
Expand Down
5 changes: 5 additions & 0 deletions packages/client/changelog/@unreleased/pr-75.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Use object set in aggregations
links:
- https://github.com/palantir/osdk-ts/pull/75
40 changes: 28 additions & 12 deletions packages/client/src/object/aggregateOrThrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,21 @@ describe("aggregateOrThrow", () => {
mockFetch,
);

const notGrouped = await aggregateOrThrow(clientCtx, Todo, {
select: {
text: "approximateDistinct",
priority: "avg",
id: ["max", "avg"],
const notGrouped = await aggregateOrThrow(
clientCtx,
Todo,
{
type: "base",
objectType: "ToDo",
},
});
{
select: {
text: "approximateDistinct",
priority: "avg",
id: ["max", "avg"],
},
},
);

expectType<number>(notGrouped.text.approximateDistinct);
expectType<number | undefined>(notGrouped.priority.avg);
Expand All @@ -148,14 +156,22 @@ describe("aggregateOrThrow", () => {
>
>(false); // subselect should hide unused keys

const grouped = await aggregateOrThrow(clientCtx, Todo, {
select: {
text: "approximateDistinct",
const grouped = await aggregateOrThrow(
clientCtx,
Todo,
{
type: "base",
objectType: "ToDo",
},
groupBy: {
text: "exact",
{
select: {
text: "approximateDistinct",
},
groupBy: {
text: "exact",
},
},
});
);
expectType<Array<any>>(grouped);
expectType<string | undefined>(grouped[0].group.text);
expectType<number>(grouped[0].values.text.approximateDistinct);
Expand Down
19 changes: 12 additions & 7 deletions packages/client/src/object/aggregateOrThrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import type { ObjectOrInterfaceDefinition } from "@osdk/api";
import { aggregateObjectsV2 } from "@osdk/gateway/requests";
import type { AggregateObjectsRequestV2 } from "@osdk/gateway/types";
import { aggregateObjectSetV2 } from "@osdk/gateway/requests";
import type { AggregateObjectsRequestV2, ObjectSet } from "@osdk/gateway/types";
import { createOpenApiRequest } from "@osdk/shared.net";
import type { ClientContext } from "@osdk/shared.net";
import invariant from "tiny-invariant";
Expand All @@ -38,6 +38,10 @@ export async function aggregateOrThrow<
>(
clientCtx: ClientContext<any>,
objectType: Q,
objectSet: ObjectSet = {
type: "base",
objectType: objectType["apiName"] as string,
},
req: AO,
): Promise<AggregationsResults<Q, AO>> {
const body: AggregateObjectsRequestV2 = {
Expand All @@ -53,17 +57,18 @@ export async function aggregateOrThrow<
}
if (req.where) {
body.where = modernToLegacyWhereClause(req.where);
// TODO: orderBy
// TODO The token stuff here sucks
}
const result = await aggregateObjectsV2(
const result = await aggregateObjectSetV2(
createOpenApiRequest(
clientCtx.stack,
clientCtx.fetch,
),
clientCtx.ontology.metadata.ontologyApiName,
objectType["apiName"],
body,
{
objectSet,
groupBy: body.groupBy,
aggregation: body.aggregation,
},
);

if (!req.groupBy) {
Expand Down
7 changes: 6 additions & 1 deletion packages/client/src/objectSet/createObjectSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export function createObjectSet<
>(
req: AO,
): Promise<AggregationsResults<Q, AO>> => {
return aggregateOrThrow(clientCtx, objectType, req);
return aggregateOrThrow<Q, AO>(
clientCtx,
objectType,
objectSet,
req,
);
},
// fetchPage: async (args?: { nextPageToken?: string }) => {
// throw "TODO";
Expand Down

0 comments on commit b2177c1

Please sign in to comment.