feat: reduce accuracy in previous days

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
sisungo committed 2026-10-18 18:02:09 +08:00
1 parent adae6fdc26
commit 30d1fb3c0b
1 file changed
+14 -14
+14 -14
View File
@@ -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 });