fix: add GET endpoints for stats summary and logs query
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:
@@ -1,16 +1,27 @@
|
||||
"""日志路由"""
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import desc
|
||||
|
||||
from ..database import get_db
|
||||
from ..config import get_settings
|
||||
from ..models.logs import PlatformLog
|
||||
from ..schemas.logs import LogCreate, LogResponse, BatchLogRequest
|
||||
from ..services.auth import decode_token
|
||||
|
||||
router = APIRouter(prefix="/api/logs", tags=["logs"])
|
||||
router = APIRouter(prefix="/logs", tags=["logs"])
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
def get_current_user_optional(authorization: Optional[str] = Header(None)):
|
||||
"""可选的用户认证"""
|
||||
if authorization and authorization.startswith("Bearer "):
|
||||
token = authorization[7:]
|
||||
return decode_token(token)
|
||||
return None
|
||||
|
||||
|
||||
def verify_api_key(x_api_key: str = Header(..., alias="X-API-Key")):
|
||||
"""验证API Key"""
|
||||
if x_api_key != settings.API_KEY:
|
||||
@@ -43,3 +54,62 @@ async def batch_write_logs(
|
||||
db.add_all(logs)
|
||||
db.commit()
|
||||
return {"success": True, "count": len(logs)}
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def query_logs(
|
||||
page: int = Query(1, ge=1),
|
||||
size: int = Query(20, ge=1, le=100),
|
||||
log_type: Optional[str] = None,
|
||||
level: Optional[str] = None,
|
||||
app_code: Optional[str] = None,
|
||||
tenant_id: Optional[str] = None,
|
||||
trace_id: Optional[str] = None,
|
||||
keyword: Optional[str] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user = Depends(get_current_user_optional)
|
||||
):
|
||||
"""查询日志列表"""
|
||||
query = db.query(PlatformLog)
|
||||
|
||||
if log_type:
|
||||
query = query.filter(PlatformLog.log_type == log_type)
|
||||
if level:
|
||||
query = query.filter(PlatformLog.level == level)
|
||||
if app_code:
|
||||
query = query.filter(PlatformLog.app_code == app_code)
|
||||
if tenant_id:
|
||||
query = query.filter(PlatformLog.tenant_id == tenant_id)
|
||||
if trace_id:
|
||||
query = query.filter(PlatformLog.trace_id == trace_id)
|
||||
if keyword:
|
||||
query = query.filter(PlatformLog.message.like(f"%{keyword}%"))
|
||||
|
||||
total = query.count()
|
||||
items = query.order_by(desc(PlatformLog.log_time)).offset((page-1)*size).limit(size).all()
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"page": page,
|
||||
"size": size,
|
||||
"items": [
|
||||
{
|
||||
"id": item.id,
|
||||
"log_type": item.log_type,
|
||||
"level": item.level,
|
||||
"app_code": item.app_code,
|
||||
"tenant_id": item.tenant_id,
|
||||
"trace_id": item.trace_id,
|
||||
"message": item.message,
|
||||
"path": item.path,
|
||||
"method": item.method,
|
||||
"status_code": item.status_code,
|
||||
"duration_ms": item.duration_ms,
|
||||
"ip_address": item.ip_address,
|
||||
"extra_data": item.extra_data,
|
||||
"stack_trace": item.stack_trace,
|
||||
"log_time": str(item.log_time) if item.log_time else None
|
||||
}
|
||||
for item in items
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user