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 committed 2025-12-13 00:55:06 -05:00
1 parent 48bf5ab76d
commit 9fd0c1cc07
1 file changed
+21 -14
+21 -14
View File
@@ -896,37 +896,44 @@ function fillHourlyGaps(
// Create a map of existing hours for O(1) lookup // Create a map of existing hours for O(1) lookup
const hourMap = new Map(data.map((d) => [d.hour, d])); 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 now = new Date();
const startDate = since const startDate = since
? new Date( ? new Date(
parseInt(since.slice(0, 4)), Date.UTC(
parseInt(since.slice(4, 6)) - 1, parseInt(since.slice(0, 4)),
parseInt(since.slice(6, 8)), parseInt(since.slice(4, 6)) - 1,
0, parseInt(since.slice(6, 8)),
0, 0,
0 0,
0
)
) )
: new Date(now.getTime() - 24 * 60 * 60 * 1000); // Default: 24 hours ago : new Date(now.getTime() - 24 * 60 * 60 * 1000); // Default: 24 hours ago
const endDate = until const endDate = until
? new Date( ? new Date(
parseInt(until.slice(0, 4)), Date.UTC(
parseInt(until.slice(4, 6)) - 1, parseInt(until.slice(0, 4)),
parseInt(until.slice(6, 8)), parseInt(until.slice(4, 6)) - 1,
23, parseInt(until.slice(6, 8)),
59, 23,
59 59,
59
)
) )
: now; : now;
// Cap endDate at current time to avoid filling future hours with zeros
const cappedEndDate = endDate > now ? now : endDate;
const result: typeof data = []; const result: typeof data = [];
// Iterate through each hour in the range // Iterate through each hour in the range
const current = new Date(startDate); const current = new Date(startDate);
current.setMinutes(0, 0, 0); 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 // Format hour key as "YYYY-MM-DD HH:00" in UTC to match storage format
const year = current.getUTCFullYear(); const year = current.getUTCFullYear();
const month = String(current.getUTCMonth() + 1).padStart(2, '0'); const month = String(current.getUTCMonth() + 1).padStart(2, '0');