# unstable-branch/sdm-dashboard architecture

> Architecture diagram of the GitHub repository unstable-branch/sdm-dashboard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/unstable-branch/sdm-dashboard
- Repository: https://github.com/unstable-branch/sdm-dashboard

## Overview

1. **The repository is split into two product lines.**  
   The primary architecture is the modern multi-user platform: Next.js frontend, Hono TypeScript API, Plumber/R compute service, PostgreSQL/PostGIS, Redis/BullMQ, and Garage-compatible object storage. In parallel, the older single-user Shiny desktop app is still present under `app.R` and `www/`, but it is not the main architectural path for new work.

2. **The browser-facing layer is the Next.js dashboard in `frontend/`.**  
   It owns route-level UI for authentication, projects, data upload/cleaning, model configuration, run monitoring, results, diagnostics, admin, and settings. The frontend uses client-side hooks/stores and map/visualization components, but it does not talk to the database or compute engine directly; all domain actions go through the API.

3. **`api/` is the orchestration and BFF layer.**  
   It is a TypeScript/Hono service that centralizes auth, CSRF, rate limiting, caching, admin endpoints, jobs, results, climate, ecology, occurrences, settings, and SDM run management. It also owns the API-to-storage/queue integration, websocket or SSE-style progress delivery, and the boundary to the Plumber service.

4. **PostgreSQL/PostGIS is the system of record.**  
   The schema in `api/src/db` and the migrations in `api/drizzle/` show that the API persists users, auth state, profiles, projects, uploads, runs, batches, provenance, resource accounting, storage metadata, and related operational state. PostGIS is material because geospatial data and project/run metadata are first-class, not just blobs.

5. **Redis is the short-lived coordination layer.**  
   It is used for queueing, rate limiting, cached responses, and background job state. The architecture relies on Redis/BullMQ-style orchestration to decouple request handling from long-running model computation and result generation.

6. **Garage-compatible object storage holds large binary artifacts.**  
   This is where uploaded source files, cleaned datasets, raster outputs, tiles, reports, and exports are stored. The API references storage services rather than embedding large files in the database, which keeps the persistence boundary clear: metadata in Postgres, heavy artifacts in object storage.

7. **Plumber/R is the compute boundary for SDM work.**  
   The `plumber/` package wraps the modeling engine in an HTTP API and background dispatcher. It is responsible for the expensive science workflows: training, prediction, diagnostics, climate downloads/projections, and export generation. The API calls into this service rather than running models in-process.

8. **The root `R/` tree is the scientific engine.**  
   It contains the reusable modelling core: covariates, data ingestion/validation, model implementations, cross-validation, calibration, prediction, ecology metrics, explainability, outputs, and legacy Shiny helpers. This is the domain library consumed by both the Plumber service and the desktop app, so it is the core computation layer of the repository.

9. **Python model adapters are a secondary compute backend.**  
   `python_models/` contains small model-specific fit/predict entrypoints and manifests, including scikit-learn and elapid-based adapters. These sit behind the R/Plumber orchestration and are part of the model execution boundary rather than the user-facing app layer.

10. **The main domain flow is upload → validate/clean → configure → run → analyze.**  
   Users upload occurrence data, the API stores metadata and sends files through cleaning/validation workflows, the frontend helps inspect detected columns and geospatial inputs, then a model run is submitted. The run is executed asynchronously by the compute side, progress is streamed back to the dashboard, and outputs/metrics/diagnostics are written to storage for later viewing.

11. **Results are treated as a separate read-heavy subsystem.**  
   The frontend has dedicated results pages and components for rasters, diagnostics, projections, overlap/threshold analysis, and downloads. On the backend, results endpoints and manifest/export services expose these artifacts without requiring rerunning the model.

12. **The legacy Shiny app remains as a self-contained local mode.**  
   `launch_app.R`, `app.R`, and the `www/` assets support the old desktop workflow with no multi-user auth layer. It is architecturally separate from the modern platform: useful for continuity and local use, but not the service-oriented path used for the current dashboard.

