get puck events

This commit is contained in:
GotthardG 2024-12-11 15:39:16 +01:00
parent 328a95d76e
commit 9cd0d81dac
2 changed files with 36 additions and 4 deletions

View File

@ -2,7 +2,7 @@ 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, PuckEventCreate
from app.schemas import Puck as PuckSchema, PuckCreate, PuckUpdate, SetTellPosition, PuckEvent
from app.models import Puck as PuckModel, Sample as SampleModel, PuckEvent as PuckEventModel
from app.dependencies import get_db
from datetime import datetime
@ -102,3 +102,28 @@ async def set_tell_position(
"tell_position": new_puck_event.tell_position,
"timestamp": new_puck_event.timestamp,
}
@router.get("/{puck_id}/last-tell-position", status_code=status.HTTP_200_OK)
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")
.order_by(PuckEventModel.timestamp.desc())
.first()
)
# If no event is found, return a 404 error
if not last_event:
raise HTTPException(
status_code=404,
detail=f"No 'tell_position' event found for puck with ID {puck_id}",
)
# Return the details of the last tell_position event
return {
"puck_id": puck_id,
"tell_position": last_event.tell_position,
"timestamp": last_event.timestamp,
}

View File

@ -150,6 +150,15 @@ class SampleCreate(BaseModel):
class Config:
populate_by_name = True
class PuckEvent(BaseModel):
id: int
puck_id: int
tell_position: Optional[str] = None
event_type: str
timestamp: datetime
class Config:
from_attributes = True
class PuckBase(BaseModel):
puck_name: str
@ -177,6 +186,7 @@ class Puck(BaseModel):
puck_type: str
puck_location_in_dewar: int
dewar_id: int
events: List[PuckEvent] = []
samples: List[Sample] = [] # List of samples within this puck
class Config:
@ -308,9 +318,6 @@ class SlotSchema(BaseModel):
class Config:
from_attributes = True
class PuckEventCreate(BaseModel):
event_type: str
class SetTellPosition(BaseModel):
tell_position: str = Field(
...,