diff --git a/src/db.ts b/src/db.ts index e3bf651..2d1262c 100644 --- a/src/db.ts +++ b/src/db.ts @@ -86,6 +86,15 @@ export async function getUptimeBuckets( return result; } +export async function getLastCheckTime( + db: D1Database, +): Promise { + const row = await db + .prepare("SELECT MAX(timestamp) as ts FROM pings") + .first<{ ts: number | null }>(); + return row?.ts ?? null; +} + export async function pruneOldPings( db: D1Database, days: number, diff --git a/src/index.ts b/src/index.ts index 82af808..574dc8c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,10 @@ export default { return handleIndex(env); } + if (path === "/health") { + return Response.json({ ok: true, timestamp: new Date().toISOString() }); + } + if (path.startsWith("/api/status")) { const res = await handleStatusRoute(env, path); if (res) return res; diff --git a/src/routes/index.ts b/src/routes/index.ts index 552b0cb..0a6c0e7 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,6 +1,6 @@ import type { Env } from "../types"; import { getManifest } from "../manifest"; -import { getLatestPing, getUptime7d } from "../db"; +import { getLatestPing, getUptime7d, getLastCheckTime } from "../db"; import { getDeviceStatus } from "../tailscale"; import { COMMIT_SHA } from "../version"; @@ -29,6 +29,11 @@ export async function handleIndex(env: Env): Promise { }), ); + const lastCheck = await getLastCheckTime(env.DB); + const lastCheckISO = lastCheck + ? new Date(lastCheck * 1000).toISOString() + : null; + const servers = machines.filter((m) => m.type === "server"); const clients = machines.filter((m) => m.type === "client"); @@ -110,7 +115,37 @@ ${clients.length > 0 ? `
${clients.map((m) => `${esc(m.name)}`).join("\n")}
` : ""} - +
${lastCheckISO ? `updated loading` : "no checks yet"}${COMMIT_SHA}
+ `;