maintenance.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. """Maintenance tracking API routes."""
  2. import logging
  3. from datetime import datetime, timezone
  4. from fastapi import APIRouter, Depends, HTTPException
  5. from sqlalchemy import select
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from sqlalchemy.orm import selectinload
  8. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  9. from backend.app.core.database import get_db
  10. from backend.app.core.permissions import Permission
  11. from backend.app.models.maintenance import MaintenanceHistory, MaintenanceType, PrinterMaintenance
  12. from backend.app.models.printer import Printer
  13. from backend.app.models.user import User
  14. from backend.app.schemas.maintenance import (
  15. MaintenanceHistoryResponse,
  16. MaintenanceStatus,
  17. MaintenanceTypeCreate,
  18. MaintenanceTypeResponse,
  19. MaintenanceTypeUpdate,
  20. PerformMaintenanceRequest,
  21. PrinterMaintenanceOverview,
  22. PrinterMaintenanceResponse,
  23. PrinterMaintenanceUpdate,
  24. )
  25. from backend.app.services.notification_service import notification_service
  26. from backend.app.utils.printer_models import get_rod_type
  27. logger = logging.getLogger(__name__)
  28. router = APIRouter(prefix="/maintenance", tags=["maintenance"])
  29. # Default maintenance types
  30. DEFAULT_MAINTENANCE_TYPES = [
  31. # Carbon rod models only (X1/P1)
  32. # Note: carbon rods must NOT be lubricated — they use plain bearings
  33. # and lubrication degrades print quality. Only cleaning is offered.
  34. {
  35. "name": "Clean Carbon Rods",
  36. "description": "Wipe carbon rods with a dry cloth",
  37. "default_interval_hours": 100.0,
  38. "icon": "Sparkles",
  39. },
  40. # Steel rod models only (P2S)
  41. {
  42. "name": "Lubricate Steel Rods",
  43. "description": "Apply lubricant to steel rods for smooth motion",
  44. "default_interval_hours": 50.0,
  45. "icon": "Droplet",
  46. },
  47. {
  48. "name": "Clean Steel Rods",
  49. "description": "Wipe steel rods with a dry cloth",
  50. "default_interval_hours": 100.0,
  51. "icon": "Sparkles",
  52. },
  53. # Linear rail models only (A1/H2)
  54. {
  55. "name": "Lubricate Linear Rails",
  56. "description": "Apply lubricant to linear rails for smooth motion",
  57. "default_interval_hours": 50.0,
  58. "icon": "Droplet",
  59. },
  60. {
  61. "name": "Clean Linear Rails",
  62. "description": "Wipe linear rails with a dry cloth to remove dust and debris",
  63. "default_interval_hours": 100.0,
  64. "icon": "Sparkles",
  65. },
  66. # Universal (all models)
  67. {
  68. "name": "Clean Nozzle/Hotend",
  69. "description": "Clean nozzle exterior and perform cold pull if needed",
  70. "default_interval_hours": 100.0,
  71. "icon": "Flame",
  72. },
  73. {
  74. "name": "Check Belt Tension",
  75. "description": "Verify and adjust belt tension for X/Y axes",
  76. "default_interval_hours": 200.0,
  77. "icon": "Ruler",
  78. },
  79. {
  80. "name": "Clean Build Plate",
  81. "description": "Deep clean build plate with IPA or soap",
  82. "default_interval_hours": 25.0,
  83. "icon": "Square",
  84. },
  85. {
  86. "name": "Check PTFE Tube",
  87. "description": "Inspect PTFE tube for wear or discoloration",
  88. "default_interval_hours": 500.0,
  89. "icon": "Cable",
  90. },
  91. ]
  92. # System types that only apply to printers with a specific rod/rail type.
  93. # "carbon" = X1/P1 series (carbon rods), "steel_rod" = P2S (steel rods),
  94. # "linear_rail" = A1/H2 series. Types not listed here apply to all printers.
  95. _ROD_TYPE_REQUIREMENTS: dict[str, str] = {
  96. "Clean Carbon Rods": "carbon",
  97. "Lubricate Steel Rods": "steel_rod",
  98. "Clean Steel Rods": "steel_rod",
  99. "Lubricate Linear Rails": "linear_rail",
  100. "Clean Linear Rails": "linear_rail",
  101. }
  102. def _should_apply_to_printer(type_name: str, printer_model: str | None) -> bool:
  103. """Check if a system maintenance type should apply to a given printer model."""
  104. rod_requirement = _ROD_TYPE_REQUIREMENTS.get(type_name)
  105. if rod_requirement is None:
  106. return True # Not model-specific, applies to all
  107. rod_type = get_rod_type(printer_model)
  108. if rod_type is None:
  109. # Unknown model — default to carbon rods (legacy behavior)
  110. return rod_requirement == "carbon"
  111. return rod_type == rod_requirement
  112. async def get_printer_total_hours(db: AsyncSession, printer_id: int) -> float:
  113. """Calculate total active hours for a printer from runtime counter plus offset.
  114. Uses the runtime_seconds counter which tracks actual machine active time
  115. (RUNNING state only — paused time is excluded since maintenance intervals
  116. measure mechanical wear, not wall-clock active time, see #1521).
  117. """
  118. # Get printer runtime and offset
  119. result = await db.execute(
  120. select(Printer.runtime_seconds, Printer.print_hours_offset).where(Printer.id == printer_id)
  121. )
  122. row = result.one_or_none()
  123. if not row:
  124. return 0.0
  125. runtime_seconds = row[0] or 0
  126. offset = row[1] or 0.0
  127. runtime_hours = runtime_seconds / 3600.0
  128. return runtime_hours + offset
  129. async def ensure_default_types(db: AsyncSession) -> None:
  130. """Ensure default maintenance types exist, remove stale/duplicate ones."""
  131. result = await db.execute(
  132. select(MaintenanceType).where(MaintenanceType.is_system.is_(True)).order_by(MaintenanceType.id)
  133. )
  134. existing = result.scalars().all()
  135. default_names = {t["name"] for t in DEFAULT_MAINTENANCE_TYPES}
  136. # Remove stale system types no longer in defaults (e.g. renamed types)
  137. # and deduplicate: if concurrent requests created the same type twice,
  138. # keep only the first (lowest id) and delete the rest.
  139. seen_names: set[str] = set()
  140. for t in existing:
  141. if t.name not in default_names or t.name in seen_names:
  142. await db.delete(t)
  143. else:
  144. seen_names.add(t.name)
  145. # Create any missing default types
  146. for type_def in DEFAULT_MAINTENANCE_TYPES:
  147. if type_def["name"] not in seen_names:
  148. new_type = MaintenanceType(
  149. name=type_def["name"],
  150. description=type_def["description"],
  151. default_interval_hours=type_def["default_interval_hours"],
  152. icon=type_def["icon"],
  153. is_system=True,
  154. )
  155. db.add(new_type)
  156. await db.commit()
  157. # ============== Maintenance Types ==============
  158. @router.get("/types", response_model=list[MaintenanceTypeResponse])
  159. async def get_maintenance_types(
  160. db: AsyncSession = Depends(get_db),
  161. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_READ),
  162. ):
  163. """Get all maintenance types."""
  164. await ensure_default_types(db)
  165. result = await db.execute(
  166. select(MaintenanceType)
  167. .where(MaintenanceType.is_deleted.is_(False))
  168. .order_by(MaintenanceType.is_system.desc(), MaintenanceType.name)
  169. )
  170. return result.scalars().all()
  171. @router.post("/types", response_model=MaintenanceTypeResponse)
  172. async def create_maintenance_type(
  173. data: MaintenanceTypeCreate,
  174. db: AsyncSession = Depends(get_db),
  175. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_CREATE),
  176. ):
  177. """Create a custom maintenance type."""
  178. new_type = MaintenanceType(
  179. name=data.name,
  180. description=data.description,
  181. default_interval_hours=data.default_interval_hours,
  182. interval_type=data.interval_type,
  183. icon=data.icon,
  184. wiki_url=data.wiki_url,
  185. is_system=False,
  186. )
  187. db.add(new_type)
  188. await db.commit()
  189. await db.refresh(new_type)
  190. return new_type
  191. @router.patch("/types/{type_id}", response_model=MaintenanceTypeResponse)
  192. async def update_maintenance_type(
  193. type_id: int,
  194. data: MaintenanceTypeUpdate,
  195. db: AsyncSession = Depends(get_db),
  196. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_UPDATE),
  197. ):
  198. """Update a maintenance type."""
  199. result = await db.execute(select(MaintenanceType).where(MaintenanceType.id == type_id))
  200. maint_type = result.scalar_one_or_none()
  201. if not maint_type:
  202. raise HTTPException(status_code=404, detail="Maintenance type not found")
  203. update_data = data.model_dump(exclude_unset=True)
  204. for key, value in update_data.items():
  205. setattr(maint_type, key, value)
  206. await db.commit()
  207. await db.refresh(maint_type)
  208. return maint_type
  209. @router.delete("/types/{type_id}")
  210. async def delete_maintenance_type(
  211. type_id: int,
  212. db: AsyncSession = Depends(get_db),
  213. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_DELETE),
  214. ):
  215. """Delete a maintenance type."""
  216. result = await db.execute(select(MaintenanceType).where(MaintenanceType.id == type_id))
  217. maint_type = result.scalar_one_or_none()
  218. if not maint_type:
  219. raise HTTPException(status_code=404, detail="Maintenance type not found")
  220. if maint_type.is_system:
  221. maint_type.is_deleted = True
  222. await db.commit()
  223. return {"status": "deleted"}
  224. await db.delete(maint_type)
  225. await db.commit()
  226. return {"status": "deleted"}
  227. @router.post("/types/restore-defaults")
  228. async def restore_default_maintenance_types(
  229. db: AsyncSession = Depends(get_db),
  230. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_DELETE),
  231. ):
  232. """Restore deleted default maintenance types."""
  233. await ensure_default_types(db)
  234. result = await db.execute(
  235. select(MaintenanceType).where(MaintenanceType.is_system.is_(True)).where(MaintenanceType.is_deleted.is_(True))
  236. )
  237. deleted_types = result.scalars().all()
  238. for maint_type in deleted_types:
  239. maint_type.is_deleted = False
  240. await db.commit()
  241. return {"restored": len(deleted_types)}
  242. # ============== Printer Maintenance ==============
  243. async def _get_printer_maintenance_internal(
  244. printer_id: int,
  245. db: AsyncSession,
  246. commit: bool = True,
  247. ) -> PrinterMaintenanceOverview:
  248. """Internal helper to get maintenance overview for a specific printer."""
  249. await ensure_default_types(db)
  250. # Get printer
  251. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  252. printer = result.scalar_one_or_none()
  253. if not printer:
  254. raise HTTPException(status_code=404, detail="Printer not found")
  255. total_hours = await get_printer_total_hours(db, printer_id)
  256. # Get all maintenance types
  257. result = await db.execute(select(MaintenanceType).where(MaintenanceType.is_deleted.is_(False)))
  258. all_types = result.scalars().all()
  259. # Get printer's maintenance items
  260. result = await db.execute(
  261. select(PrinterMaintenance)
  262. .where(PrinterMaintenance.printer_id == printer_id)
  263. .options(selectinload(PrinterMaintenance.maintenance_type))
  264. )
  265. existing_items = {item.maintenance_type_id: item for item in result.scalars().all()}
  266. maintenance_items = []
  267. due_count = 0
  268. warning_count = 0
  269. now = datetime.now(timezone.utc)
  270. for maint_type in all_types:
  271. # Skip system types that don't apply to this printer model
  272. # (e.g., "Clean Carbon Rods" for H2D which has steel rods)
  273. if maint_type.is_system and not _should_apply_to_printer(maint_type.name, printer.model):
  274. continue
  275. item = existing_items.get(maint_type.id)
  276. default_interval_type = getattr(maint_type, "interval_type", "hours") or "hours"
  277. if item:
  278. interval = item.custom_interval_hours or maint_type.default_interval_hours
  279. # Use custom interval type if set, otherwise use type's default
  280. interval_type = getattr(item, "custom_interval_type", None) or default_interval_type
  281. enabled = item.enabled
  282. last_performed_hours = item.last_performed_hours
  283. last_performed_at = item.last_performed_at
  284. item_id = item.id
  285. else:
  286. # Only auto-create maintenance items for system types
  287. # Custom types need to be manually assigned per printer
  288. if not maint_type.is_system:
  289. continue
  290. # Create default entry for this printer/type
  291. item = PrinterMaintenance(
  292. printer_id=printer_id,
  293. maintenance_type_id=maint_type.id,
  294. enabled=True,
  295. last_performed_hours=0.0,
  296. )
  297. db.add(item)
  298. await db.flush()
  299. interval = maint_type.default_interval_hours
  300. interval_type = default_interval_type
  301. enabled = True
  302. last_performed_hours = 0.0
  303. last_performed_at = None
  304. item_id = item.id
  305. # Calculate status based on interval type
  306. if interval_type == "days":
  307. # Time-based: calculate days since last performed
  308. if last_performed_at:
  309. # DB stores naive datetimes; treat as UTC for comparison
  310. if last_performed_at.tzinfo is None:
  311. last_performed_at = last_performed_at.replace(tzinfo=timezone.utc)
  312. days_since = (now - last_performed_at).total_seconds() / 86400.0
  313. else:
  314. # Never performed - consider it due
  315. days_since = interval + 1
  316. days_until = interval - days_since
  317. is_due = days_until <= 0
  318. is_warning = days_until <= (interval * 0.1) and not is_due
  319. # For compatibility, also set hours values (but they won't be primary)
  320. hours_since = total_hours - last_performed_hours
  321. hours_until = 0 # Not applicable for time-based
  322. else:
  323. # Print-hours based (default)
  324. hours_since = total_hours - last_performed_hours
  325. hours_until = interval - hours_since
  326. is_due = hours_until <= 0
  327. is_warning = hours_until <= (interval * 0.1) and not is_due
  328. # Calculate days for reference
  329. if last_performed_at:
  330. if last_performed_at.tzinfo is None:
  331. last_performed_at = last_performed_at.replace(tzinfo=timezone.utc)
  332. days_since = (now - last_performed_at).total_seconds() / 86400.0
  333. else:
  334. days_since = None
  335. days_until = None
  336. if enabled:
  337. if is_due:
  338. due_count += 1
  339. elif is_warning:
  340. warning_count += 1
  341. maintenance_items.append(
  342. MaintenanceStatus(
  343. id=item_id,
  344. printer_id=printer_id,
  345. printer_name=printer.name,
  346. printer_model=printer.model,
  347. maintenance_type_id=maint_type.id,
  348. maintenance_type_name=maint_type.name,
  349. maintenance_type_icon=maint_type.icon,
  350. maintenance_type_wiki_url=getattr(maint_type, "wiki_url", None),
  351. enabled=enabled,
  352. interval_hours=interval,
  353. interval_type=interval_type,
  354. current_hours=total_hours,
  355. hours_since_maintenance=hours_since,
  356. hours_until_due=hours_until,
  357. days_since_maintenance=days_since if interval_type == "days" else None,
  358. days_until_due=days_until if interval_type == "days" else None,
  359. is_due=is_due,
  360. is_warning=is_warning,
  361. last_performed_at=last_performed_at,
  362. )
  363. )
  364. if commit:
  365. await db.commit()
  366. return PrinterMaintenanceOverview(
  367. printer_id=printer_id,
  368. printer_name=printer.name,
  369. printer_model=printer.model,
  370. total_print_hours=total_hours,
  371. maintenance_items=maintenance_items,
  372. due_count=due_count,
  373. warning_count=warning_count,
  374. )
  375. @router.get("/printers/{printer_id}", response_model=PrinterMaintenanceOverview)
  376. async def get_printer_maintenance(
  377. printer_id: int,
  378. db: AsyncSession = Depends(get_db),
  379. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_READ),
  380. ):
  381. """Get maintenance overview for a specific printer."""
  382. return await _get_printer_maintenance_internal(printer_id, db, commit=True)
  383. @router.get("/overview", response_model=list[PrinterMaintenanceOverview])
  384. async def get_all_maintenance_overview(
  385. db: AsyncSession = Depends(get_db),
  386. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_READ),
  387. ):
  388. """Get maintenance overview for all active printers."""
  389. await ensure_default_types(db)
  390. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  391. printers = result.scalars().all()
  392. overviews = []
  393. for printer in printers:
  394. # Don't commit after each printer, commit once at the end
  395. overview = await _get_printer_maintenance_internal(printer.id, db, commit=False)
  396. overviews.append(overview)
  397. # Commit any new maintenance items created
  398. await db.commit()
  399. return overviews
  400. @router.patch("/items/{item_id}", response_model=PrinterMaintenanceResponse)
  401. async def update_printer_maintenance(
  402. item_id: int,
  403. data: PrinterMaintenanceUpdate,
  404. db: AsyncSession = Depends(get_db),
  405. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_UPDATE),
  406. ):
  407. """Update a printer maintenance item (e.g., custom interval, enabled)."""
  408. result = await db.execute(
  409. select(PrinterMaintenance)
  410. .where(PrinterMaintenance.id == item_id)
  411. .options(selectinload(PrinterMaintenance.maintenance_type))
  412. )
  413. item = result.scalar_one_or_none()
  414. if not item:
  415. raise HTTPException(status_code=404, detail="Maintenance item not found")
  416. update_data = data.model_dump(exclude_unset=True)
  417. for key, value in update_data.items():
  418. setattr(item, key, value)
  419. await db.commit()
  420. await db.refresh(item)
  421. return item
  422. @router.post("/printers/{printer_id}/assign/{type_id}", response_model=PrinterMaintenanceResponse)
  423. async def assign_maintenance_type(
  424. printer_id: int,
  425. type_id: int,
  426. db: AsyncSession = Depends(get_db),
  427. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_CREATE),
  428. ):
  429. """Assign a maintenance type to a specific printer (for custom types)."""
  430. # Verify printer exists
  431. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  432. printer = result.scalar_one_or_none()
  433. if not printer:
  434. raise HTTPException(status_code=404, detail="Printer not found")
  435. # Verify maintenance type exists
  436. result = await db.execute(select(MaintenanceType).where(MaintenanceType.id == type_id))
  437. maint_type = result.scalar_one_or_none()
  438. if not maint_type:
  439. raise HTTPException(status_code=404, detail="Maintenance type not found")
  440. # Check if already assigned
  441. result = await db.execute(
  442. select(PrinterMaintenance).where(
  443. PrinterMaintenance.printer_id == printer_id,
  444. PrinterMaintenance.maintenance_type_id == type_id,
  445. )
  446. )
  447. existing = result.scalar_one_or_none()
  448. if existing:
  449. raise HTTPException(status_code=400, detail="Maintenance type already assigned to this printer")
  450. # Create the assignment
  451. item = PrinterMaintenance(
  452. printer_id=printer_id,
  453. maintenance_type_id=type_id,
  454. enabled=True,
  455. last_performed_hours=0.0,
  456. )
  457. db.add(item)
  458. await db.commit()
  459. # Re-fetch with relationship loaded for response serialization
  460. from sqlalchemy.orm import selectinload
  461. result = await db.execute(
  462. select(PrinterMaintenance)
  463. .options(selectinload(PrinterMaintenance.maintenance_type))
  464. .where(PrinterMaintenance.id == item.id)
  465. )
  466. item = result.scalar_one()
  467. return item
  468. @router.delete("/items/{item_id}")
  469. async def remove_maintenance_item(
  470. item_id: int,
  471. db: AsyncSession = Depends(get_db),
  472. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_DELETE),
  473. ):
  474. """Remove a maintenance item (unassign a custom type from a printer)."""
  475. result = await db.execute(
  476. select(PrinterMaintenance)
  477. .where(PrinterMaintenance.id == item_id)
  478. .options(selectinload(PrinterMaintenance.maintenance_type))
  479. )
  480. item = result.scalar_one_or_none()
  481. if not item:
  482. raise HTTPException(status_code=404, detail="Maintenance item not found")
  483. # Only allow removing custom (non-system) types
  484. if item.maintenance_type.is_system:
  485. raise HTTPException(status_code=400, detail="Cannot remove system maintenance types")
  486. await db.delete(item)
  487. await db.commit()
  488. return {"status": "removed"}
  489. @router.post("/items/{item_id}/perform", response_model=MaintenanceStatus)
  490. async def perform_maintenance(
  491. item_id: int,
  492. data: PerformMaintenanceRequest,
  493. db: AsyncSession = Depends(get_db),
  494. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_UPDATE),
  495. ):
  496. """Mark maintenance as performed (reset the counter)."""
  497. result = await db.execute(
  498. select(PrinterMaintenance)
  499. .where(PrinterMaintenance.id == item_id)
  500. .options(selectinload(PrinterMaintenance.maintenance_type))
  501. )
  502. item = result.scalar_one_or_none()
  503. if not item:
  504. raise HTTPException(status_code=404, detail="Maintenance item not found")
  505. # Get printer for name
  506. result = await db.execute(select(Printer).where(Printer.id == item.printer_id))
  507. printer = result.scalar_one()
  508. # Get current hours
  509. current_hours = await get_printer_total_hours(db, item.printer_id)
  510. # Create history entry
  511. history = MaintenanceHistory(
  512. printer_maintenance_id=item.id,
  513. hours_at_maintenance=current_hours,
  514. notes=data.notes,
  515. )
  516. db.add(history)
  517. # Update item
  518. item.last_performed_at = datetime.now(timezone.utc)
  519. item.last_performed_hours = current_hours
  520. await db.commit()
  521. # MQTT relay - publish maintenance reset
  522. try:
  523. from backend.app.services.mqtt_relay import mqtt_relay
  524. await mqtt_relay.on_maintenance_reset(
  525. printer_id=item.printer_id,
  526. printer_name=printer.name,
  527. maintenance_type=item.maintenance_type.name,
  528. )
  529. except Exception:
  530. pass # Don't fail if MQTT fails
  531. # Calculate status
  532. interval = item.custom_interval_hours or item.maintenance_type.default_interval_hours
  533. interval_type = getattr(item.maintenance_type, "interval_type", "hours") or "hours"
  534. hours_since = current_hours - item.last_performed_hours
  535. hours_until = interval - hours_since
  536. return MaintenanceStatus(
  537. id=item.id,
  538. printer_id=item.printer_id,
  539. printer_name=printer.name,
  540. printer_model=printer.model,
  541. maintenance_type_id=item.maintenance_type_id,
  542. maintenance_type_name=item.maintenance_type.name,
  543. maintenance_type_icon=item.maintenance_type.icon,
  544. maintenance_type_wiki_url=getattr(item.maintenance_type, "wiki_url", None),
  545. enabled=item.enabled,
  546. interval_hours=interval,
  547. interval_type=interval_type,
  548. current_hours=current_hours,
  549. hours_since_maintenance=hours_since,
  550. hours_until_due=hours_until if interval_type == "hours" else 0,
  551. days_since_maintenance=0 if interval_type == "days" else None,
  552. days_until_due=interval if interval_type == "days" else None,
  553. is_due=False,
  554. is_warning=False,
  555. last_performed_at=item.last_performed_at,
  556. )
  557. @router.get("/items/{item_id}/history", response_model=list[MaintenanceHistoryResponse])
  558. async def get_maintenance_history(
  559. item_id: int,
  560. db: AsyncSession = Depends(get_db),
  561. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_READ),
  562. ):
  563. """Get maintenance history for a specific item."""
  564. result = await db.execute(
  565. select(MaintenanceHistory)
  566. .where(MaintenanceHistory.printer_maintenance_id == item_id)
  567. .order_by(MaintenanceHistory.performed_at.desc())
  568. )
  569. return result.scalars().all()
  570. @router.get("/summary")
  571. async def get_maintenance_summary(
  572. db: AsyncSession = Depends(get_db),
  573. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_READ),
  574. ):
  575. """Get a summary of maintenance status across all printers."""
  576. await ensure_default_types(db)
  577. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  578. printers = result.scalars().all()
  579. total_due = 0
  580. total_warning = 0
  581. printers_with_issues = []
  582. for printer in printers:
  583. overview = await get_printer_maintenance(printer.id, db)
  584. total_due += overview.due_count
  585. total_warning += overview.warning_count
  586. if overview.due_count > 0 or overview.warning_count > 0:
  587. printers_with_issues.append(
  588. {
  589. "printer_id": printer.id,
  590. "printer_name": printer.name,
  591. "due_count": overview.due_count,
  592. "warning_count": overview.warning_count,
  593. }
  594. )
  595. return {
  596. "total_due": total_due,
  597. "total_warning": total_warning,
  598. "printers_with_issues": printers_with_issues,
  599. }
  600. @router.patch("/printers/{printer_id}/hours")
  601. async def set_printer_hours(
  602. printer_id: int,
  603. total_hours: float,
  604. db: AsyncSession = Depends(get_db),
  605. _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_UPDATE),
  606. ):
  607. """Set the total print hours for a printer (adjusts offset to match).
  608. The offset is calculated as: offset = total_hours - runtime_hours
  609. Where runtime_hours comes from the runtime_seconds counter that tracks
  610. actual machine active time (RUNNING state only — paused time excluded, #1521).
  611. """
  612. # Get printer
  613. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  614. printer = result.scalar_one_or_none()
  615. if not printer:
  616. raise HTTPException(status_code=404, detail="Printer not found")
  617. # Get current runtime hours
  618. runtime_hours = (printer.runtime_seconds or 0) / 3600.0
  619. # Calculate needed offset
  620. printer.print_hours_offset = max(0, total_hours - runtime_hours)
  621. await db.commit()
  622. # Check for maintenance items that need attention and send notification
  623. try:
  624. await ensure_default_types(db)
  625. overview = await _get_printer_maintenance_internal(printer_id, db, commit=True)
  626. items_needing_attention = [
  627. {
  628. "name": item.maintenance_type_name,
  629. "is_due": item.is_due,
  630. "is_warning": item.is_warning,
  631. }
  632. for item in overview.maintenance_items
  633. if item.enabled and (item.is_due or item.is_warning)
  634. ]
  635. if items_needing_attention:
  636. await notification_service.on_maintenance_due(printer_id, printer.name, items_needing_attention, db)
  637. logger.info(
  638. f"Sent maintenance notification for printer {printer_id}: "
  639. f"{len(items_needing_attention)} items need attention"
  640. )
  641. except Exception as e:
  642. logger.warning("Failed to send maintenance notification: %s", e)
  643. return {
  644. "printer_id": printer_id,
  645. "total_hours": total_hours,
  646. "runtime_hours": runtime_hours,
  647. "offset_hours": printer.print_hours_offset,
  648. }