print_queue.py 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. """API routes for print queue management."""
  2. import json
  3. import logging
  4. import zipfile
  5. from datetime import datetime, timezone
  6. from pathlib import Path
  7. import defusedxml.ElementTree as ET
  8. from fastapi import APIRouter, Depends, HTTPException, Query
  9. from sqlalchemy import and_, func, inspect, or_, select, update
  10. from sqlalchemy.ext.asyncio import AsyncSession
  11. from sqlalchemy.orm import selectinload
  12. from backend.app.api.routes.library_variants import normalize_model_name, resolve_variant_model
  13. from backend.app.core.auth import RequirePermissionIfAuthEnabled, require_ownership_permission
  14. from backend.app.core.config import settings
  15. from backend.app.core.database import get_db
  16. from backend.app.core.permissions import Permission
  17. from backend.app.models.archive import PrintArchive
  18. from backend.app.models.library import LibraryFile
  19. from backend.app.models.print_batch import PrintBatch, PrintBatchPlate
  20. from backend.app.models.print_queue import PrintQueueItem, PrintQueueVariant
  21. from backend.app.models.printer import Printer
  22. from backend.app.models.project import Project
  23. from backend.app.models.user import User
  24. from backend.app.schemas.print_queue import (
  25. PrintBatchCreate,
  26. PrintBatchDispatchRequest,
  27. PrintBatchPlateProgress,
  28. PrintBatchPlateTarget,
  29. PrintBatchResponse,
  30. PrintBatchUngroupResponse,
  31. PrintBatchUpdate,
  32. PrintQueueBulkUpdate,
  33. PrintQueueBulkUpdateResponse,
  34. PrintQueueItemCreate,
  35. PrintQueueItemResponse,
  36. PrintQueueItemUpdate,
  37. PrintQueueReorder,
  38. QueueVariantCreate,
  39. QueueVariantSummary,
  40. )
  41. from backend.app.services.filament_deficit import compute_deficit_for_queue_item
  42. from backend.app.services.filament_requirements import overrides_for_plate
  43. from backend.app.services.notification_service import notification_service
  44. from backend.app.services.print_batch import (
  45. BatchDispatchError,
  46. dispatch_remaining,
  47. load_progress,
  48. refresh_batch_status,
  49. )
  50. from backend.app.utils.printer_models import (
  51. is_gcode_compatible,
  52. )
  53. from backend.app.utils.threemf_tools import (
  54. extract_plate_metadata_from_3mf,
  55. extract_print_time_from_3mf,
  56. )
  57. logger = logging.getLogger(__name__)
  58. router = APIRouter(prefix="/queue", tags=["queue"])
  59. def _variant_summaries(item: PrintQueueItem) -> list[QueueVariantSummary]:
  60. """Cross-model candidates for display (#671), or [] if they weren't loaded.
  61. Every route that builds a queue response eager-loads ``variants``. Reading
  62. the attribute unguarded would still be a landmine for the next one that
  63. doesn't: a lazy load on an async session raises rather than degrading, so a
  64. forgotten ``selectinload`` would turn a card render into a 500.
  65. """
  66. if "variants" in inspect(item).unloaded:
  67. return []
  68. return [
  69. QueueVariantSummary(
  70. library_file_id=v.library_file_id,
  71. filename=v.library_file.filename if v.library_file else "",
  72. target_model=v.target_model,
  73. position=v.position,
  74. )
  75. for v in item.variants
  76. ]
  77. def _extract_filament_types_from_3mf(file_path: Path, plate_id: int | None = None) -> list[str]:
  78. """Extract unique filament types from a 3MF file.
  79. Args:
  80. file_path: Path to the 3MF file
  81. plate_id: Optional plate index to filter for (for multi-plate files)
  82. Returns:
  83. List of unique filament types (e.g., ["PLA", "PETG"])
  84. """
  85. types: set[str] = set()
  86. try:
  87. with zipfile.ZipFile(file_path, "r") as zf:
  88. if "Metadata/slice_info.config" not in zf.namelist():
  89. return []
  90. content = zf.read("Metadata/slice_info.config").decode()
  91. root = ET.fromstring(content)
  92. if plate_id is not None:
  93. # Find the plate element with matching index
  94. for plate_elem in root.findall(".//plate"):
  95. plate_index = None
  96. for meta in plate_elem.findall("metadata"):
  97. if meta.get("key") == "index":
  98. try:
  99. plate_index = int(meta.get("value", "0"))
  100. except ValueError:
  101. pass # Skip plate with unparseable index
  102. break
  103. if plate_index == plate_id:
  104. for filament_elem in plate_elem.findall("filament"):
  105. filament_type = filament_elem.get("type", "")
  106. used_g = filament_elem.get("used_g", "0")
  107. try:
  108. used_grams = float(used_g)
  109. except (ValueError, TypeError):
  110. used_grams = 0
  111. if used_grams > 0 and filament_type:
  112. types.add(filament_type)
  113. break
  114. else:
  115. # No plate_id specified - extract all filaments with used_g > 0
  116. for filament_elem in root.findall(".//filament"):
  117. filament_type = filament_elem.get("type", "")
  118. used_g = filament_elem.get("used_g", "0")
  119. try:
  120. used_grams = float(used_g)
  121. except (ValueError, TypeError):
  122. used_grams = 0
  123. if used_grams > 0 and filament_type:
  124. types.add(filament_type)
  125. except Exception as e:
  126. logger.warning("Failed to extract filament types from %s: %s", file_path, e)
  127. return sorted(types)
  128. # Local alias kept so existing call sites stay compact; the implementation lives
  129. # in utils/threemf_tools.py so the notification path (main.py) can reuse it
  130. # without importing from a routes module (#1785).
  131. _extract_print_time_from_3mf = extract_print_time_from_3mf
  132. def _assert_can_queue_archive(archive: PrintArchive, current_user: User | None) -> None:
  133. """Gate turning *archive* into a print. Raises rather than returning a verdict.
  134. Shared by every route that creates queue items from an archive, so a new
  135. one can't quietly become a weaker door to the same action than
  136. ``POST /queue/`` is.
  137. Two separate checks:
  138. * IDOR fix (maziggy/bambuddy-security #2): without this, a caller with
  139. QUEUE_CREATE could queue any user's archive even without ARCHIVES_READ on
  140. it — Landon's PoC enumerated this on admin's archives as operator1. Gate
  141. on ARCHIVES_READ_ALL OR ownership. 404 (not 403) so we don't leak "this
  142. id exists but you can't queue it" for enumeration.
  143. * Reprint perm gate (#1625): the legacy ``/archives/{id}/reprint`` endpoint
  144. required ARCHIVES_REPRINT_OWN/ALL, and every route that replaces it must
  145. keep that gate or an operator with QUEUE_CREATE could reprint via a
  146. direct API call even when explicitly denied reprint perm. Mirrors the
  147. frontend ``canModify('archives', 'reprint', ...)`` helper: REPRINT_ALL
  148. allows any archive, REPRINT_OWN allows own only, ownerless archives
  149. require REPRINT_ALL (fail-closed).
  150. """
  151. if current_user is None:
  152. return
  153. if not current_user.has_permission(Permission.ARCHIVES_READ_ALL.value) and archive.created_by_id != current_user.id:
  154. raise HTTPException(404, "Archive not found")
  155. owns_archive = archive.created_by_id is not None and archive.created_by_id == current_user.id
  156. has_reprint = current_user.has_permission(Permission.ARCHIVES_REPRINT_ALL.value) or (
  157. owns_archive and current_user.has_permission(Permission.ARCHIVES_REPRINT_OWN.value)
  158. )
  159. if not has_reprint:
  160. raise HTTPException(
  161. status_code=403,
  162. detail="Permission archives:reprint_own or archives:reprint_all required",
  163. )
  164. def _assert_can_queue_library_file(library_file: LibraryFile, current_user: User | None) -> None:
  165. """Gate turning *library_file* into a print — LIBRARY_READ_ALL or ownership."""
  166. if current_user is None:
  167. return
  168. if (
  169. not current_user.has_permission(Permission.LIBRARY_READ_ALL.value)
  170. and library_file.created_by_id != current_user.id
  171. ):
  172. raise HTTPException(404, "Library file not found")
  173. async def _assert_can_dispatch_batch_sources(db: AsyncSession, batch_id: int, current_user: User | None) -> None:
  174. """Apply the ``POST /queue/`` source-file gates to everything a dispatch would print.
  175. Dispatching clones existing queue items, so without this it would be a
  176. weaker door to the same outcome: a caller holding QUEUE_CREATE and
  177. QUEUE_UPDATE_ALL but explicitly denied ``archives:reprint_*`` could start
  178. prints through an order that ``POST /queue/`` would have refused them.
  179. Every distinct source among the batch's items is checked, including the
  180. library files behind cross-model variants — those get cloned too, and any
  181. one of them may be the file that actually runs.
  182. """
  183. archive_ids = set(
  184. (
  185. await db.execute(
  186. select(PrintQueueItem.archive_id)
  187. .where(PrintQueueItem.batch_id == batch_id)
  188. .where(PrintQueueItem.archive_id.is_not(None))
  189. .distinct()
  190. )
  191. )
  192. .scalars()
  193. .all()
  194. )
  195. library_file_ids = set(
  196. (
  197. await db.execute(
  198. select(PrintQueueItem.library_file_id)
  199. .where(PrintQueueItem.batch_id == batch_id)
  200. .where(PrintQueueItem.library_file_id.is_not(None))
  201. .distinct()
  202. )
  203. )
  204. .scalars()
  205. .all()
  206. )
  207. library_file_ids |= set(
  208. (
  209. await db.execute(
  210. select(PrintQueueVariant.library_file_id)
  211. .join(PrintQueueItem, PrintQueueVariant.queue_item_id == PrintQueueItem.id)
  212. .where(PrintQueueItem.batch_id == batch_id)
  213. .distinct()
  214. )
  215. )
  216. .scalars()
  217. .all()
  218. )
  219. for archive_id in archive_ids:
  220. archive = (await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))).scalar_one_or_none()
  221. # A deleted source can't be printed; dispatch will fail on it anyway.
  222. if archive is not None:
  223. _assert_can_queue_archive(archive, current_user)
  224. for library_file_id in library_file_ids:
  225. library_file = (
  226. await db.execute(LibraryFile.active().where(LibraryFile.id == library_file_id))
  227. ).scalar_one_or_none()
  228. if library_file is not None:
  229. _assert_can_queue_library_file(library_file, current_user)
  230. async def _resolve_source_path(db: AsyncSession, item: PrintQueueItem) -> Path | None:
  231. """Resolve an existing queue item's source 3MF on disk, or None."""
  232. if item.archive_id:
  233. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  234. archive = result.scalar_one_or_none()
  235. if archive:
  236. return settings.base_dir / archive.file_path
  237. elif item.library_file_id:
  238. result = await db.execute(LibraryFile.active().where(LibraryFile.id == item.library_file_id))
  239. library_file = result.scalar_one_or_none()
  240. if library_file:
  241. lib_path = Path(library_file.file_path)
  242. return lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  243. return None
  244. def _enrich_response(item: PrintQueueItem) -> PrintQueueItemResponse:
  245. """Add nested archive/printer/library_file info to response."""
  246. # Parse ams_mapping from JSON string BEFORE model_validate
  247. ams_mapping_parsed = None
  248. if item.ams_mapping:
  249. try:
  250. ams_mapping_parsed = json.loads(item.ams_mapping)
  251. except json.JSONDecodeError:
  252. ams_mapping_parsed = None
  253. # Parse required_filament_types from JSON string
  254. required_filament_types_parsed = None
  255. if item.required_filament_types:
  256. try:
  257. required_filament_types_parsed = json.loads(item.required_filament_types)
  258. except json.JSONDecodeError:
  259. required_filament_types_parsed = None
  260. # Parse filament_overrides from JSON string
  261. filament_overrides_parsed = None
  262. if item.filament_overrides:
  263. try:
  264. filament_overrides_parsed = json.loads(item.filament_overrides)
  265. except json.JSONDecodeError:
  266. filament_overrides_parsed = None
  267. # Parse nozzle_mapping from JSON string (#1780 — H2C rack slicer-pick
  268. # preservation). Nullable opaque JSON blob stored verbatim from
  269. # BambuStudio's project_file; surface it parsed for the response model
  270. # and any future "edit print → nozzle" UI.
  271. nozzle_mapping_parsed = None
  272. if item.nozzle_mapping:
  273. try:
  274. nozzle_mapping_parsed = json.loads(item.nozzle_mapping)
  275. except json.JSONDecodeError:
  276. nozzle_mapping_parsed = None
  277. nozzles_info_parsed = None
  278. if item.nozzles_info:
  279. try:
  280. nozzles_info_parsed = json.loads(item.nozzles_info)
  281. except json.JSONDecodeError:
  282. nozzles_info_parsed = None
  283. # Create response with parsed ams_mapping
  284. item_dict = {
  285. "id": item.id,
  286. "printer_id": item.printer_id,
  287. "target_model": item.target_model,
  288. "target_location": item.target_location,
  289. "required_filament_types": required_filament_types_parsed,
  290. "filament_overrides": filament_overrides_parsed,
  291. "waiting_reason": item.waiting_reason,
  292. "archive_id": item.archive_id,
  293. "library_file_id": item.library_file_id,
  294. "position": item.position,
  295. "scheduled_time": item.scheduled_time,
  296. "require_previous_success": item.require_previous_success,
  297. "auto_off_after": item.auto_off_after,
  298. "manual_start": item.manual_start,
  299. "filament_short": bool(item.filament_short),
  300. "skip_filament_check": bool(item.skip_filament_check),
  301. "ams_mapping": ams_mapping_parsed,
  302. "plate_id": item.plate_id,
  303. "bed_levelling": item.bed_levelling,
  304. "flow_cali": item.flow_cali,
  305. "vibration_cali": item.vibration_cali,
  306. "layer_inspect": item.layer_inspect,
  307. "timelapse": item.timelapse,
  308. "use_ams": item.use_ams,
  309. "nozzle_offset_cali": item.nozzle_offset_cali,
  310. "preheat_override": item.preheat_override,
  311. "preheat_chamber_target_override": item.preheat_chamber_target_override,
  312. "status": item.status,
  313. "started_at": item.started_at,
  314. "completed_at": item.completed_at,
  315. "error_message": item.error_message,
  316. "created_at": item.created_at,
  317. # User tracking (Issue #206)
  318. "created_by_id": item.created_by_id,
  319. "created_by_username": item.created_by.username if item.created_by else None,
  320. # Batch grouping
  321. "batch_id": item.batch_id,
  322. "batch_name": item.batch.name if item.batch else None,
  323. # SJF scheduling
  324. "been_jumped": item.been_jumped,
  325. # Auto-print G-code injection
  326. "gcode_injection": item.gcode_injection,
  327. # H2C rack-swap nozzle pick (#1780)
  328. "nozzle_mapping": nozzle_mapping_parsed,
  329. "nozzles_info": nozzles_info_parsed,
  330. "cleanup_library_after_dispatch": item.cleanup_library_after_dispatch,
  331. # Cross-model alternatives (#671). Guarded rather than read directly:
  332. # every route that reaches here eager-loads the relationship, but a
  333. # caller that forgets would trigger a lazy load, and a lazy load on an
  334. # async session raises rather than degrading. An empty list is the
  335. # correct answer for the ordinary item this would most likely be.
  336. "variants": _variant_summaries(item),
  337. }
  338. response = PrintQueueItemResponse(**item_dict)
  339. if item.archive:
  340. # Soft-deleted archive: files are gone from disk but the row stays
  341. # (its filament/cost contribution still flows into stats per #1343).
  342. # Suppress the archive-derived UI surface so the queue page doesn't
  343. # 404-storm the thumbnail / plates / plate-thumbnail endpoints — the
  344. # frontend's existing truthy gate on archive_thumbnail covers it
  345. # (#1348 follow-up). The archive_deleted flag lets the UI render a
  346. # "source deleted" badge on these rows.
  347. if item.archive.deleted_at is not None:
  348. response.archive_deleted = True
  349. else:
  350. response.archive_name = item.archive.print_name or item.archive.filename
  351. response.archive_thumbnail = item.archive.thumbnail_path
  352. response.print_time_seconds = item.archive.print_time_seconds
  353. response.filament_used_grams = item.archive.filament_used_grams
  354. response.filament_type = item.archive.filament_type
  355. response.filament_color = item.archive.filament_color
  356. response.layer_height = item.archive.layer_height
  357. response.nozzle_diameter = item.archive.nozzle_diameter
  358. response.sliced_for_model = item.archive.sliced_for_model
  359. response.bed_type = item.archive.bed_type
  360. # Marks history/reprint rows whose archive carries the slicer's own
  361. # live-resolved AMS-slot pick (extra_data.slicer_ams_mapping) — see
  362. # `_extract_slicer_ams_mapping_json` in virtual_printer/manager.py.
  363. #
  364. # Only when the saved mapping was resolved against *this* row's
  365. # printer: a global tray ID means nothing on another printer, so
  366. # that's the exact condition under which the mapping is reused. A
  367. # badge on a row where nothing gets reused would be a lie (#2700
  368. # review). Model-based rows (printer_id None) never match, which is
  369. # correct — the mapping is not reused there either.
  370. extra = item.archive.extra_data if isinstance(item.archive.extra_data, dict) else {}
  371. saved_mapping = extra.get("slicer_ams_mapping")
  372. response.archive_has_slicer_ams_mapping = (
  373. isinstance(saved_mapping, dict)
  374. and isinstance(saved_mapping.get("mapping"), list)
  375. and item.printer_id is not None
  376. and saved_mapping.get("printer_id") == item.printer_id
  377. )
  378. if item.plate_id:
  379. archive_path = settings.base_dir / item.archive.file_path
  380. if archive_path.exists():
  381. # One cached parse for all three per-plate overrides (#2573).
  382. plate_meta = extract_plate_metadata_from_3mf(archive_path, item.plate_id)
  383. if plate_meta.print_time_seconds is not None:
  384. response.print_time_seconds = plate_meta.print_time_seconds
  385. if plate_meta.filament_used_grams > 0:
  386. response.filament_used_grams = plate_meta.filament_used_grams
  387. if plate_meta.bed_type:
  388. response.bed_type = plate_meta.bed_type
  389. if item.library_file:
  390. response.library_file_name = (
  391. item.library_file.file_metadata.get("print_name") if item.library_file.file_metadata else None
  392. )
  393. if not response.library_file_name:
  394. response.library_file_name = item.library_file.filename
  395. response.library_file_thumbnail = item.library_file.thumbnail_path
  396. # Get metadata from library file if no archive
  397. if not item.archive and item.library_file.file_metadata:
  398. response.print_time_seconds = item.library_file.file_metadata.get("print_time_seconds")
  399. response.filament_used_grams = item.library_file.file_metadata.get("filament_used_grams")
  400. response.filament_type = item.library_file.file_metadata.get("filament_type")
  401. response.filament_color = item.library_file.file_metadata.get("filament_color")
  402. response.layer_height = item.library_file.file_metadata.get("layer_height")
  403. response.nozzle_diameter = item.library_file.file_metadata.get("nozzle_diameter")
  404. response.sliced_for_model = item.library_file.file_metadata.get("sliced_for_model")
  405. response.bed_type = item.library_file.file_metadata.get("bed_type")
  406. if item.plate_id:
  407. lib_path = Path(item.library_file.file_path)
  408. library_file_path = lib_path if lib_path.is_absolute() else settings.base_dir / item.library_file.file_path
  409. if library_file_path.exists():
  410. # One cached parse for all three per-plate overrides (#2573).
  411. plate_meta = extract_plate_metadata_from_3mf(library_file_path, item.plate_id)
  412. if plate_meta.print_time_seconds is not None:
  413. response.print_time_seconds = plate_meta.print_time_seconds
  414. if plate_meta.filament_used_grams > 0:
  415. response.filament_used_grams = plate_meta.filament_used_grams
  416. if plate_meta.bed_type:
  417. response.bed_type = plate_meta.bed_type
  418. if item.printer:
  419. response.printer_name = item.printer.name
  420. return response
  421. @router.get("/", response_model=list[PrintQueueItemResponse])
  422. async def list_queue(
  423. printer_id: int | None = Query(None, description="Filter by printer (-1 for unassigned)"),
  424. status: str | None = Query(None, description="Filter by status"),
  425. target_model: str | None = Query(
  426. None, description="Filter by target model (also includes model-based items when combined with printer_id)"
  427. ),
  428. db: AsyncSession = Depends(get_db),
  429. auth_result: tuple[User | None, bool] = Depends(
  430. require_ownership_permission(
  431. Permission.QUEUE_READ_ALL,
  432. Permission.QUEUE_READ_OWN,
  433. )
  434. ),
  435. ):
  436. """List all queue items, optionally filtered by printer or status."""
  437. user, can_read_all = auth_result
  438. query = (
  439. select(PrintQueueItem)
  440. .options(
  441. selectinload(PrintQueueItem.archive),
  442. selectinload(PrintQueueItem.printer),
  443. selectinload(PrintQueueItem.library_file),
  444. selectinload(PrintQueueItem.created_by),
  445. selectinload(PrintQueueItem.batch),
  446. # Cross-model candidates (#671) and their files, for the card label.
  447. selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file),
  448. )
  449. .order_by(PrintQueueItem.printer_id.nulls_first(), PrintQueueItem.position)
  450. )
  451. if user is not None and not can_read_all:
  452. query = query.where(PrintQueueItem.created_by_id == user.id)
  453. if printer_id is not None:
  454. if printer_id == -1:
  455. # Special value: filter for unassigned items
  456. query = query.where(PrintQueueItem.printer_id.is_(None))
  457. else:
  458. # Resolve effective model: prefer explicit param, fall back to printer's DB model.
  459. # This ensures model-based "Any X" items are returned even when the frontend
  460. # doesn't send target_model (e.g. printer.model is NULL on the client side).
  461. effective_model = target_model
  462. if not effective_model:
  463. printer_row = (
  464. await db.execute(select(Printer.model).where(Printer.id == printer_id))
  465. ).scalar_one_or_none()
  466. effective_model = printer_row
  467. if effective_model:
  468. # Include both printer-specific items AND model-based (unassigned) items
  469. query = query.where(
  470. or_(
  471. PrintQueueItem.printer_id == printer_id,
  472. and_(
  473. PrintQueueItem.printer_id.is_(None),
  474. func.lower(PrintQueueItem.target_model) == effective_model.lower(),
  475. ),
  476. )
  477. )
  478. else:
  479. query = query.where(PrintQueueItem.printer_id == printer_id)
  480. elif target_model:
  481. query = query.where(func.lower(PrintQueueItem.target_model) == target_model.lower())
  482. if status:
  483. query = query.where(PrintQueueItem.status == status)
  484. result = await db.execute(query)
  485. items = result.scalars().all()
  486. return [_enrich_response(item) for item in items]
  487. async def _resolve_queue_variants(
  488. db: AsyncSession,
  489. specs: list[QueueVariantCreate],
  490. current_user: User | None,
  491. ) -> list[tuple[QueueVariantCreate, LibraryFile, str]]:
  492. """Validate a cross-model candidate set and pair each file with its model (#671).
  493. Validated as a set, not file by file, because the failure modes are about the
  494. set: two candidates for the same printer give the resolver no basis to choose,
  495. and a set where nothing can ever run is a job that waits forever.
  496. At least one candidate must have an active printer — the rest may not, which
  497. is deliberate. Grouping the H2C slice before the H2C arrives is a reasonable
  498. thing to do, and refusing the whole queue action over it would be worse than
  499. letting that candidate simply never match.
  500. """
  501. file_ids = [s.library_file_id for s in specs]
  502. if len(set(file_ids)) != len(file_ids):
  503. raise HTTPException(400, "The same file cannot be listed twice as a variant")
  504. rows = (await db.execute(LibraryFile.active().where(LibraryFile.id.in_(file_ids)))).scalars().all()
  505. by_id = {f.id: f for f in rows}
  506. resolved: list[tuple[QueueVariantCreate, LibraryFile, str]] = []
  507. seen_models: dict[str, str] = {}
  508. any_active_printer = False
  509. for spec in specs:
  510. library_file = by_id.get(spec.library_file_id)
  511. # Same IDOR posture as the single-file path: a file the caller cannot read
  512. # is reported as missing rather than forbidden.
  513. if not library_file or (
  514. current_user
  515. and not current_user.has_permission(Permission.LIBRARY_READ_ALL.value)
  516. and library_file.created_by_id != current_user.id
  517. ):
  518. raise HTTPException(404, f"Library file not found: {spec.library_file_id}")
  519. from backend.app.utils.filename import InvalidFilenameError, validate_print_filename
  520. try:
  521. validate_print_filename(library_file.filename)
  522. except InvalidFilenameError as e:
  523. raise HTTPException(400, str(e)) from e
  524. model = resolve_variant_model(library_file, spec.target_model)
  525. if not model:
  526. raise HTTPException(
  527. 400,
  528. f"{library_file.filename} does not say which printer it was sliced for — "
  529. "set its target model explicitly",
  530. )
  531. # Cross-model safety gate (#2578), per candidate. A set is only as safe as
  532. # its worst member, and model-based dispatch has no human in the loop.
  533. sliced_for = (library_file.file_metadata or {}).get("sliced_for_model")
  534. if not is_gcode_compatible(sliced_for, model):
  535. raise HTTPException(
  536. 400,
  537. f"{library_file.filename} was sliced for {sliced_for} and cannot be dispatched to {model} printers",
  538. )
  539. if model in seen_models:
  540. raise HTTPException(
  541. 400,
  542. f"{library_file.filename} and {seen_models[model]} are both for {model} — "
  543. "variants must target different printers",
  544. )
  545. seen_models[model] = library_file.filename
  546. has_printer = (
  547. (
  548. await db.execute(
  549. select(Printer).where(Printer.model == model).where(Printer.is_active == True) # noqa: E712
  550. )
  551. )
  552. .scalars()
  553. .first()
  554. )
  555. any_active_printer = any_active_printer or bool(has_printer)
  556. resolved.append((spec, library_file, model))
  557. if not any_active_printer:
  558. raise HTTPException(400, f"No active printers for any of: {', '.join(seen_models)}")
  559. return resolved
  560. def _variant_values(
  561. spec: QueueVariantCreate,
  562. library_file: LibraryFile,
  563. model: str,
  564. position: int,
  565. ) -> dict:
  566. """Column values for one candidate, extracted from its own 3MF.
  567. Each candidate is a different slice, so its filament requirements and print
  568. time come from its own file rather than being inherited from the item.
  569. Returns values rather than a row so a quantity>1 batch can build one row per
  570. copy without re-opening the 3MF for each.
  571. """
  572. lib_path = Path(library_file.file_path)
  573. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  574. required_types = None
  575. filament_overrides_json = None
  576. print_time = (library_file.file_metadata or {}).get("print_time_seconds")
  577. if file_path.exists():
  578. types = _extract_filament_types_from_3mf(file_path, spec.plate_id)
  579. if types:
  580. required_types = json.dumps(types)
  581. if spec.plate_id:
  582. plate_time = _extract_print_time_from_3mf(file_path, spec.plate_id)
  583. if plate_time is not None:
  584. print_time = plate_time
  585. if spec.filament_overrides:
  586. plate_overrides = overrides_for_plate(spec.filament_overrides, file_path, spec.plate_id)
  587. if plate_overrides:
  588. filament_overrides_json = json.dumps(plate_overrides)
  589. override_types = sorted({o["type"] for o in plate_overrides if "type" in o})
  590. if override_types:
  591. existing = set(json.loads(required_types)) if required_types else set()
  592. required_types = json.dumps(sorted(existing | set(override_types)))
  593. return {
  594. "position": position,
  595. "library_file_id": library_file.id,
  596. "target_model": model,
  597. "plate_id": spec.plate_id,
  598. "ams_mapping": json.dumps(spec.ams_mapping) if spec.ams_mapping else None,
  599. "nozzle_mapping": json.dumps(spec.nozzle_mapping) if spec.nozzle_mapping else None,
  600. "filament_overrides": filament_overrides_json,
  601. "required_filament_types": required_types,
  602. "print_time_seconds": print_time,
  603. }
  604. @router.post("/", response_model=PrintQueueItemResponse)
  605. async def add_to_queue(
  606. data: PrintQueueItemCreate,
  607. db: AsyncSession = Depends(get_db),
  608. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  609. ):
  610. """Add an item to the print queue."""
  611. # Normalize target_model (e.g., "Bambu Lab X1E" / "C13" -> "X1E").
  612. # normalize_model_name resolves internal codes first: the previous
  613. # `normalize_printer_model(x) or normalize_printer_model_id(x)` chain never
  614. # reached the code map, because the first call returns unknown input
  615. # unchanged — so a "C13" target stayed "C13", matched no printer row and
  616. # left the item waiting forever. Identical result for every other spelling.
  617. target_model_norm = normalize_model_name(data.target_model)
  618. # Cross-model alternatives (#671): several sliced files, whichever printer
  619. # frees up first. The whole candidate set is validated before anything is
  620. # written — a half-valid set would produce a job that can only reach some of
  621. # the printers the user asked for, with nothing to say which.
  622. variant_specs: list[tuple[QueueVariantCreate, LibraryFile, str]] = []
  623. if data.variants:
  624. if data.printer_id:
  625. raise HTTPException(
  626. 400, "Cannot specify both printer_id and variants — pick a printer or offer alternatives"
  627. )
  628. if data.archive_id or data.library_file_id:
  629. raise HTTPException(
  630. 400, "Cannot combine variants with archive_id or library_file_id — the variants are the files"
  631. )
  632. variant_specs = await _resolve_queue_variants(db, data.variants, current_user)
  633. # Mirror the first candidate onto the item so the queue listing, the SJF
  634. # grouping and the "Any H2S" label have something before a printer is
  635. # picked. Resolution overwrites it with whichever candidate actually runs.
  636. target_model_norm = variant_specs[0][2]
  637. # Validate that either archive_id or library_file_id is provided.
  638. # A cross-model item deliberately holds neither: its files live on the
  639. # variant rows. Pointing library_file_id at one of them would be worse than
  640. # useless — that FK is ON DELETE CASCADE, so deleting a single alternative
  641. # would take the whole queue item with it.
  642. if not data.archive_id and not data.library_file_id and not data.variants:
  643. raise HTTPException(400, "Either archive_id or library_file_id must be provided")
  644. # Cannot specify both printer_id and target_model
  645. if data.printer_id and target_model_norm:
  646. raise HTTPException(400, "Cannot specify both printer_id and target_model")
  647. # Validate printer exists (if assigned)
  648. if data.printer_id is not None:
  649. result = await db.execute(select(Printer).where(Printer.id == data.printer_id))
  650. if not result.scalar_one_or_none():
  651. raise HTTPException(400, "Printer not found")
  652. # Validate target_model has active printers. Skipped for cross-model items:
  653. # target_model there is just the first candidate, and _resolve_queue_variants
  654. # has already required that *some* candidate has a printer.
  655. if target_model_norm and not data.variants:
  656. result = await db.execute(
  657. select(Printer).where(Printer.model == target_model_norm).where(Printer.is_active == True) # noqa: E712
  658. )
  659. if not result.scalars().first():
  660. raise HTTPException(400, f"No active printers for model: {target_model_norm}")
  661. # Validate archive exists (if provided) and get it for filament extraction
  662. archive = None
  663. if data.archive_id:
  664. result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  665. archive = result.scalar_one_or_none()
  666. if not archive:
  667. raise HTTPException(400, "Archive not found")
  668. _assert_can_queue_archive(archive, current_user)
  669. # Validate library file exists (if provided) and get it for filament extraction
  670. library_file = None
  671. if data.library_file_id:
  672. result = await db.execute(LibraryFile.active().where(LibraryFile.id == data.library_file_id))
  673. library_file = result.scalar_one_or_none()
  674. if not library_file:
  675. raise HTTPException(400, "Library file not found")
  676. _assert_can_queue_library_file(library_file, current_user)
  677. # Bambu SD card is FAT32/exFAT — illegal filename chars would 553 at
  678. # FTP upload time (#1540). Reject at queue time so the user gets the
  679. # actionable error before waiting in queue.
  680. from backend.app.utils.filename import InvalidFilenameError, validate_print_filename
  681. try:
  682. validate_print_filename(library_file.filename)
  683. except InvalidFilenameError as e:
  684. raise HTTPException(400, str(e)) from e
  685. # Cross-model safety gate (#2578): a G-code 3MF sliced for one model must
  686. # not be queued for dispatch to an incompatible model. The UI can no longer
  687. # produce such rows, but API-created rows must be rejected here too — the
  688. # scheduler assigns model-based items to hardware with no human in the loop.
  689. if target_model_norm:
  690. sliced_for = None
  691. if archive:
  692. sliced_for = archive.sliced_for_model
  693. elif library_file and library_file.file_metadata:
  694. sliced_for = library_file.file_metadata.get("sliced_for_model")
  695. if not is_gcode_compatible(sliced_for, target_model_norm):
  696. raise HTTPException(
  697. 400,
  698. f"File was sliced for {sliced_for} and cannot be dispatched to {target_model_norm} printers",
  699. )
  700. # Extract filament types for model-based assignment (used by scheduler for validation)
  701. required_filament_types = None
  702. file_path = None
  703. if target_model_norm:
  704. # Get file path from archive or library file
  705. if archive:
  706. file_path = settings.base_dir / archive.file_path
  707. elif library_file:
  708. lib_path = Path(library_file.file_path)
  709. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  710. if file_path and file_path.exists():
  711. filament_types = _extract_filament_types_from_3mf(file_path, data.plate_id)
  712. if filament_types:
  713. required_filament_types = json.dumps(filament_types)
  714. logger.info("Extracted filament types for model-based queue: %s", filament_types)
  715. # If filament overrides are provided, update required_filament_types to match override types
  716. filament_overrides_json = None
  717. if data.filament_overrides and target_model_norm:
  718. plate_overrides = overrides_for_plate(data.filament_overrides, file_path, data.plate_id)
  719. if plate_overrides:
  720. filament_overrides_json = json.dumps(plate_overrides)
  721. # Update required_filament_types from overrides so scheduler validates against overridden types
  722. override_types = sorted({o["type"] for o in plate_overrides if "type" in o})
  723. if override_types:
  724. # Merge with existing types (overrides may only cover some slots)
  725. existing_types = set(json.loads(required_filament_types)) if required_filament_types else set()
  726. # Replace types for overridden slots, keep others
  727. all_types = existing_types | set(override_types)
  728. required_filament_types = json.dumps(sorted(all_types))
  729. # Validate quantity
  730. quantity = max(1, data.quantity)
  731. # Validate batch_id if provided. Client passes batch_id when adding items
  732. # into a pre-created batch (multi-plate auto-batch or "Group as batch" flow).
  733. # 404 keeps the existing-id leak surface low.
  734. batch = None
  735. batch_id = None
  736. if data.batch_id is not None:
  737. result = await db.execute(select(PrintBatch).where(PrintBatch.id == data.batch_id))
  738. existing_batch = result.scalar_one_or_none()
  739. if not existing_batch:
  740. raise HTTPException(404, "Batch not found")
  741. if existing_batch.status != "active":
  742. raise HTTPException(400, "Cannot add items to a non-active batch")
  743. if (
  744. current_user is not None
  745. and existing_batch.created_by_id is not None
  746. and existing_batch.created_by_id != current_user.id
  747. and not current_user.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  748. ):
  749. raise HTTPException(404, "Batch not found")
  750. batch = existing_batch
  751. batch_id = existing_batch.id
  752. # Create batch if quantity > 1 and no batch_id provided
  753. if batch_id is None and quantity > 1:
  754. # Derive batch name from source file
  755. batch_name_base = "Batch"
  756. if archive:
  757. batch_name_base = archive.print_name or archive.filename or "Batch"
  758. elif library_file:
  759. if library_file.file_metadata:
  760. batch_name_base = library_file.file_metadata.get("print_name") or library_file.filename
  761. else:
  762. batch_name_base = library_file.filename
  763. batch_name_base = batch_name_base.replace(".gcode.3mf", "").replace(".3mf", "")
  764. batch = PrintBatch(
  765. name=f"{batch_name_base} ×{quantity}",
  766. archive_id=data.archive_id,
  767. library_file_id=data.library_file_id,
  768. quantity=quantity,
  769. status="active",
  770. created_by_id=current_user.id if current_user else None,
  771. )
  772. db.add(batch)
  773. await db.flush() # Get batch.id before creating items
  774. batch_id = batch.id
  775. # Get queue scope for this printer (or for unassigned/model-based items).
  776. if data.printer_id is not None:
  777. queue_scope = (
  778. PrintQueueItem.printer_id == data.printer_id,
  779. PrintQueueItem.status == "pending",
  780. )
  781. else:
  782. # For unassigned/model-based items, scope across all unassigned.
  783. queue_scope = (
  784. PrintQueueItem.printer_id.is_(None),
  785. PrintQueueItem.status == "pending",
  786. )
  787. # Serialize concurrent queue inserts to the same scope (#1625-followup).
  788. # The race: two concurrent ASAP inserts both compute MAX(position) before
  789. # either commits; in an empty scope, both INSERT at position 1 (duplicate).
  790. # In a non-empty scope, Postgres's row-level locks on the UPDATE shift
  791. # serialize naturally, but the empty-scope path has no rows to lock.
  792. # A transaction-scoped advisory lock keyed on the printer_id closes that
  793. # window; the lock is released automatically at commit/rollback. Different
  794. # printers don't contend. SQLite serializes writes implicitly so this is a
  795. # no-op there.
  796. #
  797. # Dialect is checked against the actual session binding, NOT the
  798. # `is_sqlite()` helper, because the test fixture overrides `get_db` with a
  799. # SQLite engine while `settings.database_url` still points at Postgres
  800. # (the helper reads settings). Inspecting the connection directly is the
  801. # right shape for any code that mutates SQL based on the live dialect.
  802. from sqlalchemy import text
  803. bind = db.get_bind()
  804. if bind.dialect.name == "postgresql":
  805. scope_key = data.printer_id if data.printer_id is not None else 0
  806. # 1625 namespaces the lock so it can't collide with other advisory
  807. # locks elsewhere in the codebase.
  808. await db.execute(text("SELECT pg_advisory_xact_lock(1625, :k)"), {"k": scope_key})
  809. insert_position = max(1, data.insert_position or 1)
  810. if data.insert_at_top or data.insert_position is not None:
  811. result = await db.execute(select(func.max(PrintQueueItem.position)).where(*queue_scope))
  812. max_pos = result.scalar() or 0
  813. insert_position = min(insert_position, max_pos + 1)
  814. await db.execute(
  815. update(PrintQueueItem)
  816. .where(*queue_scope)
  817. .where(PrintQueueItem.position >= insert_position)
  818. .values(position=PrintQueueItem.position + quantity)
  819. )
  820. start_position = insert_position
  821. else:
  822. result = await db.execute(select(func.max(PrintQueueItem.position)).where(*queue_scope))
  823. max_pos = result.scalar() or 0
  824. start_position = max_pos + 1
  825. # Resolve print_time_seconds for SJF scheduling (cache on item at creation)
  826. cached_print_time = None
  827. if archive:
  828. cached_print_time = archive.print_time_seconds
  829. if data.plate_id:
  830. archive_path = settings.base_dir / archive.file_path
  831. if archive_path.exists():
  832. plate_time = _extract_print_time_from_3mf(archive_path, data.plate_id)
  833. if plate_time is not None:
  834. cached_print_time = plate_time
  835. elif library_file:
  836. if library_file.file_metadata:
  837. cached_print_time = library_file.file_metadata.get("print_time_seconds")
  838. if data.plate_id:
  839. lib_path = Path(library_file.file_path)
  840. library_file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  841. if library_file_path.exists():
  842. plate_time = _extract_print_time_from_3mf(library_file_path, data.plate_id)
  843. if plate_time is not None:
  844. cached_print_time = plate_time
  845. # Validate project exists before insert so a bogus ID yields 404, not an FK-constraint 500
  846. if data.project_id is not None:
  847. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  848. if not project_result.scalar_one_or_none():
  849. raise HTTPException(status_code=404, detail="Project not found")
  850. ams_mapping_json = json.dumps(data.ams_mapping) if data.ams_mapping else None
  851. # Reprint fallback: the caller didn't specify an explicit ams_mapping (no
  852. # per-slot filament-mapping edit was made), but the archive carries the
  853. # slicer's own live-resolved AMS-slot pick from the original print (see
  854. # `extra_data.slicer_ams_mapping`, written by the VP-queue path via
  855. # `_extract_slicer_ams_mapping_json`). Reuse it so the reprint dispatches
  856. # to the exact same physical spool instead of the scheduler re-deriving a
  857. # (possibly ambiguous) mapping from just the file's static type/color.
  858. #
  859. # Global tray IDs only mean something relative to the specific printer
  860. # they were resolved against, so this only fires when the reprint targets
  861. # that exact printer (`extra_data.slicer_ams_mapping.printer_id`) — never
  862. # for a model-based dispatch (data.printer_id is None) or a reprint aimed
  863. # at a different printer, where the same tray number can hold a
  864. # completely different spool (#2700 review).
  865. #
  866. # It also stands down when the request carries force-color-match overrides:
  867. # those are the caller asking the scheduler to match strictly against the
  868. # printer's live trays, and they are only ever applied inside
  869. # `_compute_ams_mapping_for_printer` — the function a stored mapping makes
  870. # the scheduler skip. Same precedence as the VP-side toggle pair (#2700
  871. # review).
  872. #
  873. # Note this is otherwise unconditional — it applies regardless of whether
  874. # the physical spool in that slot has changed since the original print.
  875. # #1308 covers re-verifying a stored mapping against live AMS state at
  876. # dispatch time; that check is a separate PR and, once merged, will also
  877. # catch a stale slot inherited through this fallback.
  878. wants_live_color_match = any(
  879. isinstance(o, dict) and o.get("force_color_match") for o in (data.filament_overrides or [])
  880. )
  881. if (
  882. ams_mapping_json is None
  883. and not wants_live_color_match
  884. and archive
  885. and archive.extra_data
  886. and data.printer_id is not None
  887. ):
  888. saved = archive.extra_data.get("slicer_ams_mapping")
  889. if (
  890. isinstance(saved, dict)
  891. and saved.get("printer_id") == data.printer_id
  892. and isinstance(saved.get("mapping"), list)
  893. and saved["mapping"]
  894. ):
  895. ams_mapping_json = json.dumps(saved["mapping"])
  896. items = []
  897. for i in range(quantity):
  898. item = PrintQueueItem(
  899. printer_id=data.printer_id,
  900. target_model=target_model_norm,
  901. target_location=data.target_location,
  902. required_filament_types=required_filament_types,
  903. filament_overrides=filament_overrides_json,
  904. archive_id=data.archive_id,
  905. library_file_id=data.library_file_id,
  906. scheduled_time=data.scheduled_time,
  907. require_previous_success=data.require_previous_success,
  908. auto_off_after=data.auto_off_after,
  909. manual_start=data.manual_start,
  910. skip_filament_check=data.skip_filament_check,
  911. ams_mapping=ams_mapping_json,
  912. plate_id=data.plate_id,
  913. bed_levelling=data.bed_levelling,
  914. flow_cali=data.flow_cali,
  915. vibration_cali=data.vibration_cali,
  916. layer_inspect=data.layer_inspect,
  917. timelapse=data.timelapse,
  918. use_ams=data.use_ams,
  919. nozzle_offset_cali=data.nozzle_offset_cali,
  920. preheat_override=data.preheat_override,
  921. preheat_chamber_target_override=data.preheat_chamber_target_override,
  922. gcode_injection=data.gcode_injection,
  923. cleanup_library_after_dispatch=data.cleanup_library_after_dispatch,
  924. project_id=data.project_id,
  925. position=start_position + i,
  926. status="pending",
  927. created_by_id=current_user.id if current_user else None,
  928. batch_id=batch_id,
  929. print_time_seconds=cached_print_time,
  930. )
  931. db.add(item)
  932. items.append(item)
  933. if variant_specs:
  934. variant_values = [
  935. _variant_values(spec, library_file, model, position)
  936. for position, (spec, library_file, model) in enumerate(variant_specs)
  937. ]
  938. # SJF orders pending items before any printer is known, so the row carries
  939. # the shortest candidate's estimate. Resolution replaces it with the one
  940. # that actually runs.
  941. estimates = [v["print_time_seconds"] for v in variant_values if v["print_time_seconds"]]
  942. for item in items:
  943. # Each copy in a quantity>1 batch gets its own candidate rows —
  944. # attempt counts are per-item, and two copies must be free to land on
  945. # different printers.
  946. item.variants.extend(PrintQueueVariant(**values) for values in variant_values)
  947. item.print_time_seconds = min(estimates) if estimates else None
  948. await db.commit()
  949. # Refresh the first item for the response
  950. item = items[0]
  951. await db.refresh(item)
  952. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  953. source_name = f"archive {data.archive_id}" if data.archive_id else f"library file {data.library_file_id}"
  954. target_desc = data.printer_id or (f"model {target_model_norm}" if target_model_norm else "unassigned")
  955. qty_desc = f" (×{quantity})" if quantity > 1 else ""
  956. logger.info("Added %s to queue for %s%s", source_name, target_desc, qty_desc)
  957. # MQTT relay - publish queue job added
  958. try:
  959. from backend.app.services.mqtt_relay import mqtt_relay
  960. await mqtt_relay.on_queue_job_added(
  961. job_id=item.id,
  962. filename=item.archive.filename if item.archive else "",
  963. printer_id=item.printer_id,
  964. printer_name=item.printer.name if item.printer else None,
  965. )
  966. except Exception:
  967. pass # Don't fail queue add if MQTT fails
  968. # Send notification for job added
  969. try:
  970. job_name = (
  971. item.archive.filename
  972. if item.archive
  973. else item.library_file.filename
  974. if item.library_file
  975. else f"Job #{item.id}"
  976. )
  977. job_name = job_name.replace(".gcode.3mf", "").replace(".3mf", "")
  978. if quantity > 1:
  979. job_name = f"{job_name} ×{quantity}"
  980. target = (
  981. item.printer.name if item.printer else (f"Any {item.target_model}" if target_model_norm else "Unassigned")
  982. )
  983. await notification_service.on_queue_job_added(
  984. job_name=job_name,
  985. target=target,
  986. db=db,
  987. printer_id=item.printer_id,
  988. printer_name=item.printer.name if item.printer else None,
  989. )
  990. except Exception:
  991. pass # Don't fail queue add if notification fails
  992. return _enrich_response(item)
  993. @router.patch("/bulk", response_model=PrintQueueBulkUpdateResponse)
  994. async def bulk_update_queue_items(
  995. data: PrintQueueBulkUpdate,
  996. db: AsyncSession = Depends(get_db),
  997. auth_result: tuple[User | None, bool] = Depends(
  998. require_ownership_permission(
  999. Permission.QUEUE_UPDATE_ALL,
  1000. Permission.QUEUE_UPDATE_OWN,
  1001. )
  1002. ),
  1003. ):
  1004. """Bulk update multiple queue items with the same values.
  1005. Only pending items can be updated. Non-pending items are skipped.
  1006. Items not owned by the user are also skipped (unless user has *_all permission).
  1007. """
  1008. user, can_modify_all = auth_result
  1009. if not data.item_ids:
  1010. raise HTTPException(400, "No item IDs provided")
  1011. # Get fields to update (exclude item_ids and unset fields)
  1012. update_data = data.model_dump(exclude={"item_ids"}, exclude_unset=True)
  1013. if not update_data:
  1014. raise HTTPException(400, "No fields to update")
  1015. # Validate printer_id if being changed
  1016. if "printer_id" in update_data and update_data["printer_id"] is not None:
  1017. result = await db.execute(select(Printer).where(Printer.id == update_data["printer_id"]))
  1018. if not result.scalar_one_or_none():
  1019. raise HTTPException(400, "Printer not found")
  1020. # Fetch all items
  1021. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id.in_(data.item_ids)))
  1022. items = result.scalars().all()
  1023. updated_count = 0
  1024. skipped_count = 0
  1025. for item in items:
  1026. # Skip non-pending rows and rows a dispatch worker has claimed (#2615) —
  1027. # editing a claimed row mid-upload would split it from the in-flight
  1028. # dispatch, so it's excluded from the bulk change (cancel to move it).
  1029. if item.status != "pending" or item.dispatching_at is not None:
  1030. skipped_count += 1
  1031. continue
  1032. # Ownership check
  1033. if not can_modify_all and item.created_by_id != user.id:
  1034. skipped_count += 1
  1035. continue
  1036. for field, value in update_data.items():
  1037. setattr(item, field, value)
  1038. updated_count += 1
  1039. await db.commit()
  1040. logger.info("Bulk updated %s queue items, skipped %s", updated_count, skipped_count)
  1041. return PrintQueueBulkUpdateResponse(
  1042. updated_count=updated_count,
  1043. skipped_count=skipped_count,
  1044. message=f"Updated {updated_count} items"
  1045. + (f", skipped {skipped_count} non-pending/not-owned" if skipped_count else ""),
  1046. )
  1047. # --- Batch endpoints ---
  1048. def _validate_plate_targets(
  1049. plates: list[PrintBatchPlateTarget] | None,
  1050. ) -> list[PrintBatchPlateTarget] | None:
  1051. """Reject duplicate plates and orders that ask for nothing at all.
  1052. A duplicate would violate the (batch_id, plate_id) unique constraint at
  1053. flush time — and on SQLite/PostgreSQL a NULL plate_id slips past that
  1054. constraint entirely, so the check has to happen here to catch two
  1055. "whole file" rows in one order.
  1056. """
  1057. if plates is None:
  1058. return None
  1059. if not plates:
  1060. raise HTTPException(400, "plates must contain at least one plate")
  1061. seen: set[int | None] = set()
  1062. for target in plates:
  1063. if target.plate_id in seen:
  1064. label = target.plate_id if target.plate_id is not None else "whole file"
  1065. raise HTTPException(400, f"Duplicate plate in order: {label}")
  1066. seen.add(target.plate_id)
  1067. if all(target.quantity_target == 0 for target in plates):
  1068. raise HTTPException(400, "Order must request at least one print")
  1069. return plates
  1070. async def _validate_batch_project(db: AsyncSession, project_id: int | None, current_user: User | None) -> None:
  1071. """404 on a bogus project id rather than letting the FK blow up as a 500."""
  1072. if project_id is None:
  1073. return
  1074. result = await db.execute(select(Project).where(Project.id == project_id))
  1075. if not result.scalar_one_or_none():
  1076. raise HTTPException(404, "Project not found")
  1077. async def _load_batch_for_write(
  1078. db: AsyncSession, batch_id: int, current_user: User | None, permission: Permission
  1079. ) -> PrintBatch:
  1080. """Fetch a batch the caller is allowed to modify, or 404.
  1081. 404 rather than 403 on the ownership miss, matching the rest of this
  1082. module: a 403 would confirm the id exists to someone enumerating.
  1083. """
  1084. result = await db.execute(
  1085. select(PrintBatch).options(selectinload(PrintBatch.plates)).where(PrintBatch.id == batch_id)
  1086. )
  1087. batch = result.scalar_one_or_none()
  1088. if not batch:
  1089. raise HTTPException(404, "Batch not found")
  1090. if (
  1091. current_user is not None
  1092. and batch.created_by_id is not None
  1093. and batch.created_by_id != current_user.id
  1094. and not current_user.has_permission(permission.value)
  1095. ):
  1096. raise HTTPException(404, "Batch not found")
  1097. return batch
  1098. @router.post("/batches", response_model=PrintBatchResponse)
  1099. async def create_batch(
  1100. data: PrintBatchCreate,
  1101. db: AsyncSession = Depends(get_db),
  1102. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  1103. ):
  1104. """Create a batch.
  1105. Two modes:
  1106. * ``item_ids`` provided: assign the listed pending queue items to a new
  1107. batch ("Group as batch" UI action).
  1108. * ``item_ids`` omitted/empty: create an empty batch so the client can
  1109. pass the returned ``id`` on subsequent ``POST /queue/`` calls. Used by
  1110. the multi-plate auto-batch flow in PrintModal.
  1111. ``plates`` turns the batch into an order with per-plate targets (#342):
  1112. progress is then measured against what was asked for rather than against
  1113. what happened to be queued, so a failed run still counts as owed. Omitting
  1114. it keeps the pre-#342 behaviour exactly.
  1115. """
  1116. if not data.name or not data.name.strip():
  1117. raise HTTPException(400, "Batch name is required")
  1118. plate_targets = _validate_plate_targets(data.plates)
  1119. await _validate_batch_project(db, data.project_id, current_user)
  1120. batch = PrintBatch(
  1121. name=data.name.strip()[:255],
  1122. archive_id=data.archive_id,
  1123. library_file_id=data.library_file_id,
  1124. quantity=len(data.item_ids) if data.item_ids else 1,
  1125. status="active",
  1126. created_by_id=current_user.id if current_user else None,
  1127. project_id=data.project_id,
  1128. due_date=data.due_date,
  1129. notes=data.notes,
  1130. )
  1131. db.add(batch)
  1132. await db.flush() # Need batch.id before assigning to items
  1133. if plate_targets is not None:
  1134. for target in plate_targets:
  1135. db.add(
  1136. PrintBatchPlate(
  1137. batch_id=batch.id,
  1138. plate_id=target.plate_id,
  1139. plate_name=target.plate_name,
  1140. quantity_target=target.quantity_target,
  1141. sort_order=target.sort_order,
  1142. )
  1143. )
  1144. # The legacy `quantity` column is display-only; keep it meaningful for
  1145. # anything still reading it by making it the order's total.
  1146. batch.quantity = max(1, sum(t.quantity_target for t in plate_targets))
  1147. assigned = 0
  1148. if data.item_ids:
  1149. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id.in_(data.item_ids)))
  1150. items = result.scalars().all()
  1151. for item in items:
  1152. if item.status != "pending":
  1153. continue
  1154. if item.batch_id is not None:
  1155. continue
  1156. if (
  1157. current_user is not None
  1158. and item.created_by_id != current_user.id
  1159. and not current_user.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  1160. ):
  1161. continue
  1162. item.batch_id = batch.id
  1163. assigned += 1
  1164. batch.quantity = max(assigned, 1)
  1165. await db.commit()
  1166. await db.refresh(batch)
  1167. logger.info("Created batch %s '%s' with %s assigned items", batch.id, batch.name, assigned)
  1168. return await _build_batch_response(db, batch)
  1169. @router.patch("/batches/{batch_id}", response_model=PrintBatchResponse)
  1170. async def update_batch(
  1171. batch_id: int,
  1172. data: PrintBatchUpdate,
  1173. db: AsyncSession = Depends(get_db),
  1174. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_OWN),
  1175. ):
  1176. """Edit an order's header or its per-plate targets while it runs (#342).
  1177. Production requirements change mid-run, so targets are editable. Lowering a
  1178. target below what has already been dispatched is allowed and simply leaves
  1179. ``remaining`` at zero — cancelling the surplus queue items is a separate,
  1180. explicit action, because silently deleting queued work on a number change
  1181. would be a nasty surprise.
  1182. """
  1183. batch = await _load_batch_for_write(db, batch_id, current_user, Permission.QUEUE_UPDATE_ALL)
  1184. plate_targets = _validate_plate_targets(data.plates)
  1185. if data.project_id is not None:
  1186. await _validate_batch_project(db, data.project_id, current_user)
  1187. if data.name is not None:
  1188. if not data.name.strip():
  1189. raise HTTPException(400, "Batch name is required")
  1190. batch.name = data.name.strip()[:255]
  1191. if data.project_id is not None:
  1192. batch.project_id = data.project_id
  1193. if data.due_date is not None:
  1194. batch.due_date = data.due_date
  1195. if data.notes is not None:
  1196. batch.notes = data.notes
  1197. if data.status is not None:
  1198. batch.status = data.status
  1199. if plate_targets is not None:
  1200. existing = {row.plate_id: row for row in batch.plates}
  1201. for target in plate_targets:
  1202. row = existing.pop(target.plate_id, None)
  1203. if row is None:
  1204. db.add(
  1205. PrintBatchPlate(
  1206. batch_id=batch.id,
  1207. plate_id=target.plate_id,
  1208. plate_name=target.plate_name,
  1209. quantity_target=target.quantity_target,
  1210. sort_order=target.sort_order,
  1211. )
  1212. )
  1213. else:
  1214. row.quantity_target = target.quantity_target
  1215. row.sort_order = target.sort_order
  1216. if target.plate_name is not None:
  1217. row.plate_name = target.plate_name
  1218. # Plates absent from the payload are dropped — the list is the order.
  1219. for orphan in existing.values():
  1220. await db.delete(orphan)
  1221. batch.quantity = max(1, sum(t.quantity_target for t in plate_targets))
  1222. await db.flush()
  1223. await db.refresh(batch)
  1224. # Raising a target on a finished order reopens it; lowering one on a
  1225. # running order can complete it.
  1226. await refresh_batch_status(db, batch)
  1227. await db.commit()
  1228. await db.refresh(batch)
  1229. logger.info("Updated batch %s", batch.id)
  1230. return await _build_batch_response(db, batch)
  1231. @router.post("/batches/{batch_id}/dispatch", response_model=PrintBatchResponse)
  1232. async def dispatch_batch(
  1233. batch_id: int,
  1234. data: PrintBatchDispatchRequest,
  1235. db: AsyncSession = Depends(get_db),
  1236. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  1237. ):
  1238. """Queue the runs this order still owes (#342).
  1239. Each new item is cloned from the most recent item for the same plate in
  1240. this batch, so it inherits the printer/model target, AMS mapping, filament
  1241. overrides and print options the user already chose — and the validation
  1242. those went through at creation time.
  1243. """
  1244. batch = await _load_batch_for_write(db, batch_id, current_user, Permission.QUEUE_UPDATE_ALL)
  1245. if batch.status == "cancelled":
  1246. raise HTTPException(400, "Cannot dispatch a cancelled batch")
  1247. # Dispatch starts prints, so it must not be a weaker door than POST /queue/.
  1248. await _assert_can_dispatch_batch_sources(db, batch.id, current_user)
  1249. try:
  1250. created = await dispatch_remaining(
  1251. db,
  1252. batch,
  1253. plate_id=data.plate_id,
  1254. only_plate=data.only_plate,
  1255. limit=data.limit,
  1256. created_by_id=current_user.id if current_user else None,
  1257. )
  1258. except BatchDispatchError as exc:
  1259. raise HTTPException(400, str(exc)) from exc
  1260. await db.commit()
  1261. await db.refresh(batch)
  1262. logger.info("Batch %s dispatched %d item(s)", batch.id, len(created))
  1263. return await _build_batch_response(db, batch)
  1264. @router.post("/batches/{batch_id}/ungroup", response_model=PrintBatchUngroupResponse)
  1265. async def ungroup_batch(
  1266. batch_id: int,
  1267. db: AsyncSession = Depends(get_db),
  1268. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_OWN),
  1269. ):
  1270. """Disband a batch: clear batch_id from all members and delete the batch row.
  1271. Items stay in the queue. Only ungroups items the caller owns (unless they
  1272. hold QUEUE_UPDATE_ALL). A batch with all members ungrouped is deleted.
  1273. """
  1274. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  1275. batch = result.scalar_one_or_none()
  1276. if not batch:
  1277. raise HTTPException(404, "Batch not found")
  1278. can_modify_all = current_user is None or current_user.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  1279. if not can_modify_all and batch.created_by_id != (current_user.id if current_user else None):
  1280. raise HTTPException(404, "Batch not found")
  1281. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.batch_id == batch_id))
  1282. items = result.scalars().all()
  1283. ungrouped = 0
  1284. remaining = 0
  1285. for item in items:
  1286. if not can_modify_all and item.created_by_id != (current_user.id if current_user else None):
  1287. remaining += 1
  1288. continue
  1289. item.batch_id = None
  1290. ungrouped += 1
  1291. # Delete the batch row only when all members were ungrouped — otherwise it
  1292. # still owns the items the caller couldn't touch.
  1293. if remaining == 0:
  1294. await db.delete(batch)
  1295. await db.commit()
  1296. logger.info("Ungrouped batch %s (%s items)", batch_id, ungrouped)
  1297. return PrintBatchUngroupResponse(
  1298. ungrouped_count=ungrouped,
  1299. message=f"Ungrouped {ungrouped} item(s)",
  1300. )
  1301. @router.get("/batches", response_model=list[PrintBatchResponse])
  1302. async def list_batches(
  1303. status: str | None = Query(None, description="Filter by status (active, completed, cancelled)"),
  1304. db: AsyncSession = Depends(get_db),
  1305. auth_result: tuple[User | None, bool] = Depends(
  1306. require_ownership_permission(
  1307. Permission.QUEUE_READ_ALL,
  1308. Permission.QUEUE_READ_OWN,
  1309. )
  1310. ),
  1311. ):
  1312. """List print batches with progress stats.
  1313. Batches with neither queue items nor per-plate targets are omitted. Those
  1314. are empty shells — a grouping whose items were deleted with their source
  1315. archive, or a create that never got as far as adding any — and they carry
  1316. nothing to show, track or dispatch. A brand-new order is still listed
  1317. before its first dispatch, because its targets say what it owes.
  1318. """
  1319. current_user, can_read_all = auth_result
  1320. query = (
  1321. select(PrintBatch)
  1322. .where(
  1323. select(PrintQueueItem.id).where(PrintQueueItem.batch_id == PrintBatch.id).exists()
  1324. | select(PrintBatchPlate.id).where(PrintBatchPlate.batch_id == PrintBatch.id).exists()
  1325. )
  1326. .order_by(PrintBatch.created_at.desc())
  1327. )
  1328. if status:
  1329. query = query.where(PrintBatch.status == status)
  1330. if current_user is not None and not can_read_all:
  1331. query = query.where(PrintBatch.created_by_id == current_user.id)
  1332. result = await db.execute(query)
  1333. batches = result.scalars().all()
  1334. # Resolve creator names in one query rather than one per batch.
  1335. creator_ids = {b.created_by_id for b in batches if b.created_by_id is not None}
  1336. usernames: dict[int, str] = {}
  1337. if creator_ids:
  1338. rows = await db.execute(select(User.id, User.username).where(User.id.in_(creator_ids)))
  1339. usernames = {row[0]: row[1] for row in rows.all()}
  1340. return [await _build_batch_response(db, batch, usernames=usernames) for batch in batches]
  1341. @router.get("/batches/{batch_id}", response_model=PrintBatchResponse)
  1342. async def get_batch(
  1343. batch_id: int,
  1344. db: AsyncSession = Depends(get_db),
  1345. auth_result: tuple[User | None, bool] = Depends(
  1346. require_ownership_permission(
  1347. Permission.QUEUE_READ_ALL,
  1348. Permission.QUEUE_READ_OWN,
  1349. )
  1350. ),
  1351. ):
  1352. """Get a print batch with progress stats."""
  1353. current_user, can_read_all = auth_result
  1354. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  1355. batch = result.scalar_one_or_none()
  1356. if not batch:
  1357. raise HTTPException(404, "Batch not found")
  1358. if (
  1359. current_user is not None
  1360. and not can_read_all
  1361. and (batch.created_by_id is None or batch.created_by_id != current_user.id)
  1362. ):
  1363. raise HTTPException(404, "Batch not found")
  1364. return await _build_batch_response(db, batch)
  1365. @router.delete("/batches/{batch_id}")
  1366. async def cancel_batch(
  1367. batch_id: int,
  1368. db: AsyncSession = Depends(get_db),
  1369. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_DELETE_ALL),
  1370. ):
  1371. """Cancel all pending items in a batch and mark batch as cancelled."""
  1372. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  1373. batch = result.scalar_one_or_none()
  1374. if not batch:
  1375. raise HTTPException(404, "Batch not found")
  1376. # Cancel all pending queue items in this batch
  1377. result = await db.execute(
  1378. select(PrintQueueItem).where(and_(PrintQueueItem.batch_id == batch_id, PrintQueueItem.status == "pending"))
  1379. )
  1380. pending_items = result.scalars().all()
  1381. cancelled_count = 0
  1382. for item in pending_items:
  1383. item.status = "cancelled"
  1384. cancelled_count += 1
  1385. batch.status = "cancelled"
  1386. await db.commit()
  1387. return {"message": f"Batch cancelled, {cancelled_count} pending items cancelled"}
  1388. async def _build_batch_response(
  1389. db: AsyncSession, batch: PrintBatch, *, usernames: dict[int, str] | None = None
  1390. ) -> PrintBatchResponse:
  1391. """Build a batch response with per-plate progress derived from queue items.
  1392. ``usernames`` lets the list endpoint resolve every creator in one query
  1393. instead of one per batch.
  1394. """
  1395. progress = await load_progress(db, batch)
  1396. created_by_username = None
  1397. if batch.created_by_id:
  1398. if usernames is not None:
  1399. created_by_username = usernames.get(batch.created_by_id)
  1400. else:
  1401. result = await db.execute(select(User).where(User.id == batch.created_by_id))
  1402. user = result.scalar_one_or_none()
  1403. if user:
  1404. created_by_username = user.username
  1405. return PrintBatchResponse(
  1406. id=batch.id,
  1407. name=batch.name,
  1408. archive_id=batch.archive_id,
  1409. library_file_id=batch.library_file_id,
  1410. quantity=batch.quantity,
  1411. status=batch.status,
  1412. created_at=batch.created_at,
  1413. completed_at=batch.completed_at,
  1414. created_by_id=batch.created_by_id,
  1415. created_by_username=created_by_username,
  1416. project_id=batch.project_id,
  1417. due_date=batch.due_date,
  1418. notes=batch.notes,
  1419. pending_count=progress.pending,
  1420. printing_count=progress.printing,
  1421. completed_count=progress.completed,
  1422. failed_count=progress.failed,
  1423. cancelled_count=progress.cancelled,
  1424. skipped_count=progress.skipped,
  1425. has_targets=progress.has_targets,
  1426. target_count=progress.target,
  1427. remaining_count=progress.remaining,
  1428. actual_cost=progress.actual_cost,
  1429. estimated_remaining_cost=progress.estimated_remaining_cost,
  1430. filament_used_grams=progress.filament_used_grams,
  1431. print_time_seconds=progress.print_time_seconds,
  1432. plates=[
  1433. PrintBatchPlateProgress(
  1434. plate_id=plate.plate_id,
  1435. plate_name=plate.plate_name,
  1436. quantity_target=plate.quantity_target,
  1437. dispatched=plate.dispatched,
  1438. remaining=plate.remaining,
  1439. pending_count=plate.pending,
  1440. printing_count=plate.printing,
  1441. completed_count=plate.completed,
  1442. failed_count=plate.failed,
  1443. cancelled_count=plate.cancelled,
  1444. skipped_count=plate.skipped,
  1445. actual_cost=plate.actual_cost,
  1446. estimated_remaining_cost=plate.estimated_remaining_cost,
  1447. filament_used_grams=plate.filament_used_grams,
  1448. print_time_seconds=plate.print_time_seconds,
  1449. )
  1450. for plate in progress.plates
  1451. ],
  1452. )
  1453. @router.get("/{item_id}", response_model=PrintQueueItemResponse)
  1454. async def get_queue_item(
  1455. item_id: int,
  1456. db: AsyncSession = Depends(get_db),
  1457. auth_result: tuple[User | None, bool] = Depends(
  1458. require_ownership_permission(
  1459. Permission.QUEUE_READ_ALL,
  1460. Permission.QUEUE_READ_OWN,
  1461. )
  1462. ),
  1463. ):
  1464. """Get a specific queue item."""
  1465. current_user, can_read_all = auth_result
  1466. result = await db.execute(
  1467. select(PrintQueueItem)
  1468. .options(
  1469. selectinload(PrintQueueItem.archive),
  1470. selectinload(PrintQueueItem.printer),
  1471. selectinload(PrintQueueItem.library_file),
  1472. selectinload(PrintQueueItem.created_by),
  1473. selectinload(PrintQueueItem.batch),
  1474. # Cross-model candidates (#671) and their files, for the card label.
  1475. selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file),
  1476. )
  1477. .where(PrintQueueItem.id == item_id)
  1478. )
  1479. item = result.scalar_one_or_none()
  1480. if not item:
  1481. raise HTTPException(404, "Queue item not found")
  1482. if (
  1483. current_user is not None
  1484. and not can_read_all
  1485. and (item.created_by_id is None or item.created_by_id != current_user.id)
  1486. ):
  1487. raise HTTPException(404, "Queue item not found")
  1488. return _enrich_response(item)
  1489. @router.patch("/{item_id}", response_model=PrintQueueItemResponse)
  1490. async def update_queue_item(
  1491. item_id: int,
  1492. data: PrintQueueItemUpdate,
  1493. db: AsyncSession = Depends(get_db),
  1494. auth_result: tuple[User | None, bool] = Depends(
  1495. require_ownership_permission(
  1496. Permission.QUEUE_UPDATE_ALL,
  1497. Permission.QUEUE_UPDATE_OWN,
  1498. )
  1499. ),
  1500. ):
  1501. """Update a queue item."""
  1502. user, can_modify_all = auth_result
  1503. result = await db.execute(
  1504. select(PrintQueueItem)
  1505. # Needed by the cross-model guard below, and by the response builder —
  1506. # without it _variant_summaries falls back to [] and a PATCH would strip
  1507. # the alternatives out of the payload it echoes back.
  1508. .options(selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file))
  1509. .where(PrintQueueItem.id == item_id)
  1510. )
  1511. item = result.scalar_one_or_none()
  1512. if not item:
  1513. raise HTTPException(404, "Queue item not found")
  1514. # Ownership check
  1515. if not can_modify_all:
  1516. if item.created_by_id != user.id:
  1517. raise HTTPException(403, "You can only update your own queue items")
  1518. if item.status != "pending":
  1519. raise HTTPException(400, "Can only update pending items")
  1520. # Dispatch claim (#2615): the row is pending but a scheduler worker has
  1521. # already claimed it and is uploading to its printer. Editing now (e.g.
  1522. # reassigning printer_id) would split the queue row from the in-flight
  1523. # archive/expected-print/physical command. Reject until dispatch finishes;
  1524. # to move it, cancel first (the coordinated escape) and re-queue.
  1525. if item.dispatching_at is not None:
  1526. raise HTTPException(409, "Item is being dispatched — cancel it first to make changes")
  1527. update_data = data.model_dump(exclude_unset=True)
  1528. # Normalize target_model if being updated (see add_to_queue for why the
  1529. # code map has to run first).
  1530. if "target_model" in update_data and update_data["target_model"]:
  1531. update_data["target_model"] = normalize_model_name(update_data["target_model"])
  1532. # A cross-model item (#671) owns its own printer decision: each candidate
  1533. # carries its model, and the resolver folds the winner onto the row at
  1534. # dispatch. Assigning a printer here would leave a row with variants *and* a
  1535. # printer_id, and the fixed-printer branch of the scheduler wins that race —
  1536. # so it would dispatch a row whose library_file_id is still null and die in
  1537. # the upload. Narrowing target_model is refused for the same reason: it
  1538. # would silently discard every alternative the user queued.
  1539. #
  1540. # Compared against the current value rather than merely present, because the
  1541. # edit dialog re-sends target_model unchanged on every save.
  1542. if item.variants:
  1543. for field in ("printer_id", "target_model"):
  1544. if field in update_data and update_data[field] != getattr(item, field):
  1545. raise HTTPException(
  1546. 400,
  1547. "This job has printer alternatives — remove them before assigning a printer or model",
  1548. )
  1549. # Cannot specify both printer_id and target_model
  1550. new_printer_id = update_data.get("printer_id", item.printer_id)
  1551. new_target_model = update_data.get("target_model", item.target_model)
  1552. if new_printer_id and new_target_model:
  1553. raise HTTPException(400, "Cannot specify both printer_id and target_model")
  1554. # Validate new printer_id if being changed (and not None)
  1555. if "printer_id" in update_data and update_data["printer_id"] is not None:
  1556. result = await db.execute(select(Printer).where(Printer.id == update_data["printer_id"]))
  1557. if not result.scalar_one_or_none():
  1558. raise HTTPException(400, "Printer not found")
  1559. # Validate target_model has active printers
  1560. if "target_model" in update_data and update_data["target_model"]:
  1561. result = await db.execute(
  1562. select(Printer).where(Printer.model == update_data["target_model"]).where(Printer.is_active == True) # noqa: E712
  1563. )
  1564. if not result.scalars().first():
  1565. raise HTTPException(400, f"No active printers for model: {update_data['target_model']}")
  1566. # Cross-model safety gate (#2578) — same check as the create route, so
  1567. # a mismatched target can't be introduced by editing either.
  1568. sliced_for = None
  1569. if item.archive_id:
  1570. result = await db.execute(select(PrintArchive.sliced_for_model).where(PrintArchive.id == item.archive_id))
  1571. sliced_for = result.scalar_one_or_none()
  1572. elif item.library_file_id:
  1573. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  1574. lib = result.scalar_one_or_none()
  1575. if lib and lib.file_metadata:
  1576. sliced_for = lib.file_metadata.get("sliced_for_model")
  1577. if not is_gcode_compatible(sliced_for, update_data["target_model"]):
  1578. raise HTTPException(
  1579. 400,
  1580. f"File was sliced for {sliced_for} and cannot be dispatched to {update_data['target_model']} printers",
  1581. )
  1582. # Serialize ams_mapping to JSON for TEXT column storage
  1583. if "ams_mapping" in update_data:
  1584. update_data["ams_mapping"] = json.dumps(update_data["ams_mapping"]) if update_data["ams_mapping"] else None
  1585. # Serialize filament_overrides to JSON for TEXT column storage, keeping only
  1586. # the slots this item's plate actually prints (#2551 — same shared-override
  1587. # list the create path narrows).
  1588. if "filament_overrides" in update_data:
  1589. overrides = update_data["filament_overrides"]
  1590. if overrides:
  1591. overrides = overrides_for_plate(
  1592. overrides,
  1593. await _resolve_source_path(db, item),
  1594. update_data.get("plate_id", item.plate_id),
  1595. )
  1596. update_data["filament_overrides"] = json.dumps(overrides) if overrides else None
  1597. # Serialize H2C rack-swap nozzle pick (#1780) to JSON for TEXT column
  1598. # storage; same Text-as-opaque-blob convention as ams_mapping above.
  1599. if "nozzle_mapping" in update_data:
  1600. update_data["nozzle_mapping"] = (
  1601. json.dumps(update_data["nozzle_mapping"]) if update_data["nozzle_mapping"] else None
  1602. )
  1603. # Re-check the dispatch claim right before mutating (#2615). Several awaited
  1604. # validations ran since the guard above, and a scheduler worker may have
  1605. # claimed the row in that gap. A fresh read (item isn't dirty yet, so no
  1606. # autoflush races the check) narrows the window to effectively nothing.
  1607. claimed = (
  1608. await db.execute(select(PrintQueueItem.dispatching_at).where(PrintQueueItem.id == item_id))
  1609. ).scalar_one_or_none()
  1610. if claimed is not None:
  1611. raise HTTPException(409, "Item is being dispatched — cancel it first to make changes")
  1612. for field, value in update_data.items():
  1613. setattr(item, field, value)
  1614. await db.commit()
  1615. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  1616. logger.info("Updated queue item %s", item_id)
  1617. return _enrich_response(item)
  1618. @router.delete("/{item_id}")
  1619. async def delete_queue_item(
  1620. item_id: int,
  1621. db: AsyncSession = Depends(get_db),
  1622. auth_result: tuple[User | None, bool] = Depends(
  1623. require_ownership_permission(
  1624. Permission.QUEUE_DELETE_ALL,
  1625. Permission.QUEUE_DELETE_OWN,
  1626. )
  1627. ),
  1628. ):
  1629. """Remove an item from the queue."""
  1630. user, can_modify_all = auth_result
  1631. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  1632. item = result.scalar_one_or_none()
  1633. if not item:
  1634. raise HTTPException(404, "Queue item not found")
  1635. # Ownership check
  1636. if not can_modify_all:
  1637. if item.created_by_id != user.id:
  1638. raise HTTPException(403, "You can only delete your own queue items")
  1639. if item.status == "printing":
  1640. raise HTTPException(400, "Cannot delete item that is currently printing")
  1641. await db.delete(item)
  1642. await db.commit()
  1643. logger.info("Deleted queue item %s", item_id)
  1644. return {"message": "Queue item deleted"}
  1645. @router.post("/reorder")
  1646. async def reorder_queue(
  1647. data: PrintQueueReorder,
  1648. db: AsyncSession = Depends(get_db),
  1649. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_ALL),
  1650. ):
  1651. """Bulk update positions for queue items."""
  1652. for reorder_item in data.items:
  1653. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == reorder_item.id))
  1654. item = result.scalar_one_or_none()
  1655. if item and item.status == "pending":
  1656. item.position = reorder_item.position
  1657. await db.commit()
  1658. logger.info("Reordered %s queue items", len(data.items))
  1659. return {"message": f"Reordered {len(data.items)} items"}
  1660. @router.post("/printer/{printer_id}/resume")
  1661. async def resume_queue_after_failure(
  1662. printer_id: int,
  1663. db: AsyncSession = Depends(get_db),
  1664. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_ALL),
  1665. ):
  1666. """Clear the previous-success gate for a printer and restore skipped items.
  1667. Single atomic op (#1818):
  1668. * Sets ``gate_acknowledged=True`` on every ``failed`` / ``aborted`` queue
  1669. item for this printer that's still in the scheduler's lookback window,
  1670. so the next ``_check_previous_success`` call ignores them.
  1671. * Restores ``skipped`` items whose ``error_message`` matches the
  1672. scheduler's exact "Previous print failed or was aborted" gate string
  1673. back to ``pending`` (clears ``error_message`` + ``completed_at``).
  1674. Returns counts so the UI can render a precise toast. No-op endpoint
  1675. (zero counts) when called against a printer with no gate to clear.
  1676. """
  1677. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1678. printer = result.scalar_one_or_none()
  1679. if not printer:
  1680. raise HTTPException(404, "Printer not found")
  1681. ack_result = await db.execute(
  1682. select(PrintQueueItem)
  1683. .where(PrintQueueItem.printer_id == printer_id)
  1684. .where(PrintQueueItem.status.in_(["failed", "aborted"]))
  1685. .where(PrintQueueItem.gate_acknowledged == False) # noqa: E712
  1686. )
  1687. to_ack = ack_result.scalars().all()
  1688. for failed_item in to_ack:
  1689. failed_item.gate_acknowledged = True
  1690. restore_result = await db.execute(
  1691. select(PrintQueueItem)
  1692. .where(PrintQueueItem.printer_id == printer_id)
  1693. .where(PrintQueueItem.status == "skipped")
  1694. .where(PrintQueueItem.error_message == "Previous print failed or was aborted")
  1695. )
  1696. to_restore = restore_result.scalars().all()
  1697. for skipped_item in to_restore:
  1698. skipped_item.status = "pending"
  1699. skipped_item.error_message = None
  1700. skipped_item.completed_at = None
  1701. await db.commit()
  1702. logger.info(
  1703. "Resume after failure on printer %s: acknowledged %d failure(s), restored %d skipped item(s)",
  1704. printer_id,
  1705. len(to_ack),
  1706. len(to_restore),
  1707. )
  1708. return {"acknowledged": len(to_ack), "restored": len(to_restore)}
  1709. @router.post("/{item_id}/cancel")
  1710. async def cancel_queue_item(
  1711. item_id: int,
  1712. db: AsyncSession = Depends(get_db),
  1713. auth_result: tuple[User | None, bool] = Depends(
  1714. require_ownership_permission(
  1715. Permission.QUEUE_UPDATE_ALL,
  1716. Permission.QUEUE_UPDATE_OWN,
  1717. )
  1718. ),
  1719. ):
  1720. """Cancel a pending queue item."""
  1721. user, can_modify_all = auth_result
  1722. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  1723. item = result.scalar_one_or_none()
  1724. if not item:
  1725. raise HTTPException(404, "Queue item not found")
  1726. # Ownership check
  1727. if not can_modify_all:
  1728. if item.created_by_id != user.id:
  1729. raise HTTPException(403, "You can only cancel your own queue items")
  1730. if item.status not in ("pending",):
  1731. raise HTTPException(400, f"Cannot cancel item with status '{item.status}'")
  1732. item.status = "cancelled"
  1733. item.completed_at = datetime.now(timezone.utc)
  1734. await db.commit()
  1735. logger.info("Cancelled queue item %s", item_id)
  1736. return {"message": "Queue item cancelled"}
  1737. @router.post("/{item_id}/stop")
  1738. async def stop_queue_item(
  1739. item_id: int,
  1740. db: AsyncSession = Depends(get_db),
  1741. auth_result: tuple[User | None, bool] = Depends(
  1742. require_ownership_permission(
  1743. Permission.QUEUE_UPDATE_ALL,
  1744. Permission.QUEUE_UPDATE_OWN,
  1745. )
  1746. ),
  1747. ):
  1748. """Stop an actively printing queue item.
  1749. Ownership-scoped (#1625-followup): callers with QUEUE_UPDATE_OWN can stop
  1750. their own items; callers with QUEUE_UPDATE_ALL can stop any item. Mirrors
  1751. the /cancel shape. Pre-fix this required QUEUE_UPDATE_ALL — Operators
  1752. holding only _OWN saw the Stop button in the queue UI but got 403 on click.
  1753. """
  1754. from backend.app.services.printer_manager import printer_manager
  1755. user, can_modify_all = auth_result
  1756. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  1757. item = result.scalar_one_or_none()
  1758. if not item:
  1759. raise HTTPException(404, "Queue item not found")
  1760. # Ownership check — mirrors /cancel. Ownerless items (created_by_id IS NULL)
  1761. # require _ALL: stop is destructive and an _OWN holder can't claim "they
  1762. # own it" the way /start does (#1670).
  1763. if not can_modify_all and user is not None:
  1764. if item.created_by_id is None or item.created_by_id != user.id:
  1765. raise HTTPException(403, "You can only stop your own queue items")
  1766. if item.status != "printing":
  1767. raise HTTPException(400, f"Can only stop items that are printing, current status: '{item.status}'")
  1768. # Capture values we need for background task
  1769. printer_id = item.printer_id
  1770. auto_off_after = item.auto_off_after
  1771. # Try to send stop command to printer
  1772. stop_sent = False
  1773. try:
  1774. stop_sent = printer_manager.stop_print(printer_id)
  1775. if not stop_sent:
  1776. logger.warning("stop_print returned False for printer %s - printer may not be connected", printer_id)
  1777. except Exception as e:
  1778. logger.error("Error sending stop command for queue item %s: %s", item_id, e)
  1779. # Mark this printer as user-stopped BEFORE the first await so that if the
  1780. # MQTT on_print_complete callback fires during the db.commit() yield the flag
  1781. # is already set and the "failed" status will be correctly overridden to
  1782. # "cancelled" (preventing a spurious "print failed" notification).
  1783. try:
  1784. from backend.app.main import mark_printer_stopped_by_user
  1785. mark_printer_stopped_by_user(printer_id)
  1786. except Exception as _mark_err:
  1787. logger.warning("Failed to mark printer %s as user-stopped: %s", printer_id, _mark_err)
  1788. # Update queue item status regardless - if printer is off, print is already stopped
  1789. item.status = "cancelled"
  1790. item.completed_at = datetime.now(timezone.utc)
  1791. item.error_message = "Stopped by user" if stop_sent else "Stopped by user (printer was offline)"
  1792. # Reconcile the linked archive when the printer is offline (#2603). When the
  1793. # stop command reaches the printer it later reports the stop over MQTT and
  1794. # on_print_complete flips the archive to cancelled/failed. When the printer is
  1795. # offline no such event ever arrives, so the archive would stay "printing"
  1796. # forever (queue row cancelled, archive still printing — the reporter's
  1797. # archive 436). Close it out here, mirroring what the MQTT path would have
  1798. # done. Only touch a still-"printing" archive so we never overwrite a real
  1799. # completion that raced in.
  1800. if not stop_sent and item.archive_id:
  1801. archive = await db.get(PrintArchive, item.archive_id)
  1802. if archive and archive.status == "printing":
  1803. archive.status = "cancelled"
  1804. archive.completed_at = datetime.now(timezone.utc)
  1805. archive.failure_reason = "Stopped by user (printer was offline)"
  1806. await db.commit()
  1807. logger.info("Stopped printing queue item %s (stop command sent: %s)", item_id, stop_sent)
  1808. # Schedule power-off if the queue item opted in. Delegates to the smart-plug
  1809. # manager so the off honours each plug's configured strategy (time delay or
  1810. # temperature threshold), is cancelled if the printer starts printing again,
  1811. # and never cuts power on a loaded print (#1890). Previously an inline block
  1812. # hardcoded a 50°C / 600s cooldown wait and powered off on the timeout
  1813. # regardless of print state.
  1814. if auto_off_after:
  1815. from backend.app.services.smart_plug_manager import smart_plug_manager
  1816. try:
  1817. await smart_plug_manager.schedule_off_after_queue_job(printer_id, db)
  1818. except Exception as e:
  1819. logger.warning("Auto-off: Failed to schedule power-off for printer %s: %s", printer_id, e)
  1820. return {"message": "Print stopped" if stop_sent else "Queue item cancelled (printer was offline)"}
  1821. @router.post("/{item_id}/start")
  1822. async def start_queue_item(
  1823. item_id: int,
  1824. skip_filament_check: bool = Query(default=False),
  1825. db: AsyncSession = Depends(get_db),
  1826. auth_result: tuple[User | None, bool] = Depends(
  1827. require_ownership_permission(
  1828. Permission.QUEUE_UPDATE_ALL,
  1829. Permission.QUEUE_UPDATE_OWN,
  1830. )
  1831. ),
  1832. ):
  1833. """Manually start a staged (manual_start) queue item.
  1834. Ownership-scoped (#1625-followup): callers with QUEUE_UPDATE_OWN can
  1835. start their own items + claim ownership of NULL-owner items (VP-uploaded
  1836. items arrive unattributed per #1670). Callers with QUEUE_UPDATE_ALL can
  1837. start any item. Pre-fix this required QUEUE_UPDATE_OWN with no ownership
  1838. check, so _OWN holders could start anyone's queue items via direct API.
  1839. Clears the manual_start flag so the scheduler picks it up. When
  1840. ``skip_filament_check`` is false (the default) the live filament
  1841. deficit (#1496) is checked first — if the assigned spool can't satisfy
  1842. a slot's required grams, the route returns ``409`` with the deficit
  1843. payload so the caller can show a confirm dialog and retry with
  1844. ``skip_filament_check=true``.
  1845. """
  1846. user, can_modify_all = auth_result
  1847. result = await db.execute(
  1848. select(PrintQueueItem)
  1849. .options(
  1850. selectinload(PrintQueueItem.archive),
  1851. selectinload(PrintQueueItem.printer),
  1852. selectinload(PrintQueueItem.library_file),
  1853. selectinload(PrintQueueItem.batch),
  1854. selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file),
  1855. )
  1856. .where(PrintQueueItem.id == item_id)
  1857. )
  1858. item = result.scalar_one_or_none()
  1859. if not item:
  1860. raise HTTPException(404, "Queue item not found")
  1861. # Ownership check — softer than /cancel because /start is the entry point
  1862. # for #1670's VP-import flow: an unowned item is claimable by the first
  1863. # _OWN holder who clicks ▶, and the route below credits them as owner.
  1864. # An item with a DIFFERENT owner → 403.
  1865. if not can_modify_all and user is not None:
  1866. if item.created_by_id is not None and item.created_by_id != user.id:
  1867. raise HTTPException(403, "You can only start your own queue items")
  1868. if item.status != "pending":
  1869. raise HTTPException(400, f"Can only start pending items, current status: '{item.status}'")
  1870. # Live deficit check — re-evaluated against current spool state, so a
  1871. # spool swap between scheduler flagging and the user clicking ▶ clears
  1872. # the block automatically.
  1873. if not skip_filament_check:
  1874. deficit = await compute_deficit_for_queue_item(db, item)
  1875. if deficit:
  1876. raise HTTPException(
  1877. status_code=409,
  1878. detail={
  1879. "code": "insufficient_filament",
  1880. "deficit": [d.to_dict() for d in deficit],
  1881. },
  1882. )
  1883. # Print Anyway / no deficit: clear the flags and let the scheduler dispatch.
  1884. item.manual_start = False
  1885. item.filament_short = False
  1886. # Persist the user's "Print Anyway" decision so the scheduler does not
  1887. # immediately re-flag this item on the next tick (#1698-followup). The
  1888. # pre-fix behaviour bounced between "user said anyway" and
  1889. # "scheduler re-blocked on same deficit" forever.
  1890. if skip_filament_check:
  1891. item.skip_filament_check = True
  1892. # Credit the clicker as the item's owner when no prior owner is set —
  1893. # VP-uploaded queue items arrive over FTP unattributed, so without this
  1894. # the print log's User column stays blank even when auth is on
  1895. # (#1670). An item that already has a creator (UI-added queue items)
  1896. # keeps that attribution; the dispatcher is not promoted over the
  1897. # original uploader.
  1898. if user is not None and item.created_by_id is None:
  1899. item.created_by_id = user.id
  1900. await db.commit()
  1901. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  1902. logger.info(
  1903. "Manually started queue item %s (cleared manual_start; skip_filament_check=%s)",
  1904. item_id,
  1905. skip_filament_check,
  1906. )
  1907. return _enrich_response(item)