From 4c0b22e7cf0600cc5fec2b9a4b790638b2d6a6e1 Mon Sep 17 00:00:00 2001 From: Thibault Le Ouay Date: Thu, 7 Nov 2024 10:52:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20run=20cli=20(#1088)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 run cli * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- apps/checker/handlers/checker.go | 2 ++ apps/checker/handlers/tcp.go | 3 +++ apps/checker/http.go | 3 +++ apps/server/src/checker/index.ts | 7 +++++-- apps/server/src/v1/monitors/run/post.ts | 17 +++++++++-------- apps/server/src/v1/monitors/schema.ts | 7 +++++++ 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/apps/checker/handlers/checker.go b/apps/checker/handlers/checker.go index 6470227360..0e14f9e3eb 100644 --- a/apps/checker/handlers/checker.go +++ b/apps/checker/handlers/checker.go @@ -88,6 +88,7 @@ func (h Handler) HTTPCheckerHandler(c *gin.Context) { } var called int + var result checker.Response op := func() error { @@ -177,6 +178,7 @@ func (h Handler) HTTPCheckerHandler(c *gin.Context) { result = res result.Region = h.Region + result.JobType = "http" // it's in error if not successful if isSuccessfull { diff --git a/apps/checker/handlers/tcp.go b/apps/checker/handlers/tcp.go index e51409bf1e..bb8d4be692 100644 --- a/apps/checker/handlers/tcp.go +++ b/apps/checker/handlers/tcp.go @@ -17,6 +17,7 @@ import ( type TCPResponse struct { Region string `json:"region"` ErrorMessage string `json:"errorMessage"` + JobType string `json:"jobType"` RequestId int64 `json:"requestId,omitempty"` WorkspaceID int64 `json:"workspaceId"` MonitorID int64 `json:"monitorId"` @@ -95,6 +96,7 @@ func (h Handler) TCPHandler(c *gin.Context) { } var called int + var response TCPResponse op := func() error { @@ -268,6 +270,7 @@ func (h Handler) TCPHandlerRegion(c *gin.Context) { }, Latency: res.TCPDone - res.TCPStart, Region: h.Region, + JobType: "tcp", } timingAsString, err := json.Marshal(res) diff --git a/apps/checker/http.go b/apps/checker/http.go index 32c903426c..101a2d837a 100644 --- a/apps/checker/http.go +++ b/apps/checker/http.go @@ -36,6 +36,7 @@ type Response struct { Body string `json:"body,omitempty"` Error string `json:"error,omitempty"` Region string `json:"region"` + JobType string `json:"jobType"` Latency int64 `json:"latency"` Timestamp int64 `json:"timestamp"` Status int `json:"status,omitempty"` @@ -80,6 +81,7 @@ func Http(ctx context.Context, client *http.Client, inputData request.HttpChecke if inputData.Method != http.MethodGet { head := req.Header _, ok := head["Content-Type"] + if !ok { // by default we set the content type to application/json if it's a POST request req.Header.Set("Content-Type", "application/json") @@ -127,6 +129,7 @@ func Http(ctx context.Context, client *http.Client, inputData request.HttpChecke return Response{}, fmt.Errorf("error with monitorURL %s: %w", inputData.URL, err) } + defer response.Body.Close() body, err := io.ReadAll(response.Body) diff --git a/apps/server/src/checker/index.ts b/apps/server/src/checker/index.ts index 8006a24a90..e2d18fbbf7 100644 --- a/apps/server/src/checker/index.ts +++ b/apps/server/src/checker/index.ts @@ -200,7 +200,10 @@ checkerRoute.post("/updateStatus", async (c) => { .from(workspace) .where(eq(workspace.id, monitor.workspaceId)) .get(); - if (currentWorkspace?.plan !== "free") { + if ( + !!currentWorkspace?.plan && + currentWorkspace?.plan !== "free" + ) { await triggerScreenshot({ data: { url: monitor.url, @@ -302,7 +305,7 @@ checkerRoute.post("/updateStatus", async (c) => { .where(eq(workspace.id, monitor.workspaceId)) .get(); - if (currentWorkspace?.plan !== "free") { + if (!!currentWorkspace?.plan && currentWorkspace?.plan !== "free") { await triggerScreenshot({ data: { url: monitor.url, diff --git a/apps/server/src/v1/monitors/run/post.ts b/apps/server/src/v1/monitors/run/post.ts index ce62967ca3..add95e11b4 100644 --- a/apps/server/src/v1/monitors/run/post.ts +++ b/apps/server/src/v1/monitors/run/post.ts @@ -12,7 +12,12 @@ 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, TCPTriggerResult } from "../schema"; +import { + HTTPTriggerResult, + ParamsSchema, + TCPTriggerResult, + TriggerResult, +} from "../schema"; const triggerMonitor = createRoute({ method: "post", @@ -193,13 +198,9 @@ export function registerRunMonitor(api: typeof monitorsApi) { // console.log(result); const bodies = await Promise.all(result.map((r) => r.json())); - let data = null; - if (row.jobType === "http") { - data = z.array(HTTPTriggerResult).safeParse(bodies); - } - if (row.jobType === "tcp") { - data = z.array(TCPTriggerResult).safeParse(bodies); - } + // let data = null; + + const data = z.array(TriggerResult).safeParse(bodies); if (!data) { throw new HTTPException(400, { message: "Something went wrong" }); diff --git a/apps/server/src/v1/monitors/schema.ts b/apps/server/src/v1/monitors/schema.ts index 75625ea2b5..6dcd654f4b 100644 --- a/apps/server/src/v1/monitors/schema.ts +++ b/apps/server/src/v1/monitors/schema.ts @@ -234,6 +234,7 @@ const timingSchema = z.object({ }); export const HTTPTriggerResult = z.object({ + jobType: z.literal("http"), status: z.number(), latency: z.number(), region: z.enum(flyRegions), @@ -249,6 +250,7 @@ const tcptimingSchema = z.object({ }); export const TCPTriggerResult = z.object({ + jobType: z.literal("tcp"), latency: z.number(), region: z.enum(flyRegions), timestamp: z.number(), @@ -257,6 +259,11 @@ export const TCPTriggerResult = z.object({ errorMessage: z.string().optional().nullable(), }); +export const TriggerResult = z.discriminatedUnion("jobType", [ + HTTPTriggerResult, + TCPTriggerResult, +]); + export const ResultRun = z.object({ latency: z.number().int(), // in ms statusCode: z.number().int().nullable().default(null),