print_log.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import logging
  2. from datetime import datetime
  3. from fastapi import APIRouter, Depends, HTTPException, Query
  4. from fastapi.responses import FileResponse
  5. from sqlalchemy import delete, func, select
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from backend.app.core.auth import RequireCameraStreamTokenIfAuthEnabled, RequirePermissionIfAuthEnabled
  8. from backend.app.core.config import settings
  9. from backend.app.core.database import get_db
  10. from backend.app.core.permissions import Permission
  11. from backend.app.models.print_log import PrintLogEntry
  12. from backend.app.models.user import User
  13. from backend.app.schemas.print_log import PrintLogEntrySchema, PrintLogResponse
  14. logger = logging.getLogger(__name__)
  15. router = APIRouter(prefix="/print-log", tags=["print-log"])
  16. @router.get("/", response_model=PrintLogResponse)
  17. async def get_print_log(
  18. search: str | None = None,
  19. printer_id: int | None = None,
  20. created_by_username: str | None = None,
  21. status: str | None = None,
  22. date_from: datetime | None = None,
  23. date_to: datetime | None = None,
  24. limit: int = Query(default=50, ge=1, le=500),
  25. offset: int = Query(default=0, ge=0),
  26. db: AsyncSession = Depends(get_db),
  27. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_READ),
  28. ):
  29. """Get the print log."""
  30. query = select(PrintLogEntry)
  31. count_query = select(func.count(PrintLogEntry.id))
  32. if printer_id is not None:
  33. query = query.where(PrintLogEntry.printer_id == printer_id)
  34. count_query = count_query.where(PrintLogEntry.printer_id == printer_id)
  35. if created_by_username:
  36. query = query.where(PrintLogEntry.created_by_username == created_by_username)
  37. count_query = count_query.where(PrintLogEntry.created_by_username == created_by_username)
  38. if status:
  39. query = query.where(PrintLogEntry.status == status)
  40. count_query = count_query.where(PrintLogEntry.status == status)
  41. if search:
  42. query = query.where(PrintLogEntry.print_name.ilike(f"%{search}%"))
  43. count_query = count_query.where(PrintLogEntry.print_name.ilike(f"%{search}%"))
  44. if date_from:
  45. query = query.where(PrintLogEntry.created_at >= date_from)
  46. count_query = count_query.where(PrintLogEntry.created_at >= date_from)
  47. if date_to:
  48. query = query.where(PrintLogEntry.created_at <= date_to)
  49. count_query = count_query.where(PrintLogEntry.created_at <= date_to)
  50. # Get total count
  51. total_result = await db.execute(count_query)
  52. total = total_result.scalar() or 0
  53. # Get paginated results
  54. query = query.order_by(PrintLogEntry.created_at.desc()).offset(offset).limit(limit)
  55. result = await db.execute(query)
  56. entries = result.scalars().all()
  57. return PrintLogResponse(
  58. items=[
  59. PrintLogEntrySchema(
  60. id=e.id,
  61. print_name=e.print_name,
  62. printer_name=e.printer_name,
  63. printer_id=e.printer_id,
  64. status=e.status,
  65. started_at=e.started_at,
  66. completed_at=e.completed_at,
  67. duration_seconds=e.duration_seconds,
  68. filament_type=e.filament_type,
  69. filament_color=e.filament_color,
  70. filament_used_grams=e.filament_used_grams,
  71. thumbnail_path=e.thumbnail_path,
  72. created_by_username=e.created_by_username,
  73. created_at=e.created_at,
  74. )
  75. for e in entries
  76. ],
  77. total=total,
  78. )
  79. @router.get("/{entry_id}/thumbnail")
  80. async def get_print_log_thumbnail(
  81. entry_id: int,
  82. db: AsyncSession = Depends(get_db),
  83. _: None = RequireCameraStreamTokenIfAuthEnabled,
  84. ):
  85. """Get the thumbnail for a print log entry.
  86. Requires a stream token query param (?token=xxx) when auth is enabled.
  87. Self-heals stale entries: when thumbnail_path points to a file that no
  88. longer exists on disk (archive was deleted, or print failed before the
  89. thumbnail was ever written), NULL the path on the entry so subsequent
  90. page renders skip the request entirely. The frontend's <img> tag is
  91. gated on entry.thumbnail_path being truthy, so the next fetch of the
  92. log list will simply not request this thumbnail again.
  93. """
  94. entry = await db.get(PrintLogEntry, entry_id)
  95. if not entry or not entry.thumbnail_path:
  96. raise HTTPException(404, "Thumbnail not found")
  97. thumb_path = settings.base_dir / entry.thumbnail_path
  98. if not thumb_path.exists():
  99. entry.thumbnail_path = None
  100. await db.commit()
  101. raise HTTPException(404, "Thumbnail file not found")
  102. return FileResponse(
  103. path=thumb_path,
  104. media_type="image/png",
  105. headers={"Cache-Control": "public, max-age=86400"},
  106. )
  107. @router.delete("/")
  108. async def clear_print_log(
  109. db: AsyncSession = Depends(get_db),
  110. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_DELETE_ALL),
  111. ):
  112. """Clear the print log.
  113. Only deletes log entries. Archives and queue items are never touched.
  114. """
  115. result = await db.execute(delete(PrintLogEntry))
  116. deleted = result.rowcount
  117. await db.commit()
  118. logger.info("Print log cleared: %d entries deleted", deleted)
  119. return {"deleted": deleted}