archive.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. # Video filenames present in the printer's /timelapse directory when this
  30. # print started (#2704). The printer writes its video only at print end, so
  31. # anything not in this list belongs to this print — a comparison that needs
  32. # no clock, which matters because a LAN-only printer can't reach Bambu's NTP
  33. # server and its filename timestamps are arbitrarily wrong. Persisted (not
  34. # just held in memory) so the diff survives a restart and so the manual
  35. # "Scan for Timelapse" button can use it instead of guessing from
  36. # timestamps. NULL for archives predating this, and for baselines taken at
  37. # completion time, which are useless by construction.
  38. timelapse_baseline: Mapped[list | None] = mapped_column(JSON, nullable=True)
  39. source_3mf_path: Mapped[str | None] = mapped_column(String(500)) # Original project 3MF from slicer
  40. f3d_path: Mapped[str | None] = mapped_column(String(500)) # Fusion 360 design file
  41. # Print details from 3MF / printer
  42. print_name: Mapped[str | None] = mapped_column(String(255))
  43. print_time_seconds: Mapped[int | None] = mapped_column(Integer)
  44. filament_used_grams: Mapped[float | None] = mapped_column(Float)
  45. filament_type: Mapped[str | None] = mapped_column(String(50))
  46. filament_color: Mapped[str | None] = mapped_column(String(200))
  47. layer_height: Mapped[float | None] = mapped_column(Float)
  48. total_layers: Mapped[int | None] = mapped_column(Integer)
  49. nozzle_diameter: Mapped[float | None] = mapped_column(Float)
  50. bed_temperature: Mapped[int | None] = mapped_column(Integer)
  51. bed_type: Mapped[str | None] = mapped_column(String(64)) # e.g. "Cool Plate", "Textured PEI Plate"
  52. nozzle_temperature: Mapped[int | None] = mapped_column(Integer)
  53. # Printer model this file was sliced for (extracted from 3MF metadata)
  54. sliced_for_model: Mapped[str | None] = mapped_column(String(50), nullable=True)
  55. # Print result
  56. status: Mapped[str] = mapped_column(String(20), default="completed")
  57. started_at: Mapped[datetime | None] = mapped_column(DateTime)
  58. completed_at: Mapped[datetime | None] = mapped_column(DateTime)
  59. # Printer-assigned subtask identifier from MQTT. Used to resume the same
  60. # archive row across a backend restart during a long-running print (#972):
  61. # if the same subtask_id reappears after restart, we know it's the same
  62. # print and keep the original row instead of cancel-then-create.
  63. subtask_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
  64. # Which plate of a multi-plate 3MF this print was for (1-based), copied from
  65. # the queue item at dispatch (#2603). A whole multi-plate 3MF is uploaded
  66. # under one filename with no plate suffix, so the parser can't recover the
  67. # selected plate and extra_data holds all-plates aggregate metadata; without
  68. # this the history UI can't tell which plate was printed and falls back to
  69. # Plate 1. NULL for archives with no specific selected plate.
  70. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  71. # Extended metadata (JSON blob for flexibility)
  72. extra_data: Mapped[dict | None] = mapped_column(JSON)
  73. # MakerWorld info (auto-extracted from 3MF)
  74. makerworld_url: Mapped[str | None] = mapped_column(String(500))
  75. designer: Mapped[str | None] = mapped_column(String(255))
  76. # User-defined external link (Printables, Thingiverse, etc.)
  77. external_url: Mapped[str | None] = mapped_column(String(500))
  78. # User additions
  79. is_favorite: Mapped[bool] = mapped_column(Boolean, default=False)
  80. tags: Mapped[str | None] = mapped_column(Text)
  81. notes: Mapped[str | None] = mapped_column(Text)
  82. cost: Mapped[float | None] = mapped_column(Float)
  83. photos: Mapped[list | None] = mapped_column(JSON) # List of photo filenames
  84. failure_reason: Mapped[str | None] = mapped_column(String(100)) # For failed prints
  85. quantity: Mapped[int] = mapped_column(Integer, default=1) # Number of items printed
  86. # Energy tracking
  87. energy_kwh: Mapped[float | None] = mapped_column(Float) # Energy consumed in kWh
  88. energy_cost: Mapped[float | None] = mapped_column(Float) # Cost of energy consumed
  89. # Plug lifetime counter captured at print start; delta at print end becomes energy_kwh.
  90. # Persisted so per-print tracking survives backend restarts mid-print (#941).
  91. energy_start_kwh: Mapped[float | None] = mapped_column(Float)
  92. # Timestamps
  93. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  94. # Soft-delete sentinel (#1343). When non-null, the UI hides this archive
  95. # from listings (its files have already been removed from disk) but the
  96. # stats endpoint keeps counting it — deleting nine of ten Benchies no
  97. # longer wipes their filament / time / cost contribution from Quick Stats.
  98. # The opt-in "Also remove from statistics" checkbox in the delete dialog
  99. # bypasses the soft-delete path and hard-deletes the row.
  100. deleted_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, default=None, index=True)
  101. # User tracking (who uploaded/created this archive)
  102. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  103. # Relationships
  104. printer: Mapped["Printer | None"] = relationship(back_populates="archives")
  105. project: Mapped["Project | None"] = relationship(back_populates="archives")
  106. created_by: Mapped["User | None"] = relationship()
  107. from backend.app.models.printer import Printer # noqa: E402, F811
  108. from backend.app.models.project import Project # noqa: E402, F811
  109. from backend.app.models.user import User # noqa: E402, F811