failure_analysis.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. from collections import defaultdict
  2. from datetime import date, datetime, time, timedelta, timezone
  3. from sqlalchemy import and_, func, select
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from backend.app.models.print_log import PrintLogEntry
  6. from backend.app.models.printer import Printer
  7. class FailureAnalysisService:
  8. """Service for analyzing print failure patterns.
  9. Reads from print_log_entries (per-event data) rather than print_archives
  10. so reprints contribute each run and orphan events (archive deleted, log
  11. row survived via ON DELETE SET NULL) still count consistently with
  12. Quick Stats. The archive-based predecessor diverged from Quick Stats
  13. after #1378 moved the rest of the page to per-event aggregation.
  14. """
  15. def __init__(self, db: AsyncSession):
  16. self.db = db
  17. async def analyze_failures(
  18. self,
  19. days: int | None = None,
  20. date_from: date | None = None,
  21. date_to: date | None = None,
  22. printer_id: int | None = None,
  23. project_id: int | None = None,
  24. created_by_id: int | None = None,
  25. ) -> dict:
  26. """Analyze failure patterns across logged print events."""
  27. # Build base query — separate date vs non-date filters for trend reuse
  28. base_filter = []
  29. non_date_filter = []
  30. if date_from or date_to:
  31. if date_from:
  32. dt_from = datetime.combine(date_from, time.min, tzinfo=timezone.utc)
  33. base_filter.append(PrintLogEntry.created_at >= dt_from)
  34. if date_to:
  35. dt_to = datetime.combine(date_to, time.max, tzinfo=timezone.utc)
  36. base_filter.append(PrintLogEntry.created_at <= dt_to)
  37. range_start = dt_from if date_from else datetime.now(timezone.utc) - timedelta(days=365)
  38. range_end = dt_to if date_to else datetime.now(timezone.utc)
  39. effective_days = max((range_end - range_start).days, 1)
  40. else:
  41. effective_days = days if days is not None else 30
  42. cutoff_date = datetime.now(timezone.utc) - timedelta(days=effective_days)
  43. base_filter.append(PrintLogEntry.created_at >= cutoff_date)
  44. if printer_id:
  45. non_date_filter.append(PrintLogEntry.printer_id == printer_id)
  46. # project_id is an archive-level concept; PrintLogEntry has no project
  47. # link, so we resolve it by archive_id where present.
  48. if project_id:
  49. from backend.app.models.archive import PrintArchive
  50. project_archive_ids = await self.db.execute(
  51. select(PrintArchive.id).where(PrintArchive.project_id == project_id)
  52. )
  53. archive_ids = [row[0] for row in project_archive_ids.fetchall()]
  54. if archive_ids:
  55. non_date_filter.append(PrintLogEntry.archive_id.in_(archive_ids))
  56. else:
  57. # No archives in this project → nothing to count
  58. non_date_filter.append(PrintLogEntry.id.is_(None))
  59. if created_by_id is not None:
  60. if created_by_id == -1:
  61. non_date_filter.append(PrintLogEntry.created_by_id.is_(None))
  62. else:
  63. non_date_filter.append(PrintLogEntry.created_by_id == created_by_id)
  64. base_filter.extend(non_date_filter)
  65. # Total counts
  66. total_result = await self.db.execute(select(func.count(PrintLogEntry.id)).where(and_(*base_filter)))
  67. total_prints = total_result.scalar() or 0
  68. failed_result = await self.db.execute(
  69. select(func.count(PrintLogEntry.id)).where(
  70. and_(*base_filter, PrintLogEntry.status.in_(["failed", "aborted"]))
  71. )
  72. )
  73. failed_prints = failed_result.scalar() or 0
  74. failure_rate = (failed_prints / total_prints * 100) if total_prints > 0 else 0
  75. # Failures by reason
  76. reason_result = await self.db.execute(
  77. select(
  78. PrintLogEntry.failure_reason,
  79. func.count(PrintLogEntry.id).label("count"),
  80. )
  81. .where(and_(*base_filter, PrintLogEntry.status.in_(["failed", "aborted"])))
  82. .group_by(PrintLogEntry.failure_reason)
  83. .order_by(func.count(PrintLogEntry.id).desc())
  84. )
  85. failures_by_reason = {(row[0] or "Unknown"): row[1] for row in reason_result.fetchall()}
  86. # Failures by filament type
  87. filament_result = await self.db.execute(
  88. select(
  89. PrintLogEntry.filament_type,
  90. func.count(PrintLogEntry.id).label("count"),
  91. )
  92. .where(and_(*base_filter, PrintLogEntry.status.in_(["failed", "aborted"])))
  93. .group_by(PrintLogEntry.filament_type)
  94. .order_by(func.count(PrintLogEntry.id).desc())
  95. )
  96. failures_by_filament = {(row[0] or "Unknown"): row[1] for row in filament_result.fetchall()}
  97. # Failures by printer
  98. printer_result = await self.db.execute(
  99. select(
  100. PrintLogEntry.printer_id,
  101. func.count(PrintLogEntry.id).label("count"),
  102. )
  103. .where(
  104. and_(
  105. *base_filter,
  106. PrintLogEntry.status.in_(["failed", "aborted"]),
  107. PrintLogEntry.printer_id.isnot(None),
  108. )
  109. )
  110. .group_by(PrintLogEntry.printer_id)
  111. .order_by(func.count(PrintLogEntry.id).desc())
  112. )
  113. failures_by_printer_id = {row[0]: row[1] for row in printer_result.fetchall()}
  114. # Get printer names
  115. if failures_by_printer_id:
  116. printers_result = await self.db.execute(
  117. select(Printer.id, Printer.name).where(Printer.id.in_(failures_by_printer_id.keys()))
  118. )
  119. printer_names = {row[0]: row[1] for row in printers_result.fetchall()}
  120. failures_by_printer = {
  121. printer_names.get(pid, f"Printer {pid}"): count for pid, count in failures_by_printer_id.items()
  122. }
  123. else:
  124. failures_by_printer = {}
  125. # Failures by hour of day
  126. failed_events_result = await self.db.execute(
  127. select(PrintLogEntry.started_at).where(
  128. and_(
  129. *base_filter,
  130. PrintLogEntry.status.in_(["failed", "aborted"]),
  131. PrintLogEntry.started_at.isnot(None),
  132. )
  133. )
  134. )
  135. failures_by_hour = defaultdict(int)
  136. for (started_at,) in failed_events_result.fetchall():
  137. if started_at:
  138. hour = started_at.hour
  139. failures_by_hour[hour] += 1
  140. failures_by_hour_complete = {h: failures_by_hour.get(h, 0) for h in range(24)}
  141. # Recent failures
  142. recent_result = await self.db.execute(
  143. select(PrintLogEntry)
  144. .where(and_(*base_filter, PrintLogEntry.status.in_(["failed", "aborted"])))
  145. .order_by(PrintLogEntry.created_at.desc())
  146. .limit(10)
  147. )
  148. recent_failures = [
  149. {
  150. "id": e.archive_id,
  151. "print_name": e.print_name,
  152. "failure_reason": e.failure_reason,
  153. "filament_type": e.filament_type,
  154. "printer_id": e.printer_id,
  155. "created_at": e.created_at.isoformat() if e.created_at else None,
  156. }
  157. for e in recent_result.scalars().all()
  158. ]
  159. # Failure rate trend (by week)
  160. trend_data = []
  161. num_weeks = max(effective_days // 7, 1)
  162. for i in range(num_weeks):
  163. week_end = datetime.now(timezone.utc) - timedelta(weeks=i)
  164. week_start = week_end - timedelta(weeks=1)
  165. week_filter = [
  166. PrintLogEntry.created_at >= week_start,
  167. PrintLogEntry.created_at < week_end,
  168. *non_date_filter,
  169. ]
  170. week_total = await self.db.execute(select(func.count(PrintLogEntry.id)).where(and_(*week_filter)))
  171. week_failed = await self.db.execute(
  172. select(func.count(PrintLogEntry.id)).where(
  173. and_(*week_filter, PrintLogEntry.status.in_(["failed", "aborted"]))
  174. )
  175. )
  176. total = week_total.scalar() or 0
  177. failed = week_failed.scalar() or 0
  178. rate = (failed / total * 100) if total > 0 else 0
  179. trend_data.append(
  180. {
  181. "week_start": week_start.date().isoformat(),
  182. "total_prints": total,
  183. "failed_prints": failed,
  184. "failure_rate": round(rate, 1),
  185. }
  186. )
  187. trend_data.reverse() # Oldest first
  188. return {
  189. "period_days": effective_days,
  190. "total_prints": total_prints,
  191. "failed_prints": failed_prints,
  192. "failure_rate": round(failure_rate, 1),
  193. "failures_by_reason": failures_by_reason,
  194. "failures_by_filament": failures_by_filament,
  195. "failures_by_printer": failures_by_printer,
  196. "failures_by_hour": failures_by_hour_complete,
  197. "recent_failures": recent_failures,
  198. "trend": trend_data,
  199. }