# himanshukumarrout/crowdshield architecture

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

- Interactive diagram: https://gitdiagram.com/himanshukumarrout/crowdshield
- Repository: https://github.com/himanshukumarrout/crowdshield

## Overview

#### Purpose and System Shape
CrowdShield is a crowd-safety MVP: simulated venue telemetry is ingested, risk-scored, persisted, and presented to operators and citizens. It is a multi-runtime system, not a single application: FastAPI backend, React/Vite simulation, React/Vite operator console, and Expo mobile app. Root orchestration is in `docker-compose.yml`.

#### Telemetry Production
The digital twin drives crowd agents and emits per-zone state for five venue zones. `Simulation/src/engine/CrowdEngine.js` produces density, speed, surge, bottleneck, and flow-conflict signals; `Simulation/src/engine/TelemetrySync.js` conditionally POSTs them once per tick with retry backoff. `Simulation/src/App.jsx` is the simulation entry/composition point. `Ai/detect.py` is a planned alternative computer-vision producer, not integrated into the main flow.

#### API Ingestion and Live State
`Server/main.py` mounts the FastAPI routes. `Server/routers/crowd.py` accepts validated telemetry models from `Server/models.py`, updates process-local latest metrics in `Server/shared_data.py`, scores incoming metrics, and attempts durable persistence. The in-memory store allows live operation when Supabase credentials or persistence are unavailable, but it is single-process and non-durable.

#### Risk and Operational Decisions
`Server/services/risk_engine.py` converts density, average speed, surge, bottleneck, and flow conflict into a 0–100 score, level, and reasons. `Server/routers/risk.py` exposes calculation and event retrieval; `Server/services/recommendation_engine.py` maps level to recommended operator actions. Risk-event persistence is intended to occur only on level changes, creating the boundary between continuous telemetry and actionable events.

#### Persistence, Identity, and Security
Supabase supplies Postgres, Auth, and RLS. `Server/db.py` is the backend persistence adapter, while `App/db/migrations/0001_db_init.sql` and `App/db/migrations/0002_backend_persistence.sql` define/extend core domain storage: profiles, venues, crowd data, risk events, incidents, and alerts. The backend degrades gracefully without Supabase; Web and App directly use Supabase for authenticated user/role flows. A known schema/data gap is that flow conflict is consumed by scoring but not persisted with crowd data.

#### Operator Console
The Vite React console starts at `Web/src/main.jsx`; routing and authenticated access boundaries are composed in `Web/src/router/AppRoutes.jsx` and `Web/src/router/ProtectedRoute.jsx`. `Web/src/lib/useLiveData.js` polls FastAPI metrics and risk events (rather than subscribing to a stream), feeding dashboard views such as `Web/src/features/admin/pages/Dashboard.jsx`. Supabase authentication distinguishes protected operator access; the API remains the live-telemetry authority.

#### Citizen Mobile Experience
The Expo Router root is `App/app/_layout.tsx`, with auth and tab routes under `App/app/(auth)` and `App/app/(tabs)`. `App/lib/api.ts` polls backend live-risk APIs, while `App/components/RiskNotificationProvider.tsx` turns changing risk state into local/push notification behavior through Expo Notifications. App services also access Supabase for authentication and citizen-domain records such as incident reporting.

#### Runtime and Deployment Boundaries
Python 3.10+/Uvicorn runs `Server/`; Node/Vite runs `Web/` and `Simulation/`; Expo runs `App/`. Docker development containers are defined by `Server/Dockerfile`, `Web/Dockerfile`, `Simulation/Dockerfile`, and `docker-compose.yml`, with the server health gate before web/simulation startup. External dependencies are Supabase and Expo notification services; no message broker, WebSocket/SSE layer, or production worker tier currently exists.

