archive.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. class ArchiveUpdate(ArchiveBase):
  12. printer_id: int | None = None
  13. project_id: int | None = None
  14. status: str | None = None # Allow changing status (e.g., clearing failed flag)
  15. class ArchiveDuplicate(BaseModel):
  16. """Reference to a duplicate archive."""
  17. id: int
  18. print_name: str | None
  19. created_at: datetime
  20. match_type: str # "exact" (hash match) or "similar" (name match)
  21. class ArchiveResponse(BaseModel):
  22. id: int
  23. printer_id: int | None
  24. project_id: int | None = None
  25. project_name: str | None = None # Included for convenience
  26. filename: str
  27. file_path: str
  28. file_size: int
  29. content_hash: str | None
  30. thumbnail_path: str | None
  31. timelapse_path: str | None
  32. source_3mf_path: str | None = None # Original project 3MF from slicer
  33. # Duplicate detection
  34. duplicates: list[ArchiveDuplicate] | None = None
  35. duplicate_count: int = 0 # Quick count for list views
  36. # Object count (computed from extra_data.printable_objects)
  37. object_count: int | None = None
  38. print_name: str | None
  39. print_time_seconds: int | None # Estimated time from slicer
  40. actual_time_seconds: int | None = None # Computed from started_at/completed_at
  41. time_accuracy: float | None = None # Percentage: 100 = perfect, >100 = faster than estimated
  42. filament_used_grams: float | None
  43. filament_type: str | None
  44. filament_color: str | None
  45. layer_height: float | None
  46. total_layers: int | None = None
  47. nozzle_diameter: float | None
  48. bed_temperature: int | None
  49. nozzle_temperature: int | None
  50. status: str
  51. started_at: datetime | None
  52. completed_at: datetime | None
  53. extra_data: dict | None
  54. makerworld_url: str | None
  55. designer: str | None
  56. is_favorite: bool
  57. tags: str | None
  58. notes: str | None
  59. cost: float | None
  60. photos: list | None
  61. failure_reason: str | None
  62. quantity: int = 1 # Number of items printed
  63. # Energy tracking
  64. energy_kwh: float | None = None
  65. energy_cost: float | None = None
  66. created_at: datetime
  67. @model_validator(mode="after")
  68. def compute_object_count(self) -> "ArchiveResponse":
  69. """Compute object_count from extra_data.printable_objects if not set."""
  70. if self.object_count is None and self.extra_data:
  71. printable_objects = self.extra_data.get("printable_objects")
  72. if printable_objects and isinstance(printable_objects, dict):
  73. self.object_count = len(printable_objects)
  74. return self
  75. class Config:
  76. from_attributes = True
  77. class ArchiveStats(BaseModel):
  78. total_prints: int
  79. successful_prints: int
  80. failed_prints: int
  81. total_print_time_hours: float
  82. total_filament_grams: float
  83. total_cost: float
  84. prints_by_filament_type: dict
  85. prints_by_printer: dict
  86. # Time accuracy stats
  87. average_time_accuracy: float | None = None # Average across all prints with data
  88. time_accuracy_by_printer: dict | None = None # Per-printer accuracy
  89. # Energy stats
  90. total_energy_kwh: float = 0.0
  91. total_energy_cost: float = 0.0
  92. class ProjectPageImage(BaseModel):
  93. """Image embedded in 3MF project page."""
  94. name: str
  95. path: str # Path within 3MF
  96. url: str # API URL to fetch image
  97. class ProjectPageResponse(BaseModel):
  98. """Project page data extracted from 3MF file."""
  99. # Model info
  100. title: str | None = None
  101. description: str | None = None # HTML content
  102. designer: str | None = None
  103. designer_user_id: str | None = None
  104. license: str | None = None
  105. copyright: str | None = None
  106. creation_date: str | None = None
  107. modification_date: str | None = None
  108. origin: str | None = None # "original" or "remix"
  109. # Profile info
  110. profile_title: str | None = None
  111. profile_description: str | None = None
  112. profile_cover: str | None = None
  113. profile_user_id: str | None = None
  114. profile_user_name: str | None = None
  115. # MakerWorld info
  116. design_model_id: str | None = None
  117. design_profile_id: str | None = None
  118. design_region: str | None = None
  119. # Images
  120. model_pictures: list[ProjectPageImage] = []
  121. profile_pictures: list[ProjectPageImage] = []
  122. thumbnails: list[ProjectPageImage] = []
  123. class ProjectPageUpdate(BaseModel):
  124. """Update project page data in 3MF file."""
  125. title: str | None = None
  126. description: str | None = None
  127. designer: str | None = None
  128. license: str | None = None
  129. copyright: str | None = None
  130. profile_title: str | None = None
  131. profile_description: str | None = None
  132. class ReprintRequest(BaseModel):
  133. """Request body for reprinting an archive."""
  134. # AMS slot mapping: list of tray IDs for each filament slot in the 3MF
  135. # Global tray ID = (ams_id * 4) + slot_id, external = 254
  136. ams_mapping: list[int] | None = None
  137. # Print options
  138. bed_levelling: bool = True
  139. flow_cali: bool = False
  140. vibration_cali: bool = True
  141. layer_inspect: bool = False
  142. timelapse: bool = False
  143. use_ams: bool = True # Not exposed in UI, but needed for API