get puck events

This commit is contained in:
GotthardG 2024-12-11 16:50:59 +01:00
parent f83dc1ea58
commit 3b79951a43

View File

@ -3,7 +3,7 @@ from sqlalchemy.orm import Session
from typing import List from typing import List
import uuid import uuid
from app.schemas import Puck as PuckSchema, PuckCreate, PuckUpdate, SetTellPosition, PuckEvent 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 app.dependencies import get_db
from datetime import datetime 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]) @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 # Query the Slot table to ensure the slot exists
slot = db.query(SlotModel).filter(SlotModel.id == slot_id).first() 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: if not slot.dewar:
raise HTTPException(status_code=404, detail="No dewar associated with the given slot") 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) # Find the most recent dewar associated with a "beamline" event type
# Query related `LogisticsEvent` or the latest assignment of this dewar recent_beamline_event = (
recent_slot = ( db.query(LogisticsEventModel)
db.query(SlotModel) .filter(
.join(LogisticsEventModel, LogisticsEventModel.slot_id == slot_id,
SlotModel.id == LogisticsEventModel.slot_id) # Assumes LogisticsEvent tracks slot changes LogisticsEventModel.event_type == "Beamline" # Filter by "beamline"
.filter(SlotModel.dewar_unique_id == slot.dewar_unique_id) # Match slots by dewar's unique ID )
.order_by(LogisticsEventModel.timestamp.desc()) # Most recent event .order_by(LogisticsEventModel.timestamp.desc()) # Order by most recent event
.first() .first()
) )
# Confirm if a recent slot association exists # Confirm if we found a "beamline" event for the slot
if not recent_slot or not recent_slot.dewar: if not recent_beamline_event:
raise HTTPException(status_code=404, detail="No recent slot association found for the dewar") 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 # Retrieve the dewar linked to the "beamline" event
pucks = db.query(PuckModel).filter(PuckModel.dewar_id == recent_slot.dewar.id).all() 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 the list of pucks
return pucks return pucks