|
| 1 | +import { RunId } from "@trigger.dev/core/v3/isomorphic"; |
| 2 | +import type { PrismaClientOrTransaction, TaskRunStatus } from "@trigger.dev/database"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { |
| 5 | + countSessionRuns, |
| 6 | + countSessionRunsAtOrBefore, |
| 7 | + decodeSessionRunCursor, |
| 8 | + encodeSessionRunCursor, |
| 9 | + findSessionRunCursor, |
| 10 | + findSessionRunsPage, |
| 11 | + type SessionRunCursor, |
| 12 | + type SessionRunRow, |
| 13 | +} from "~/services/realtime/sessionRunPage.server"; |
| 14 | +import { getRunFinishedAt } from "~/v3/eventRepository/traceInsertedAtBound"; |
| 15 | +import type { runTraceUnavailableBeforeRead } from "./runTraceSetup.server"; |
| 16 | +import { startActiveSpan } from "~/v3/tracer.server"; |
| 17 | + |
| 18 | +/** Query params, following `GET /api/v1/sessions`. A bad cursor fails the parse (400). */ |
| 19 | +export const SessionRunsSearchParams = z |
| 20 | + .object({ |
| 21 | + "page[size]": z.coerce.number().int().min(1).max(100).default(20), |
| 22 | + "page[after]": z |
| 23 | + .string() |
| 24 | + .transform((value, ctx) => { |
| 25 | + const cursor = decodeSessionRunCursor(value); |
| 26 | + if (!cursor) { |
| 27 | + ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Invalid cursor" }); |
| 28 | + return z.NEVER; |
| 29 | + } |
| 30 | + return cursor; |
| 31 | + }) |
| 32 | + .optional(), |
| 33 | + run: z |
| 34 | + .string() |
| 35 | + .regex(/^run_[a-z0-9]+$/, "Invalid run ID") |
| 36 | + .optional(), |
| 37 | + }) |
| 38 | + .refine((value) => !(value["page[after]"] && value.run), { |
| 39 | + message: "Cannot pass both page[after] and run", |
| 40 | + path: ["run"], |
| 41 | + }); |
| 42 | + |
| 43 | +export const sessionRunSelect = { |
| 44 | + id: true, |
| 45 | + friendlyId: true, |
| 46 | + projectId: true, |
| 47 | + runtimeEnvironmentId: true, |
| 48 | + status: true, |
| 49 | + createdAt: true, |
| 50 | + startedAt: true, |
| 51 | + completedAt: true, |
| 52 | + updatedAt: true, |
| 53 | + taskEventStore: true, |
| 54 | + logsDeletedAt: true, |
| 55 | +} as const; |
| 56 | + |
| 57 | +export type SessionRunRecord = { |
| 58 | + id: string; |
| 59 | + friendlyId: string; |
| 60 | + projectId: string; |
| 61 | + runtimeEnvironmentId: string; |
| 62 | + status: TaskRunStatus; |
| 63 | + createdAt: Date; |
| 64 | + startedAt: Date | null; |
| 65 | + completedAt: Date | null; |
| 66 | + updatedAt: Date; |
| 67 | + taskEventStore: string; |
| 68 | + logsDeletedAt: Date | null; |
| 69 | +}; |
| 70 | + |
| 71 | +type RunTraceUnavailableReason = NonNullable<ReturnType<typeof runTraceUnavailableBeforeRead>>; |
| 72 | + |
| 73 | +type SessionRunItem = { |
| 74 | + number: number; |
| 75 | + friendlyId: string; |
| 76 | + reason: string; |
| 77 | + afterCrash: boolean; |
| 78 | + triggeredAt: string; |
| 79 | + status: TaskRunStatus | null; |
| 80 | + createdAt: string | null; |
| 81 | + startedAt: string | null; |
| 82 | + completedAt: string | null; |
| 83 | + finishedAt: string | null; |
| 84 | +}; |
| 85 | + |
| 86 | +export type SessionRunsPage = { |
| 87 | + data: SessionRunItem[]; |
| 88 | + pagination: { next?: string }; |
| 89 | + totalCount: number; |
| 90 | + view: Record<string, { unavailable?: RunTraceUnavailableReason }>; |
| 91 | +}; |
| 92 | + |
| 93 | +export type SessionRunsPresenterDeps = { |
| 94 | + replica: PrismaClientOrTransaction; |
| 95 | + findRunsByIds: (ids: string[]) => Promise<Map<string, SessionRunRecord>>; |
| 96 | + unavailableBeforeRead: typeof runTraceUnavailableBeforeRead; |
| 97 | + emergencySpanCap: number | undefined; |
| 98 | + v1RerouteEnabled: boolean; |
| 99 | +}; |
| 100 | + |
| 101 | +export type SessionRunsQuery = { |
| 102 | + session: { id: string; projectId: string; runtimeEnvironmentId: string }; |
| 103 | + pageSize: number; |
| 104 | + /** Older runs than this cursor. */ |
| 105 | + after?: SessionRunCursor; |
| 106 | + /** The page ending at this run, for deep links. */ |
| 107 | + runFriendlyId?: string; |
| 108 | + showDeletedLogs: boolean; |
| 109 | +}; |
| 110 | + |
| 111 | +const CRASH_STATUSES: TaskRunStatus[] = ["CRASHED", "SYSTEM_FAILURE"]; |
| 112 | + |
| 113 | +export class SessionRunsPresenter { |
| 114 | + constructor(private readonly deps: SessionRunsPresenterDeps) {} |
| 115 | + |
| 116 | + /** Null when `runFriendlyId` isn't a run of this session. */ |
| 117 | + public call(query: SessionRunsQuery): Promise<SessionRunsPage | null> { |
| 118 | + return startActiveSpan("SessionRunsPresenter.call", (span) => { |
| 119 | + span.setAttribute("session.id", query.session.id); |
| 120 | + span.setAttribute("sessionRuns.pageSize", query.pageSize); |
| 121 | + span.setAttribute("sessionRuns.deepLink", query.runFriendlyId !== undefined); |
| 122 | + return this.#call(query).then((page) => { |
| 123 | + span.setAttribute("sessionRuns.rows", page?.data.length ?? 0); |
| 124 | + return page; |
| 125 | + }); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + async #call(query: SessionRunsQuery): Promise<SessionRunsPage | null> { |
| 130 | + const { replica } = this.deps; |
| 131 | + const sessionId = query.session.id; |
| 132 | + |
| 133 | + const anchor = query.runFriendlyId |
| 134 | + ? await findSessionRunCursor(replica, { |
| 135 | + sessionId, |
| 136 | + runId: RunId.fromFriendlyId(query.runFriendlyId), |
| 137 | + }) |
| 138 | + : undefined; |
| 139 | + if (anchor === null) return null; |
| 140 | + |
| 141 | + const [rows, totalCount] = await Promise.all([ |
| 142 | + findSessionRunsPage(replica, { |
| 143 | + sessionId, |
| 144 | + before: query.after, |
| 145 | + atOrBefore: anchor, |
| 146 | + take: query.pageSize + 1, |
| 147 | + }), |
| 148 | + countSessionRuns(replica, { sessionId }), |
| 149 | + ]); |
| 150 | + |
| 151 | + const pageRows = rows.slice(0, query.pageSize); |
| 152 | + const olderRow = rows.at(query.pageSize); |
| 153 | + const oldestRow = pageRows.at(-1); |
| 154 | + |
| 155 | + if (!oldestRow) { |
| 156 | + return { data: [], pagination: {}, totalCount, view: {} }; |
| 157 | + } |
| 158 | + |
| 159 | + const [oldestNumber, runsById] = await Promise.all([ |
| 160 | + olderRow ? countSessionRunsAtOrBefore(replica, { sessionId, cursor: oldestRow }) : 1, |
| 161 | + this.#findSessionRuns(query.session, rows), |
| 162 | + ]); |
| 163 | + |
| 164 | + const data = pageRows.map((row, index) => { |
| 165 | + const predecessor = pageRows[index + 1] ?? olderRow; |
| 166 | + return toItem({ |
| 167 | + row, |
| 168 | + number: oldestNumber + (pageRows.length - 1 - index), |
| 169 | + run: runsById.get(row.runId), |
| 170 | + predecessorRun: predecessor ? runsById.get(predecessor.runId) : undefined, |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + return { |
| 175 | + data, |
| 176 | + pagination: olderRow ? { next: encodeSessionRunCursor(oldestRow) } : {}, |
| 177 | + totalCount, |
| 178 | + view: this.#view(pageRows, runsById, query.showDeletedLogs), |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + // Runs outside the session's project and environment are treated as missing. |
| 183 | + async #findSessionRuns( |
| 184 | + session: SessionRunsQuery["session"], |
| 185 | + rows: SessionRunRow[] |
| 186 | + ): Promise<Map<string, SessionRunRecord>> { |
| 187 | + const runs = await this.deps.findRunsByIds(rows.map((row) => row.runId)); |
| 188 | + const inSession = new Map<string, SessionRunRecord>(); |
| 189 | + for (const [id, run] of runs) { |
| 190 | + if ( |
| 191 | + run.projectId === session.projectId && |
| 192 | + run.runtimeEnvironmentId === session.runtimeEnvironmentId |
| 193 | + ) { |
| 194 | + inSession.set(id, run); |
| 195 | + } |
| 196 | + } |
| 197 | + return inSession; |
| 198 | + } |
| 199 | + |
| 200 | + #view( |
| 201 | + rows: SessionRunRow[], |
| 202 | + runsById: Map<string, SessionRunRecord>, |
| 203 | + showDeletedLogs: boolean |
| 204 | + ): SessionRunsPage["view"] { |
| 205 | + const view: SessionRunsPage["view"] = {}; |
| 206 | + for (const row of rows) { |
| 207 | + const run = runsById.get(row.runId); |
| 208 | + if (!run) continue; |
| 209 | + view[run.friendlyId] = { |
| 210 | + unavailable: this.deps.unavailableBeforeRead(run, { |
| 211 | + showDeletedLogs, |
| 212 | + emergencySpanCap: this.deps.emergencySpanCap, |
| 213 | + v1RerouteEnabled: this.deps.v1RerouteEnabled, |
| 214 | + }), |
| 215 | + }; |
| 216 | + } |
| 217 | + return view; |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +function toItem({ |
| 222 | + row, |
| 223 | + number, |
| 224 | + run, |
| 225 | + predecessorRun, |
| 226 | +}: { |
| 227 | + row: SessionRunRow; |
| 228 | + number: number; |
| 229 | + run: SessionRunRecord | undefined; |
| 230 | + predecessorRun: SessionRunRecord | undefined; |
| 231 | +}): SessionRunItem { |
| 232 | + return { |
| 233 | + number, |
| 234 | + friendlyId: run?.friendlyId ?? RunId.toFriendlyId(row.runId), |
| 235 | + reason: row.reason, |
| 236 | + afterCrash: predecessorRun ? CRASH_STATUSES.includes(predecessorRun.status) : false, |
| 237 | + triggeredAt: row.triggeredAt.toISOString(), |
| 238 | + status: run?.status ?? null, |
| 239 | + createdAt: run?.createdAt.toISOString() ?? null, |
| 240 | + startedAt: run?.startedAt?.toISOString() ?? null, |
| 241 | + completedAt: run?.completedAt?.toISOString() ?? null, |
| 242 | + finishedAt: (run && getRunFinishedAt(run)?.toISOString()) ?? null, |
| 243 | + }; |
| 244 | +} |
0 commit comments