Refactor AareDB backend and update schemas and paths.
Revised backend schema definitions, removing unnecessary attributes and adding new configurations. Updated file path references to align with the aaredb structure. Cleaned up redundant notebook content and commented out unused database regeneration logic in the backend. Added posting a result to the database
This commit is contained in:
@ -14,8 +14,9 @@ from app.schemas import (
|
||||
SampleResult,
|
||||
ExperimentParametersCreate,
|
||||
ExperimentParametersRead,
|
||||
# ResultResponse,
|
||||
# ResultCreate,
|
||||
ImageInfo,
|
||||
ResultResponse,
|
||||
ResultCreate,
|
||||
)
|
||||
from app.models import (
|
||||
Puck as PuckModel,
|
||||
@ -25,7 +26,7 @@ from app.models import (
|
||||
Dewar as DewarModel,
|
||||
ExperimentParameters as ExperimentParametersModel,
|
||||
# ExperimentParameters,
|
||||
# Results,
|
||||
Results as ResultsModel,
|
||||
)
|
||||
from app.dependencies import get_db
|
||||
import logging
|
||||
@ -246,8 +247,13 @@ async def get_sample_results(active_pgroup: str, db: Session = Depends(get_db)):
|
||||
|
||||
results = []
|
||||
for sample in samples:
|
||||
# Query images associated with the sample.
|
||||
images = db.query(ImageModel).filter(ImageModel.sample_id == sample.id).all()
|
||||
# Query images associated with the sample, including the related event_type
|
||||
images = (
|
||||
db.query(ImageModel)
|
||||
.options(joinedload(ImageModel.sample_event))
|
||||
.filter(ImageModel.sample_id == sample.id)
|
||||
.all()
|
||||
)
|
||||
|
||||
# Query experiment parameters (which include beamline parameters) for the
|
||||
# sample.
|
||||
@ -259,27 +265,34 @@ async def get_sample_results(active_pgroup: str, db: Session = Depends(get_db)):
|
||||
print("Experiment Parameters for sample", sample.id, experiment_parameters)
|
||||
|
||||
results.append(
|
||||
{
|
||||
"sample_id": sample.id,
|
||||
"sample_name": sample.sample_name,
|
||||
"puck_name": sample.puck.puck_name if sample.puck else None,
|
||||
"dewar_name": sample.puck.dewar.dewar_name
|
||||
SampleResult(
|
||||
sample_id=sample.id,
|
||||
sample_name=sample.sample_name,
|
||||
puck_name=sample.puck.puck_name if sample.puck else None,
|
||||
dewar_name=sample.puck.dewar.dewar_name
|
||||
if (sample.puck and sample.puck.dewar)
|
||||
else None,
|
||||
"images": [
|
||||
{"id": img.id, "filepath": img.filepath, "comment": img.comment}
|
||||
images=[
|
||||
ImageInfo(
|
||||
id=img.id,
|
||||
filepath=img.filepath,
|
||||
event_type=img.sample_event.event_type
|
||||
if img.sample_event
|
||||
else "Unknown",
|
||||
comment=img.comment,
|
||||
)
|
||||
for img in images
|
||||
],
|
||||
"experiment_runs": [
|
||||
{
|
||||
"id": ex.id,
|
||||
"run_number": ex.run_number,
|
||||
"beamline_parameters": ex.beamline_parameters,
|
||||
"sample_id": ex.sample_id,
|
||||
}
|
||||
experiment_runs=[
|
||||
ExperimentParametersRead(
|
||||
id=ex.id,
|
||||
run_number=ex.run_number,
|
||||
beamline_parameters=ex.beamline_parameters,
|
||||
sample_id=ex.sample_id,
|
||||
)
|
||||
for ex in experiment_parameters
|
||||
],
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
return results
|
||||
@ -318,32 +331,49 @@ def create_experiment_parameters_for_sample(
|
||||
db.commit()
|
||||
db.refresh(new_exp)
|
||||
|
||||
# Create a "Collecting" sample event associated with the new experiment parameters
|
||||
new_event = SampleEventModel(
|
||||
sample_id=sample_id,
|
||||
event_type="Collecting", # The event type
|
||||
timestamp=datetime.now(), # Use current timestamp
|
||||
)
|
||||
db.add(new_event)
|
||||
db.commit()
|
||||
|
||||
return new_exp
|
||||
|
||||
|
||||
# @router.post("/results", response_model=ResultResponse)
|
||||
# def create_result(result: ResultCreate, db: Session = Depends(get_db)):
|
||||
# # Validate sample_id and result_id (optional but recommended)
|
||||
# sample = db.query(SampleModel).filter_by(id=result.sample_id).first()
|
||||
# if not sample:
|
||||
# raise HTTPException(status_code=404, detail="Sample not found")
|
||||
#
|
||||
# experiment = db.query(ExperimentParameters).filter_by(id=result.result_id).first()
|
||||
# if not experiment:
|
||||
# raise HTTPException(status_code=404, detail="Experiment parameters not found")
|
||||
#
|
||||
# # Create a new Results entry
|
||||
# result_obj = Results(
|
||||
# sample_id=result.sample_id,
|
||||
# result_id=result.result_id,
|
||||
# result=result.result
|
||||
# )
|
||||
# db.add(result_obj)
|
||||
# db.commit()
|
||||
# db.refresh(result_obj)
|
||||
#
|
||||
# return result_obj
|
||||
#
|
||||
@router.post("/processing-results", response_model=ResultResponse)
|
||||
def create_result(payload: ResultCreate, db: Session = Depends(get_db)):
|
||||
# Check experiment existence
|
||||
experiment = (
|
||||
db.query(ExperimentParametersModel)
|
||||
.filter(ExperimentParametersModel.id == payload.run_id)
|
||||
.first()
|
||||
)
|
||||
if not experiment:
|
||||
raise HTTPException(
|
||||
status_code=404, detail="Experiment parameters (run) not found"
|
||||
)
|
||||
|
||||
result_entry = ResultsModel(
|
||||
sample_id=payload.sample_id,
|
||||
run_id=payload.run_id,
|
||||
result=payload.result.model_dump(), # Serialize entire result to JSON
|
||||
)
|
||||
|
||||
db.add(result_entry)
|
||||
db.commit()
|
||||
db.refresh(result_entry)
|
||||
|
||||
return ResultResponse(
|
||||
id=result_entry.id,
|
||||
sample_id=result_entry.sample_id,
|
||||
run_id=result_entry.run_id,
|
||||
result=payload.result, # return original payload directly
|
||||
)
|
||||
|
||||
|
||||
# @router.get("/results", response_model=list[ResultResponse])
|
||||
# def get_results(sample_id: int, result_id: int, db: Session = Depends(get_db)):
|
||||
# query = db.query(Results)
|
||||
|
Reference in New Issue
Block a user