get puck events

This commit is contained in:
GotthardG 2024-12-11 16:59:22 +01:00
parent c3e476002a
commit 5d7dcebe2e

View File

@ -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]) @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)): 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 Retrieve all pucks for the most recent dewar associated with the given slot_id,
with a 'beamline' event type. 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() slot = db.query(SlotModel).filter(SlotModel.id == slot_id).first()
if not slot: if not slot:
logger.error(f"No slot found with ID={slot_id}.")
raise HTTPException(status_code=404, detail="Slot not found") raise HTTPException(status_code=404, detail="Slot not found")
# Check if this slot has an associated dewar logger.info(f"Slot found: {slot}")
if not slot.dewar:
raise HTTPException(status_code=404, detail="No dewar associated with the given 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 = ( recent_beamline_event = (
db.query(LogisticsEventModel) db.query(LogisticsEventModel)
.filter( .filter(
LogisticsEventModel.slot_id == slot_id, 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() .first()
) )
# Confirm if we found a "beamline" event for the slot
if not recent_beamline_event: if not recent_beamline_event:
logger.error(f"No 'beamline' event found for slot_id={slot_id}.")
raise HTTPException( raise HTTPException(
status_code=404, status_code=404,
detail="No 'beamline' event found for the given slot" 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() dewar = db.query(DewarModel).filter(DewarModel.id == recent_beamline_event.dewar_id).first()
if not dewar: if not dewar:
logger.error(
f"No dewar found for the most recent beamline event. Dewar ID={recent_beamline_event.dewar_id}"
)
raise HTTPException( raise HTTPException(
status_code=404, status_code=404,
detail="No dewar associated with the most recent 'beamline' event" 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() 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 return pucks