from datetime import datetime from sqlalchemy import Boolean, DateTime, Index, Integer, String, func from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class SourceHealth(Base): __tablename__ = "source_health" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) source_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="资源站ID") check_type: Mapped[str] = mapped_column(String(20), nullable=False, comment="api/play") is_success: Mapped[bool] = mapped_column(Boolean, nullable=False, comment="是否成功") response_time_ms: Mapped[int | None] = mapped_column(Integer, comment="响应时间(ms)") error_message: Mapped[str | None] = mapped_column(String, comment="错误信息") checked_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) __table_args__ = ( # 查询最新健康记录:source_id + checked_at 倒序 Index("ix_source_health_source_checked", "source_id", "checked_at"), )