feat: refactor stage 1
This commit is contained in:
@@ -34,6 +34,7 @@ export const internal = new Elysia({ prefix: "/internal", detail: { hide: true }
|
||||
|
||||
const monitors = await sql`
|
||||
SELECT m.id, m.url, m.method, m.request_headers, m.request_body, m.timeout_ms, m.interval_s, m.query, m.regions,
|
||||
m.max_retries, m.retry_interval_s,
|
||||
EXTRACT(EPOCH FROM (
|
||||
CASE
|
||||
WHEN last.checked_at IS NULL THEN now()
|
||||
|
||||
@@ -12,6 +12,9 @@ const MonitorBody = t.Object({
|
||||
request_body: t.Optional(t.Nullable(t.String({ maxLength: 65536, description: "Request body for POST/PUT/PATCH (max 64KB)" }))),
|
||||
timeout_ms: t.Optional(t.Number({ minimum: 1000, maximum: 60000, default: 10000, description: "Request timeout in ms" })),
|
||||
interval_s: t.Optional(t.Number({ minimum: 2, default: 30, description: "Check interval in seconds (minimum 2)" })),
|
||||
max_retries: t.Optional(t.Number({ minimum: 0, maximum: 10, default: 0, description: "Retry a failing check up to N times before declaring DOWN" })),
|
||||
retry_interval_s: t.Optional(t.Number({ minimum: 1, maximum: 600, default: 30, description: "Seconds between retries" })),
|
||||
resend_interval: t.Optional(t.Number({ minimum: 0, maximum: 1000, default: 0, description: "Re-alert every Nth consecutive down beat. 0 = never resend." })),
|
||||
query: t.Optional(t.Any({ description: "PingQL query — filter conditions for up/down" })),
|
||||
regions: t.Optional(t.Array(t.String(), { description: "Regions to run checks from. Empty array = all regions." })),
|
||||
});
|
||||
@@ -47,7 +50,7 @@ export const monitors = new Elysia({ prefix: "/monitors" })
|
||||
const ssrfError = await validateMonitorUrl(body.url);
|
||||
if (ssrfError) { set.status = 400; return { error: ssrfError }; }
|
||||
const [monitor] = await sql`
|
||||
INSERT INTO monitors (account_id, name, url, method, request_headers, request_body, timeout_ms, interval_s, query, regions)
|
||||
INSERT INTO monitors (account_id, name, url, method, request_headers, request_body, timeout_ms, interval_s, max_retries, retry_interval_s, resend_interval, query, regions)
|
||||
VALUES (
|
||||
${accountId}, ${body.name}, ${body.url},
|
||||
${(body.method ?? 'GET').toUpperCase()},
|
||||
@@ -55,6 +58,9 @@ export const monitors = new Elysia({ prefix: "/monitors" })
|
||||
${body.request_body ?? null},
|
||||
${body.timeout_ms ?? 10000},
|
||||
${interval},
|
||||
${body.max_retries ?? 0},
|
||||
${body.retry_interval_s ?? 30},
|
||||
${body.resend_interval ?? 0},
|
||||
${body.query ? sql.json(body.query) : null},
|
||||
${sql.array(regions)}
|
||||
)
|
||||
@@ -103,6 +109,9 @@ export const monitors = new Elysia({ prefix: "/monitors" })
|
||||
request_body = COALESCE(${body.request_body ?? null}, request_body),
|
||||
timeout_ms = COALESCE(${body.timeout_ms ?? null}, timeout_ms),
|
||||
interval_s = COALESCE(${body.interval_s ?? null}, interval_s),
|
||||
max_retries = COALESCE(${body.max_retries ?? null}, max_retries),
|
||||
retry_interval_s = COALESCE(${body.retry_interval_s ?? null}, retry_interval_s),
|
||||
resend_interval = COALESCE(${body.resend_interval ?? null}, resend_interval),
|
||||
query = COALESCE(${body.query ? sql.json(body.query) : null}, query),
|
||||
regions = COALESCE(${body.regions ? sql.array(body.regions) : null}, regions)
|
||||
WHERE id = ${params.id} AND account_id = ${accountId}
|
||||
|
||||
@@ -55,7 +55,10 @@ export const ingest = new Elysia()
|
||||
const token = headers["x-monitor-token"];
|
||||
if (!safeTokenCompare(token, process.env.MONITOR_TOKEN)) { set.status = 401; return { error: "Unauthorized" }; }
|
||||
|
||||
const [monitor_check] = await sql`SELECT id FROM monitors WHERE id = ${body.monitor_id}`;
|
||||
const [monitor_check] = await sql`
|
||||
SELECT id, account_id, last_state, consecutive_down, resend_interval
|
||||
FROM monitors WHERE id = ${body.monitor_id}
|
||||
`;
|
||||
if (!monitor_check) { set.status = 404; return { error: "Monitor not found" }; }
|
||||
|
||||
const meta = body.meta ? { ...body.meta } : {};
|
||||
@@ -68,8 +71,33 @@ export const ingest = new Elysia()
|
||||
const scheduledAt = body.scheduled_at ? new Date(body.scheduled_at) : null;
|
||||
const jitterMs = body.jitter_ms ?? null;
|
||||
|
||||
// Important-beat + resend bookkeeping. Important = transition or resend tick.
|
||||
const newState = body.up ? 'up' : 'down';
|
||||
const prevState: string | null = monitor_check.last_state;
|
||||
let consecutiveDown: number = monitor_check.consecutive_down ?? 0;
|
||||
const resendInterval: number = monitor_check.resend_interval ?? 0;
|
||||
let important = false;
|
||||
|
||||
if (prevState !== newState) {
|
||||
important = true;
|
||||
consecutiveDown = body.up ? 0 : 1;
|
||||
} else if (!body.up) {
|
||||
consecutiveDown += 1;
|
||||
if (resendInterval > 0 && consecutiveDown > 1 && (consecutiveDown - 1) % resendInterval === 0) {
|
||||
important = true;
|
||||
}
|
||||
} else {
|
||||
consecutiveDown = 0;
|
||||
}
|
||||
|
||||
await sql`
|
||||
UPDATE monitors
|
||||
SET last_state = ${newState}, consecutive_down = ${consecutiveDown}
|
||||
WHERE id = ${body.monitor_id}
|
||||
`;
|
||||
|
||||
const [ping] = await sql`
|
||||
INSERT INTO pings (monitor_id, checked_at, scheduled_at, jitter_ms, status_code, latency_ms, up, error, meta, region, run_id)
|
||||
INSERT INTO pings (monitor_id, checked_at, scheduled_at, jitter_ms, status_code, latency_ms, up, important, error, meta, region, run_id)
|
||||
VALUES (
|
||||
${body.monitor_id},
|
||||
${checkedAt ?? sql`now()`},
|
||||
@@ -78,6 +106,7 @@ export const ingest = new Elysia()
|
||||
${body.status_code ?? null},
|
||||
${body.latency_ms ?? null},
|
||||
${body.up},
|
||||
${important},
|
||||
${body.error ?? null},
|
||||
${Object.keys(meta).length > 0 ? sql.json(meta) : null},
|
||||
${body.region ?? null},
|
||||
@@ -90,8 +119,7 @@ export const ingest = new Elysia()
|
||||
await sql`INSERT INTO ping_bodies (ping_id, body) VALUES (${ping.id}, ${responseBody})`;
|
||||
}
|
||||
|
||||
const [monitor] = await sql`SELECT account_id FROM monitors WHERE id = ${body.monitor_id}`;
|
||||
if (monitor) publish(monitor.account_id, ping);
|
||||
publish(monitor_check.account_id, ping);
|
||||
|
||||
return { ok: true };
|
||||
}, {
|
||||
|
||||
Reference in New Issue
Block a user