Spaces:
Paused
Paused
Update admin_routes.py
Browse files- admin_routes.py +41 -2
admin_routes.py
CHANGED
|
@@ -13,15 +13,54 @@ import bcrypt
|
|
| 13 |
from datetime import datetime, timedelta
|
| 14 |
from typing import Dict, List, Optional, Any
|
| 15 |
from pathlib import Path
|
| 16 |
-
|
| 17 |
from fastapi import APIRouter, HTTPException, Depends, Header
|
| 18 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 19 |
from pydantic import BaseModel, Field
|
| 20 |
import jwt
|
| 21 |
-
|
| 22 |
from utils import log
|
| 23 |
from config_provider import ConfigProvider
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# ===================== Dynamic Config Loading =====================
|
| 26 |
def get_jwt_config():
|
| 27 |
"""Get JWT configuration based on work_mode"""
|
|
|
|
| 13 |
from datetime import datetime, timedelta
|
| 14 |
from typing import Dict, List, Optional, Any
|
| 15 |
from pathlib import Path
|
|
|
|
| 16 |
from fastapi import APIRouter, HTTPException, Depends, Header
|
| 17 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 18 |
from pydantic import BaseModel, Field
|
| 19 |
import jwt
|
|
|
|
| 20 |
from utils import log
|
| 21 |
from config_provider import ConfigProvider
|
| 22 |
|
| 23 |
+
# Activity log retention policy (keep last 30 days)
|
| 24 |
+
ACTIVITY_LOG_RETENTION_DAYS = 30
|
| 25 |
+
ACTIVITY_LOG_MAX_ENTRIES = 10000
|
| 26 |
+
|
| 27 |
+
async def cleanup_activity_log():
|
| 28 |
+
"""Cleanup old activity log entries"""
|
| 29 |
+
while True:
|
| 30 |
+
try:
|
| 31 |
+
config = load_config()
|
| 32 |
+
|
| 33 |
+
if "activity_log" in config:
|
| 34 |
+
# Calculate cutoff date
|
| 35 |
+
cutoff_date = datetime.utcnow() - timedelta(days=ACTIVITY_LOG_RETENTION_DAYS)
|
| 36 |
+
|
| 37 |
+
# Filter recent entries
|
| 38 |
+
original_count = len(config["activity_log"])
|
| 39 |
+
config["activity_log"] = [
|
| 40 |
+
log for log in config["activity_log"]
|
| 41 |
+
if datetime.fromisoformat(log["timestamp"].replace("Z", "+00:00")) > cutoff_date
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
# Also limit by max entries
|
| 45 |
+
if len(config["activity_log"]) > ACTIVITY_LOG_MAX_ENTRIES:
|
| 46 |
+
config["activity_log"] = config["activity_log"][-ACTIVITY_LOG_MAX_ENTRIES:]
|
| 47 |
+
|
| 48 |
+
# Save if anything was removed
|
| 49 |
+
removed_count = original_count - len(config["activity_log"])
|
| 50 |
+
if removed_count > 0:
|
| 51 |
+
save_config(config)
|
| 52 |
+
log(f"🧹 Cleaned up {removed_count} old activity log entries")
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
log(f"❌ Activity log cleanup error: {e}")
|
| 56 |
+
|
| 57 |
+
# Run cleanup once per day
|
| 58 |
+
await asyncio.sleep(86400) # 24 hours
|
| 59 |
+
|
| 60 |
+
# Start cleanup task when module loads
|
| 61 |
+
def start_cleanup_task():
|
| 62 |
+
asyncio.create_task(cleanup_activity_log())
|
| 63 |
+
|
| 64 |
# ===================== Dynamic Config Loading =====================
|
| 65 |
def get_jwt_config():
|
| 66 |
"""Get JWT configuration based on work_mode"""
|