diff --git a/analytics.py b/analytics.py index 6c2a468..24981fe 100644 --- a/analytics.py +++ b/analytics.py @@ -155,20 +155,49 @@ def dashboard(): cursor = log_db.execute(query, params_for_filter) rows = cursor.fetchall() - # 2. Daily access trend (line chart) - # We'll group by day using substr(timestamp, 1, 10) -> YYYY-MM-DD - query = f''' - SELECT substr(timestamp, 1, 10) AS date, COUNT(*) AS count - FROM file_access_log - WHERE timestamp >= ? {filetype_filter_sql} - GROUP BY date - ORDER BY date - ''' + # 2. Distinct device trend + # We'll group by hour if "today", by day if "7days"/"30days", by month if "365days" + if timeframe == 'today': + # Group by hour: substr(timestamp, 12, 2) -> HH + query = f''' + SELECT substr(timestamp, 12, 2) AS bucket, COUNT(DISTINCT device_id) AS count + FROM file_access_log + WHERE timestamp >= ? {filetype_filter_sql} + GROUP BY bucket + ORDER BY bucket + ''' + elif timeframe in ('7days', '30days'): + # Group by day: substr(timestamp, 1, 10) -> YYYY-MM-DD + query = f''' + SELECT substr(timestamp, 1, 10) AS bucket, COUNT(DISTINCT device_id) AS count + FROM file_access_log + WHERE timestamp >= ? {filetype_filter_sql} + GROUP BY bucket + ORDER BY bucket + ''' + elif timeframe == '365days': + # Group by month: substr(timestamp, 1, 7) -> YYYY-MM + query = f''' + SELECT substr(timestamp, 1, 7) AS bucket, COUNT(DISTINCT device_id) AS count + FROM file_access_log + WHERE timestamp >= ? {filetype_filter_sql} + GROUP BY bucket + ORDER BY bucket + ''' + else: + # Default: group by day + query = f''' + SELECT substr(timestamp, 1, 10) AS bucket, COUNT(DISTINCT device_id) AS count + FROM file_access_log + WHERE timestamp >= ? {filetype_filter_sql} + GROUP BY bucket + ORDER BY bucket + ''' with log_db: cursor = log_db.execute(query, params_for_filter) - daily_rows = cursor.fetchall() - daily_access_data = [ - dict(date=r[0], count=r[1]) for r in daily_rows + distinct_device_data_rows = cursor.fetchall() + distinct_device_data = [ + dict(bucket=r[0], count=r[1]) for r in distinct_device_data_rows ] # 3. Timeframe-based aggregation @@ -322,7 +351,7 @@ def dashboard(): "dashboard.html", timeframe=timeframe, rows=rows, - daily_access_data=daily_access_data, + distinct_device_data=distinct_device_data, user_agent_data=user_agent_data, folder_data=folder_data, location_data=location_data, diff --git a/templates/dashboard.html b/templates/dashboard.html index 375270a..7dee0bd 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -70,8 +70,8 @@
-
Downloads
- +
eindeutige Nutzer nach Zeit
+
@@ -80,7 +80,7 @@
Downloads nach Zeit
- +
@@ -170,7 +170,7 @@