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
This commit is contained in:
kaitranntt
2025-12-13 00:55:06 -05:00
parent 48bf5ab76d
commit 9fd0c1cc07
+21 -14
View File
@@ -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');