diff --git a/analytics.py b/analytics.py index cf3ac60..889b7b7 100644 --- a/analytics.py +++ b/analytics.py @@ -162,7 +162,7 @@ def dashboard(): if session['timeframe'] == 'last24hours': # Group by hour: substr(timestamp, 12, 2) -> HH 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 @@ -207,7 +207,7 @@ def dashboard(): if session['timeframe'] == 'last24hours': # Hour: substr(timestamp, 12, 2) -> HH 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 6bb385b..b486d4c 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -216,15 +216,22 @@ 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 local time (e.g., "14") - const localHour = 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(); - // Use local date components since the timestamp is already local - const localStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), localHour); - const localEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), localHour + 1); - return `${localStart.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} - ${localEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`; + + // 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, assume the bucket is already in local date format const localDate = new Date(item.bucket); return localDate.toLocaleDateString(); } else if (timeframe === '365days') {