log
This commit is contained in:
@@ -79,6 +79,16 @@ class Activity(Base):
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow, onupdate=utcnow)
|
||||
|
||||
|
||||
class SystemLogEntry(Base):
|
||||
__tablename__ = "system_log_entries"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow, index=True)
|
||||
source: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
message: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
user_id: Mapped[int | None] = mapped_column(ForeignKey("sync_users.id", ondelete="SET NULL"))
|
||||
|
||||
|
||||
class SyncRun(Base):
|
||||
__tablename__ = "sync_runs"
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from sqlalchemy import and_, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.models import Activity, ActivityStatus, SyncRun, SyncRunStatus, SyncUser, utcnow
|
||||
from app.db.models import Activity, ActivityStatus, SyncRun, SyncRunStatus, SystemLogEntry, SyncUser, utcnow
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -191,6 +191,26 @@ class ActivityRepository:
|
||||
)
|
||||
|
||||
|
||||
class SystemLogRepository:
|
||||
def __init__(self, session: Session) -> None:
|
||||
self.session = session
|
||||
|
||||
def add(self, *, source: str, message: str, user_id: int | None = None) -> SystemLogEntry:
|
||||
entry = SystemLogEntry(source=source, message=message[:2000], user_id=user_id)
|
||||
self.session.add(entry)
|
||||
self.session.commit()
|
||||
return entry
|
||||
|
||||
def list_recent(self, limit: int = 50) -> list[SystemLogEntry]:
|
||||
return list(
|
||||
self.session.scalars(
|
||||
select(SystemLogEntry)
|
||||
.order_by(SystemLogEntry.created_at.desc(), SystemLogEntry.id.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class SyncRunRepository:
|
||||
def __init__(self, session: Session) -> None:
|
||||
self.session = session
|
||||
|
||||
Reference in New Issue
Block a user