From 3b79951a4370e8ce6f49d8ef57a0f3d75244172e Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:50:59 +0100 Subject: [PATCH] get puck events --- backend/app/routers/puck.py | 44 +++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/backend/app/routers/puck.py b/backend/app/routers/puck.py index c616103..43d54f6 100644 --- a/backend/app/routers/puck.py +++ b/backend/app/routers/puck.py @@ -3,7 +3,7 @@ from sqlalchemy.orm import Session from typing import List import uuid from app.schemas import Puck as PuckSchema, PuckCreate, PuckUpdate, SetTellPosition, PuckEvent -from app.models import Puck as PuckModel, Sample as SampleModel, PuckEvent as PuckEventModel, Slot as SlotModel, LogisticsEvent as LogisticsEventModel +from app.models import Puck as PuckModel, Sample as SampleModel, PuckEvent as PuckEventModel, Slot as SlotModel, LogisticsEvent as LogisticsEventModel, Dewar as DewarModel from app.dependencies import get_db from datetime import datetime @@ -129,9 +129,10 @@ 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_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. """ # Query the Slot table to ensure the slot exists slot = db.query(SlotModel).filter(SlotModel.id == slot_id).first() @@ -142,23 +143,34 @@ async def get_pucks_by_latest_dewar_slot(slot_id: int, db: Session = Depends(get if not slot.dewar: raise HTTPException(status_code=404, detail="No dewar associated with the given slot") - # Find the most recent slot for the associated dewar (using a timestamp or event record) - # Query related `LogisticsEvent` or the latest assignment of this dewar - recent_slot = ( - db.query(SlotModel) - .join(LogisticsEventModel, - SlotModel.id == LogisticsEventModel.slot_id) # Assumes LogisticsEvent tracks slot changes - .filter(SlotModel.dewar_unique_id == slot.dewar_unique_id) # Match slots by dewar's unique ID - .order_by(LogisticsEventModel.timestamp.desc()) # Most recent event + # Find the most recent dewar associated with a "beamline" event type + recent_beamline_event = ( + db.query(LogisticsEventModel) + .filter( + LogisticsEventModel.slot_id == slot_id, + LogisticsEventModel.event_type == "Beamline" # Filter by "beamline" + ) + .order_by(LogisticsEventModel.timestamp.desc()) # Order by most recent event .first() ) - # Confirm if a recent slot association exists - if not recent_slot or not recent_slot.dewar: - raise HTTPException(status_code=404, detail="No recent slot association found for the dewar") + # Confirm if we found a "beamline" event for the slot + if not recent_beamline_event: + raise HTTPException( + status_code=404, + detail="No 'beamline' event found for the given slot" + ) - # Retrieve all pucks from the dewar currently in the recent slot - pucks = db.query(PuckModel).filter(PuckModel.dewar_id == recent_slot.dewar.id).all() + # Retrieve the dewar linked to the "beamline" event + dewar = db.query(DewarModel).filter(DewarModel.id == recent_beamline_event.dewar_id).first() + if not dewar: + raise HTTPException( + status_code=404, + detail="No dewar associated with the most recent 'beamline' event" + ) + + # Retrieve all pucks associated with the dewar + pucks = db.query(PuckModel).filter(PuckModel.dewar_id == dewar.id).all() # Return the list of pucks return pucks