From 5d7dcebe2e94e1af0c1c41bd2128cbd91c1cb1db Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:59:22 +0100 Subject: [PATCH] get puck events --- backend/app/routers/puck.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/backend/app/routers/puck.py b/backend/app/routers/puck.py index 3a48d63..5664205 100644 --- a/backend/app/routers/puck.py +++ b/backend/app/routers/puck.py @@ -131,46 +131,57 @@ async def get_last_tell_position(puck_id: str, db: Session = Depends(get_db)): @router.get("/slot/{slot_id}", response_model=List[PuckSchema]) async def get_pucks_by_latest_beamline_dewar_slot(slot_id: int, db: Session = Depends(get_db)): """ - Retrieve all pucks for the most recent dewar associated with the given slot_id - with a 'beamline' event type. + Retrieve all pucks for the most recent dewar associated with the given slot_id, + where the last logistics event is of type 'beamline'. """ - # Query the Slot table to ensure the slot exists + # Step 1: Verify the slot exists slot = db.query(SlotModel).filter(SlotModel.id == slot_id).first() if not slot: + logger.error(f"No slot found with ID={slot_id}.") raise HTTPException(status_code=404, detail="Slot not found") - # Check if this slot has an associated dewar - if not slot.dewar: - raise HTTPException(status_code=404, detail="No dewar associated with the given slot") + logger.info(f"Slot found: {slot}") - # Find the most recent dewar associated with a "beamline" event type + # Step 2: Fetch the most recent 'beamline' event for the slot recent_beamline_event = ( db.query(LogisticsEventModel) .filter( LogisticsEventModel.slot_id == slot_id, - LogisticsEventModel.event_type == "beamline" # Filter by "beamline" + LogisticsEventModel.event_type == "beamline" ) - .order_by(LogisticsEventModel.timestamp.desc()) # Order by most recent event + .order_by(LogisticsEventModel.timestamp.desc()) .first() ) - # Confirm if we found a "beamline" event for the slot if not recent_beamline_event: + logger.error(f"No 'beamline' event found for slot_id={slot_id}.") raise HTTPException( status_code=404, detail="No 'beamline' event found for the given slot" ) - # Retrieve the dewar linked to the "beamline" event + logger.info(f"Found beamline event: {recent_beamline_event}") + + # Step 3: Retrieve the Dewar from the recent event dewar = db.query(DewarModel).filter(DewarModel.id == recent_beamline_event.dewar_id).first() + if not dewar: + logger.error( + f"No dewar found for the most recent beamline event. Dewar ID={recent_beamline_event.dewar_id}" + ) raise HTTPException( status_code=404, detail="No dewar associated with the most recent 'beamline' event" ) - # Retrieve all pucks associated with the dewar + logger.info(f"Associated dewar found: {dewar}") + + # Step 4: Retrieve all pucks associated with the Dewar pucks = db.query(PuckModel).filter(PuckModel.dewar_id == dewar.id).all() - # Return the list of pucks + if not pucks: + logger.warning(f"No pucks found for Dewar ID={dewar.id}.") + else: + logger.info(f"Retrieved pucks for Dewar ID={dewar.id}: {pucks}") + return pucks