diff --git a/apps/server/src/v1/monitors/run/post.ts b/apps/server/src/v1/monitors/run/post.ts index c3df8786aa..ce62967ca3 100644 --- a/apps/server/src/v1/monitors/run/post.ts +++ b/apps/server/src/v1/monitors/run/post.ts @@ -12,7 +12,7 @@ import { HTTPException } from "hono/http-exception"; import type { monitorsApi } from ".."; import { env } from "../../../env"; import { openApiErrorResponses } from "../../../libs/errors/openapi-error-responses"; -import { HTTPTriggerResult, ParamsSchema } from "../schema"; +import { HTTPTriggerResult, ParamsSchema, TCPTriggerResult } from "../schema"; const triggerMonitor = createRoute({ method: "post", @@ -37,7 +37,7 @@ const triggerMonitor = createRoute({ 200: { content: { "application/json": { - schema: z.array(HTTPTriggerResult), + schema: z.array(HTTPTriggerResult).or(z.array(TCPTriggerResult)), }, }, description: "All the historical metrics", @@ -193,8 +193,17 @@ export function registerRunMonitor(api: typeof monitorsApi) { // console.log(result); const bodies = await Promise.all(result.map((r) => r.json())); - console.log(bodies); - const data = z.array(HTTPTriggerResult).safeParse(bodies); + let data = null; + if (row.jobType === "http") { + data = z.array(HTTPTriggerResult).safeParse(bodies); + } + if (row.jobType === "tcp") { + data = z.array(TCPTriggerResult).safeParse(bodies); + } + + if (!data) { + throw new HTTPException(400, { message: "Something went wrong" }); + } if (!data.success) { console.log(data.error); diff --git a/apps/server/src/v1/monitors/schema.ts b/apps/server/src/v1/monitors/schema.ts index 68452e718d..75625ea2b5 100644 --- a/apps/server/src/v1/monitors/schema.ts +++ b/apps/server/src/v1/monitors/schema.ts @@ -243,6 +243,20 @@ export const HTTPTriggerResult = z.object({ error: z.string().optional().nullable(), }); +const tcptimingSchema = z.object({ + tcpStart: z.number(), + tcpDone: z.number(), +}); + +export const TCPTriggerResult = z.object({ + latency: z.number(), + region: z.enum(flyRegions), + timestamp: z.number(), + timing: tcptimingSchema, + error: z.number().optional().nullable(), + errorMessage: z.string().optional().nullable(), +}); + export const ResultRun = z.object({ latency: z.number().int(), // in ms statusCode: z.number().int().nullable().default(null),