Files
WebApi/webapi/preview.py
T
alberto ec5d945f71 Cleanup of the TEA parity work: no prose in code, no hacks, everything in its place
1. No prose in code
- Remove parity-added Python comments and docstrings across the EEG, FHIR,
  execution, preview, training and run code and tests. Preserve endpoint
  descriptions as explicit FastAPI schema metadata.

2. No hacks, no glued code
- Read the declared TEA patient filters and training inputs directly and
  use the same TEA check for exposing and validating reference options.
- Give the Dagster GraphQL request one helper. Keep lock backoff and
  cancellation polling because their timing and error behavior are
  covered by run tests and they handle different Dagster states.
- Use the renamed FHIR report ID helper in the hospital test. Keep
  TEA_GATE_SLOW local to that test and preserve the test assertions.

3. Everything in its place
- Keep endpoint and run handling in WebApi and reuse Core and Persistance
  for domain rules and reads. Leave tests in the existing testpaths.
2026-11-30 13:41:49 -06:00

51 lines
1.8 KiB
Python

from datetime import date
from typing import Annotated
from dagster_graphql import DagsterGraphQLClient
from fastapi import APIRouter, Depends, Form, HTTPException, UploadFile
from pydantic import BaseModel, WithJsonSchema
from persistance import previews
from webapi import runs
router = APIRouter(prefix="/tea/preview", tags=["preview"])
_MAX_FILE_BYTES = 200 * 1024 * 1024
_BinaryUpload = Annotated[UploadFile, WithJsonSchema({"type": "string", "format": "binary"})]
class PreviewStatus(BaseModel):
state: runs.State
result: previews.PreviewResult | None
@router.post("")
def start_preview(
edf: _BinaryUpload,
ados2: _BinaryUpload | None = None,
birth_date: Annotated[date | None, Form()] = None,
pipeline: Annotated[str, Form()] = "tea",
client: DagsterGraphQLClient = Depends(runs.dagster),
) -> dict[str, str]:
for file in (edf, ados2):
if file is not None and file.size is not None and file.size > _MAX_FILE_BYTES:
raise HTTPException(413, detail="preview files may be at most 200 MiB each")
config = {
"edf": previews.put(edf.file.read()),
"ados2": previews.put(ados2.file.read()) if ados2 is not None else None,
"birth_date": birth_date.isoformat() if birth_date else None,
"pipeline": pipeline,
}
return runs.submit(client, "tea_preview", "preview_study", config, domain="tea")
@router.get("/{run_id}")
def preview_status(run_id: str) -> PreviewStatus:
run = runs.run_status(run_id, outputs=True)
if run.job != "tea_preview":
raise HTTPException(404, detail=f"no TEA preview run {run_id}")
raw = (run.outputs or {}).get("preview_study.result") if run.state == "succeeded" else None
return PreviewStatus(
state=run.state,
result=previews.PreviewResult.model_validate(raw) if raw is not None else None,
)