Skip to content

Commit

Permalink
🐛 run cli (#1088)
Browse files Browse the repository at this point in the history
* 🐛 run cli

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
thibaultleouay and autofix-ci[bot] authored Nov 7, 2024
1 parent 88dea54 commit 4c0b22e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions apps/checker/handlers/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (h Handler) HTTPCheckerHandler(c *gin.Context) {
}

var called int

var result checker.Response

op := func() error {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions apps/checker/handlers/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -95,6 +96,7 @@ func (h Handler) TCPHandler(c *gin.Context) {
}

var called int

var response TCPResponse

op := func() error {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions apps/checker/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions apps/server/src/checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions apps/server/src/v1/monitors/run/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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" });
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/v1/monitors/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(),
Expand All @@ -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),
Expand Down

0 comments on commit 4c0b22e

Please sign in to comment.