Skip to content

Commit ed1f4ce

Browse files
committed
Expose a new API endpoint to list events for a given task run
1 parent b6b25b0 commit ed1f4ce

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { json } from "@remix-run/server-runtime";
2+
import { z } from "zod";
3+
import { getTaskEventStoreTableForRun } from "~/v3/taskEventStore.server";
4+
import { createLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server";
5+
import { eventRepository } from "~/v3/eventRepository.server";
6+
import { ApiRetrieveRunPresenter } from "~/presenters/v3/ApiRetrieveRunPresenter.server";
7+
8+
const ParamsSchema = z.object({
9+
runId: z.string(), // This is the run friendly ID
10+
});
11+
12+
// TODO: paginate the results
13+
export const loader = createLoaderApiRoute(
14+
{
15+
params: ParamsSchema,
16+
allowJWT: true,
17+
corsStrategy: "all",
18+
findResource: (params, auth) => {
19+
return ApiRetrieveRunPresenter.findRun(params.runId, auth.environment);
20+
},
21+
shouldRetryNotFound: true,
22+
authorization: {
23+
action: "read",
24+
resource: (run) => ({
25+
runs: run.friendlyId,
26+
tags: run.runTags,
27+
batch: run.batch?.friendlyId,
28+
tasks: run.taskIdentifier,
29+
}),
30+
superScopes: ["read:runs", "read:all", "admin"],
31+
},
32+
},
33+
async ({ resource: run }) => {
34+
const runEvents = await eventRepository.getRunEvents(
35+
getTaskEventStoreTableForRun(run),
36+
run.friendlyId,
37+
run.createdAt,
38+
run.completedAt ?? undefined
39+
);
40+
41+
// TODO: return only relevant fields, avoid returning the whole events
42+
return json(
43+
{
44+
events: runEvents.map((event) => {
45+
return JSON.parse(
46+
JSON.stringify(event, (_, value) =>
47+
// needed as JSON.stringify doesn't know how to handle BigInt values by default
48+
typeof value === "bigint" ? value.toString() : value
49+
)
50+
);
51+
}),
52+
},
53+
{ status: 200 }
54+
);
55+
}
56+
);

0 commit comments

Comments
 (0)