# nafeytauheed-cyber/medigrid architecture

> Architecture diagram of the GitHub repository nafeytauheed-cyber/medigrid, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nafeytauheed-cyber/medigrid
- Repository: https://github.com/nafeytauheed-cyber/medigrid

## Overview

MediGrid is a city-wide emergency bed reservation and patient-transfer network. Dispatchers submit a patient’s condition, bed type, priority, and location through the API; SmartMatch filters hospitals, Redis proposes a nearby bed and hold, and PostgreSQL commits the authoritative reservation. Hospital staff confirm arrivals and manage admissions, discharges, and transfer sagas. A worker publishes committed outbox events to RabbitMQ consumers; notification deltas and a database watermark feed live SSE updates. Operations surfaces metrics, queue state, cache recovery, and DLQ replay. Some subsystem wiring is documented in the README; unsampled implementation details are not inferred.

Main entry points: [`app/web/app.py`](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/app.py) (HTTP API), [`app/transfers.py`](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/transfers.py) (Transfer saga), [`app/web/static/ops.js`](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/ops.js) (Operations page), [`app/dispatch.py`](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/dispatch.py) (Bed search and hold)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["User interfaces"]
  node_browser["Web console<br/>[common.js]"]
  node_dispatch_ui["Dispatch page<br/>[dispatch.js]"]
  node_hospital_ui["Hospital console<br/>[hospital.js]"]
  node_control_ui["Control map<br/>[control.js]"]
  node_ops_ui["Operations page<br/>[ops.js]"]
  node_api["HTTP API<br/>[app.py]"]
end

subgraph group_domain["Care workflows"]
  node_dispatch["Bed search and hold<br/>[dispatch.py]"]
  node_smartmatch["Capability matching<br/>[smartmatch.py]"]
  node_bed_lifecycle["Admission lifecycle<br/>[beds.py]"]
  node_transfer_saga["Transfer saga<br/>[transfers.py]"]
end

subgraph group_messaging["Event processing"]
  node_worker_loops["Outbox and timers<br/>[loops.py]"]
  node_rabbit["RabbitMQ"]
  node_consumers["Consumer groups<br/>[consumers.py]"]
end

subgraph group_liveops["Live views and operations"]
  node_stream["SSE updates<br/>[stream.py]"]
  node_ops["Operations API<br/>[ops.py]"]
  node_metrics["Metrics<br/>[metrics.py]"]
  node_chaos["Scenario controls<br/>[chaos.py]"]
end

subgraph group_platform["State and infrastructure"]
  node_postgres[("PostgreSQL")]
  node_redis[("Redis cache")]
  node_cache_recovery["Cache recovery<br/>[cache.py]"]
  node_seed["Synthetic network seed<br/>[seed.py]"]
end

node_dispatcher(("Dispatcher"))
node_hospital_user(("Hospital staff"))
node_operator(("Operator"))

node_dispatcher -->|"uses"| node_dispatch_ui
node_hospital_user -->|"uses"| node_hospital_ui
node_operator -->|"uses"| node_ops_ui
node_browser -->|"calls"| node_api
node_dispatch_ui -->|"submits requests"| node_api
node_hospital_ui -->|"manages beds"| node_api
node_control_ui -->|"loads map"| node_api
node_ops_ui -->|"loads operations"| node_api
node_api -->|"routes dispatch"| node_dispatch
node_api -->|"routes bed actions"| node_bed_lifecycle
node_api -->|"routes transfers"| node_transfer_saga
node_dispatch -->|"filters hospitals"| node_smartmatch
node_transfer_saga -->|"checks capability"| node_smartmatch
node_dispatch -->|"proposes and holds"| node_redis
node_dispatch -->|"commits reservation"| node_postgres
node_bed_lifecycle -->|"updates state"| node_postgres
node_transfer_saga -->|"commits saga"| node_postgres
node_bed_lifecycle -->|"settles cache hold"| node_redis
node_transfer_saga -->|"settles cache hold"| node_redis
node_worker_loops -->|"reads outbox"| node_postgres
node_worker_loops -->|"publishes events"| node_rabbit
node_rabbit -->|"delivers events"| node_consumers
node_consumers -->|"records processing"| node_postgres
node_consumers -->|"handles saga events"| node_transfer_saga
node_consumers -->|"publishes UI deltas"| node_redis
node_stream -->|"subscribes to deltas"| node_redis
node_stream -->|"polls watermark"| node_postgres
node_api -->|"serves event stream"| node_stream
node_ops_ui -->|"requests status"| node_ops
node_ops -->|"reads metrics"| node_metrics
node_ops -->|"inspects queues"| node_rabbit
node_ops -->|"shows cache state"| node_cache_recovery
node_cache_recovery -->|"fences generations"| node_postgres
node_cache_recovery -->|"rebuilds generation"| node_redis
node_seed -->|"seeds hospitals and beds"| node_postgres
node_ops_ui -->|"starts scenarios"| node_chaos
node_chaos -->|"exercises dispatch"| node_api

