fix time bucket order

This commit is contained in:
lelo 2025-04-02 07:22:44 +02:00
parent f5ed562ea9
commit 407f14bf01
2 changed files with 20 additions and 19 deletions

View File

@ -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

View File

@ -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;
}
});