Fix SQL bucketing using Julian Day calculation

This commit is contained in:
nmemmert committed 2025-12-15 12:22:57 -05:00
1 parent b1b1c36839
commit 234ac59fe8
1 file changed
+7 -5
+7 -5
View File
@@ -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,