diff --git a/semilabs-status-cf-worker/src/index.js b/semilabs-status-cf-worker/src/index.js index d45676d..4a9d34c 100644 --- a/semilabs-status-cf-worker/src/index.js +++ b/semilabs-status-cf-worker/src/index.js @@ -31,22 +31,22 @@ export default { async fetch(request, env, ctx) { const url = new URL(request.url); const timestamp = Math.floor(Date.now() / 1000); - const recent = timestamp - 7 * 24 * 60 * 60; if (url.pathname === '/api/recent-services.json') { - return env.DB.prepare('SELECT * FROM status WHERE timestamp >= ?') - .bind(recent) - .all() - .then((result) => { - return new Response(JSON.stringify(result.results), { - headers: { 'Content-Type': 'application/json' }, - }); - }) - .catch((err) => { - return new Response(JSON.stringify({ error: err.message }), { - headers: { 'Content-Type': 'application/json' }, - }); - }); + const today = await env.DB.prepare('SELECT * FROM status WHERE timestamp >= ?') + .bind(timestamp - 24 * 60 * 60) + .all(); + const raw_before_today = await env.DB.prepare('SELECT * FROM status WHERE timestamp >= ? AND timestamp < ?') + .bind(timestamp - 7 * 24 * 60 * 60, timestamp - 24 * 60 * 60) + .all(); + const before_today = raw_before_today.results.filter((row) => { + const date = new Date(row.timestamp * 1000); + return date.getUTCMinutes() < 15 || row.stat.startsWith('down'); + }); + + return new Response(JSON.stringify(before_today.concat(today.results)), { + headers: { 'Content-Type': 'application/json' }, + }); } return new Response('Not Found', { status: 404 });