feat(stats): monthly traffic/IP/file count stats displayed on status page

This commit is contained in:
OpenCode Bot
2026-05-11 22:11:11 +08:00
parent 9c7f8f0f62
commit 4561aec7e0
3 changed files with 64 additions and 2 deletions

View File

@@ -119,3 +119,28 @@ def is_traffic_exceeded(ip, additional_bytes, direction='upload'):
else:
total += additional_bytes
return total > DAILY_TRAFFIC_LIMIT
def get_monthly_stats():
now = datetime.utcnow()
month_start = now.strftime('%Y-%m-01')
conn = get_db()
traffic_row = conn.execute(
'SELECT COALESCE(SUM(upload_bytes),0) AS up, COALESCE(SUM(download_bytes),0) AS down FROM ip_traffic WHERE date >= ?',
(month_start,)
).fetchone()
ip_count = conn.execute(
'SELECT COUNT(DISTINCT ip) FROM ip_traffic WHERE date >= ?',
(month_start,)
).fetchone()[0]
file_count = conn.execute(
'SELECT COUNT(*) FROM files WHERE created_at >= ?',
(month_start,)
).fetchone()[0]
conn.close()
total = (traffic_row['up'] or 0) + (traffic_row['down'] or 0)
return {
'total_bytes': total,
'total_gb': round(total / (1024**3), 2),
'ip_count': ip_count,
'file_count': file_count,
}