Add endpoints and logic for fetching associations by beamtime

Introduced endpoints to fetch pucks, dewars, and samples by beamtime ID. Updated backend logic to ensure consistency between dewars, pucks, and samples assignments. Enhanced frontend to display and handle beamline-specific associations dynamically.
This commit is contained in:
GotthardG
2025-05-07 11:19:00 +02:00
parent e341459590
commit 0fa038be94
5 changed files with 217 additions and 308 deletions

View File

@ -682,9 +682,16 @@ dewar_to_beamtime = {
dewar.id: random.choice([1, 2]) for dewar in dewars # Or use actual beamtime ids
}
# Update dewars and their pucks with consistent beamtime
for dewar in dewars:
dewar.beamtime_id = dewar_to_beamtime[dewar.id]
for puck in pucks:
dewar_id = puck.dewar_id # Assuming puck has dewar_id
assigned_beamtime = dewar_to_beamtime[dewar_id]
puck.beamtime_id = (
assigned_beamtime # Associate puck to the same beamtime as its dewar
)
positions_with_samples = random.randint(1, 16)
occupied_positions = random.sample(range(1, 17), positions_with_samples)
@ -696,7 +703,7 @@ for puck in pucks:
sample_name=f"Sample{sample_id_counter:03}",
position=pos,
puck_id=puck.id,
beamtime_id=assigned_beamtime, # IMPORTANT: Use the dewar's beamtime
beamtime_id=assigned_beamtime,
)
samples.append(sample)
sample_id_counter += 1

View File

@ -718,3 +718,13 @@ async def get_single_shipment(id: int, db: Session = Depends(get_db)):
except SQLAlchemyError as e:
logging.error(f"Database error occurred: {e}")
raise HTTPException(status_code=500, detail="Internal server error")
@dewar_router.get(
"/by-beamtime/{beamtime_id}",
response_model=List[DewarSchema],
operation_id="get_dewars_by_beamtime",
)
async def get_dewars_by_beamtime(beamtime_id: int, db: Session = Depends(get_db)):
"""List all dewars assigned to a given beamtime."""
return db.query(DewarModel).filter(DewarModel.beamtime_id == beamtime_id).all()

View File

@ -683,3 +683,13 @@ async def assign_beamtime_to_puck(
sample.beamtime_id = None if beamtime_id == 0 else beamtime_id
db.commit()
return {"status": "success", "puck_id": puck.id, "beamtime_id": beamtime_id}
@router.get(
"/by-beamtime/{beamtime_id}",
response_model=List[PuckSchema],
operation_id="get_pucks_by_beamtime",
)
async def get_pucks_by_beamtime(beamtime_id: int, db: Session = Depends(get_db)):
"""List all pucks assigned to a given beamtime."""
return db.query(PuckModel).filter(PuckModel.beamtime_id == beamtime_id).all()

View File

@ -471,3 +471,13 @@ async def get_results_for_run_and_sample(
]
return formatted_results
@router.get(
"/by-beamtime/{beamtime_id}",
response_model=List[SampleSchema],
operation_id="get_samples_by_beamtime",
)
async def get_samples_by_beamtime(beamtime_id: int, db: Session = Depends(get_db)):
"""List all samples assigned to a given beamtime."""
return db.query(SampleModel).filter(SampleModel.beamtime_id == beamtime_id).all()