click node_browser "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/common.js"
click node_dispatch_ui "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/dispatch.js"
click node_hospital_ui "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/hospital.js"
click node_control_ui "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/control.js"
click node_ops_ui "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/ops.js"
click node_api "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/app.py"
click node_dispatch "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/dispatch.py"
click node_smartmatch "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/smartmatch.py"
click node_bed_lifecycle "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/beds.py"
click node_transfer_saga "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/transfers.py"
click node_worker_loops "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/worker/loops.py"
click node_consumers "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/worker/consumers.py"
click node_stream "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/stream.py"
click node_ops "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/ops.py"
click node_metrics "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/metrics.py"
click node_cache_recovery "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/cache.py"
click node_seed "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/seed.py"
click node_chaos "https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/chaos.py"

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_browser,node_dispatch_ui,node_hospital_ui,node_control_ui,node_ops_ui,node_api toneBlue
class node_dispatch,node_smartmatch,node_bed_lifecycle,node_transfer_saga toneAmber
class node_worker_loops,node_rabbit,node_consumers toneMint
class node_stream,node_ops,node_metrics,node_chaos toneRose
class node_postgres,node_redis,node_cache_recovery,node_seed,node_dispatcher,node_hospital_user,node_operator toneIndigo
```

## Components

### User interfaces

- [Web console](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/common.js): `app/web/static/common.js`
- [Dispatch page](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/dispatch.js): `app/web/static/dispatch.js`
- [Hospital console](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/hospital.js): `app/web/static/hospital.js`
- [Control map](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/control.js): `app/web/static/control.js`
- [Operations page](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/static/ops.js): `app/web/static/ops.js`
- [HTTP API](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/app.py): `app/web/app.py`

### Care workflows

- [Bed search and hold](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/dispatch.py): `app/dispatch.py`
- [Capability matching](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/smartmatch.py): `app/smartmatch.py`
- [Admission lifecycle](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/beds.py): `app/beds.py`
- [Transfer saga](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/transfers.py): `app/transfers.py`

### Event processing

- [Outbox and timers](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/worker/loops.py): `app/worker/loops.py`
- **RabbitMQ**
- [Consumer groups](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/worker/consumers.py): `app/worker/consumers.py`

### Live views and operations

- [SSE updates](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/web/stream.py): `app/web/stream.py`
- [Operations API](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/ops.py): `app/ops.py`
- [Metrics](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/metrics.py): `app/metrics.py`
- [Scenario controls](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/chaos.py): `app/chaos.py`

### State and infrastructure

- **PostgreSQL**
- **Redis cache**
- [Cache recovery](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/cache.py): `app/cache.py`
- [Synthetic network seed](https://github.com/nafeytauheed-cyber/medigrid/blob/main/app/seed.py): `app/seed.py`

### Other components

- **Dispatcher**
- **Hospital staff**
- **Operator**

## Connections

- Dispatcher → Dispatch page: uses
- Hospital staff → Hospital console: uses
- Operator → Operations page: uses
- Web console → HTTP API: calls (evidence: `app/web/app.py`)
- Dispatch page → HTTP API: submits requests
- Hospital console → HTTP API: manages beds
- Control map → HTTP API: loads map
- Operations page → HTTP API: loads operations
- HTTP API → Bed search and hold: routes dispatch (evidence: `app/web/app.py`)
- HTTP API → Admission lifecycle: routes bed actions (evidence: `app/web/app.py`)
- HTTP API → Transfer saga: routes transfers (evidence: `app/web/app.py`)
- Bed search and hold → Capability matching: filters hospitals (evidence: `app/dispatch.py`)
- Transfer saga → Capability matching: checks capability (evidence: `app/transfers.py`)
- Bed search and hold → Redis cache: proposes and holds
- Bed search and hold → PostgreSQL: commits reservation
- Admission lifecycle → PostgreSQL: updates state (evidence: `app/beds.py`)
- Transfer saga → PostgreSQL: commits saga (evidence: `app/transfers.py`)
- Admission lifecycle → Redis cache: settles cache hold (evidence: `app/beds.py`)
- Transfer saga → Redis cache: settles cache hold (evidence: `app/transfers.py`)
- Outbox and timers → PostgreSQL: reads outbox
- Outbox and timers → RabbitMQ: publishes events
- RabbitMQ → Consumer groups: delivers events
- Consumer groups → PostgreSQL: records processing (evidence: `app/worker/consumers.py`)
- Consumer groups → Transfer saga: handles saga events (evidence: `app/worker/consumers.py`)
- Consumer groups → Redis cache: publishes UI deltas
- SSE updates → Redis cache: subscribes to deltas
- SSE updates → PostgreSQL: polls watermark
- HTTP API → SSE updates: serves event stream (evidence: `app/web/app.py`)
- Operations page → Operations API: requests status
- Operations API → Metrics: reads metrics (evidence: `app/ops.py`)
- Operations API → RabbitMQ: inspects queues (evidence: `app/ops.py`)
- Operations API → Cache recovery: shows cache state (evidence: `app/ops.py`)
- Cache recovery → PostgreSQL: fences generations (evidence: `app/cache.py`)
- Cache recovery → Redis cache: rebuilds generation (evidence: `app/cache.py`)
- Synthetic network seed → PostgreSQL: seeds hospitals and beds (evidence: `app/seed.py`)
- Operations page → Scenario controls: starts scenarios
- Scenario controls → HTTP API: exercises dispatch (evidence: `app/chaos.py`)
