Initial commit: 000-platform project skeleton
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
45
backend/app/routers/stats.py
Normal file
45
backend/app/routers/stats.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""统计上报路由"""
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..config import get_settings
|
||||
from ..models.stats import AICallEvent
|
||||
from ..schemas.stats import AICallEventCreate, AICallEventResponse, BatchReportRequest
|
||||
|
||||
router = APIRouter(prefix="/api/stats", tags=["stats"])
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
def verify_api_key(x_api_key: str = Header(..., alias="X-API-Key")):
|
||||
"""验证API Key"""
|
||||
if x_api_key != settings.API_KEY:
|
||||
raise HTTPException(status_code=401, detail="Invalid API Key")
|
||||
return x_api_key
|
||||
|
||||
|
||||
@router.post("/report", response_model=AICallEventResponse)
|
||||
async def report_ai_call(
|
||||
event: AICallEventCreate,
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(verify_api_key)
|
||||
):
|
||||
"""上报AI调用事件"""
|
||||
db_event = AICallEvent(**event.model_dump())
|
||||
db.add(db_event)
|
||||
db.commit()
|
||||
db.refresh(db_event)
|
||||
return db_event
|
||||
|
||||
|
||||
@router.post("/report/batch")
|
||||
async def batch_report_ai_calls(
|
||||
request: BatchReportRequest,
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(verify_api_key)
|
||||
):
|
||||
"""批量上报AI调用事件"""
|
||||
events = [AICallEvent(**e.model_dump()) for e in request.events]
|
||||
db.add_all(events)
|
||||
db.commit()
|
||||
return {"success": True, "count": len(events)}
|
||||
Reference in New Issue
Block a user