feat(history): record what happens to a request
This commit is contained in:
1 parent
53927f7e11
commit
0c504790df
6 files changed
+114
-3
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel, ConfigDict, field_serializer
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..db import History, get_session
|
||||
|
||||
router = APIRouter(prefix="/api/history", tags=["history"])
|
||||
|
||||
|
||||
class HistoryOut(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
at: datetime.datetime
|
||||
event: str
|
||||
request_id: int | None
|
||||
title: str
|
||||
media_type: str
|
||||
source: str
|
||||
dest: str
|
||||
message: str
|
||||
|
||||
# SQLite drops the zone, and a bare timestamp reads as local time in JS
|
||||
@field_serializer("at")
|
||||
def _utc(self, at: datetime.datetime) -> str:
|
||||
return at.replace(tzinfo=datetime.UTC).isoformat()
|
||||
|
||||
|
||||
@router.get("", response_model=list[HistoryOut])
|
||||
def list_history(limit: int = 200, session: Session = Depends(get_session)):
|
||||
"""Newest first."""
|
||||
stmt = select(History).order_by(History.id.desc()).limit(min(limit, 1000))
|
||||
return session.scalars(stmt).all()
|
||||
Reference in new issue
Block a user