Complete admin dashboard with user management, config and settings tabs

This commit is contained in:
2025-11-30 22:44:10 +01:00
parent d31a132808
commit 80318a8d43
5 changed files with 840 additions and 398 deletions

107
main.py
View File

@@ -1005,6 +1005,113 @@ async def get_tracked_files(is_admin: bool = Depends(require_admin)):
}
@app.get("/admin/users")
async def get_admin_users(is_admin: bool = Depends(require_admin)):
"""Get all users with their usage stats (requires admin auth)"""
from services.auth_service import load_users
from models.subscription import PLANS
users_data = load_users()
users_list = []
for user_id, user_data in users_data.items():
plan = user_data.get("plan", "free")
plan_info = PLANS.get(plan, PLANS["free"])
users_list.append({
"id": user_id,
"email": user_data.get("email", ""),
"name": user_data.get("name", ""),
"plan": plan,
"subscription_status": user_data.get("subscription_status", "active"),
"docs_translated_this_month": user_data.get("docs_translated_this_month", 0),
"pages_translated_this_month": user_data.get("pages_translated_this_month", 0),
"extra_credits": user_data.get("extra_credits", 0),
"created_at": user_data.get("created_at", ""),
"plan_limits": {
"docs_per_month": plan_info.get("docs_per_month", 0),
"max_pages_per_doc": plan_info.get("max_pages_per_doc", 0),
}
})
# Sort by created_at descending (newest first)
users_list.sort(key=lambda x: x.get("created_at", ""), reverse=True)
return {
"total": len(users_list),
"users": users_list
}
@app.get("/admin/stats")
async def get_admin_stats(is_admin: bool = Depends(require_admin)):
"""Get comprehensive admin statistics (requires admin auth)"""
from services.auth_service import load_users
from models.subscription import PLANS
users_data = load_users()
# Calculate stats
total_users = len(users_data)
plan_distribution = {}
total_docs_translated = 0
total_pages_translated = 0
active_users = 0 # Users who translated something this month
for user_data in users_data.values():
plan = user_data.get("plan", "free")
plan_distribution[plan] = plan_distribution.get(plan, 0) + 1
docs = user_data.get("docs_translated_this_month", 0)
pages = user_data.get("pages_translated_this_month", 0)
total_docs_translated += docs
total_pages_translated += pages
if docs > 0:
active_users += 1
# Get cache stats
cache_stats = _translation_cache.get_stats()
return {
"users": {
"total": total_users,
"active_this_month": active_users,
"by_plan": plan_distribution
},
"translations": {
"docs_this_month": total_docs_translated,
"pages_this_month": total_pages_translated
},
"cache": cache_stats,
"config": {
"translation_service": config.TRANSLATION_SERVICE,
"max_file_size_mb": config.MAX_FILE_SIZE_MB,
"supported_extensions": list(config.SUPPORTED_EXTENSIONS)
}
}
@app.post("/admin/config/provider")
async def update_default_provider(
provider: str = Form(...),
is_admin: bool = Depends(require_admin)
):
"""Update the default translation provider (requires admin auth)"""
valid_providers = ["google", "openrouter", "ollama", "deepl", "libre", "openai"]
if provider not in valid_providers:
raise HTTPException(status_code=400, detail=f"Invalid provider. Must be one of: {valid_providers}")
# Update config (in production, this would persist to database/env)
config.TRANSLATION_SERVICE = provider
return {
"status": "success",
"message": f"Default provider updated to {provider}",
"provider": provider
}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)