diff --git a/server/index.js b/server/index.js index 842ad16..d3aaaa0 100644 --- a/server/index.js +++ b/server/index.js @@ -472,19 +472,21 @@ app.get('/api/history/overview', (req, res) => { const intervalHours = days <= 7 ? 1 : days <= 14 ? 3 : 6; console.log(`Bucketing with ${intervalHours} hour intervals`); - // Simplified query - directly group by time buckets - const intervalSeconds = intervalHours * 3600; + // Use a reliable bucketing approach with Julian Day Numbers + // Convert to julian day, multiply by 24 for hours, divide by intervalHours and round + const bucketCalc = `ROUND((julianday(checked_at) * 24) / ?) * ? / 24`; + recentChecks = db.prepare(` SELECT - datetime((CAST(strftime('%s', checked_at) AS INTEGER) / ?) * ?, 'unixepoch') AS checked_at, + datetime(ROUND((julianday(checked_at) * 24) / ?) * ? / 24) AS checked_at, AVG(CASE WHEN status='up' THEN response_time ELSE NULL END) AS avg_up_response, SUM(CASE WHEN status='up' THEN 1 ELSE 0 END) AS up_count, COUNT(*) AS total_count FROM checks WHERE resource_id = ? AND checked_at > datetime('now', ?) - GROUP BY (CAST(strftime('%s', checked_at) AS INTEGER) / ?) + GROUP BY ROUND((julianday(checked_at) * 24) / ?) * ? / 24 ORDER BY checked_at ASC - `).all(intervalSeconds, intervalSeconds, resource.id, `-${days} days`, intervalSeconds).map(row => ({ + `).all(intervalHours, intervalHours, intervalHours, intervalHours, resource.id, `-${days} days`, intervalHours, intervalHours).map(row => ({ status: row.up_count >= Math.ceil(row.total_count/2) ? 'up' : 'down', response_time: Math.round(row.avg_up_response || 0), checked_at: row.checked_at,