|
| 1 | +import { ActionFunctionArgs } from "@remix-run/server-runtime"; |
| 2 | +import { MachinePresetName } from "@trigger.dev/core/v3"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { prisma } from "~/db.server"; |
| 5 | +import { validateJWTTokenAndRenew } from "~/services/apiAuth.server"; |
| 6 | +import { logger } from "~/services/logger.server"; |
| 7 | +import { workerQueue } from "~/services/worker.server"; |
| 8 | +import { machinePresetFromName } from "~/v3/machinePresets.server"; |
| 9 | +import { reportUsageEvent } from "~/v3/openMeter.server"; |
| 10 | + |
| 11 | +const JWTPayloadSchema = z.object({ |
| 12 | + environment_id: z.string(), |
| 13 | + org_id: z.string(), |
| 14 | + project_id: z.string(), |
| 15 | + run_id: z.string(), |
| 16 | + machine_preset: z.string(), |
| 17 | +}); |
| 18 | + |
| 19 | +const BodySchema = z.object({ |
| 20 | + durationMs: z.number(), |
| 21 | +}); |
| 22 | + |
| 23 | +export async function action({ request }: ActionFunctionArgs) { |
| 24 | + // Ensure this is a POST request |
| 25 | + if (request.method.toUpperCase() !== "POST") { |
| 26 | + return { status: 405, body: "Method Not Allowed" }; |
| 27 | + } |
| 28 | + |
| 29 | + const jwtResult = await validateJWTTokenAndRenew(request, JWTPayloadSchema); |
| 30 | + |
| 31 | + if (!jwtResult) { |
| 32 | + return { status: 401, body: "Unauthorized" }; |
| 33 | + } |
| 34 | + |
| 35 | + const rawJson = await request.json(); |
| 36 | + |
| 37 | + const json = BodySchema.safeParse(rawJson); |
| 38 | + |
| 39 | + if (!json.success) { |
| 40 | + logger.error("Failed to parse request body", { rawJson }); |
| 41 | + |
| 42 | + return { status: 400, body: "Bad Request" }; |
| 43 | + } |
| 44 | + |
| 45 | + const preset = machinePresetFromName(jwtResult.payload.machine_preset as MachinePresetName); |
| 46 | + |
| 47 | + logger.debug("[/api/v1/usage/ingest] Reporting usage", { jwtResult, json: json.data, preset }); |
| 48 | + |
| 49 | + if (json.data.durationMs > 0) { |
| 50 | + const costInCents = json.data.durationMs * preset.centsPerMs; |
| 51 | + |
| 52 | + await prisma.taskRun.update({ |
| 53 | + where: { |
| 54 | + id: jwtResult.payload.run_id, |
| 55 | + }, |
| 56 | + data: { |
| 57 | + usageDurationMs: { |
| 58 | + increment: json.data.durationMs, |
| 59 | + }, |
| 60 | + costInCents: { |
| 61 | + increment: json.data.durationMs * preset.centsPerMs, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + try { |
| 67 | + await reportUsageEvent({ |
| 68 | + source: "webapp", |
| 69 | + type: "usage", |
| 70 | + subject: jwtResult.payload.org_id, |
| 71 | + data: { |
| 72 | + durationMs: json.data.durationMs, |
| 73 | + costInCents: String(costInCents), |
| 74 | + }, |
| 75 | + }); |
| 76 | + } catch (e) { |
| 77 | + logger.error("Failed to report usage event, enqueing v3.reportUsage", { error: e }); |
| 78 | + |
| 79 | + await workerQueue.enqueue("v3.reportUsage", { |
| 80 | + orgId: jwtResult.payload.org_id, |
| 81 | + data: { |
| 82 | + costInCents: String(costInCents), |
| 83 | + }, |
| 84 | + additionalData: { |
| 85 | + durationMs: json.data.durationMs, |
| 86 | + }, |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return new Response(null, { |
| 92 | + status: 200, |
| 93 | + headers: { |
| 94 | + "x-trigger-jwt": jwtResult.jwt, |
| 95 | + }, |
| 96 | + }); |
| 97 | +} |
0 commit comments