How himanshukumarrout/crowdshield works
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: crowd.py Telemetry Ingestion · risk.py Risk API · docker-compose.yml Docker Compose · main.py FastAPI App
Components 21
Simulation
- Simulation App
Simulation/src/App.jsx - Crowd Engine · Simulates five-zone crowd signals.
Simulation/src/engine/CrowdEngine.js - Telemetry Sync · Posts once per tick with retry backoff.
Simulation/src/engine/TelemetrySync.js - CV Detector (planned) · Not integrated into the main telemetry flow.
Ai/detect.py
Backend API
- FastAPI App
Server/main.py - Telemetry Ingestion · Validates metrics, updates live state, and initiates scoring.
Server/routers/crowd.py - Latest Metrics Store · Single-process fallback live state; non-durable.
Server/shared_data.py - Risk Engine · Produces score, level, and reasons from crowd signals.
Server/services/risk_engine.py - Risk API · Serves calculations and risk events.
Server/routers/risk.py - Recommendation Engine · Maps risk level to operator actions.
Server/services/recommendation_engine.py - Persistence Adapter · Gracefully degrades when persistence is unavailable.
Server/db.py
Operator Console
- Protected Web Routes · Composes authenticated operator access.
Web/src/router/AppRoutes.jsx - Web Live Data · Polls FastAPI metrics and risk events.
Web/src/lib/useLiveData.js - Operator Dashboard
Web/src/features/admin/pages/Dashboard.jsx
Citizen App
- Expo Router · Hosts auth and tab route trees.
App/app/_layout.tsx - Mobile API Client · Polls backend live-risk APIs.
App/lib/api.ts - Risk Notifications · Turns changing risk state into local or push notifications.
App/components/RiskNotificationProvider.tsx
Platform
- Docker Compose · Starts server before web and simulation.
docker-compose.yml - Supabase
- Domain Schema · Defines profiles, venues, crowd data, events, incidents, and alerts.
App/db/migrations/0002_backend_persistence.sql - Expo Notifications
Connections 26
- 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