print_log.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, String, func
  3. from sqlalchemy.orm import Mapped, mapped_column
  4. from backend.app.core.database import Base
  5. class PrintLogEntry(Base):
  6. """Independent print log entry. Written when print events occur.
  7. This is a separate table from archives/queue — clearing the log
  8. never touches archives or queue items.
  9. archive_id is a nullable FK so log entries survive archive deletion (ON
  10. DELETE SET NULL). Aggregating runs per archive — for the per-archive
  11. "Print Log" view and for statistics that should not double-count
  12. overwritten archives (#1378) — is done via WHERE archive_id = X.
  13. """
  14. __tablename__ = "print_log_entries"
  15. id: Mapped[int] = mapped_column(primary_key=True)
  16. archive_id: Mapped[int | None] = mapped_column(
  17. ForeignKey("print_archives.id", ondelete="SET NULL"), nullable=True, index=True
  18. )
  19. # Which queue item produced this run, when one did. Printer-initiated
  20. # prints have none. Batch cost/energy roll-up joins on this (#342): the
  21. # archive alone can't attribute a run to an order because several orders
  22. # — and plain reprints — share one archive.
  23. queue_item_id: Mapped[int | None] = mapped_column(
  24. ForeignKey("print_queue.id", ondelete="SET NULL"), nullable=True, index=True
  25. )
  26. print_name: Mapped[str | None] = mapped_column(String(255))
  27. # Which plate of a multi-plate 3MF this run printed (1-based). The archive
  28. # holds one plate, its latest run's, and every run of a file shares that
  29. # archive, so a run's own plate is kept here. NULL when not known.
  30. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  31. printer_name: Mapped[str | None] = mapped_column(String(255))
  32. printer_id: Mapped[int | None] = mapped_column(Integer)
  33. status: Mapped[str] = mapped_column(String(20)) # completed, failed, stopped, cancelled, skipped
  34. started_at: Mapped[datetime | None] = mapped_column(DateTime)
  35. completed_at: Mapped[datetime | None] = mapped_column(DateTime)
  36. duration_seconds: Mapped[int | None] = mapped_column(Integer)
  37. filament_type: Mapped[str | None] = mapped_column(String(50))
  38. filament_color: Mapped[str | None] = mapped_column(String(50))
  39. filament_used_grams: Mapped[float | None] = mapped_column(Float)
  40. cost: Mapped[float | None] = mapped_column(Float)
  41. energy_kwh: Mapped[float | None] = mapped_column(Float)
  42. energy_cost: Mapped[float | None] = mapped_column(Float)
  43. # Printer wear for this run: duration at the printer's hourly wear cost (#694).
  44. wear_cost: Mapped[float | None] = mapped_column(Float)
  45. # True when the run's times were not measured: a print closed at reconnect
  46. # (#3310), whose end is its start plus the slicer's estimate when it
  47. # completed, or the reconnect (duration 0). Counted in
  48. # the totals, left out of the time accuracy. NULL for a measured run.
  49. duration_unmeasured: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
  50. failure_reason: Mapped[str | None] = mapped_column(String(100))
  51. # User quality verdict ('good' / 'reject'), mirrored from the archive on
  52. # PATCH exactly like status/failure_reason (#1444 mirror) so verdict-aware
  53. # statistics can stay on this table (#1898).
  54. user_verdict: Mapped[str | None] = mapped_column(String(10), nullable=True)
  55. thumbnail_path: Mapped[str | None] = mapped_column(String(500))
  56. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  57. created_by_username: Mapped[str | None] = mapped_column(String(100))
  58. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())