archive.py 5.7 KB

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