Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default defineConfig({
{ label: "Overview", slug: "backend/overview" },
{ label: "API Reference", slug: "backend/api-reference" },
{ label: "Database", slug: "backend/database" },
{ label: "Result Storage", slug: "backend/result-storage" },
{
label: "Simulation Lifecycle",
slug: "backend/simulation-lifecycle",
Expand Down
29 changes: 27 additions & 2 deletions public/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ paths:
the response will include results only for the specified estimator. If no `estimator_name` is provided,
the response will include results for all estimators associated with the job. Additionally, you can
specify pages using the `page_number` or `page_numbers` parameter to limit the response to specific pages.
Pages come as signed links to the results bucket, which the client fetches through nginx (/yaptide-results/).
parameters:
- in: query
name: job_id
Expand Down Expand Up @@ -1377,9 +1378,33 @@ components:
task_state:
type: string
enum: [PENDING, RUNNING, COMPLETED, FAILED]
Page:
Page:
type: object
description: Page object
description: A result page in the results bucket, as signed links to its objects (a Zarr v3 array). A page
stored before the bucket and not moved there yet comes as the page itself, with its values in data.values.
properties:
page_number:
type: integer
name:
type: string
dimensions:
type: integer
format:
type: string
enum: [zarr-v3]
description: How the page is stored
url:
type: string
description: Signed link of the page's zarr.json, valid 15 minutes
files:
type: object
description: Signed link of each of the page's objects, valid 15 minutes; c/0 answers 404 for a page without
values
properties:
zarr.json:
type: string
c/0:
type: string
Estimator:
type: object
description: Estimator object includes estimator's name, associated metadata, and list of pages.
Expand Down
32 changes: 25 additions & 7 deletions src/content/docs/api-reference/results.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ description: Simulation results, estimators, inputs, and log file retrieval.

## Get Results

Retrieve simulation results with optional filtering by estimator and page.
Retrieve simulation results with optional filtering by estimator and page. Each page comes as **signed links** to
its objects in the results bucket, not as its values: the browser fetches them through nginx
(`/yaptide-results/...`), see [Result Storage](/for_developers/backend/result-storage/).

```http
GET /results?job_id=abc123-def456
Expand Down Expand Up @@ -39,10 +41,12 @@ GET /results?job_id=abc123-def456
{
"page_number": 1,
"name": "Z (Dose)",
"data": {
"values": [0.0, 0.001, 0.015, ...],
"dimensions": [[0.0, 0.5, 1.0, ...]],
"unit": "Gy"
"dimensions": 1,
"format": "zarr-v3",
"url": "https://<backend>/yaptide-results/simulations/17/z_profile_/1/3f2a9c0d1e4b5a67/zarr.json?X-Amz-...",
"files": {
"zarr.json": "https://<backend>/yaptide-results/simulations/17/z_profile_/1/3f2a9c0d1e4b5a67/zarr.json?X-Amz-...",
"c/0": "https://<backend>/yaptide-results/simulations/17/z_profile_/1/3f2a9c0d1e4b5a67/c/0?X-Amz-..."
}
}
]
Expand All @@ -67,7 +71,18 @@ GET /results?job_id=abc123-def456&page_numbers=1-3,5

Returns only the specified pages across all estimators.

> **Note:** Result data is stored compressed (zlib) in the database. The API decompresses transparently.
**A stored page:**

| Field | Description |
|---|---|
| `page_number`, `name`, `dimensions` | The page, as kept in the database |
| `format` | `zarr-v3`: a Zarr v3 array of float32 values in one shard |
| `files` | Signed link for each object: `zarr.json` (metadata; its `attributes.yaptide_page` is the page as the JSON pages had it, without the values) and `c/0` (the shard) |
| `url` | The link of `zarr.json` |

The links last 15 minutes; ask `/results` again for new ones. A page without values has no shard, and its `c/0` link
answers 404, which a Zarr reader takes as the fill value. A page stored before the results bucket and not yet moved
there (`db_manage copy-pages-to-storage`) still comes as the page itself, with `data.values`.

---

Expand Down Expand Up @@ -180,7 +195,10 @@ Log files are useful for debugging failed simulations — they contain the simul

## Internal: Upload Results

**Worker-facing only.** Simulation workers call this to save results to the database.
**Worker-facing only.** The merge of a direct simulation and the collect job of a batch simulation call this with
the merged results. Each page comes with its values; the backend writes it to the results bucket as a Zarr array and
keeps where it is in the database. Pages stored before for the same estimator and page are replaced, and their objects
removed. Answers `202` when stored, `500` when the bucket did not take the pages (nothing is kept then).

```http
POST /results
Expand Down
23 changes: 15 additions & 8 deletions src/content/docs/architecture/data-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ POST /jobs/direct
- POSTs status updates to `POST /tasks` (primaries completed, estimated time)
4. When all N tasks complete, a **merge task** (`get_job_results`) runs:
- Averages estimator results across all tasks
- Compresses and stores results in the database (`EstimatorModel` → `PageModel`)
- POSTs them to `POST /results`, which writes each page to the results bucket as a Zarr array and records it in
the database (`EstimatorModel` → `PageModel` → `PageObjectModel`)
5. Job state transitions: `PENDING → RUNNING → MERGING_QUEUED → MERGING_RUNNING → COMPLETED`

**Polling**: The UI polls `GET /jobs/direct?job_id=<id>` at intervals to check status and fetch partial progress.
Expand All @@ -97,7 +98,8 @@ POST /jobs/batch
- Uploads watcher and data-sender scripts
- Submits a **Slurm array job** (`sbatch`) + a collect job
3. The cluster's **watcher script** monitors each array task and POSTs progress back to the YAPTIDE backend.
4. When all array tasks finish, the **collect job** gathers results and POSTs them to `POST /results`.
4. When all array tasks finish, the **collect job** gathers results and POSTs them to `POST /results`, which stores
them as for direct execution.

**Polling**: The UI polls `GET /jobs/batch?job_id=<id>`. The backend also checks cluster status via `sacct` over SSH.

Expand All @@ -122,10 +124,14 @@ Regardless of the execution path, results follow the same structure:
- **Estimators** — named result containers (e.g., `detector_dose`, `mesh_fluence`)
- **Pages** — individual scoring dimensions within an estimator (e.g., energy bins, spatial slices)

Each page contains:
- `page_name` — descriptive name
- `page_dimension` — number of dimensions
- `compressed_data` — gzip-compressed JSON with axes, values, and metadata
Each page is a Zarr v3 array in the results bucket: its values (float32) in one shard, and its axes, unit and
metadata as attributes of its `zarr.json`. The database keeps the page's `page_name`, `page_dimension` and where it is
stored (`PageObjectModel`).

`GET /results` answers with **signed links** to each page's objects, never with the values. The browser fetches them
from nginx (`/yaptide-results/`), which passes them to the bucket; result data does not go through Flask. See
[Result Storage](/for_developers/backend/result-storage/) and
[Results Loading](/for_developers/frontend/results-loading/).

The UI renders results using **JSRoot** (CERN ROOT's JavaScript library):
- 1D histograms (e.g., Bragg peak depth-dose curves)
Expand All @@ -135,13 +141,14 @@ The UI renders results using **JSRoot** (CERN ROOT's JavaScript library):

## Data Persistence

All simulation data is stored in PostgreSQL with gzip compression.
Simulation data is stored in PostgreSQL with gzip compression, except the result pages, which are kept in the S3
bucket.

| Data | Storage |
|---|---|
| Input files (JSON or raw) | `InputModel.compressed_data` |
| Estimator metadata | `EstimatorModel` (name, filename) |
| Result pages | `PageModel.compressed_data` |
| Result pages | Zarr arrays in the results bucket; `PageModel` + `PageObjectModel` (key, size, format) |
| Simulation logs | `LogfilesModel.compressed_data` |
| Job state and metadata | `SimulationModel` + `TaskModel` |

Expand Down
22 changes: 13 additions & 9 deletions src/content/docs/architecture/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ YAPTIDE is composed of **three repositories** that work together to provide a co
│ │ └─────────────────────────┘ │ │
│ └──────────────────────┬───────────────────────────┘ │
└─────────────────────────┼───────────────────────────────┘
│ HTTPS (REST API)
│ HTTPS (REST API, result pages)
┌─────────────────────────┼───────────────────────────────┐
│ Nginx │ :8443 │
│ ┌───────┴───────┐ │
│ │ Flask API │ :6000 │
│ └───┬───────┬──┘ │
│ │ │ │
│ ┌──────────┤ ├──────────┐ │
│ ▼ ▼ ▼ ▼ │
│ ├──── /yaptide-results/ ───┐ │
│ ┌───────┴───────┐ ▼ │
│ │ Flask API │ :6000 ┌──────────┐│
│ └───┬───────┬──┘ ──────▶│ Garage ││
│ │ │ │ (S3, the ││
│ ┌──────────┤ ├──────────┐ │ pages) ││
│ ▼ ▼ ▼ ▼ └──────────┘│
│ ┌──────────┐ ┌───────┐ ┌─────────────────────┐ │
│ │ Redis │ │ PgSQL │ │ Celery Workers │ │
│ │ (broker) │ │ (DB) │ │ │ │
Expand Down Expand Up @@ -62,12 +63,15 @@ YAPTIDE is composed of **three repositories** that work together to provide a co

### Backend (`yaptide` repository)

The backend is a **Flask** API server with **Celery** for async task execution and **PostgreSQL** for persistence.
The backend is a **Flask** API server with **Celery** for async task execution, **PostgreSQL** for persistence and
an S3 bucket (**Garage**) for result pages.

- **REST API** — Handles authentication, job submission, status polling, and result retrieval.
- **Simulation Worker** — Celery worker that runs simulator binaries (SHIELD-HIT12A, FLUKA) in temporary directories, monitors progress, and posts updates back to Flask.
- **Helper Worker** — Celery worker for HPC batch job submission via SSH (PLGrid/Slurm) and cleanup tasks.
- **Nginx** — Reverse proxy with TLS termination.
- **Garage** — S3-compatible object storage holding the result pages as Zarr arrays; Flask writes them, the browser
reads them with signed links. See [Result Storage](/for_developers/backend/result-storage/).
- **Nginx** — Reverse proxy with TLS termination; passes `/yaptide-results/` to Garage.

### UI (`ui` repository)

Expand Down
22 changes: 18 additions & 4 deletions src/content/docs/backend/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SimulationModel ──< TaskModel (polymorphic)
└── BatchTaskModel

SimulationModel ──< InputModel
SimulationModel ──< EstimatorModel ──< PageModel
SimulationModel ──< EstimatorModel ──< PageModel ──o PageObjectModel
SimulationModel ──< LogfilesModel

ClusterModel ──< BatchSimulationModel
Expand Down Expand Up @@ -147,7 +147,20 @@ ClusterModel ──< BatchSimulationModel
| `page_number` | Integer | 0-based page index |
| `page_name` | String | Descriptive name |
| `page_dimension` | Integer | Number of dimensions |
| `compressed_data` | LargeBinary | gzip-compressed result data (axes, values) |
| `compressed_data` | LargeBinary | Empty for pages in the results bucket. Pages stored before the bucket keep their gzip-compressed JSON (axes, values) here until `db_manage copy-pages-to-storage` and `clear-pages-in-postgres` move them |

**`PageObject`** — where a page is in the results bucket (one row per page at most).

| Column | Type | Description |
|---|---|---|
| `page_id` | Integer (PK, FK → Page, cascade) | |
| `key` | String | Prefix of the page's objects, `simulations/<sim>/<estimator file>/<page>/<revision>/` |
| `size` | Integer | Size of the page's objects in bytes |
| `format` | String | `zarr-v3` |

The page's values are only in the bucket, so the `PageObject` migration (`9e2b41c7d5a3`) refuses to downgrade while
the table has rows: `db_manage move-pages-to-postgres` brings the pages back first. See
[Result Storage](/for_developers/backend/result-storage/).

**`Logfiles`** — simulation log output.

Expand All @@ -168,7 +181,8 @@ ClusterModel ──< BatchSimulationModel

## Data Compression

All large binary fields use gzip compression:
Result pages are not in the database: they are Zarr arrays in the results bucket, each chunk compressed with zstd
([Result Storage](/for_developers/backend/result-storage/)). The other large binary fields use gzip compression:

```python
from yaptide.persistence.models import compress, decompress
Expand All @@ -180,7 +194,7 @@ model.compressed_data = compress(json.dumps(data).encode())
data = json.loads(decompress(model.compressed_data).decode())
```

This reduces storage for large simulation results (estimator pages can be several MB uncompressed).
This reduces storage for inputs, estimator metadata and logs.

## Migration Workflow

Expand Down
Loading