feat: add admin usage statistics and LLM configuration management

This commit is contained in:
2026-03-12 16:30:24 +08:00
parent 202f2fa83b
commit 7f25dd09f6
16 changed files with 757 additions and 300 deletions

View File

@@ -14,9 +14,9 @@ from core.security import (
decode_access_token
)
from db.database import get_db
from db.crud import get_user_by_username, get_user_by_email, create_user, change_user_password, update_user_aliyun_key, get_user_preferences, update_user_preferences, can_user_use_local_model, update_user_llm_config
from schemas.user import User, UserCreate, Token, PasswordChange, AliyunKeyUpdate, AliyunKeyVerifyResponse, UserPreferences, UserPreferencesResponse
from schemas.audiobook import LLMConfigUpdate, LLMConfigResponse
from db.crud import get_user_by_username, get_user_by_email, create_user, change_user_password, get_user_preferences, update_user_preferences, can_user_use_local_model, get_system_setting
from schemas.user import User, UserCreate, Token, PasswordChange, AliyunKeyVerifyResponse, UserPreferences, UserPreferencesResponse
from schemas.audiobook import LLMConfigResponse
router = APIRouter(prefix="/auth", tags=["authentication"])
@@ -137,70 +137,6 @@ async def change_password(
return user
@router.post("/aliyun-key", response_model=User)
@limiter.limit("5/minute")
async def set_aliyun_key(
request: Request,
key_data: AliyunKeyUpdate,
current_user: Annotated[User, Depends(get_current_user)],
db: Session = Depends(get_db)
):
from core.security import encrypt_api_key
from core.tts_service import AliyunTTSBackend
api_key = key_data.api_key.strip()
aliyun_backend = AliyunTTSBackend(api_key=api_key, region=settings.ALIYUN_REGION)
health = await aliyun_backend.health_check()
if not health.get("available", False):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid Aliyun API key. Please check your API key and try again."
)
encrypted_key = encrypt_api_key(api_key)
user = update_user_aliyun_key(
db,
user_id=current_user.id,
encrypted_api_key=encrypted_key
)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return user
@router.delete("/aliyun-key")
@limiter.limit("5/minute")
async def delete_aliyun_key(
request: Request,
current_user: Annotated[User, Depends(get_current_user)],
db: Session = Depends(get_db)
):
user = update_user_aliyun_key(
db,
user_id=current_user.id,
encrypted_api_key=None
)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
prefs = get_user_preferences(db, current_user.id)
if prefs.get("default_backend") == "aliyun":
prefs["default_backend"] = "local"
update_user_preferences(db, current_user.id, prefs)
return {"message": "Aliyun API key deleted", "preferences_updated": True}
@router.get("/aliyun-key/verify", response_model=AliyunKeyVerifyResponse)
@limiter.limit("10/minute")
async def verify_aliyun_key(
@@ -211,33 +147,20 @@ async def verify_aliyun_key(
from core.security import decrypt_api_key
from core.tts_service import AliyunTTSBackend
if not current_user.aliyun_api_key:
return AliyunKeyVerifyResponse(
valid=False,
message="No Aliyun API key configured"
)
api_key = decrypt_api_key(current_user.aliyun_api_key)
encrypted = get_system_setting(db, "aliyun_api_key")
if not encrypted:
return AliyunKeyVerifyResponse(valid=False, message="No Aliyun API key configured")
api_key = decrypt_api_key(encrypted)
if not api_key:
return AliyunKeyVerifyResponse(
valid=False,
message="Failed to decrypt API key"
)
return AliyunKeyVerifyResponse(valid=False, message="Failed to decrypt API key")
aliyun_backend = AliyunTTSBackend(api_key=api_key, region=settings.ALIYUN_REGION)
health = await aliyun_backend.health_check()
if health.get("available", False):
return AliyunKeyVerifyResponse(
valid=True,
message="Aliyun API key is valid and working"
)
else:
return AliyunKeyVerifyResponse(
valid=False,
message="Aliyun API key is not working. Please check your API key."
)
return AliyunKeyVerifyResponse(valid=True, message="Aliyun API key is valid and working")
return AliyunKeyVerifyResponse(valid=False, message="Aliyun API key is not working.")
@router.get("/preferences", response_model=UserPreferencesResponse)
@limiter.limit("30/minute")
@@ -288,61 +211,15 @@ async def update_preferences(
return {"message": "Preferences updated successfully"}
@router.put("/llm-config")
@limiter.limit("10/minute")
async def set_llm_config(
request: Request,
config: LLMConfigUpdate,
current_user: Annotated[User, Depends(get_current_user)],
db: Session = Depends(get_db)
):
from core.security import encrypt_api_key
from core.llm_service import LLMService
api_key = config.api_key.strip()
base_url = config.base_url.strip()
model = config.model.strip()
# Validate LLM config by sending a test request
llm = LLMService(base_url=base_url, api_key=api_key, model=model)
try:
await llm.chat("You are a test assistant.", "Reply with 'ok'.")
except Exception as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"LLM API validation failed: {e}"
)
encrypted_key = encrypt_api_key(api_key)
update_user_llm_config(
db,
user_id=current_user.id,
llm_api_key=encrypted_key,
llm_base_url=base_url,
llm_model=model,
)
return {"message": "LLM config updated successfully"}
@router.get("/llm-config", response_model=LLMConfigResponse)
@limiter.limit("30/minute")
async def get_llm_config(
request: Request,
current_user: Annotated[User, Depends(get_current_user)],
):
return LLMConfigResponse(
base_url=current_user.llm_base_url,
model=current_user.llm_model,
has_key=bool(current_user.llm_api_key),
)
@router.delete("/llm-config")
@limiter.limit("10/minute")
async def delete_llm_config(
request: Request,
current_user: Annotated[User, Depends(get_current_user)],
db: Session = Depends(get_db)
):
update_user_llm_config(db, user_id=current_user.id, clear=True)
return {"message": "LLM config deleted"}
return LLMConfigResponse(
base_url=get_system_setting(db, "llm_base_url"),
model=get_system_setting(db, "llm_model"),
has_key=bool(get_system_setting(db, "llm_api_key")),
)