maintenance.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. """Maintenance tracking API routes."""
  2. import logging
  3. from datetime import datetime
  4. from typing import List
  5. from fastapi import APIRouter, Depends, HTTPException
  6. from sqlalchemy import select, func
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from sqlalchemy.orm import selectinload
  9. from backend.app.core.database import get_db
  10. from backend.app.models.maintenance import MaintenanceType, PrinterMaintenance, MaintenanceHistory
  11. from backend.app.models.printer import Printer
  12. from backend.app.models.archive import PrintArchive
  13. from backend.app.services.notification_service import notification_service
  14. from backend.app.schemas.maintenance import (
  15. MaintenanceTypeCreate,
  16. MaintenanceTypeUpdate,
  17. MaintenanceTypeResponse,
  18. PrinterMaintenanceCreate,
  19. PrinterMaintenanceUpdate,
  20. PrinterMaintenanceResponse,
  21. MaintenanceHistoryResponse,
  22. MaintenanceStatus,
  23. PrinterMaintenanceOverview,
  24. PerformMaintenanceRequest,
  25. )
  26. logger = logging.getLogger(__name__)
  27. router = APIRouter(prefix="/maintenance", tags=["maintenance"])
  28. # Default maintenance types
  29. DEFAULT_MAINTENANCE_TYPES = [
  30. {
  31. "name": "Lubricate Linear Rails",
  32. "description": "Apply lubricant to linear rails and rods for smooth motion",
  33. "default_interval_hours": 50.0,
  34. "icon": "Droplet",
  35. },
  36. {
  37. "name": "Clean Nozzle/Hotend",
  38. "description": "Clean nozzle exterior and perform cold pull if needed",
  39. "default_interval_hours": 100.0,
  40. "icon": "Flame",
  41. },
  42. {
  43. "name": "Check Belt Tension",
  44. "description": "Verify and adjust belt tension for X/Y axes",
  45. "default_interval_hours": 200.0,
  46. "icon": "Ruler",
  47. },
  48. {
  49. "name": "Clean Carbon Rods",
  50. "description": "Wipe carbon rods with a dry cloth",
  51. "default_interval_hours": 100.0,
  52. "icon": "Sparkles",
  53. },
  54. {
  55. "name": "Clean Build Plate",
  56. "description": "Deep clean build plate with IPA or soap",
  57. "default_interval_hours": 25.0,
  58. "icon": "Square",
  59. },
  60. {
  61. "name": "Check PTFE Tube",
  62. "description": "Inspect PTFE tube for wear or discoloration",
  63. "default_interval_hours": 500.0,
  64. "icon": "Cable",
  65. },
  66. ]
  67. async def get_printer_total_hours(db: AsyncSession, printer_id: int) -> float:
  68. """Calculate total print hours for a printer from archives plus offset."""
  69. # Get archive hours
  70. result = await db.execute(
  71. select(func.sum(PrintArchive.print_time_seconds))
  72. .where(PrintArchive.printer_id == printer_id)
  73. .where(PrintArchive.status == "completed")
  74. )
  75. total_seconds = result.scalar() or 0
  76. archive_hours = total_seconds / 3600.0
  77. # Get printer offset
  78. result = await db.execute(
  79. select(Printer.print_hours_offset).where(Printer.id == printer_id)
  80. )
  81. offset = result.scalar() or 0.0
  82. return archive_hours + offset
  83. async def ensure_default_types(db: AsyncSession) -> None:
  84. """Ensure default maintenance types exist."""
  85. result = await db.execute(
  86. select(MaintenanceType).where(MaintenanceType.is_system == True)
  87. )
  88. existing = result.scalars().all()
  89. existing_names = {t.name for t in existing}
  90. for type_def in DEFAULT_MAINTENANCE_TYPES:
  91. if type_def["name"] not in existing_names:
  92. new_type = MaintenanceType(
  93. name=type_def["name"],
  94. description=type_def["description"],
  95. default_interval_hours=type_def["default_interval_hours"],
  96. icon=type_def["icon"],
  97. is_system=True,
  98. )
  99. db.add(new_type)
  100. await db.commit()
  101. # ============== Maintenance Types ==============
  102. @router.get("/types", response_model=List[MaintenanceTypeResponse])
  103. async def get_maintenance_types(db: AsyncSession = Depends(get_db)):
  104. """Get all maintenance types."""
  105. await ensure_default_types(db)
  106. result = await db.execute(
  107. select(MaintenanceType).order_by(MaintenanceType.is_system.desc(), MaintenanceType.name)
  108. )
  109. return result.scalars().all()
  110. @router.post("/types", response_model=MaintenanceTypeResponse)
  111. async def create_maintenance_type(
  112. data: MaintenanceTypeCreate,
  113. db: AsyncSession = Depends(get_db),
  114. ):
  115. """Create a custom maintenance type."""
  116. new_type = MaintenanceType(
  117. name=data.name,
  118. description=data.description,
  119. default_interval_hours=data.default_interval_hours,
  120. icon=data.icon,
  121. is_system=False,
  122. )
  123. db.add(new_type)
  124. await db.commit()
  125. await db.refresh(new_type)
  126. return new_type
  127. @router.patch("/types/{type_id}", response_model=MaintenanceTypeResponse)
  128. async def update_maintenance_type(
  129. type_id: int,
  130. data: MaintenanceTypeUpdate,
  131. db: AsyncSession = Depends(get_db),
  132. ):
  133. """Update a maintenance type."""
  134. result = await db.execute(
  135. select(MaintenanceType).where(MaintenanceType.id == type_id)
  136. )
  137. maint_type = result.scalar_one_or_none()
  138. if not maint_type:
  139. raise HTTPException(status_code=404, detail="Maintenance type not found")
  140. update_data = data.model_dump(exclude_unset=True)
  141. for key, value in update_data.items():
  142. setattr(maint_type, key, value)
  143. await db.commit()
  144. await db.refresh(maint_type)
  145. return maint_type
  146. @router.delete("/types/{type_id}")
  147. async def delete_maintenance_type(
  148. type_id: int,
  149. db: AsyncSession = Depends(get_db),
  150. ):
  151. """Delete a custom maintenance type."""
  152. result = await db.execute(
  153. select(MaintenanceType).where(MaintenanceType.id == type_id)
  154. )
  155. maint_type = result.scalar_one_or_none()
  156. if not maint_type:
  157. raise HTTPException(status_code=404, detail="Maintenance type not found")
  158. if maint_type.is_system:
  159. raise HTTPException(status_code=400, detail="Cannot delete system maintenance type")
  160. await db.delete(maint_type)
  161. await db.commit()
  162. return {"status": "deleted"}
  163. # ============== Printer Maintenance ==============
  164. async def _get_printer_maintenance_internal(
  165. printer_id: int,
  166. db: AsyncSession,
  167. commit: bool = True,
  168. ) -> PrinterMaintenanceOverview:
  169. """Internal helper to get maintenance overview for a specific printer."""
  170. await ensure_default_types(db)
  171. # Get printer
  172. result = await db.execute(
  173. select(Printer).where(Printer.id == printer_id)
  174. )
  175. printer = result.scalar_one_or_none()
  176. if not printer:
  177. raise HTTPException(status_code=404, detail="Printer not found")
  178. total_hours = await get_printer_total_hours(db, printer_id)
  179. # Get all maintenance types
  180. result = await db.execute(select(MaintenanceType))
  181. all_types = result.scalars().all()
  182. # Get printer's maintenance items
  183. result = await db.execute(
  184. select(PrinterMaintenance)
  185. .where(PrinterMaintenance.printer_id == printer_id)
  186. .options(selectinload(PrinterMaintenance.maintenance_type))
  187. )
  188. existing_items = {item.maintenance_type_id: item for item in result.scalars().all()}
  189. maintenance_items = []
  190. due_count = 0
  191. warning_count = 0
  192. for maint_type in all_types:
  193. item = existing_items.get(maint_type.id)
  194. if item:
  195. interval = item.custom_interval_hours or maint_type.default_interval_hours
  196. enabled = item.enabled
  197. last_performed_hours = item.last_performed_hours
  198. last_performed_at = item.last_performed_at
  199. item_id = item.id
  200. else:
  201. # Create default entry for this printer/type
  202. item = PrinterMaintenance(
  203. printer_id=printer_id,
  204. maintenance_type_id=maint_type.id,
  205. enabled=True,
  206. last_performed_hours=0.0,
  207. )
  208. db.add(item)
  209. await db.flush()
  210. interval = maint_type.default_interval_hours
  211. enabled = True
  212. last_performed_hours = 0.0
  213. last_performed_at = None
  214. item_id = item.id
  215. hours_since = total_hours - last_performed_hours
  216. hours_until = interval - hours_since
  217. is_due = hours_until <= 0
  218. is_warning = hours_until <= (interval * 0.1) and not is_due
  219. if enabled:
  220. if is_due:
  221. due_count += 1
  222. elif is_warning:
  223. warning_count += 1
  224. maintenance_items.append(MaintenanceStatus(
  225. id=item_id,
  226. printer_id=printer_id,
  227. printer_name=printer.name,
  228. maintenance_type_id=maint_type.id,
  229. maintenance_type_name=maint_type.name,
  230. maintenance_type_icon=maint_type.icon,
  231. enabled=enabled,
  232. interval_hours=interval,
  233. current_hours=total_hours,
  234. hours_since_maintenance=hours_since,
  235. hours_until_due=hours_until,
  236. is_due=is_due,
  237. is_warning=is_warning,
  238. last_performed_at=last_performed_at,
  239. ))
  240. if commit:
  241. await db.commit()
  242. return PrinterMaintenanceOverview(
  243. printer_id=printer_id,
  244. printer_name=printer.name,
  245. total_print_hours=total_hours,
  246. maintenance_items=maintenance_items,
  247. due_count=due_count,
  248. warning_count=warning_count,
  249. )
  250. @router.get("/printers/{printer_id}", response_model=PrinterMaintenanceOverview)
  251. async def get_printer_maintenance(
  252. printer_id: int,
  253. db: AsyncSession = Depends(get_db),
  254. ):
  255. """Get maintenance overview for a specific printer."""
  256. return await _get_printer_maintenance_internal(printer_id, db, commit=True)
  257. @router.get("/overview", response_model=List[PrinterMaintenanceOverview])
  258. async def get_all_maintenance_overview(db: AsyncSession = Depends(get_db)):
  259. """Get maintenance overview for all active printers."""
  260. await ensure_default_types(db)
  261. result = await db.execute(
  262. select(Printer).where(Printer.is_active == True)
  263. )
  264. printers = result.scalars().all()
  265. overviews = []
  266. for printer in printers:
  267. # Don't commit after each printer, commit once at the end
  268. overview = await _get_printer_maintenance_internal(printer.id, db, commit=False)
  269. overviews.append(overview)
  270. # Commit any new maintenance items created
  271. await db.commit()
  272. return overviews
  273. @router.patch("/items/{item_id}", response_model=PrinterMaintenanceResponse)
  274. async def update_printer_maintenance(
  275. item_id: int,
  276. data: PrinterMaintenanceUpdate,
  277. db: AsyncSession = Depends(get_db),
  278. ):
  279. """Update a printer maintenance item (e.g., custom interval, enabled)."""
  280. result = await db.execute(
  281. select(PrinterMaintenance)
  282. .where(PrinterMaintenance.id == item_id)
  283. .options(selectinload(PrinterMaintenance.maintenance_type))
  284. )
  285. item = result.scalar_one_or_none()
  286. if not item:
  287. raise HTTPException(status_code=404, detail="Maintenance item not found")
  288. update_data = data.model_dump(exclude_unset=True)
  289. for key, value in update_data.items():
  290. setattr(item, key, value)
  291. await db.commit()
  292. await db.refresh(item)
  293. return item
  294. @router.post("/items/{item_id}/perform", response_model=MaintenanceStatus)
  295. async def perform_maintenance(
  296. item_id: int,
  297. data: PerformMaintenanceRequest,
  298. db: AsyncSession = Depends(get_db),
  299. ):
  300. """Mark maintenance as performed (reset the counter)."""
  301. result = await db.execute(
  302. select(PrinterMaintenance)
  303. .where(PrinterMaintenance.id == item_id)
  304. .options(selectinload(PrinterMaintenance.maintenance_type))
  305. )
  306. item = result.scalar_one_or_none()
  307. if not item:
  308. raise HTTPException(status_code=404, detail="Maintenance item not found")
  309. # Get printer for name
  310. result = await db.execute(
  311. select(Printer).where(Printer.id == item.printer_id)
  312. )
  313. printer = result.scalar_one()
  314. # Get current hours
  315. current_hours = await get_printer_total_hours(db, item.printer_id)
  316. # Create history entry
  317. history = MaintenanceHistory(
  318. printer_maintenance_id=item.id,
  319. hours_at_maintenance=current_hours,
  320. notes=data.notes,
  321. )
  322. db.add(history)
  323. # Update item
  324. item.last_performed_at = datetime.utcnow()
  325. item.last_performed_hours = current_hours
  326. await db.commit()
  327. # Calculate status
  328. interval = item.custom_interval_hours or item.maintenance_type.default_interval_hours
  329. hours_since = current_hours - item.last_performed_hours
  330. hours_until = interval - hours_since
  331. return MaintenanceStatus(
  332. id=item.id,
  333. printer_id=item.printer_id,
  334. printer_name=printer.name,
  335. maintenance_type_id=item.maintenance_type_id,
  336. maintenance_type_name=item.maintenance_type.name,
  337. maintenance_type_icon=item.maintenance_type.icon,
  338. enabled=item.enabled,
  339. interval_hours=interval,
  340. current_hours=current_hours,
  341. hours_since_maintenance=hours_since,
  342. hours_until_due=hours_until,
  343. is_due=False,
  344. is_warning=False,
  345. last_performed_at=item.last_performed_at,
  346. )
  347. @router.get("/items/{item_id}/history", response_model=List[MaintenanceHistoryResponse])
  348. async def get_maintenance_history(
  349. item_id: int,
  350. db: AsyncSession = Depends(get_db),
  351. ):
  352. """Get maintenance history for a specific item."""
  353. result = await db.execute(
  354. select(MaintenanceHistory)
  355. .where(MaintenanceHistory.printer_maintenance_id == item_id)
  356. .order_by(MaintenanceHistory.performed_at.desc())
  357. )
  358. return result.scalars().all()
  359. @router.get("/summary")
  360. async def get_maintenance_summary(db: AsyncSession = Depends(get_db)):
  361. """Get a summary of maintenance status across all printers."""
  362. await ensure_default_types(db)
  363. result = await db.execute(
  364. select(Printer).where(Printer.is_active == True)
  365. )
  366. printers = result.scalars().all()
  367. total_due = 0
  368. total_warning = 0
  369. printers_with_issues = []
  370. for printer in printers:
  371. overview = await get_printer_maintenance(printer.id, db)
  372. total_due += overview.due_count
  373. total_warning += overview.warning_count
  374. if overview.due_count > 0 or overview.warning_count > 0:
  375. printers_with_issues.append({
  376. "printer_id": printer.id,
  377. "printer_name": printer.name,
  378. "due_count": overview.due_count,
  379. "warning_count": overview.warning_count,
  380. })
  381. return {
  382. "total_due": total_due,
  383. "total_warning": total_warning,
  384. "printers_with_issues": printers_with_issues,
  385. }
  386. @router.patch("/printers/{printer_id}/hours")
  387. async def set_printer_hours(
  388. printer_id: int,
  389. total_hours: float,
  390. db: AsyncSession = Depends(get_db),
  391. ):
  392. """Set the total print hours for a printer (adjusts offset to match)."""
  393. # Get printer
  394. result = await db.execute(
  395. select(Printer).where(Printer.id == printer_id)
  396. )
  397. printer = result.scalar_one_or_none()
  398. if not printer:
  399. raise HTTPException(status_code=404, detail="Printer not found")
  400. # Get current archive hours
  401. result = await db.execute(
  402. select(func.sum(PrintArchive.print_time_seconds))
  403. .where(PrintArchive.printer_id == printer_id)
  404. .where(PrintArchive.status == "completed")
  405. )
  406. total_seconds = result.scalar() or 0
  407. archive_hours = total_seconds / 3600.0
  408. # Calculate needed offset
  409. printer.print_hours_offset = max(0, total_hours - archive_hours)
  410. await db.commit()
  411. # Check for maintenance items that need attention and send notification
  412. try:
  413. await ensure_default_types(db)
  414. overview = await _get_printer_maintenance_internal(printer_id, db, commit=True)
  415. items_needing_attention = [
  416. {
  417. "name": item.maintenance_type_name,
  418. "is_due": item.is_due,
  419. "is_warning": item.is_warning,
  420. }
  421. for item in overview.maintenance_items
  422. if item.enabled and (item.is_due or item.is_warning)
  423. ]
  424. if items_needing_attention:
  425. await notification_service.on_maintenance_due(
  426. printer_id, printer.name, items_needing_attention, db
  427. )
  428. logger.info(
  429. f"Sent maintenance notification for printer {printer_id}: "
  430. f"{len(items_needing_attention)} items need attention"
  431. )
  432. except Exception as e:
  433. logger.warning(f"Failed to send maintenance notification: {e}")
  434. return {
  435. "printer_id": printer_id,
  436. "total_hours": total_hours,
  437. "archive_hours": archive_hours,
  438. "offset_hours": printer.print_hours_offset,
  439. }