Main entry points: [`Server/routers/crowd.py`](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/routers/crowd.py) (Telemetry Ingestion), [`Server/routers/risk.py`](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/routers/risk.py) (Risk API), [`docker-compose.yml`](https://github.com/himanshukumarrout/crowdshield/blob/main/docker-compose.yml) (Docker Compose), [`Server/main.py`](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/main.py) (FastAPI App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_simulation["Simulation"]
  node_sim_app["Simulation App<br/>React entry<br/>[App.jsx]"]
  node_crowd_engine["Crowd Engine<br/>telemetry producer<br/>[CrowdEngine.js]"]
  node_telemetry_sync["Telemetry Sync<br/>HTTP publisher<br/>[TelemetrySync.js]"]
  node_cv_producer["CV Detector &#40;planned&#41;<br/>alternative producer<br/>[detect.py]"]
end

subgraph group_backend["Backend API"]
  node_api_main["FastAPI App<br/>API composition<br/>[main.py]"]
  node_telemetry_route["Telemetry Ingestion<br/>crowd route<br/>[crowd.py]"]
  node_live_state[("Latest Metrics Store<br/>process-local state<br/>[shared_data.py]")]
  node_risk_engine["Risk Engine<br/>scoring service<br/>[risk_engine.py]"]
  node_risk_route["Risk API<br/>risk route<br/>[risk.py]"]
  node_recommendations["Recommendation Engine<br/>decision service"]
  node_persistence["Persistence Adapter<br/>Supabase adapter<br/>[db.py]"]
end

subgraph group_web["Operator Console"]
  node_web_routes["Protected Web Routes<br/>React routing<br/>[AppRoutes.jsx]"]
  node_web_live_data["Web Live Data<br/>API polling hook<br/>[useLiveData.js]"]
  node_dashboard["Operator Dashboard<br/>dashboard view<br/>[Dashboard.jsx]"]
end

subgraph group_mobile["Citizen App"]
  node_mobile_routes["Expo Router<br/>mobile route root<br/>[_layout.tsx]"]
  node_mobile_api["Mobile API Client<br/>API polling client<br/>[api.ts]"]
  node_risk_notifications["Risk Notifications<br/>notification provider"]
end

subgraph group_platform["Platform"]
  node_compose{{"Docker Compose<br/>orchestration<br/>[docker-compose.yml]"}}
  node_supabase[("Supabase<br/>Postgres, Auth, RLS")]
  node_migrations["Domain Schema<br/>SQL migrations"]
  node_expo["Expo Notifications<br/>external notification service"]
end

node_compose -->|"runs"| node_api_main
node_compose -->|"runs after server health"| node_sim_app
node_compose -->|"runs after server health"| node_web_routes
node_sim_app -->|"drives"| node_crowd_engine
node_crowd_engine -->|"zone metrics"| node_telemetry_sync
node_telemetry_sync -->|"POST telemetry"| node_telemetry_route
node_cv_producer -.->|"potential telemetry source"| node_telemetry_route
node_api_main -->|"mounts"| node_telemetry_route
node_api_main -->|"mounts"| node_risk_route
node_telemetry_route -->|"updates"| node_live_state
node_telemetry_route -->|"scores metrics"| node_risk_engine
node_risk_engine -->|"risk level"| node_recommendations
node_telemetry_route -->|"stores telemetry and events"| node_persistence
node_risk_route -->|"reads current metrics"| node_live_state
node_risk_route -->|"retrieves events"| node_persistence
node_persistence -->|"persists to"| node_supabase
node_migrations -->|"defines schema"| node_supabase
node_web_routes -->|"auth and roles"| node_supabase
node_web_routes -->|"renders authenticated views"| node_web_live_data
node_web_live_data -->|"polls"| node_risk_route
node_web_live_data -->|"feeds"| node_dashboard
node_mobile_routes -->|"auth and citizen records"| node_supabase
node_mobile_routes -->|"uses"| node_mobile_api
node_mobile_api -->|"polls live risk"| node_risk_route
node_mobile_api -->|"risk changes"| node_risk_notifications
node_risk_notifications -->|"local/push notifications"| node_expo

click node_compose "https://github.com/himanshukumarrout/crowdshield/blob/main/docker-compose.yml"
click node_sim_app "https://github.com/himanshukumarrout/crowdshield/blob/main/Simulation/src/App.jsx"
click node_crowd_engine "https://github.com/himanshukumarrout/crowdshield/blob/main/Simulation/src/engine/CrowdEngine.js"
click node_telemetry_sync "https://github.com/himanshukumarrout/crowdshield/blob/main/Simulation/src/engine/TelemetrySync.js"
click node_cv_producer "https://github.com/himanshukumarrout/crowdshield/blob/main/Ai/detect.py"
click node_api_main "https://github.com/himanshukumarrout/crowdshield/blob/main/Server/main.py"
click node_telemetry_route "https://github.com/himanshukumarrout/crowdshield/blob/main/Server/routers/crowd.py"
click node_live_state "https://github.com/himanshukumarrout/crowdshield/blob/main/Server/shared_data.py"
click node_risk_engine "https://github.com/himanshukumarrout/crowdshield/blob/main/Server/services/risk_engine.py"
click node_risk_route "https://github.com/himanshukumarrout/crowdshield/blob/main/Server/routers/risk.py"
click node_recommendations "https://github.com/himanshukumarrout/crowdshield/blob/main/Server/services/recommendation_engine.py"
click node_persistence "https://github.com/himanshukumarrout/crowdshield/blob/main/Server/db.py"
click node_migrations "https://github.com/himanshukumarrout/crowdshield/blob/main/App/db/migrations/0002_backend_persistence.sql"
click node_web_routes "https://github.com/himanshukumarrout/crowdshield/blob/main/Web/src/router/AppRoutes.jsx"
click node_web_live_data "https://github.com/himanshukumarrout/crowdshield/blob/main/Web/src/lib/useLiveData.js"
click node_dashboard "https://github.com/himanshukumarrout/crowdshield/blob/main/Web/src/features/admin/pages/Dashboard.jsx"
click node_mobile_routes "https://github.com/himanshukumarrout/crowdshield/blob/main/App/app/_layout.tsx"
click node_mobile_api "https://github.com/himanshukumarrout/crowdshield/blob/main/App/lib/api.ts"
click node_risk_notifications "https://github.com/himanshukumarrout/crowdshield/blob/main/App/components/RiskNotificationProvider.tsx"

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_sim_app,node_crowd_engine,node_telemetry_sync,node_cv_producer toneBlue
class node_api_main,node_telemetry_route,node_live_state,node_risk_engine,node_risk_route,node_recommendations,node_persistence toneAmber
class node_web_routes,node_web_live_data,node_dashboard toneMint
class node_mobile_routes,node_mobile_api,node_risk_notifications toneRose
class node_compose,node_supabase,node_migrations,node_expo toneIndigo
```

## Components

### Simulation

- [Simulation App](https://github.com/himanshukumarrout/crowdshield/blob/main/Simulation/src/App.jsx): React entry · `Simulation/src/App.jsx`
- [Crowd Engine](https://github.com/himanshukumarrout/crowdshield/blob/main/Simulation/src/engine/CrowdEngine.js): telemetry producer · Simulates five-zone crowd signals. · `Simulation/src/engine/CrowdEngine.js`
- [Telemetry Sync](https://github.com/himanshukumarrout/crowdshield/blob/main/Simulation/src/engine/TelemetrySync.js): HTTP publisher · Posts once per tick with retry backoff. · `Simulation/src/engine/TelemetrySync.js`
- [CV Detector (planned)](https://github.com/himanshukumarrout/crowdshield/blob/main/Ai/detect.py): alternative producer · Not integrated into the main telemetry flow. · `Ai/detect.py`

### Backend API

- [FastAPI App](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/main.py): API composition · `Server/main.py`
- [Telemetry Ingestion](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/routers/crowd.py): crowd route · Validates metrics, updates live state, and initiates scoring. · `Server/routers/crowd.py`
- [Latest Metrics Store](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/shared_data.py): process-local state · Single-process fallback live state; non-durable. · `Server/shared_data.py`
- [Risk Engine](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/services/risk_engine.py): scoring service · Produces score, level, and reasons from crowd signals. · `Server/services/risk_engine.py`
- [Risk API](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/routers/risk.py): risk route · Serves calculations and risk events. · `Server/routers/risk.py`
- [Recommendation Engine](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/services/recommendation_engine.py): decision service · Maps risk level to operator actions. · `Server/services/recommendation_engine.py`
- [Persistence Adapter](https://github.com/himanshukumarrout/crowdshield/blob/main/Server/db.py): Supabase adapter · Gracefully degrades when persistence is unavailable. · `Server/db.py`

### Operator Console

- [Protected Web Routes](https://github.com/himanshukumarrout/crowdshield/blob/main/Web/src/router/AppRoutes.jsx): React routing · Composes authenticated operator access. · `Web/src/router/AppRoutes.jsx`
- [Web Live Data](https://github.com/himanshukumarrout/crowdshield/blob/main/Web/src/lib/useLiveData.js): API polling hook · Polls FastAPI metrics and risk events. · `Web/src/lib/useLiveData.js`
- [Operator Dashboard](https://github.com/himanshukumarrout/crowdshield/blob/main/Web/src/features/admin/pages/Dashboard.jsx): dashboard view · `Web/src/features/admin/pages/Dashboard.jsx`

### Citizen App

- [Expo Router](https://github.com/himanshukumarrout/crowdshield/blob/main/App/app/_layout.tsx): mobile route root · Hosts auth and tab route trees. · `App/app/_layout.tsx`
- [Mobile API Client](https://github.com/himanshukumarrout/crowdshield/blob/main/App/lib/api.ts): API polling client · Polls backend live-risk APIs. · `App/lib/api.ts`
- [Risk Notifications](https://github.com/himanshukumarrout/crowdshield/blob/main/App/components/RiskNotificationProvider.tsx): notification provider · Turns changing risk state into local or push notifications. · `App/components/RiskNotificationProvider.tsx`

### Platform

- [Docker Compose](https://github.com/himanshukumarrout/crowdshield/blob/main/docker-compose.yml): orchestration · Starts server before web and simulation. · `docker-compose.yml`
- **Supabase**: Postgres, Auth, RLS
- [Domain Schema](https://github.com/himanshukumarrout/crowdshield/blob/main/App/db/migrations/0002_backend_persistence.sql): SQL migrations · Defines profiles, venues, crowd data, events, incidents, and alerts. · `App/db/migrations/0002_backend_persistence.sql`
- **Expo Notifications**: external notification service

## Connections

- Docker Compose → FastAPI App: runs
- Docker Compose → Simulation App: runs after server health
- Docker Compose → Protected Web Routes: runs after server health
- Simulation App → Crowd Engine: drives
- Crowd Engine → Telemetry Sync: zone metrics
- Telemetry Sync → Telemetry Ingestion: POST telemetry
- CV Detector (planned) → Telemetry Ingestion: potential telemetry source
- FastAPI App → Telemetry Ingestion: mounts
- FastAPI App → Risk API: mounts
- Telemetry Ingestion → Latest Metrics Store: updates
- Telemetry Ingestion → Risk Engine: scores metrics
- Risk Engine → Recommendation Engine: risk level
- Telemetry Ingestion → Persistence Adapter: stores telemetry and events
- Risk API → Latest Metrics Store: reads current metrics
- Risk API → Persistence Adapter: retrieves events
- Persistence Adapter → Supabase: persists to
- Domain Schema → Supabase: defines schema
- Protected Web Routes → Supabase: auth and roles
- Protected Web Routes → Web Live Data: renders authenticated views
- Web Live Data → Risk API: polls
- Web Live Data → Operator Dashboard: feeds
- Expo Router → Supabase: auth and citizen records
- Expo Router → Mobile API Client: uses
- Mobile API Client → Risk API: polls live risk
- Mobile API Client → Risk Notifications: risk changes
- Risk Notifications → Expo Notifications: local/push notifications
