spool_usage_history.py 1.2 KB

12345678910111213141516171819202122232425
  1. from datetime import datetime
  2. from sqlalchemy import DateTime, Float, ForeignKey, Integer, String, func
  3. from sqlalchemy.orm import Mapped, mapped_column
  4. from backend.app.core.database import Base
  5. class SpoolUsageHistory(Base):
  6. """Record of filament consumption for a spool during a print."""
  7. __tablename__ = "spool_usage_history"
  8. id: Mapped[int] = mapped_column(primary_key=True)
  9. spool_id: Mapped[int] = mapped_column(ForeignKey("spool.id", ondelete="CASCADE"))
  10. printer_id: Mapped[int | None] = mapped_column(ForeignKey("printers.id", ondelete="SET NULL"))
  11. print_name: Mapped[str | None] = mapped_column(String(500))
  12. # The spool's consumption outlives the archive: deleting the archive only
  13. # drops the link.
  14. archive_id: Mapped[int | None] = mapped_column(ForeignKey("print_archives.id", ondelete="SET NULL"), nullable=True)
  15. weight_used: Mapped[float] = mapped_column(Float, default=0)
  16. percent_used: Mapped[int] = mapped_column(Integer, default=0)
  17. status: Mapped[str] = mapped_column(String(20), default="completed") # completed/failed/aborted
  18. cost: Mapped[float | None] = mapped_column(Float) # Calculated cost for this usage event
  19. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())