fix time bucket order
This commit is contained in:
parent
f5ed562ea9
commit
407f14bf01
@ -158,9 +158,9 @@ def dashboard():
|
|||||||
# 2. Distinct device trend
|
# 2. Distinct device trend
|
||||||
# We'll group by hour if "today", by day if "7days"/"30days", by month if "365days"
|
# We'll group by hour if "today", by day if "7days"/"30days", by month if "365days"
|
||||||
if timeframe == 'last24hours':
|
if timeframe == 'last24hours':
|
||||||
# Group by hour: substr(timestamp, 12, 2) -> HH
|
# Group by hour
|
||||||
query = f'''
|
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
|
FROM file_access_log
|
||||||
WHERE timestamp >= ? {filetype_filter_sql}
|
WHERE timestamp >= ? {filetype_filter_sql}
|
||||||
GROUP BY bucket
|
GROUP BY bucket
|
||||||
@ -203,9 +203,9 @@ def dashboard():
|
|||||||
# 3. Timeframe-based aggregation
|
# 3. Timeframe-based aggregation
|
||||||
# We'll group by hour if "today", by day if "7days"/"30days", by month if "365days".
|
# We'll group by hour if "today", by day if "7days"/"30days", by month if "365days".
|
||||||
if timeframe == 'last24hours':
|
if timeframe == 'last24hours':
|
||||||
# Hour: substr(timestamp, 12, 2) -> HH
|
# Group by Hour
|
||||||
query = f'''
|
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
|
FROM file_access_log
|
||||||
WHERE timestamp >= ? {filetype_filter_sql}
|
WHERE timestamp >= ? {filetype_filter_sql}
|
||||||
GROUP BY bucket
|
GROUP BY bucket
|
||||||
|
|||||||
@ -180,28 +180,29 @@
|
|||||||
const timeframe = "{{ timeframe }}"; // e.g., 'last24hours', '7days', '30days', or '365days'
|
const timeframe = "{{ timeframe }}"; // e.g., 'last24hours', '7days', '30days', or '365days'
|
||||||
const shiftedLabels = timeframeData.map(item => {
|
const shiftedLabels = timeframeData.map(item => {
|
||||||
if (timeframe === 'last24hours') {
|
if (timeframe === 'last24hours') {
|
||||||
// For 'last24hours', the bucket is an hour in UTC (e.g., "14")
|
// item.bucket will be something like "2025-04-01T15"
|
||||||
const utcHour = parseInt(item.bucket, 10);
|
const bucketDate = new Date(item.bucket + ":00:00"); // Convert to a full datetime by appending minutes and seconds.
|
||||||
const now = new Date();
|
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));
|
// Check if this bucket corresponds to the current hour
|
||||||
const utcEnd = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), utcHour + 1));
|
const isCurrentHour =
|
||||||
// Convert to local time strings, e.g., "16:00"
|
bucketDate.getFullYear() === now.getFullYear() &&
|
||||||
const localStart = utcStart.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
bucketDate.getMonth() === now.getMonth() &&
|
||||||
const localEnd = utcEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
bucketDate.getDate() === now.getDate() &&
|
||||||
return `${localStart} - ${localEnd}`;
|
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') {
|
} else if (timeframe === '7days' || timeframe === '30days') {
|
||||||
// For these timeframes, the bucket is a date in the format "YYYY-MM-DD"
|
const localDate = new Date(item.bucket);
|
||||||
const utcDate = new Date(item.bucket + 'T00:00:00Z');
|
return localDate.toLocaleDateString();
|
||||||
return utcDate.toLocaleDateString(); // Adjust formatting as needed
|
|
||||||
} else if (timeframe === '365days') {
|
} else if (timeframe === '365days') {
|
||||||
// For this timeframe, the bucket is a month in the format "YYYY-MM"
|
|
||||||
const [year, month] = item.bucket.split('-');
|
const [year, month] = item.bucket.split('-');
|
||||||
const dateObj = new Date(year, month - 1, 1);
|
const dateObj = new Date(year, month - 1, 1);
|
||||||
// Format to something like "Mar 2025"
|
|
||||||
return dateObj.toLocaleString([], { month: 'short', year: 'numeric' });
|
return dateObj.toLocaleString([], { month: 'short', year: 'numeric' });
|
||||||
} else {
|
} else {
|
||||||
// Fallback: use the bucket value as-is
|
|
||||||
return item.bucket;
|
return item.bucket;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user