from datetime import datetime from sqlalchemy import DateTime, Index, Integer, String, func from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class SyncLog(Base): __tablename__ = "sync_log" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) source_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="资源站ID") range_type: Mapped[str] = mapped_column(String(20), nullable=False, default="daily", comment="同步范围: all/monthly/weekly/daily") status: Mapped[str] = mapped_column(String(20), nullable=False, comment="running/success/failed/skipped") started_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, comment="开始时间") finished_at: Mapped[datetime | None] = mapped_column(DateTime, comment="结束时间") total_fetched: Mapped[int] = mapped_column(Integer, default=0, comment="拉取总数") new_count: Mapped[int] = mapped_column(Integer, default=0, comment="新增数") update_count: Mapped[int] = mapped_column(Integer, default=0, comment="更新数") fail_count: Mapped[int] = mapped_column(Integer, default=0, comment="失败数") error_message: Mapped[str | None] = mapped_column(String, comment="错误信息") duration_seconds: Mapped[int | None] = mapped_column(Integer, comment="耗时秒数") __table_args__ = ( # 同步日志列表:按 source_id 过滤 + 时间倒序 Index("ix_sync_log_source_started", "source_id", "started_at"), )