feat(stats): monthly traffic/IP/file count stats displayed on status page
This commit is contained in:
25
database.py
25
database.py
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user