archive.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 | None
  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. plate_id: int | None = None # Selected plate of a multi-plate 3MF (#2603)
  46. print_time_seconds: int | None # Estimated time from slicer
  47. actual_time_seconds: int | None = None # Computed from started_at/completed_at
  48. # Percentage: 100 = perfect, >100 = faster than estimated
  49. time_accuracy: float | None = None
  50. filament_used_grams: float | None
  51. filament_type: str | None
  52. filament_color: str | None
  53. layer_height: float | None
  54. total_layers: int | None = None
  55. nozzle_diameter: float | None
  56. bed_temperature: int | None
  57. bed_type: str | None = None # e.g. "Cool Plate", "Textured PEI Plate" (from 3MF curr_bed_type)
  58. nozzle_temperature: int | None
  59. sliced_for_model: str | None = None # Printer model this file was sliced for
  60. status: str
  61. started_at: datetime | None
  62. completed_at: datetime | None
  63. extra_data: dict | None
  64. makerworld_url: str | None
  65. designer: str | None
  66. # User-defined link (Printables, Thingiverse, etc.)
  67. external_url: str | None = None
  68. is_favorite: bool
  69. tags: str | None
  70. notes: str | None
  71. cost: float | None
  72. photos: list | None
  73. failure_reason: str | None
  74. quantity: int = 1 # Number of items printed
  75. # Energy tracking
  76. energy_kwh: float | None = None
  77. energy_cost: float | None = None
  78. created_at: datetime | None
  79. # User tracking (Issue #206)
  80. created_by_id: int | None = None
  81. created_by_username: str | None = None
  82. # Per-archive run aggregates (#1378). Computed from PrintLogEntry — one
  83. # row per actual print event — so reprints contribute to these counters
  84. # without overwriting the source archive's first-run data.
  85. run_count: int = 0
  86. last_run_at: datetime | None = None
  87. total_filament_actual_grams: float | None = None
  88. successful_run_count: int = 0
  89. failed_run_count: int = 0
  90. @model_validator(mode="after")
  91. def compute_object_count(self) -> "ArchiveResponse":
  92. """Compute object_count from extra_data.printable_objects if not set."""
  93. if self.object_count is None and self.extra_data:
  94. printable_objects = self.extra_data.get("printable_objects")
  95. if printable_objects and isinstance(printable_objects, dict):
  96. self.object_count = len(printable_objects)
  97. return self
  98. class Config:
  99. from_attributes = True
  100. class ArchiveSlim(BaseModel):
  101. """Lightweight archive response for stats/dashboard widgets."""
  102. printer_id: int | None
  103. print_name: str | None
  104. print_time_seconds: int | None
  105. actual_time_seconds: int | None = None
  106. filament_used_grams: float | None
  107. filament_type: str | None
  108. filament_color: str | None
  109. status: str
  110. started_at: datetime | None
  111. completed_at: datetime | None
  112. cost: float | None
  113. energy_kwh: float | None = None
  114. energy_cost: float | None = None
  115. quantity: int = 1
  116. created_at: datetime | None
  117. class Config:
  118. from_attributes = True
  119. class ArchiveStats(BaseModel):
  120. total_prints: int
  121. successful_prints: int
  122. failed_prints: int
  123. # User/system-stopped prints (PrintLogEntry.status in stopped/cancelled/
  124. # skipped). Defaulted so older clients that don't send this field still
  125. # validate against historical fixtures.
  126. cancelled_prints: int = 0
  127. total_print_time_hours: float
  128. total_filament_grams: float
  129. total_cost: float
  130. prints_by_filament_type: dict
  131. prints_by_printer: dict
  132. # Time accuracy stats
  133. # Average across all prints with data
  134. average_time_accuracy: float | None = None
  135. time_accuracy_by_printer: dict | None = None # Per-printer accuracy
  136. # Energy stats
  137. total_energy_kwh: float = 0.0
  138. total_energy_cost: float = 0.0
  139. # Set when the date-range query in "total consumption" mode is running on
  140. # incomplete snapshot history — e.g. right after a fresh upgrade before the
  141. # hourly snapshot loop has built up a baseline. Frontend shows a tooltip.
  142. energy_data_warming_up: bool = False
  143. class ProjectPageImage(BaseModel):
  144. """Image embedded in 3MF project page."""
  145. name: str
  146. path: str # Path within 3MF
  147. url: str # API URL to fetch image
  148. class ProjectPageResponse(BaseModel):
  149. """Project page data extracted from 3MF file."""
  150. # Model info
  151. title: str | None = None
  152. description: str | None = None # HTML content
  153. designer: str | None = None
  154. designer_user_id: str | None = None
  155. license: str | None = None
  156. copyright: str | None = None
  157. creation_date: str | None = None
  158. modification_date: str | None = None
  159. origin: str | None = None # "original" or "remix"
  160. # Profile info
  161. profile_title: str | None = None
  162. profile_description: str | None = None
  163. profile_cover: str | None = None
  164. profile_user_id: str | None = None
  165. profile_user_name: str | None = None
  166. # MakerWorld info
  167. design_model_id: str | None = None
  168. design_profile_id: str | None = None
  169. design_region: str | None = None
  170. # Images
  171. model_pictures: list[ProjectPageImage] = []
  172. profile_pictures: list[ProjectPageImage] = []
  173. thumbnails: list[ProjectPageImage] = []
  174. class ProjectPageUpdate(BaseModel):
  175. """Update project page data in 3MF file."""
  176. title: str | None = None
  177. description: str | None = None
  178. designer: str | None = None
  179. license: str | None = None
  180. copyright: str | None = None
  181. profile_title: str | None = None
  182. profile_description: str | None = None