-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathWaitpointPresenter.server.ts
More file actions
118 lines (109 loc) · 3.7 KB
/
WaitpointPresenter.server.ts
File metadata and controls
118 lines (109 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { isWaitpointOutputTimeout, prettyPrintPacket } from "@trigger.dev/core/v3";
import { clickhouseFactory } from "~/services/clickhouse/clickhouseFactory.server";
import { generateHttpCallbackUrl } from "~/services/httpCallback.server";
import { logger } from "~/services/logger.server";
import { BasePresenter } from "./basePresenter.server";
import { NextRunListPresenter, type NextRunListItem } from "./NextRunListPresenter.server";
import { waitpointStatusToApiStatus } from "./WaitpointListPresenter.server";
export type WaitpointDetail = NonNullable<Awaited<ReturnType<WaitpointPresenter["call"]>>>;
export class WaitpointPresenter extends BasePresenter {
public async call({
friendlyId,
environmentId,
projectId,
}: {
friendlyId: string;
environmentId: string;
projectId: string;
}) {
const waitpoint = await this._replica.waitpoint.findFirst({
where: {
friendlyId,
environmentId,
},
select: {
id: true,
friendlyId: true,
type: true,
status: true,
idempotencyKey: true,
userProvidedIdempotencyKey: true,
idempotencyKeyExpiresAt: true,
inactiveIdempotencyKey: true,
output: true,
outputType: true,
outputIsError: true,
completedAfter: true,
completedAt: true,
createdAt: true,
connectedRuns: {
select: {
friendlyId: true,
},
take: 5,
},
tags: true,
environment: {
select: {
apiKey: true,
organizationId: true,
},
},
},
});
if (!waitpoint) {
logger.error(`WaitpointPresenter: Waitpoint not found`, {
friendlyId,
});
return null;
}
const output =
waitpoint.outputType === "application/store"
? `/resources/packets/${environmentId}/${waitpoint.output}`
: typeof waitpoint.output !== "undefined" && waitpoint.output !== null
? await prettyPrintPacket(waitpoint.output, waitpoint.outputType ?? undefined)
: undefined;
let isTimeout = false;
if (waitpoint.outputIsError && output) {
if (isWaitpointOutputTimeout(output)) {
isTimeout = true;
}
}
const connectedRunIds = waitpoint.connectedRuns.map((run) => run.friendlyId);
const connectedRuns: NextRunListItem[] = [];
if (connectedRunIds.length > 0) {
const clickhouse = await clickhouseFactory.getClickhouseForOrganization(waitpoint.environment.organizationId, "standard");
const runPresenter = new NextRunListPresenter(this._prisma, clickhouse);
const { runs } = await runPresenter.call(
waitpoint.environment.organizationId,
environmentId,
{
projectId: projectId,
runId: connectedRunIds,
pageSize: 5,
period: "31d",
}
);
connectedRuns.push(...runs);
}
return {
id: waitpoint.friendlyId,
type: waitpoint.type,
url: generateHttpCallbackUrl(waitpoint.id, waitpoint.environment.apiKey),
status: waitpointStatusToApiStatus(waitpoint.status, waitpoint.outputIsError),
idempotencyKey: waitpoint.idempotencyKey,
userProvidedIdempotencyKey: waitpoint.userProvidedIdempotencyKey,
idempotencyKeyExpiresAt: waitpoint.idempotencyKeyExpiresAt,
inactiveIdempotencyKey: waitpoint.inactiveIdempotencyKey,
output: output,
outputType: waitpoint.outputType,
outputIsError: waitpoint.outputIsError,
timeoutAt: waitpoint.completedAfter,
completedAfter: waitpoint.completedAfter,
completedAt: waitpoint.completedAt,
createdAt: waitpoint.createdAt,
tags: waitpoint.tags,
connectedRuns,
};
}
}