archive.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. from datetime import datetime
  2. from typing import Annotated
  3. from pydantic import BaseModel, BeforeValidator, model_validator
  4. from backend.app.utils.filename import clean_display_name
  5. # Free text, punctuation and all -- only control characters are taken out, and
  6. # only on the way in (#2832). Anything that turns a name into a path sanitises
  7. # it there instead, where the budget and the fallback are known.
  8. DisplayName = Annotated[str | None, BeforeValidator(clean_display_name)]
  9. class ArchiveBase(BaseModel):
  10. print_name: DisplayName = None
  11. is_favorite: bool | None = None
  12. tags: str | None = None
  13. notes: str | None = None
  14. cost: float | None = None
  15. failure_reason: str | None = None
  16. quantity: int | None = None # Number of items printed
  17. # User-defined link (Printables, Thingiverse, etc.)
  18. external_url: str | None = None
  19. class ArchiveUpdate(ArchiveBase):
  20. printer_id: int | None = None
  21. project_id: int | None = None
  22. # Allow changing status (e.g., clearing failed flag)
  23. status: str | None = None
  24. class ArchiveDuplicate(BaseModel):
  25. """Reference to a duplicate archive."""
  26. id: int
  27. print_name: str | None
  28. created_at: datetime | None
  29. match_type: str # "exact" (hash match) or "similar" (name match)
  30. class ArchiveResponse(BaseModel):
  31. id: int
  32. printer_id: int | None
  33. project_id: int | None = None
  34. project_name: str | None = None # Included for convenience
  35. filename: str
  36. file_path: str
  37. file_size: int
  38. content_hash: str | None
  39. thumbnail_path: str | None
  40. timelapse_path: str | None
  41. source_3mf_path: str | None = None # Original project 3MF from slicer
  42. f3d_path: str | None = None # Fusion 360 design file
  43. # Duplicate detection
  44. duplicates: list[ArchiveDuplicate] | None = None
  45. duplicate_count: int = 0 # Quick count for list views
  46. duplicate_sequence: int = 0 # 0 = original, 1+ = nth duplicate
  47. original_archive_id: int | None = None # ID of the first/original archive
  48. # Object count (computed from extra_data.printable_objects)
  49. object_count: int | None = None
  50. print_name: str | None
  51. plate_id: int | None = None # Selected plate of a multi-plate 3MF (#2603)
  52. print_time_seconds: int | None # Estimated time from slicer
  53. actual_time_seconds: int | None = None # Computed from started_at/completed_at
  54. # Percentage: 100 = perfect, >100 = faster than estimated
  55. time_accuracy: float | None = None
  56. filament_used_grams: float | None
  57. filament_type: str | None
  58. filament_color: str | None
  59. layer_height: float | None
  60. total_layers: int | None = None
  61. nozzle_diameter: float | None
  62. bed_temperature: int | None
  63. bed_type: str | None = None # e.g. "Cool Plate", "Textured PEI Plate" (from 3MF curr_bed_type)
  64. nozzle_temperature: int | None
  65. sliced_for_model: str | None = None # Printer model this file was sliced for
  66. status: str
  67. started_at: datetime | None
  68. completed_at: datetime | None
  69. extra_data: dict | None
  70. makerworld_url: str | None
  71. designer: str | None
  72. # User-defined link (Printables, Thingiverse, etc.)
  73. external_url: str | None = None
  74. is_favorite: bool
  75. tags: str | None
  76. notes: str | None
  77. cost: float | None
  78. photos: list | None
  79. failure_reason: str | None
  80. quantity: int = 1 # Number of items printed
  81. # Energy tracking
  82. energy_kwh: float | None = None
  83. energy_cost: float | None = None
  84. created_at: datetime | None
  85. # User tracking (Issue #206)
  86. created_by_id: int | None = None
  87. created_by_username: str | None = None
  88. # Per-archive run aggregates (#1378). Computed from PrintLogEntry — one
  89. # row per actual print event — so reprints contribute to these counters
  90. # without overwriting the source archive's first-run data.
  91. run_count: int = 0
  92. last_run_at: datetime | None = None
  93. total_filament_actual_grams: float | None = None
  94. successful_run_count: int = 0
  95. failed_run_count: int = 0
  96. @model_validator(mode="after")
  97. def compute_object_count(self) -> "ArchiveResponse":
  98. """Compute object_count from extra_data.printable_objects if not set."""
  99. if self.object_count is None and self.extra_data:
  100. printable_objects = self.extra_data.get("printable_objects")
  101. if printable_objects and isinstance(printable_objects, dict):
  102. self.object_count = len(printable_objects)
  103. return self
  104. class Config:
  105. from_attributes = True
  106. class ArchiveSlim(BaseModel):
  107. """Lightweight archive response for stats/dashboard widgets."""
  108. printer_id: int | None
  109. print_name: str | None
  110. print_time_seconds: int | None
  111. actual_time_seconds: int | None = None
  112. filament_used_grams: float | None
  113. filament_type: str | None
  114. filament_color: str | None
  115. status: str
  116. started_at: datetime | None
  117. completed_at: datetime | None
  118. cost: float | None
  119. energy_kwh: float | None = None
  120. energy_cost: float | None = None
  121. quantity: int = 1
  122. created_at: datetime | None
  123. class Config:
  124. from_attributes = True
  125. class ArchiveStats(BaseModel):
  126. total_prints: int
  127. successful_prints: int
  128. failed_prints: int
  129. # User/system-stopped prints (PrintLogEntry.status in stopped/cancelled/
  130. # skipped). Defaulted so older clients that don't send this field still
  131. # validate against historical fixtures.
  132. cancelled_prints: int = 0
  133. total_print_time_hours: float
  134. total_filament_grams: float
  135. total_cost: float
  136. prints_by_filament_type: dict
  137. prints_by_printer: dict
  138. # Time accuracy stats
  139. # Average across all prints with data
  140. average_time_accuracy: float | None = None
  141. time_accuracy_by_printer: dict | None = None # Per-printer accuracy
  142. # Energy stats
  143. total_energy_kwh: float = 0.0
  144. total_energy_cost: float = 0.0
  145. # Set when the date-range query in "total consumption" mode is running on
  146. # incomplete snapshot history — e.g. right after a fresh upgrade before the
  147. # hourly snapshot loop has built up a baseline. Frontend shows a tooltip.
  148. energy_data_warming_up: bool = False
  149. class ProjectPageImage(BaseModel):
  150. """Image embedded in 3MF project page."""
  151. name: str
  152. path: str # Path within 3MF
  153. url: str # API URL to fetch image
  154. class ProjectPageResponse(BaseModel):
  155. """Project page data extracted from 3MF file."""
  156. # Model info
  157. title: str | None = None
  158. description: str | None = None # HTML content
  159. designer: str | None = None
  160. designer_user_id: str | None = None
  161. license: str | None = None
  162. copyright: str | None = None
  163. creation_date: str | None = None
  164. modification_date: str | None = None
  165. origin: str | None = None # "original" or "remix"
  166. # Profile info
  167. profile_title: str | None = None
  168. profile_description: str | None = None
  169. profile_cover: str | None = None
  170. profile_user_id: str | None = None
  171. profile_user_name: str | None = None
  172. # MakerWorld info
  173. design_model_id: str | None = None
  174. design_profile_id: str | None = None
  175. design_region: str | None = None
  176. # Images
  177. model_pictures: list[ProjectPageImage] = []
  178. profile_pictures: list[ProjectPageImage] = []
  179. thumbnails: list[ProjectPageImage] = []
  180. class ProjectPageUpdate(BaseModel):
  181. """Update project page data in 3MF file."""
  182. title: str | None = None
  183. description: str | None = None
  184. designer: str | None = None
  185. license: str | None = None
  186. copyright: str | None = None
  187. profile_title: str | None = None
  188. profile_description: str | None = None