56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database import get_db
|
|
from app.models.db_models import FilterLog
|
|
|
|
router = APIRouter(prefix="/api/logs", tags=["logs"])
|
|
|
|
|
|
@router.get("/")
|
|
def get_logs(
|
|
account_id: int | None = None,
|
|
level: str | None = None,
|
|
limit: int = Query(default=100, le=500),
|
|
offset: int = 0,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
query = db.query(FilterLog).order_by(FilterLog.created_at.desc())
|
|
if account_id:
|
|
query = query.filter(FilterLog.account_id == account_id)
|
|
if level:
|
|
query = query.filter(FilterLog.level == level)
|
|
total = query.count()
|
|
logs = query.offset(offset).limit(limit).all()
|
|
return {
|
|
"total": total,
|
|
"logs": [
|
|
{
|
|
"id": log.id,
|
|
"account_id": log.account_id,
|
|
"account_name": log.account_name,
|
|
"level": log.level.value if log.level else "info",
|
|
"message": log.message,
|
|
"rule_name": log.rule_name,
|
|
"action_type": log.action_type,
|
|
"mail_uid": log.mail_uid,
|
|
"mail_subject": log.mail_subject,
|
|
"mail_from": log.mail_from,
|
|
"folder": log.folder,
|
|
"details": log.details,
|
|
"created_at": log.created_at.isoformat() if log.created_at else None,
|
|
}
|
|
for log in logs
|
|
],
|
|
}
|
|
|
|
|
|
@router.delete("/")
|
|
def clear_logs(account_id: int | None = None, db: Session = Depends(get_db)):
|
|
query = db.query(FilterLog)
|
|
if account_id:
|
|
query = query.filter(FilterLog.account_id == account_id)
|
|
count = query.delete()
|
|
db.commit()
|
|
return {"deleted": count}
|