Files
signalscout-backend/app/db/models.py
T
2026-07-08 23:39:41 +08:00

73 lines
3.4 KiB
Python

from __future__ import annotations
from datetime import datetime
from typing import Optional
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, JSON, String, Text, func
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
"""Shared SQLAlchemy declarative base for app models and Alembic metadata."""
class Topic(Base):
__tablename__ = "topics"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(120), nullable=False, unique=True)
description: Mapped[str] = mapped_column(Text, nullable=False)
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
updated_at: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), onupdate=func.now(), nullable=False
)
class AgentRun(Base):
__tablename__ = "agent_runs"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
status: Mapped[str] = mapped_column(String(32), nullable=False)
started_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
error: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
request_payload: Mapped[dict] = mapped_column(JSON, nullable=False)
raw_response: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
signals: Mapped[list["Signal"]] = relationship(back_populates="run")
report: Mapped[Optional[Report]] = relationship(back_populates="run", uselist=False)
class Signal(Base):
__tablename__ = "signals"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
run_id: Mapped[int] = mapped_column(ForeignKey("agent_runs.id"), nullable=False, index=True)
source_type: Mapped[str] = mapped_column(String(20), nullable=False, default="news", index=True)
topic: Mapped[str] = mapped_column(String(120), nullable=False, index=True)
title: Mapped[str] = mapped_column(String(300), nullable=False)
summary: Mapped[str] = mapped_column(Text, nullable=False)
source_url: Mapped[str] = mapped_column(Text, nullable=False)
source_url_hash: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
source_name: Mapped[str] = mapped_column(String(200), nullable=False)
published_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
importance: Mapped[int] = mapped_column(Integer, nullable=False)
entities: Mapped[list[str]] = mapped_column(JSON, nullable=False, default=list)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
run: Mapped[AgentRun] = relationship(back_populates="signals")
class Report(Base):
__tablename__ = "reports"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
run_id: Mapped[int] = mapped_column(ForeignKey("agent_runs.id"), nullable=False)
title: Mapped[str] = mapped_column(String(200), nullable=False)
content_md: Mapped[str] = mapped_column(Text, nullable=False)
file_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
run: Mapped[AgentRun] = relationship(back_populates="report")