Files
2026-06-18 21:07:34 +01:00

128 lines
5.3 KiB
Python

from __future__ import annotations
from datetime import date, datetime
from sqlalchemy import Boolean, Date, DateTime, Float, ForeignKey, Index, Integer, String, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db import Base
class Device(Base):
__tablename__ = "devices"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)
device_type: Mapped[str] = mapped_column(String(64), nullable=False)
enable_cloud_service: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
hub_device_id: Mapped[str | None] = mapped_column(String(64))
last_seen_at: Mapped[datetime | None] = mapped_column(DateTime)
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,
)
readings: Mapped[list["Reading"]] = relationship(back_populates="device")
scheduled_reports: Mapped[list["ScheduledReport"]] = relationship(back_populates="device")
class Reading(Base):
__tablename__ = "readings"
__table_args__ = (
Index("ix_readings_device_recorded", "device_id", "recorded_at"),
Index("ix_readings_recorded", "recorded_at"),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
device_id: Mapped[str] = mapped_column(ForeignKey("devices.id"), nullable=False)
recorded_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
temperature: Mapped[float | None] = mapped_column(Float)
humidity: Mapped[float | None] = mapped_column(Float)
battery: Mapped[int | None] = mapped_column(Integer)
version: Mapped[str | None] = mapped_column(String(32))
device: Mapped[Device] = relationship(back_populates="readings")
class ReportRecipient(Base):
__tablename__ = "report_recipients"
__table_args__ = (UniqueConstraint("email", name="uq_report_recipients_email"),)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
email: Mapped[str] = mapped_column(String(255), nullable=False)
name: Mapped[str | None] = mapped_column(String(255))
active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
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,
)
scheduled_reports: Mapped[list["ScheduledReport"]] = relationship(back_populates="recipient")
class ScheduledReport(Base):
__tablename__ = "scheduled_reports"
__table_args__ = (
Index("ix_scheduled_reports_active_cadence", "active", "cadence"),
UniqueConstraint(
"recipient_id",
"cadence",
"send_time",
"weekday",
"monthday",
"device_id",
name="uq_scheduled_report_config",
),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
recipient_id: Mapped[int] = mapped_column(ForeignKey("report_recipients.id"), nullable=False)
cadence: Mapped[str] = mapped_column(String(16), nullable=False)
send_time: Mapped[str] = mapped_column(String(5), nullable=False, default="08:00")
weekday: Mapped[int | None] = mapped_column(Integer)
monthday: Mapped[int | None] = mapped_column(Integer)
device_id: Mapped[str | None] = mapped_column(ForeignKey("devices.id"))
active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
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,
)
recipient: Mapped[ReportRecipient] = relationship(back_populates="scheduled_reports")
device: Mapped[Device | None] = relationship(back_populates="scheduled_reports")
runs: Mapped[list["ScheduledReportRun"]] = relationship(back_populates="scheduled_report")
class ScheduledReportRun(Base):
__tablename__ = "scheduled_report_runs"
__table_args__ = (
UniqueConstraint(
"scheduled_report_id",
"period_start",
"period_end",
name="uq_scheduled_report_run_period",
),
Index("ix_scheduled_report_runs_status", "status"),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
scheduled_report_id: Mapped[int] = mapped_column(ForeignKey("scheduled_reports.id"), nullable=False)
period_start: Mapped[date] = mapped_column(Date, nullable=False)
period_end: Mapped[date] = mapped_column(Date, nullable=False)
status: Mapped[str] = mapped_column(String(16), nullable=False, default="pending")
pdf_path: Mapped[str | None] = mapped_column(String(1024))
error: Mapped[str | None] = mapped_column(String(1024))
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
sent_at: Mapped[datetime | None] = mapped_column(DateTime)
scheduled_report: Mapped[ScheduledReport] = relationship(back_populates="runs")