from datetime import datetime, timezone from typing import Optional from sqlalchemy import Index, Integer, String, DateTime, Boolean, text from sqlalchemy.orm import mapped_column, Mapped from app.database import Base class ExternalApiLog(Base): __tablename__ = "external_api_logs" id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) direction: Mapped[str] = mapped_column(String(32), index=True) # "upstream" | "website" target_type: Mapped[str] = mapped_column(String(32), index=True) # "upstream" | "website" target_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True) target_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) method: Mapped[str] = mapped_column(String(16)) path: Mapped[str] = mapped_column(String(1024)) url_host: Mapped[str] = mapped_column(String(255)) status_code: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True) success: Mapped[bool] = mapped_column(Boolean, default=True, index=True) duration_ms: Mapped[int] = mapped_column(Integer) error_type: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) error_message: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.now(timezone.utc), index=True) __table_args__ = ( Index("ix_external_api_logs_created_at_desc", text("created_at DESC")), )