Refactor Dewar service methods and improve field handling

Updated Dewar API methods to use protected endpoints for enhanced security and consistency. Added `pgroups` handling in various frontend components and modified the LogisticsView contact field for clarity. Simplified backend router imports for better readability.
This commit is contained in:
GotthardG
2025-01-30 13:39:49 +01:00
parent 44582cf38e
commit c2215860bf
20 changed files with 304 additions and 262 deletions

View File

@ -251,6 +251,9 @@ async def get_pucks_with_tell_position(db: Session = Depends(get_db)):
dewar_name=str(dewar.dewar_name)
if dewar and dewar.dewar_name
else None,
pgroup=str(dewar.pgroups)
if dewar.pgroups
else None, # will be replaced later by puck pgroup
samples=[
Sample(
id=sample.id,
@ -413,6 +416,7 @@ async def get_pucks_by_slot(slot_identifier: str, db: Session = Depends(get_db))
dewar_ids = [dewar.id for dewar in dewars]
dewar_map = {dewar.id: dewar.dewar_name for dewar in dewars}
dewar_pgroups = {dewar.id: dewar.pgroups for dewar in dewars}
# Subquery to fetch the latest event for each puck (any type of event)
latest_event_subquery = (
@ -430,9 +434,9 @@ async def get_pucks_by_slot(slot_identifier: str, db: Session = Depends(get_db))
PuckModel,
PuckEventModel.event_type,
PuckEventModel.tell_position,
DewarModel, # Include DewarModel
)
.join( # Join pucks with the latest event
# (outer join to include pucks without events)
latest_event_subquery,
PuckModel.id == latest_event_subquery.c.puck_id,
isouter=True,
@ -443,6 +447,11 @@ async def get_pucks_by_slot(slot_identifier: str, db: Session = Depends(get_db))
& (PuckEventModel.timestamp == latest_event_subquery.c.latest_event_time),
isouter=True,
)
.join( # Join with DewarModel to get dewar details
DewarModel,
PuckModel.dewar_id == DewarModel.id,
isouter=True,
)
.filter(PuckModel.dewar_id.in_(dewar_ids)) # Restrict pucks to relevant dewars
.all()
)
@ -458,13 +467,16 @@ async def get_pucks_by_slot(slot_identifier: str, db: Session = Depends(get_db))
# Prepare the final response
results = []
for puck, event_type, tell_position in pucks_with_latest_events:
for puck, event_type, dewar, tell_position in pucks_with_latest_events:
logger.debug(
f"Puck ID: {puck.id}, Name: {puck.puck_name}, Event Type: {event_type}, "
f"Tell Position: {tell_position}"
)
dewar_name = dewar_map.get(puck.dewar_id, "Unknown")
pgroup = dewar_pgroups.get(
puck.dewar_id
) # will be replaced later by puck pgroup
# For pucks with no events or whose latest event is `puck_removed`, set
# `tell_position` to None
@ -475,6 +487,7 @@ async def get_pucks_by_slot(slot_identifier: str, db: Session = Depends(get_db))
results.append(
PuckWithTellPosition(
id=puck.id,
pgroup=pgroup,
puck_name=puck.puck_name,
puck_type=puck.puck_type,
puck_location_in_dewar=int(puck.puck_location_in_dewar)