Fix formatting with black
This commit is contained in:
@ -2,8 +2,21 @@ from fastapi import APIRouter, HTTPException, status, Depends
|
||||
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, Dewar as DewarModel
|
||||
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,
|
||||
Dewar as DewarModel,
|
||||
)
|
||||
from app.dependencies import get_db
|
||||
from datetime import datetime
|
||||
import logging
|
||||
@ -13,6 +26,7 @@ router = APIRouter()
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.get("/", response_model=List[PuckSchema])
|
||||
async def get_pucks(db: Session = Depends(get_db)):
|
||||
return db.query(PuckModel).all()
|
||||
@ -35,8 +49,7 @@ async def get_pucks_with_tell_position(db: Session = Depends(get_db)):
|
||||
if not pucks:
|
||||
logger.info("No pucks with tell_position found.") # Log for debugging
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="No pucks with a `tell_position` found."
|
||||
status_code=404, detail="No pucks with a `tell_position` found."
|
||||
)
|
||||
|
||||
result = []
|
||||
@ -67,6 +80,7 @@ async def get_pucks_with_tell_position(db: Session = Depends(get_db)):
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/{puck_id}", response_model=PuckSchema)
|
||||
async def get_puck(puck_id: str, db: Session = Depends(get_db)):
|
||||
puck = db.query(PuckModel).filter(PuckModel.id == puck_id).first()
|
||||
@ -77,13 +91,13 @@ async def get_puck(puck_id: str, db: Session = Depends(get_db)):
|
||||
|
||||
@router.post("/", response_model=PuckSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def create_puck(puck: PuckCreate, db: Session = Depends(get_db)) -> PuckSchema:
|
||||
puck_id = f'PUCK-{uuid.uuid4().hex[:8].upper()}'
|
||||
puck_id = f"PUCK-{uuid.uuid4().hex[:8].upper()}"
|
||||
db_puck = PuckModel(
|
||||
id=puck_id,
|
||||
puck_name=puck.puck_name,
|
||||
puck_type=puck.puck_type,
|
||||
puck_location_in_dewar=puck.puck_location_in_dewar,
|
||||
dewar_id=puck.dewar_id
|
||||
dewar_id=puck.dewar_id,
|
||||
)
|
||||
db.add(db_puck)
|
||||
db.commit()
|
||||
@ -92,7 +106,9 @@ async def create_puck(puck: PuckCreate, db: Session = Depends(get_db)) -> PuckSc
|
||||
|
||||
|
||||
@router.put("/{puck_id}", response_model=PuckSchema)
|
||||
async def update_puck(puck_id: str, updated_puck: PuckUpdate, db: Session = Depends(get_db)):
|
||||
async def update_puck(
|
||||
puck_id: str, updated_puck: PuckUpdate, db: Session = Depends(get_db)
|
||||
):
|
||||
puck = db.query(PuckModel).filter(PuckModel.id == puck_id).first()
|
||||
if not puck:
|
||||
raise HTTPException(status_code=404, detail="Puck not found")
|
||||
@ -115,17 +131,18 @@ async def delete_puck(puck_id: str, db: Session = Depends(get_db)):
|
||||
db.commit()
|
||||
return
|
||||
|
||||
|
||||
@router.put("/{puck_id}/tell_position", status_code=status.HTTP_200_OK)
|
||||
async def set_tell_position(
|
||||
puck_id: int,
|
||||
request: SetTellPosition,
|
||||
db: Session = Depends(get_db)
|
||||
puck_id: int, request: SetTellPosition, db: Session = Depends(get_db)
|
||||
):
|
||||
# Get the requested tell_position
|
||||
tell_position = request.tell_position
|
||||
|
||||
# Define valid positions
|
||||
valid_positions = [f"{letter}{num}" for letter in "ABCDEF" for num in range(1, 6)] + ["null", None]
|
||||
valid_positions = [
|
||||
f"{letter}{num}" for letter in "ABCDEF" for num in range(1, 6)
|
||||
] + ["null", None]
|
||||
|
||||
# Validate tell_position
|
||||
if tell_position not in valid_positions:
|
||||
@ -161,7 +178,10 @@ async def get_last_tell_position(puck_id: str, db: Session = Depends(get_db)):
|
||||
# Query the most recent tell_position_set event for the given puck_id
|
||||
last_event = (
|
||||
db.query(PuckEventModel)
|
||||
.filter(PuckEventModel.puck_id == puck_id, PuckEventModel.event_type == "tell_position_set")
|
||||
.filter(
|
||||
PuckEventModel.puck_id == puck_id,
|
||||
PuckEventModel.event_type == "tell_position_set",
|
||||
)
|
||||
.order_by(PuckEventModel.timestamp.desc())
|
||||
.first()
|
||||
)
|
||||
@ -182,10 +202,7 @@ async def get_last_tell_position(puck_id: str, db: Session = Depends(get_db)):
|
||||
|
||||
|
||||
@router.get("/slot/{slot_identifier}", response_model=List[dict])
|
||||
async def get_pucks_by_slot(
|
||||
slot_identifier: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
async def get_pucks_by_slot(slot_identifier: str, db: Session = Depends(get_db)):
|
||||
"""
|
||||
Retrieve all pucks associated with all dewars linked to the given slot
|
||||
(by ID or keyword) via 'beamline' events.
|
||||
@ -200,28 +217,29 @@ async def get_pucks_by_slot(
|
||||
"PXIII": 49,
|
||||
"X06SA": 47,
|
||||
"X10SA": 48,
|
||||
"X06DA": 49
|
||||
"X06DA": 49,
|
||||
}
|
||||
|
||||
# Check if the slot identifier is an alias or ID
|
||||
try:
|
||||
slot_id = int(slot_identifier) # If the user provided a numeric ID
|
||||
alias = next((k for k, v in slot_aliases.items() if v == slot_id), slot_identifier)
|
||||
alias = next(
|
||||
(k for k, v in slot_aliases.items() if v == slot_id), slot_identifier
|
||||
)
|
||||
except ValueError:
|
||||
slot_id = slot_aliases.get(slot_identifier.upper()) # Try mapping alias
|
||||
alias = slot_identifier.upper() # Keep alias as-is for error messages
|
||||
if not slot_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Invalid slot identifier. Must be an ID or one of the following: PXI, PXII, PXIII, X06SA, X10SA, X06DA."
|
||||
detail="Invalid slot identifier. Must be an ID or one of the following: PXI, PXII, PXIII, X06SA, X10SA, X06DA.",
|
||||
)
|
||||
|
||||
# Verify that the slot exists
|
||||
slot = db.query(SlotModel).filter(SlotModel.id == slot_id).first()
|
||||
if not slot:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Slot not found for identifier '{alias}'."
|
||||
status_code=404, detail=f"Slot not found for identifier '{alias}'."
|
||||
)
|
||||
|
||||
logger.info(f"Slot found: ID={slot.id}, Label={slot.label}")
|
||||
@ -231,7 +249,7 @@ async def get_pucks_by_slot(
|
||||
db.query(LogisticsEventModel)
|
||||
.filter(
|
||||
LogisticsEventModel.slot_id == slot_id,
|
||||
LogisticsEventModel.event_type == "beamline"
|
||||
LogisticsEventModel.event_type == "beamline",
|
||||
)
|
||||
.order_by(LogisticsEventModel.timestamp.desc())
|
||||
.all()
|
||||
@ -240,8 +258,7 @@ async def get_pucks_by_slot(
|
||||
if not beamline_events:
|
||||
logger.warning(f"No dewars associated to this beamline '{alias}'.")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"No dewars found for the given beamline '{alias}'."
|
||||
status_code=404, detail=f"No dewars found for the given beamline '{alias}'."
|
||||
)
|
||||
|
||||
logger.info(f"Found {len(beamline_events)} beamline events for slot_id={slot_id}.")
|
||||
@ -253,8 +270,7 @@ async def get_pucks_by_slot(
|
||||
if not dewars:
|
||||
logger.warning(f"No dewars found for beamline '{alias}'.")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"No dewars found for beamline '{alias}'."
|
||||
status_code=404, detail=f"No dewars found for beamline '{alias}'."
|
||||
)
|
||||
|
||||
logger.info(f"Found {len(dewars)} dewars for beamline '{alias}'.")
|
||||
@ -273,7 +289,7 @@ async def get_pucks_by_slot(
|
||||
logger.warning(f"No pucks found for dewars associated with beamline '{alias}'.")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"No pucks found for dewars associated with beamline '{alias}'."
|
||||
detail=f"No pucks found for dewars associated with beamline '{alias}'.",
|
||||
)
|
||||
|
||||
logger.info(f"Found {len(puck_list)} pucks for beamline '{alias}'.")
|
||||
@ -285,10 +301,10 @@ async def get_pucks_by_slot(
|
||||
"puck_name": puck.puck_name,
|
||||
"puck_type": puck.puck_type,
|
||||
"dewar_id": puck.dewar_id,
|
||||
"dewar_name": dewar_mapping.get(puck.dewar_id) # Link dewar_name
|
||||
"dewar_name": dewar_mapping.get(puck.dewar_id), # Link dewar_name
|
||||
}
|
||||
for puck in puck_list
|
||||
]
|
||||
|
||||
# Return the list of pucks with their associated dewar names
|
||||
return puck_output
|
||||
return puck_output
|
||||
|
Reference in New Issue
Block a user