From 9fd0c1cc074c2d14b6978aba001b3b6552b06642 Mon Sep 17 00:00:00 2001 From: kaitranntt Date: Sat, 13 Dec 2025 00:55:06 -0500 Subject: [PATCH] fix(analytics): use UTC dates and cap hourly chart at current time - use Date.UTC() for consistent timezone handling in fillHourlyGaps - cap endDate at current time to avoid filling future hours with zeros - fixes chart showing zeros extending beyond current moment --- src/web-server/usage-routes.ts | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/web-server/usage-routes.ts b/src/web-server/usage-routes.ts index ebd058b7..2f255b62 100644 --- a/src/web-server/usage-routes.ts +++ b/src/web-server/usage-routes.ts @@ -896,37 +896,44 @@ function fillHourlyGaps( // Create a map of existing hours for O(1) lookup const hourMap = new Map(data.map((d) => [d.hour, d])); - // Determine the hour range + // Determine the hour range (use UTC to match stored hour keys) const now = new Date(); const startDate = since ? new Date( - parseInt(since.slice(0, 4)), - parseInt(since.slice(4, 6)) - 1, - parseInt(since.slice(6, 8)), - 0, - 0, - 0 + Date.UTC( + parseInt(since.slice(0, 4)), + parseInt(since.slice(4, 6)) - 1, + parseInt(since.slice(6, 8)), + 0, + 0, + 0 + ) ) : new Date(now.getTime() - 24 * 60 * 60 * 1000); // Default: 24 hours ago const endDate = until ? new Date( - parseInt(until.slice(0, 4)), - parseInt(until.slice(4, 6)) - 1, - parseInt(until.slice(6, 8)), - 23, - 59, - 59 + Date.UTC( + parseInt(until.slice(0, 4)), + parseInt(until.slice(4, 6)) - 1, + parseInt(until.slice(6, 8)), + 23, + 59, + 59 + ) ) : now; + // Cap endDate at current time to avoid filling future hours with zeros + const cappedEndDate = endDate > now ? now : endDate; + const result: typeof data = []; // Iterate through each hour in the range const current = new Date(startDate); current.setMinutes(0, 0, 0); - while (current <= endDate) { + while (current <= cappedEndDate) { // Format hour key as "YYYY-MM-DD HH:00" in UTC to match storage format const year = current.getUTCFullYear(); const month = String(current.getUTCMonth() + 1).padStart(2, '0');