archive.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from datetime import datetime
  2. from sqlalchemy import JSON, Boolean, DateTime, Float, ForeignKey, Integer, String, Text, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class PrintArchive(Base):
  6. __tablename__ = "print_archives"
  7. id: Mapped[int] = mapped_column(primary_key=True)
  8. printer_id: Mapped[int | None] = mapped_column(ForeignKey("printers.id"), nullable=True)
  9. project_id: Mapped[int | None] = mapped_column(ForeignKey("projects.id", ondelete="SET NULL"), nullable=True)
  10. # Which library file this run was dispatched from (#1897). Set by the queue
  11. # scheduler when it archives a library-file print; older rows are matched by
  12. # content_hash/filename instead. SET NULL so deleting a file keeps history.
  13. library_file_id: Mapped[int | None] = mapped_column(
  14. ForeignKey("library_files.id", ondelete="SET NULL"), nullable=True
  15. )
  16. # File info
  17. filename: Mapped[str] = mapped_column(String(255))
  18. file_path: Mapped[str] = mapped_column(String(500))
  19. file_size: Mapped[int] = mapped_column(Integer)
  20. content_hash: Mapped[str | None] = mapped_column(String(64)) # SHA256 hash for duplicate detection
  21. thumbnail_path: Mapped[str | None] = mapped_column(String(500))
  22. timelapse_path: Mapped[str | None] = mapped_column(String(500))
  23. # True when Bambuddy forced timelapse recording on for this print so the
  24. # finish-photo extractor (#1397) could pull the post-park-pre-drop frame.
  25. # The cleanup path uses this to know the timelapse should be deleted
  26. # both locally and on the printer's SD after extraction — the user
  27. # didn't opt in to a timelapse recording.
  28. bambuddy_forced_timelapse: Mapped[bool] = mapped_column(Boolean, default=False, server_default="0")
  29. source_3mf_path: Mapped[str | None] = mapped_column(String(500)) # Original project 3MF from slicer
  30. f3d_path: Mapped[str | None] = mapped_column(String(500)) # Fusion 360 design file
  31. # Print details from 3MF / printer
  32. print_name: Mapped[str | None] = mapped_column(String(255))
  33. print_time_seconds: Mapped[int | None] = mapped_column(Integer)
  34. filament_used_grams: Mapped[float | None] = mapped_column(Float)
  35. filament_type: Mapped[str | None] = mapped_column(String(50))
  36. filament_color: Mapped[str | None] = mapped_column(String(200))
  37. layer_height: Mapped[float | None] = mapped_column(Float)
  38. total_layers: Mapped[int | None] = mapped_column(Integer)
  39. nozzle_diameter: Mapped[float | None] = mapped_column(Float)
  40. bed_temperature: Mapped[int | None] = mapped_column(Integer)
  41. bed_type: Mapped[str | None] = mapped_column(String(64)) # e.g. "Cool Plate", "Textured PEI Plate"
  42. nozzle_temperature: Mapped[int | None] = mapped_column(Integer)
  43. # Printer model this file was sliced for (extracted from 3MF metadata)
  44. sliced_for_model: Mapped[str | None] = mapped_column(String(50), nullable=True)
  45. # Print result
  46. status: Mapped[str] = mapped_column(String(20), default="completed")
  47. started_at: Mapped[datetime | None] = mapped_column(DateTime)
  48. completed_at: Mapped[datetime | None] = mapped_column(DateTime)
  49. # Printer-assigned subtask identifier from MQTT. Used to resume the same
  50. # archive row across a backend restart during a long-running print (#972):
  51. # if the same subtask_id reappears after restart, we know it's the same
  52. # print and keep the original row instead of cancel-then-create.
  53. subtask_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
  54. # Which plate of a multi-plate 3MF this print was for (1-based), copied from
  55. # the queue item at dispatch (#2603). A whole multi-plate 3MF is uploaded
  56. # under one filename with no plate suffix, so the parser can't recover the
  57. # selected plate and extra_data holds all-plates aggregate metadata; without
  58. # this the history UI can't tell which plate was printed and falls back to
  59. # Plate 1. NULL for archives with no specific selected plate.
  60. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  61. # Extended metadata (JSON blob for flexibility)
  62. extra_data: Mapped[dict | None] = mapped_column(JSON)
  63. # MakerWorld info (auto-extracted from 3MF)
  64. makerworld_url: Mapped[str | None] = mapped_column(String(500))
  65. designer: Mapped[str | None] = mapped_column(String(255))
  66. # User-defined external link (Printables, Thingiverse, etc.)
  67. external_url: Mapped[str | None] = mapped_column(String(500))
  68. # User additions
  69. is_favorite: Mapped[bool] = mapped_column(Boolean, default=False)
  70. tags: Mapped[str | None] = mapped_column(Text)
  71. notes: Mapped[str | None] = mapped_column(Text)
  72. cost: Mapped[float | None] = mapped_column(Float)
  73. photos: Mapped[list | None] = mapped_column(JSON) # List of photo filenames
  74. failure_reason: Mapped[str | None] = mapped_column(String(100)) # For failed prints
  75. quantity: Mapped[int] = mapped_column(Integer, default=1) # Number of items printed
  76. # Energy tracking
  77. energy_kwh: Mapped[float | None] = mapped_column(Float) # Energy consumed in kWh
  78. energy_cost: Mapped[float | None] = mapped_column(Float) # Cost of energy consumed
  79. # Plug lifetime counter captured at print start; delta at print end becomes energy_kwh.
  80. # Persisted so per-print tracking survives backend restarts mid-print (#941).
  81. energy_start_kwh: Mapped[float | None] = mapped_column(Float)
  82. # Timestamps
  83. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  84. # Soft-delete sentinel (#1343). When non-null, the UI hides this archive
  85. # from listings (its files have already been removed from disk) but the
  86. # stats endpoint keeps counting it — deleting nine of ten Benchies no
  87. # longer wipes their filament / time / cost contribution from Quick Stats.
  88. # The opt-in "Also remove from statistics" checkbox in the delete dialog
  89. # bypasses the soft-delete path and hard-deletes the row.
  90. deleted_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, default=None, index=True)
  91. # User tracking (who uploaded/created this archive)
  92. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  93. # Relationships
  94. printer: Mapped["Printer | None"] = relationship(back_populates="archives")
  95. project: Mapped["Project | None"] = relationship(back_populates="archives")
  96. created_by: Mapped["User | None"] = relationship()
  97. from backend.app.models.printer import Printer # noqa: E402, F811
  98. from backend.app.models.project import Project # noqa: E402, F811
  99. from backend.app.models.user import User # noqa: E402, F811