get puck events
This commit is contained in:
parent
328a95d76e
commit
9cd0d81dac
@ -2,7 +2,7 @@ from fastapi import APIRouter, HTTPException, status, Depends
|
|||||||
from sqlalchemy.orm import Session
|
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, 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.models import Puck as PuckModel, Sample as SampleModel, PuckEvent as PuckEventModel
|
||||||
from app.dependencies import get_db
|
from app.dependencies import get_db
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@ -102,3 +102,28 @@ async def set_tell_position(
|
|||||||
"tell_position": new_puck_event.tell_position,
|
"tell_position": new_puck_event.tell_position,
|
||||||
"timestamp": new_puck_event.timestamp,
|
"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,
|
||||||
|
}
|
@ -150,6 +150,15 @@ class SampleCreate(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
populate_by_name = True
|
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):
|
class PuckBase(BaseModel):
|
||||||
puck_name: str
|
puck_name: str
|
||||||
@ -177,6 +186,7 @@ class Puck(BaseModel):
|
|||||||
puck_type: str
|
puck_type: str
|
||||||
puck_location_in_dewar: int
|
puck_location_in_dewar: int
|
||||||
dewar_id: int
|
dewar_id: int
|
||||||
|
events: List[PuckEvent] = []
|
||||||
samples: List[Sample] = [] # List of samples within this puck
|
samples: List[Sample] = [] # List of samples within this puck
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
@ -308,9 +318,6 @@ class SlotSchema(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
from_attributes = True
|
from_attributes = True
|
||||||
|
|
||||||
class PuckEventCreate(BaseModel):
|
|
||||||
event_type: str
|
|
||||||
|
|
||||||
class SetTellPosition(BaseModel):
|
class SetTellPosition(BaseModel):
|
||||||
tell_position: str = Field(
|
tell_position: str = Field(
|
||||||
...,
|
...,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user