13. **Composition and release tooling reinforce the boundaries.**  
   Dockerfiles and compose files define the modern stack as independently deployable services, while CI and tests cover API contracts, frontend behavior, R model correctness, and end-to-end flows. The repo structure therefore maps cleanly to a diagram with distinct UI, API, compute, persistence, and legacy desktop lanes.

Main entry points: [`docker-compose.yml`](https://github.com/unstable-branch/sdm-dashboard/blob/main/docker-compose.yml) (Compose files), [`api`](https://github.com/unstable-branch/sdm-dashboard/tree/main/api) (API), [`plumber`](https://github.com/unstable-branch/sdm-dashboard/tree/main/plumber) (Plumber), [`api/src/routes`](https://github.com/unstable-branch/sdm-dashboard/tree/main/api/src/routes) (Domain routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_modern["Modern platform"]
  node_frontend["Frontend<br/>Next.js dashboard"]
  node_api["API<br/>Hono BFF"]
  node_frontend_data["Data tools<br/>Upload and clean UI"]
  node_frontend_model["Model setup<br/>Run configuration UI"]
  node_frontend_results["Results UI<br/>Read-heavy views"]
  node_frontend_admin["Admin UI<br/>Ops console"]
  node_api_auth["Auth layer<br/>Middleware and sessions<br/>[auth.ts]"]
  node_api_routes["Domain routes<br/>REST endpoints"]
  node_api_services["Service layer<br/>Storage and queue"]
  node_api_db["Schema layer<br/>Drizzle schema"]
end

subgraph group_legacy["Legacy desktop"]
  node_legacy_app["Shiny app<br/>Legacy desktop UI<br/>[app.R]"]
  node_legacy_www["Web assets<br/>Legacy assets"]
end

subgraph group_engine["Scientific engine"]
  node_plumber["Plumber<br/>R compute API"]
  node_r_core["R core<br/>SDM engine"]
  node_python_models["Python adapters<br/>Model backends"]
end

subgraph group_infra["Infrastructure"]
  node_postgres[("PostgreSQL<br/>System of record")]
  node_redis[("Redis<br/>Coordination cache<br/>[queue.ts]")]
  node_object_storage[("Object storage<br/>Artifact store")]
  node_pipeline["Pipeline scripts<br/>Automation"]
  node_compose["Compose files<br/>Deployment wiring<br/>[docker-compose.yml]"]
end

node_frontend -->|"calls"| node_api
node_frontend_data -->|"uploads"| node_api_routes
node_frontend_model -->|"configures"| node_api_routes
node_frontend_results -->|"reads"| node_api_routes
node_frontend_admin -->|"manages"| node_api_routes
node_api -->|"persists"| node_postgres
node_api -->|"queues/cache"| node_redis
node_api -->|"stores artifacts"| node_object_storage
node_api -->|"dispatches"| node_plumber
node_api_auth -->|"protects"| node_api
node_api_routes -->|"orchestrates"| node_api_services
node_api_services -->|"syncs"| node_plumber
node_api_services -->|"uses"| node_redis
node_api_services -->|"writes"| node_object_storage
node_api_db -->|"defines"| node_postgres
node_plumber -->|"executes"| node_r_core
node_plumber -->|"adapts"| node_python_models
node_r_core -->|"produces"| node_object_storage
node_r_core -.->|"reads/writes"| node_postgres
node_legacy_app -->|"uses"| node_legacy_www
node_legacy_app -->|"reuses"| node_r_core
node_pipeline -->|"drives"| node_compose
node_compose -->|"runs"| node_frontend
node_compose -->|"runs"| node_api
node_compose -->|"runs"| node_plumber
node_compose -->|"provisions"| node_postgres
node_compose -->|"provisions"| node_redis
node_compose -->|"connects"| node_object_storage

click node_frontend "https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend"
click node_api "https://github.com/unstable-branch/sdm-dashboard/tree/main/api"
click node_plumber "https://github.com/unstable-branch/sdm-dashboard/tree/main/plumber"
click node_r_core "https://github.com/unstable-branch/sdm-dashboard/tree/main/R"
click node_python_models "https://github.com/unstable-branch/sdm-dashboard/tree/main/python_models"
click node_postgres "https://github.com/unstable-branch/sdm-dashboard/tree/main/api/src/db"
click node_redis "https://github.com/unstable-branch/sdm-dashboard/blob/main/api/src/services/queue.ts"
click node_object_storage "https://github.com/unstable-branch/sdm-dashboard/tree/main/garage"
click node_legacy_app "https://github.com/unstable-branch/sdm-dashboard/blob/main/app.R"
click node_legacy_www "https://github.com/unstable-branch/sdm-dashboard/tree/main/www"
click node_frontend_data "https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/data"
click node_frontend_model "https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/model"
click node_frontend_results "https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/results"
click node_frontend_admin "https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/admin"
click node_api_auth "https://github.com/unstable-branch/sdm-dashboard/blob/main/api/src/middleware/auth.ts"
click node_api_routes "https://github.com/unstable-branch/sdm-dashboard/tree/main/api/src/routes"
click node_api_services "https://github.com/unstable-branch/sdm-dashboard/tree/main/api/src/services"
click node_api_db "https://github.com/unstable-branch/sdm-dashboard/tree/main/api/drizzle"
click node_pipeline "https://github.com/unstable-branch/sdm-dashboard/tree/main/scripts"
click node_compose "https://github.com/unstable-branch/sdm-dashboard/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend,node_api,node_frontend_data,node_frontend_model,node_frontend_results,node_frontend_admin,node_api_auth,node_api_routes,node_api_services,node_api_db toneBlue
class node_legacy_app,node_legacy_www toneAmber
class node_plumber,node_r_core,node_python_models toneMint
class node_postgres,node_redis,node_object_storage,node_pipeline,node_compose toneRose
```

## Components

### Modern platform

Primary multi-user dashboard, API, compute, and persistence path.

- [Frontend](https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend): Next.js dashboard · Browser-facing multi-user UI for auth, projects, data, runs, results, admin, and settings. · `frontend`
- [API](https://github.com/unstable-branch/sdm-dashboard/tree/main/api): Hono BFF · TypeScript orchestration layer for auth, jobs, results, domain endpoints, and service integration. · `api`
- [Data tools](https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/data): Upload and clean UI · Route-level screens for file upload, column detection, cleaning, climate inputs, and map previews. · `frontend/src/app/(dashboard)/data`
- [Model setup](https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/model): Run configuration UI · UI for species, extent, covariates, model selection, parameters, and run history. · `frontend/src/app/(dashboard)/model`
- [Results UI](https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/results): Read-heavy views · Results pages for rasters, diagnostics, projections, metrics, and downloads. · `frontend/src/app/(dashboard)/results`
- [Admin UI](https://github.com/unstable-branch/sdm-dashboard/tree/main/frontend/src/app/(dashboard)/admin): Ops console · Admin screens for users, system status, diagnostics, logs, and database views. · `frontend/src/app/(dashboard)/admin`
- [Auth layer](https://github.com/unstable-branch/sdm-dashboard/blob/main/api/src/middleware/auth.ts): Middleware and sessions · Authentication, CSRF, rate limiting, and access control for the platform API. · `api/src/middleware/auth.ts`
- [Domain routes](https://github.com/unstable-branch/sdm-dashboard/tree/main/api/src/routes): REST endpoints · Projects, occurrences, ecology, climate, diagnostics, settings, jobs, results, and SDM orchestration endpoints. · `api/src/routes`
- [Service layer](https://github.com/unstable-branch/sdm-dashboard/tree/main/api/src/services): Storage and queue · Plumber sync, queueing, websocket progress, model payloads, email, encryption, and storage integration. · `api/src/services`
- [Schema layer](https://github.com/unstable-branch/sdm-dashboard/tree/main/api/drizzle): Drizzle schema · Database schema and migrations defining platform state and metadata. · `api/drizzle`

### Legacy desktop

Older single-user Shiny mode kept for local continuity.

- [Shiny app](https://github.com/unstable-branch/sdm-dashboard/blob/main/app.R): Legacy desktop UI · Older single-user Shiny workflow for local use and continuity. · `app.R`
- [Web assets](https://github.com/unstable-branch/sdm-dashboard/tree/main/www): Legacy assets · Static assets and scripts for the desktop Shiny interface. · `www`

### Scientific engine

Reusable R domain logic and model execution core.

- [Plumber](https://github.com/unstable-branch/sdm-dashboard/tree/main/plumber): R compute API · HTTP and background dispatch boundary for expensive SDM workflows and model execution. · `plumber`
- [R core](https://github.com/unstable-branch/sdm-dashboard/tree/main/R): SDM engine · Reusable R science library for data handling, modeling, validation, ecology, and outputs. · `R`
- [Python adapters](https://github.com/unstable-branch/sdm-dashboard/tree/main/python_models): Model backends · Secondary model fit/predict entrypoints for scikit-learn and elapid-based adapters. · `python_models`

### Infrastructure

Deployment, storage, and runtime wiring.

- [PostgreSQL](https://github.com/unstable-branch/sdm-dashboard/tree/main/api/src/db): System of record · Primary relational store for users, auth, projects, uploads, runs, provenance, and operational state. · `api/src/db`
- [Redis](https://github.com/unstable-branch/sdm-dashboard/blob/main/api/src/services/queue.ts): Coordination cache · Short-lived queueing, rate limiting, cached responses, and background job state. · `api/src/services/queue.ts`
- [Object storage](https://github.com/unstable-branch/sdm-dashboard/tree/main/garage): Artifact store · Garage-compatible binary storage for uploads, cleaned datasets, rasters, tiles, reports, and exports. · `garage`
- [Pipeline scripts](https://github.com/unstable-branch/sdm-dashboard/tree/main/scripts): Automation · Release, smoke-test, benchmark, and setup scripts that exercise and package the stack. · `scripts`
- [Compose files](https://github.com/unstable-branch/sdm-dashboard/blob/main/docker-compose.yml): Deployment wiring · Docker and compose definitions for running the modern services together. · `docker-compose.yml`

## Connections

- Frontend → API: calls · All domain actions flow through the API boundary.
- Data tools → Domain routes: uploads · Data screens submit cleaning, validation, climate, and file actions through backend endpoints.
- Model setup → Domain routes: configures · Model configuration and run submission are coordinated via API endpoints.
- Results UI → Domain routes: reads · Results pages fetch manifests, metrics, rasters, and diagnostics from the API.
- Admin UI → Domain routes: manages · Admin screens use backend admin and operational endpoints.
- API → PostgreSQL: persists · The API stores users, projects, uploads, runs, provenance, and settings in PostgreSQL/PostGIS.
- API → Redis: queues/cache · The API uses Redis for jobs, caching, and rate limiting.
- API → Object storage: stores artifacts · The API manages large files and generated outputs in object storage.
- API → Plumber: dispatches · The API hands off long-running SDM work to the compute service.
- Auth layer → API: protects · Authentication and request controls wrap the API boundary.
- Domain routes → Service layer: orchestrates · Routes delegate to storage, queue, progress, and integration services.
- Service layer → Plumber: syncs · Service code maintains the integration contract with the R compute service.
- Service layer → Redis: uses · Queue and event services rely on Redis-backed coordination.
- Service layer → Object storage: writes · Artifact and file services place binaries into object storage.
- Schema layer → PostgreSQL: defines · Migrations and schema define the relational system of record.
- Plumber → R core: executes · Plumber invokes the reusable R modeling engine for science workflows.
- Plumber → Python adapters: adapts · Plumber can route selected model execution into Python backends.
- R core → Object storage: produces · Model runs and exports generate artifacts that are stored externally.
- R core → PostgreSQL: reads/writes · The science layer consumes and updates run/data metadata through the platform boundary.
- Shiny app → Web assets: uses · The Shiny desktop app is paired with its static assets.
- Shiny app → R core: reuses · Legacy local mode shares the same R scientific engine.
- Pipeline scripts → Compose files: drives · Automation and release scripts work with the deployment definitions.
- Compose files → Frontend: runs · Compose files define the deployable frontend service.
- Compose files → API: runs · Compose files define the deployable API service.
- Compose files → Plumber: runs · Compose files define the deployable compute service.
- Compose files → PostgreSQL: provisions · Deployment wiring includes the database service or connection target.
- Compose files → Redis: provisions · Deployment wiring includes Redis for coordination.
- Compose files → Object storage: connects · Deployment wiring integrates compatible object storage.
