archive.py 7.6 KB

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