50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy import func, select
|
|
|
|
from app.core.deps import DbDep, require_perm
|
|
from app.core.exceptions import ok
|
|
from app.models import OpLog
|
|
|
|
router = APIRouter(prefix="/op-logs", tags=["op-log"])
|
|
|
|
|
|
@router.get("", dependencies=[Depends(require_perm("log:op:view"))])
|
|
async def list_op_logs(
|
|
db: DbDep,
|
|
page: int = Query(1, ge=1),
|
|
size: int = Query(20, ge=1, le=200),
|
|
module: str | None = None,
|
|
operator_id: int | None = None,
|
|
) -> dict:
|
|
stmt = select(OpLog)
|
|
if module:
|
|
stmt = stmt.where(OpLog.module == module)
|
|
if operator_id:
|
|
stmt = stmt.where(OpLog.operator_id == operator_id)
|
|
|
|
total = (await db.execute(
|
|
select(func.count()).select_from(stmt.order_by(None).subquery())
|
|
)).scalar() or 0
|
|
stmt = stmt.order_by(OpLog.id.desc()).offset((page - 1) * size).limit(size)
|
|
rows = list((await db.execute(stmt)).scalars().all())
|
|
|
|
def _row(r: OpLog) -> dict:
|
|
return {
|
|
"id": r.id,
|
|
"operator_id": r.operator_id,
|
|
"operator_name": r.operator_name,
|
|
"module": r.module,
|
|
"action": r.action,
|
|
"method": r.method,
|
|
"path": r.path,
|
|
"status_code": r.status_code,
|
|
"ip": r.ip,
|
|
"cost_ms": r.cost_ms,
|
|
"payload": r.payload,
|
|
"created_at": r.created_at.isoformat() if r.created_at else None,
|
|
}
|
|
|
|
return ok({"list": [_row(r) for r in rows], "total": total, "page": page, "size": size})
|