smart_plug_energy_snapshot.py 929 B

12345678910111213141516171819202122
  1. from datetime import datetime
  2. from sqlalchemy import DateTime, Float, ForeignKey, Index, Integer
  3. from sqlalchemy.orm import Mapped, mapped_column
  4. from backend.app.core.database import Base
  5. class SmartPlugEnergySnapshot(Base):
  6. """Hourly snapshot of a smart plug's lifetime energy counter.
  7. Powers date-range queries in "total consumption" energy mode. For a given
  8. range we sum `(last_snapshot_in_range - last_snapshot_before_range)` per plug.
  9. """
  10. __tablename__ = "smart_plug_energy_snapshots"
  11. __table_args__ = (Index("ix_plug_energy_snapshots_plug_time", "plug_id", "recorded_at"),)
  12. id: Mapped[int] = mapped_column(Integer, primary_key=True)
  13. plug_id: Mapped[int] = mapped_column(ForeignKey("smart_plugs.id", ondelete="CASCADE"), nullable=False)
  14. recorded_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
  15. lifetime_kwh: Mapped[float] = mapped_column(Float, nullable=False)