archive_comparison.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from sqlalchemy.ext.asyncio import AsyncSession
  2. from sqlalchemy import select
  3. from sqlalchemy.orm import selectinload
  4. from backend.app.models.archive import PrintArchive
  5. class ArchiveComparisonService:
  6. """Service for comparing print archives."""
  7. # Fields to compare
  8. COMPARABLE_FIELDS = [
  9. ("layer_height", "Layer Height", "mm"),
  10. ("nozzle_diameter", "Nozzle Diameter", "mm"),
  11. ("bed_temperature", "Bed Temperature", "°C"),
  12. ("nozzle_temperature", "Nozzle Temperature", "°C"),
  13. ("filament_type", "Filament Type", None),
  14. ("filament_used_grams", "Filament Used", "g"),
  15. ("print_time_seconds", "Print Time", "s"),
  16. ("total_layers", "Total Layers", None),
  17. ("status", "Status", None),
  18. ]
  19. def __init__(self, db: AsyncSession):
  20. self.db = db
  21. async def compare_archives(self, archive_ids: list[int]) -> dict:
  22. """Compare multiple archives side by side.
  23. Args:
  24. archive_ids: List of 2-5 archive IDs to compare
  25. Returns:
  26. Dictionary with comparison results
  27. """
  28. if len(archive_ids) < 2:
  29. raise ValueError("At least 2 archives required for comparison")
  30. if len(archive_ids) > 5:
  31. raise ValueError("Maximum 5 archives can be compared at once")
  32. # Fetch archives
  33. result = await self.db.execute(
  34. select(PrintArchive)
  35. .options(selectinload(PrintArchive.project))
  36. .where(PrintArchive.id.in_(archive_ids))
  37. )
  38. archives = {a.id: a for a in result.scalars().all()}
  39. if len(archives) != len(archive_ids):
  40. missing = set(archive_ids) - set(archives.keys())
  41. raise ValueError(f"Archives not found: {missing}")
  42. # Preserve order from input
  43. ordered_archives = [archives[id] for id in archive_ids]
  44. # Build basic info for each archive
  45. archive_info = [
  46. {
  47. "id": a.id,
  48. "print_name": a.print_name or a.filename,
  49. "status": a.status,
  50. "created_at": a.created_at.isoformat() if a.created_at else None,
  51. "printer_id": a.printer_id,
  52. "project_name": a.project.name if a.project else None,
  53. }
  54. for a in ordered_archives
  55. ]
  56. # Build field comparison
  57. comparison = []
  58. differences = []
  59. for field_name, display_name, unit in self.COMPARABLE_FIELDS:
  60. values = [getattr(a, field_name) for a in ordered_archives]
  61. # Format values for display
  62. formatted_values = []
  63. for v in values:
  64. if v is None:
  65. formatted_values.append(None)
  66. elif field_name == "print_time_seconds":
  67. # Format as human-readable time
  68. hours = int(v) // 3600
  69. minutes = (int(v) % 3600) // 60
  70. formatted_values.append(f"{hours}h {minutes}m" if hours else f"{minutes}m")
  71. elif isinstance(v, float):
  72. formatted_values.append(round(v, 2))
  73. else:
  74. formatted_values.append(v)
  75. # Check if values differ
  76. non_none_values = [v for v in values if v is not None]
  77. has_difference = len(set(str(v) for v in non_none_values)) > 1 if non_none_values else False
  78. field_data = {
  79. "field": field_name,
  80. "label": display_name,
  81. "unit": unit,
  82. "values": formatted_values,
  83. "raw_values": values,
  84. "has_difference": has_difference,
  85. }
  86. comparison.append(field_data)
  87. if has_difference:
  88. differences.append(field_data)
  89. # Analyze success/failure correlation
  90. success_correlation = self._analyze_success_correlation(ordered_archives)
  91. return {
  92. "archives": archive_info,
  93. "comparison": comparison,
  94. "differences": differences,
  95. "success_correlation": success_correlation,
  96. }
  97. def _analyze_success_correlation(self, archives: list[PrintArchive]) -> dict:
  98. """Analyze what settings correlate with success/failure."""
  99. successful = [a for a in archives if a.status == "completed"]
  100. failed = [a for a in archives if a.status == "failed"]
  101. if not successful or not failed:
  102. return {
  103. "has_both_outcomes": False,
  104. "message": "Need both successful and failed prints to analyze correlation",
  105. }
  106. # Find settings that differ between successful and failed
  107. insights = []
  108. for field_name, display_name, unit in self.COMPARABLE_FIELDS:
  109. if field_name == "status":
  110. continue
  111. success_values = [getattr(a, field_name) for a in successful if getattr(a, field_name) is not None]
  112. failed_values = [getattr(a, field_name) for a in failed if getattr(a, field_name) is not None]
  113. if not success_values or not failed_values:
  114. continue
  115. # For numeric fields, compare averages
  116. if isinstance(success_values[0], (int, float)):
  117. success_avg = sum(success_values) / len(success_values)
  118. failed_avg = sum(failed_values) / len(failed_values)
  119. if abs(success_avg - failed_avg) > 0.1 * max(abs(success_avg), abs(failed_avg), 0.01):
  120. direction = "higher" if success_avg > failed_avg else "lower"
  121. insights.append({
  122. "field": field_name,
  123. "label": display_name,
  124. "success_avg": round(success_avg, 2),
  125. "failed_avg": round(failed_avg, 2),
  126. "insight": f"Successful prints had {direction} {display_name}",
  127. })
  128. else:
  129. # For categorical fields, check if success uses different values
  130. success_set = set(str(v) for v in success_values)
  131. failed_set = set(str(v) for v in failed_values)
  132. if success_set != failed_set:
  133. insights.append({
  134. "field": field_name,
  135. "label": display_name,
  136. "success_values": list(success_set),
  137. "failed_values": list(failed_set),
  138. "insight": f"Different {display_name} used in successful vs failed prints",
  139. })
  140. return {
  141. "has_both_outcomes": True,
  142. "successful_count": len(successful),
  143. "failed_count": len(failed),
  144. "insights": insights,
  145. }
  146. async def find_similar_archives(
  147. self,
  148. archive_id: int,
  149. limit: int = 10,
  150. ) -> list[dict]:
  151. """Find archives with similar settings for comparison.
  152. Args:
  153. archive_id: The archive to find similar ones for
  154. limit: Maximum number of results
  155. Returns:
  156. List of similar archives with match reasons
  157. """
  158. # Get the reference archive
  159. result = await self.db.execute(
  160. select(PrintArchive).where(PrintArchive.id == archive_id)
  161. )
  162. reference = result.scalar_one_or_none()
  163. if not reference:
  164. raise ValueError("Archive not found")
  165. # Find similar archives
  166. similar = []
  167. # By same print name
  168. if reference.print_name:
  169. result = await self.db.execute(
  170. select(PrintArchive)
  171. .where(
  172. PrintArchive.id != archive_id,
  173. PrintArchive.print_name == reference.print_name,
  174. )
  175. .order_by(PrintArchive.created_at.desc())
  176. .limit(limit)
  177. )
  178. for a in result.scalars().all():
  179. similar.append({
  180. "archive": {
  181. "id": a.id,
  182. "print_name": a.print_name or a.filename,
  183. "status": a.status,
  184. "created_at": a.created_at.isoformat() if a.created_at else None,
  185. },
  186. "match_reason": "Same print name",
  187. "match_score": 100,
  188. })
  189. # By content hash
  190. if reference.content_hash and len(similar) < limit:
  191. result = await self.db.execute(
  192. select(PrintArchive)
  193. .where(
  194. PrintArchive.id != archive_id,
  195. PrintArchive.content_hash == reference.content_hash,
  196. )
  197. .order_by(PrintArchive.created_at.desc())
  198. .limit(limit - len(similar))
  199. )
  200. for a in result.scalars().all():
  201. if not any(s["archive"]["id"] == a.id for s in similar):
  202. similar.append({
  203. "archive": {
  204. "id": a.id,
  205. "print_name": a.print_name or a.filename,
  206. "status": a.status,
  207. "created_at": a.created_at.isoformat() if a.created_at else None,
  208. },
  209. "match_reason": "Same file content",
  210. "match_score": 95,
  211. })
  212. # By same filament type
  213. if reference.filament_type and len(similar) < limit:
  214. result = await self.db.execute(
  215. select(PrintArchive)
  216. .where(
  217. PrintArchive.id != archive_id,
  218. PrintArchive.filament_type == reference.filament_type,
  219. )
  220. .order_by(PrintArchive.created_at.desc())
  221. .limit(limit - len(similar))
  222. )
  223. for a in result.scalars().all():
  224. if not any(s["archive"]["id"] == a.id for s in similar):
  225. similar.append({
  226. "archive": {
  227. "id": a.id,
  228. "print_name": a.print_name or a.filename,
  229. "status": a.status,
  230. "created_at": a.created_at.isoformat() if a.created_at else None,
  231. },
  232. "match_reason": f"Same filament type ({reference.filament_type})",
  233. "match_score": 50,
  234. })
  235. # Sort by match score
  236. similar.sort(key=lambda x: x["match_score"], reverse=True)
  237. return similar[:limit]