From 407f14bf0138c69df880357c6a7a459e06191607 Mon Sep 17 00:00:00 2001 From: lelo Date: Wed, 2 Apr 2025 07:22:44 +0200 Subject: [PATCH] fix time bucket order --- analytics.py | 8 ++++---- templates/dashboard.html | 31 ++++++++++++++++--------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/analytics.py b/analytics.py index 51f3ab4..3991f0e 100644 --- a/analytics.py +++ b/analytics.py @@ -158,9 +158,9 @@ def dashboard(): # 2. Distinct device trend # We'll group by hour if "today", by day if "7days"/"30days", by month if "365days" if timeframe == 'last24hours': - # Group by hour: substr(timestamp, 12, 2) -> HH + # Group by hour query = f''' - SELECT substr(timestamp, 12, 2) AS bucket, COUNT(DISTINCT device_id) AS count + SELECT substr(timestamp, 1, 13) AS bucket, COUNT(DISTINCT device_id) AS count FROM file_access_log WHERE timestamp >= ? {filetype_filter_sql} GROUP BY bucket @@ -203,9 +203,9 @@ def dashboard(): # 3. Timeframe-based aggregation # We'll group by hour if "today", by day if "7days"/"30days", by month if "365days". if timeframe == 'last24hours': - # Hour: substr(timestamp, 12, 2) -> HH + # Group by Hour query = f''' - SELECT substr(timestamp, 12, 2) AS bucket, COUNT(*) AS count + SELECT substr(timestamp, 1, 13) AS bucket, COUNT(*) AS count FROM file_access_log WHERE timestamp >= ? {filetype_filter_sql} GROUP BY bucket diff --git a/templates/dashboard.html b/templates/dashboard.html index 933caca..c2bc62c 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -180,28 +180,29 @@ const timeframe = "{{ timeframe }}"; // e.g., 'last24hours', '7days', '30days', or '365days' const shiftedLabels = timeframeData.map(item => { if (timeframe === 'last24hours') { - // For 'last24hours', the bucket is an hour in UTC (e.g., "14") - const utcHour = parseInt(item.bucket, 10); + // item.bucket will be something like "2025-04-01T15" + const bucketDate = new Date(item.bucket + ":00:00"); // Convert to a full datetime by appending minutes and seconds. const now = new Date(); - // Create Date objects for the start and end of the hour in UTC - const utcStart = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), utcHour)); - const utcEnd = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), utcHour + 1)); - // Convert to local time strings, e.g., "16:00" - const localStart = utcStart.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); - const localEnd = utcEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); - return `${localStart} - ${localEnd}`; + + // Check if this bucket corresponds to the current hour + const isCurrentHour = + bucketDate.getFullYear() === now.getFullYear() && + bucketDate.getMonth() === now.getMonth() && + bucketDate.getDate() === now.getDate() && + bucketDate.getHours() === now.getHours(); + + // If it is the current hour, use the current time as the end; otherwise, add one hour. + const bucketEnd = isCurrentHour ? now : new Date(bucketDate.getTime() + 3600 * 1000); + + return `${bucketDate.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} - ${bucketEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`; } else if (timeframe === '7days' || timeframe === '30days') { - // For these timeframes, the bucket is a date in the format "YYYY-MM-DD" - const utcDate = new Date(item.bucket + 'T00:00:00Z'); - return utcDate.toLocaleDateString(); // Adjust formatting as needed + const localDate = new Date(item.bucket); + return localDate.toLocaleDateString(); } else if (timeframe === '365days') { - // For this timeframe, the bucket is a month in the format "YYYY-MM" const [year, month] = item.bucket.split('-'); const dateObj = new Date(year, month - 1, 1); - // Format to something like "Mar 2025" return dateObj.toLocaleString([], { month: 'short', year: 'numeric' }); } else { - // Fallback: use the bucket value as-is return item.bucket; } });