|
| 1 | +import type { PrismaClientOrTransaction } from "~/db.server"; |
| 2 | +import type { SessionStatus } from "~/services/sessionsRepository/sessionsRepository.server"; |
| 3 | + |
| 4 | +const SESSION_START_SPAN_MESSAGE = "sessions.start()"; |
| 5 | + |
| 6 | +export type StartedSession = { |
| 7 | + friendlyId: string; |
| 8 | + externalId: string; |
| 9 | + status: SessionStatus; |
| 10 | + /** The trace already belongs to this session, so it isn't linked. */ |
| 11 | + isCurrent: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +/** The `externalId` of a `sessions.start()` span. The `session` attribute falls back to the type, so it isn't used. */ |
| 15 | +export function startedSessionExternalId(span: { |
| 16 | + message: string; |
| 17 | + properties: unknown; |
| 18 | +}): string | undefined { |
| 19 | + if (span.message !== SESSION_START_SPAN_MESSAGE) return; |
| 20 | + if (!span.properties || typeof span.properties !== "object") return; |
| 21 | + |
| 22 | + const externalId = (span.properties as Record<string, unknown>).externalId; |
| 23 | + if (typeof externalId !== "string" || externalId.length === 0) return; |
| 24 | + |
| 25 | + return externalId; |
| 26 | +} |
| 27 | + |
| 28 | +/** Session runs start their own trace, so the root run says which session a trace is in. */ |
| 29 | +export function traceRootRunId(run: { id: string; rootTaskRunId: string | null }): string { |
| 30 | + return run.rootTaskRunId ?? run.id; |
| 31 | +} |
| 32 | + |
| 33 | +export async function findStartedSession( |
| 34 | + db: PrismaClientOrTransaction, |
| 35 | + { |
| 36 | + environmentId, |
| 37 | + externalId, |
| 38 | + rootRunId, |
| 39 | + userId, |
| 40 | + }: { |
| 41 | + environmentId: string; |
| 42 | + externalId: string; |
| 43 | + rootRunId: string; |
| 44 | + userId: string; |
| 45 | + } |
| 46 | +): Promise<StartedSession | undefined> { |
| 47 | + const [session, viewableEnvironment, rootSessionRun] = await Promise.all([ |
| 48 | + db.session.findFirst({ |
| 49 | + where: { runtimeEnvironmentId: environmentId, externalId }, |
| 50 | + select: { id: true, friendlyId: true, closedAt: true, expiresAt: true }, |
| 51 | + }), |
| 52 | + // Same rule as the session page: dev environments are visible to their owner only. |
| 53 | + db.runtimeEnvironment.findFirst({ |
| 54 | + where: { |
| 55 | + id: environmentId, |
| 56 | + OR: [{ type: { not: "DEVELOPMENT" } }, { orgMember: { userId } }], |
| 57 | + }, |
| 58 | + select: { id: true }, |
| 59 | + }), |
| 60 | + db.sessionRun.findFirst({ |
| 61 | + where: { runId: rootRunId }, |
| 62 | + select: { sessionId: true }, |
| 63 | + }), |
| 64 | + ]); |
| 65 | + |
| 66 | + if (!session || !viewableEnvironment) return; |
| 67 | + |
| 68 | + return { |
| 69 | + friendlyId: session.friendlyId, |
| 70 | + externalId, |
| 71 | + status: |
| 72 | + session.closedAt != null |
| 73 | + ? "CLOSED" |
| 74 | + : session.expiresAt != null && session.expiresAt.getTime() < Date.now() |
| 75 | + ? "EXPIRED" |
| 76 | + : "ACTIVE", |
| 77 | + isCurrent: rootSessionRun?.sessionId === session.id, |
| 78 | + }; |
| 79 | +} |
0 commit comments