feat: filter pings

This commit is contained in:
2026-03-26 12:20:09 +04:00
parent 091096aa11
commit 7b5411ab64
3 changed files with 149 additions and 21 deletions
+32 -2
View File
@@ -149,10 +149,40 @@ export const monitors = new Elysia({ prefix: "/monitors" })
SELECT id FROM monitors WHERE id = ${params.id} AND account_id = ${accountId}
`;
if (!monitor) { set.status = 404; return { error: "Not found" }; }
const limit = Math.min(Number(query.limit) || 100, 1000);
const filter = query.filter; // "up", "down", "events", or undefined/all
const before = query.before; // cursor: ISO timestamp for pagination
const cursorClause = before ? sql`AND checked_at < ${new Date(before)}` : sql``;
if (filter === "up") {
return sql`
SELECT * FROM pings WHERE monitor_id = ${params.id} AND up = true ${cursorClause}
ORDER BY checked_at DESC LIMIT ${limit}
`;
}
if (filter === "down") {
return sql`
SELECT * FROM pings WHERE monitor_id = ${params.id} AND up = false ${cursorClause}
ORDER BY checked_at DESC LIMIT ${limit}
`;
}
if (filter === "events") {
// State changes: pings where `up` differs from the previous ping's `up` for this monitor
return sql`
SELECT * FROM (
SELECT *, LAG(up) OVER (ORDER BY checked_at) AS prev_up
FROM pings WHERE monitor_id = ${params.id}
) t
WHERE prev_up IS NULL OR up != prev_up
${before ? sql`AND checked_at < ${new Date(before)}` : sql``}
ORDER BY checked_at DESC LIMIT ${limit}
`;
}
return sql`
SELECT * FROM pings
WHERE monitor_id = ${params.id}
SELECT * FROM pings WHERE monitor_id = ${params.id} ${cursorClause}
ORDER BY checked_at DESC LIMIT ${limit}
`;
}, { detail: { summary: "Get ping history", tags: ["monitors"] } });