Fix 24h bar timezone mismatch and add time-interval uptime calculation

Bars were all grey for 24h view because SQL bucket keys (UTC) never
matched frontend keys (local timezone). Added toUTCKey() helper to
generate UTC keys for all ranges. Replaced check-counting uptime with
time-interval method for 24h/7d: time between consecutive up checks
counts as uptime, intervals involving down checks count as downtime,
uncovered time is unknown.
This commit is contained in:
Shivam Patel
2026-02-09 04:15:34 -05:00
parent d1fd790533
commit e47a719d79
2 changed files with 100 additions and 60 deletions

View File

@@ -59,6 +59,16 @@ const RANGE_CONFIG: Record<TimeRange, { bars: number; bucketMs: number }> = {
// --- Helpers ---
function toUTCKey(d: Date, mode: 'hour' | 'day' | 'month'): string {
const Y = d.getUTCFullYear();
const M = String(d.getUTCMonth() + 1).padStart(2, '0');
if (mode === 'month') return `${Y}-${M}`;
const D = String(d.getUTCDate()).padStart(2, '0');
if (mode === 'day') return `${Y}-${M}-${D}`;
const H = String(d.getUTCHours()).padStart(2, '0');
return `${Y}-${M}-${D} ${H}:00`;
}
function bucketStatus(upCount: number, total: number): BarStatus {
if (total === 0) return 'unknown';
if (upCount === total) return 'up';
@@ -81,7 +91,7 @@ function buildBars(range: TimeRange, history: HistoryBucket[] | undefined): BarD
if (range === '24h') {
for (let i = config.bars - 1; i >= 0; i--) {
const d = subHours(now, i);
const key = format(d, 'yyyy-MM-dd HH') + ':00';
const key = toUTCKey(d, 'hour');
const h = bucketMap.get(key);
const hourStart = new Date(d);
hourStart.setMinutes(0, 0, 0);
@@ -95,7 +105,7 @@ function buildBars(range: TimeRange, history: HistoryBucket[] | undefined): BarD
} else if (range === '7d' || range === '30d') {
for (let i = config.bars - 1; i >= 0; i--) {
const d = subDays(now, i);
const key = format(d, 'yyyy-MM-dd');
const key = toUTCKey(d, 'day');
const h = bucketMap.get(key);
bars.push({
label: format(d, 'MMM d, yyyy'),
@@ -108,7 +118,7 @@ function buildBars(range: TimeRange, history: HistoryBucket[] | undefined): BarD
// 365d — monthly buckets
for (let i = 11; i >= 0; i--) {
const d = subMonths(now, i);
const key = format(d, 'yyyy-MM');
const key = toUTCKey(d, 'month');
const h = bucketMap.get(key);
bars.push({
label: format(d, 'MMM yyyy'),