# sanjitdev/surakkha architecture

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

- Interactive diagram: https://gitdiagram.com/sanjitdev/surakkha
- Repository: https://github.com/sanjitdev/surakkha

## Overview

#### Purpose and topology
Surakkha is a monorepo for real-time water-quality monitoring: simulated devices send telemetry, the backend persists/evaluates it, and a React dashboard presents live device, reading, incident, and simulator-control views. Workspace boundaries are `packages/api`, `packages/web`, `packages/simulator`, `packages/db`, and `packages/shared`; Compose runs API, web, simulator, and Postgres (`docker-compose.yml`, `pnpm-workspace.yaml`).

#### Runtime entry points
The API is a Node/TypeScript process rooted at `packages/api/src/index.ts`; it hosts HTTP routes and WebSocket ingestion/realtime behavior in one process. The device simulator starts from `packages/simulator/src/index.ts` and exposes its control surface through `packages/simulator/src/control/server.ts`. The browser SPA enters at `packages/web/src/main.tsx`, while Nginx serves its built assets (`packages/web/nginx.conf`).

#### Shared contracts boundary
`packages/shared` is the required cross-package contract layer; feature packages should not import each other’s internal types. Telemetry framing is defined in `packages/shared/src/telemetry.ts`, with related event, auth/RBAC, incident, rule, dashboard, and simulator models colocated under `packages/shared/src/`. This is the principal boundary between simulator, API, and web, and the telemetry contract is versioned/frozen at v1.

#### Telemetry ingest and fan-out
Devices/simulator connect over plain WebSocket to the API ingestion server (`packages/api/src/ingest/server.ts`) and submit frames validated/processed by `packages/api/src/ingest/frame.ts`. Ingestion includes sequencing and rate-limit controls (`packages/api/src/ingest/sequence.ts`, `packages/api/src/ingest/rateLimit.ts`), then publishes accepted readings through subscriber infrastructure (`packages/api/src/ingest/subscriber.ts`). The web client consumes live updates via `packages/web/src/realtime/socketClient.ts` and dashboard socket integration (`packages/web/src/dashboard/useDashboardSocket.ts`).

#### Persistence and rule evaluation
Postgres 15 is the sole durable store, accessed through Prisma schema/migrations in `packages/db/prisma/schema.prisma` and `packages/db/prisma/migrations/`. Database seed logic establishes initial devices, users, and thresholds/rules (`packages/db/prisma/seed.ts`, `packages/db/prisma/thresholdTable.ts`). The API rule stage loads/caches rule data and evaluates incoming readings (`packages/api/src/rules/cache.ts`, `packages/api/src/rules/engine.ts`, `packages/api/src/rules/hooks.ts`), forming the alert/incident trigger boundary.

#### API security and feature surfaces
Authentication uses HS256 JWT helpers and auth routes (`packages/api/src/auth/jwt.ts`, `packages/api/src/auth/router.ts`); authorization is centralized in `packages/api/src/middleware/authorize.ts`. API surfaces currently organize around devices, latest readings, recent incidents, and admin simulator control (`packages/api/src/devices/router.ts`, `packages/api/src/readings/latestRouter.ts`, `packages/api/src/admin/simulatorRouter.ts`). Audit behavior is centralized in `packages/api/src/audit.ts`, rather than embedded exclusively in individual routes.

#### User-facing flows
The dashboard composes device/map, live-reading, and recent-incident regions (`packages/web/src/dashboard/Dashboard.tsx`, `packages/web/src/dashboard/MapRegion.tsx`, `packages/web/src/dashboard/LiveReadingsRegion.tsx`). Authentication and role-aware navigation are implemented through the auth/access and shell layers (`packages/web/src/auth/LoginShell.tsx`, `packages/web/src/access/RbacRoute.tsx`, `packages/web/src/shell/AppShell.tsx`). Admins control simulator devices from `packages/web/src/admin/simulator/SimulatorPage.tsx`; that client calls API control endpoints, which communicate with the separate simulator process rather than bypassing the ingestion wire contract.

#### Infrastructure constraints
Development and local deployment use Docker Compose, with Postgres health-gated before the Node API and web served through Nginx (`docker-compose.yml`, `packages/api/Dockerfile`, `packages/web/Dockerfile`, `packages/simulator/Dockerfile`). The intended v1 architecture deliberately has no queue, Redis, or time-series database: ingestion, rules, workflows, and scheduled work remain in the API process, with Postgres as the system of record.

Main entry points: [`packages/api/src/devices/router.ts`](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/devices/router.ts) (Feature and admin routes), [`packages/api/src/index.ts`](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/index.ts) (API host), [`packages/web/src/main.tsx`](https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/main.tsx) (React dashboard), [`packages/api/src/ingest/server.ts`](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/ingest/server.ts) (Telemetry ingest)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_contracts["Shared contracts"]
  node_shared["Shared v1 models<br/>contract package<br/>[telemetry.ts]"]
end

subgraph group_api["API process"]
  node_api["API host<br/>Node HTTP/WebSocket process<br/>[index.ts]"]
  node_api_auth["Auth and authorization<br/>security layer<br/>[router.ts]"]
  node_api_routes["Feature and admin routes<br/>HTTP API<br/>[router.ts]"]
  node_ingest["Telemetry ingest<br/>WebSocket pipeline<br/>[server.ts]"]
  node_subscribers["Realtime subscribers<br/>fan-out<br/>[subscriber.ts]"]
  node_rules["Rule evaluation<br/>alert boundary<br/>[engine.ts]"]
  node_audit["Audit service<br/>audit logging<br/>[audit.ts]"]
end

subgraph group_clients["Clients"]
  node_simulator["Device simulator<br/>Node process<br/>[index.ts]"]
  node_simulator_control["Simulator control server<br/>control surface<br/>[server.ts]"]
  node_web_spa["React dashboard<br/>browser SPA<br/>[main.tsx]"]
  node_dashboard["Dashboard regions<br/>UI composition<br/>[Dashboard.tsx]"]
  node_web_auth["Auth and RBAC shell<br/>UI access layer<br/>[LoginShell.tsx]"]
  node_realtime_client["Live socket client<br/>WebSocket client<br/>[socketClient.ts]"]
  node_admin_simulator["Simulator admin UI<br/>admin page<br/>[SimulatorPage.tsx]"]
end

subgraph group_data["Data and infrastructure"]
  node_postgres[("Postgres 15<br/>system of record<br/>[schema.prisma]")]
  node_db_seed["Database seed data<br/>Prisma seed<br/>[seed.ts]"]
  node_compose{{"Docker Compose<br/>local deployment<br/>[docker-compose.yml]"}}
  node_nginx["Nginx web server<br/>static host<br/>[nginx.conf]"]
end

node_simulator -->|"hosts"| node_simulator_control
node_simulator -.->|"uses contracts"| node_shared
node_simulator -->|"telemetry WebSocket"| node_ingest
node_web_spa -->|"renders"| node_dashboard
node_web_spa -->|"enforces access"| node_web_auth
node_web_spa -->|"admin view"| node_admin_simulator
node_dashboard -->|"live updates"| node_realtime_client
node_realtime_client -->|"WebSocket"| node_subscribers
node_admin_simulator -->|"control requests"| node_api_routes
node_web_spa -.->|"uses models"| node_shared
node_api -->|"hosts"| node_api_auth
node_api -->|"hosts"| node_api_routes
node_api -->|"hosts"| node_ingest
node_api_routes -->|"authorizes"| node_api_auth
node_api_routes -->|"controls"| node_simulator_control
node_api_routes -->|"queries"| node_postgres
node_api_routes -->|"audits"| node_audit
node_ingest -->|"persists readings"| node_postgres
node_ingest -->|"accepted readings"| node_rules
node_ingest -->|"publishes"| node_subscribers
node_rules -->|"loads rules and stores incidents"| node_postgres
node_api -.->|"uses contracts"| node_shared
node_db_seed -->|"initializes"| node_postgres
node_nginx -->|"serves build"| node_web_spa
node_compose -->|"runs"| node_postgres
node_compose -->|"runs"| node_api
node_compose -->|"runs"| node_simulator
node_compose -->|"runs"| node_nginx

click node_shared "https://github.com/sanjitdev/surakkha/blob/main/packages/shared/src/telemetry.ts"
click node_simulator "https://github.com/sanjitdev/surakkha/blob/main/packages/simulator/src/index.ts"
click node_simulator_control "https://github.com/sanjitdev/surakkha/blob/main/packages/simulator/src/control/server.ts"
click node_web_spa "https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/main.tsx"
click node_dashboard "https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/dashboard/Dashboard.tsx"
click node_web_auth "https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/auth/LoginShell.tsx"
click node_realtime_client "https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/realtime/socketClient.ts"
click node_admin_simulator "https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/admin/simulator/SimulatorPage.tsx"
click node_api "https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/index.ts"
click node_api_auth "https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/auth/router.ts"
click node_api_routes "https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/devices/router.ts"
click node_ingest "https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/ingest/server.ts"
click node_subscribers "https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/ingest/subscriber.ts"
click node_rules "https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/rules/engine.ts"
click node_audit "https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/audit.ts"
click node_postgres "https://github.com/sanjitdev/surakkha/blob/main/packages/db/prisma/schema.prisma"
click node_db_seed "https://github.com/sanjitdev/surakkha/blob/main/packages/db/prisma/seed.ts"
click node_compose "https://github.com/sanjitdev/surakkha/blob/main/docker-compose.yml"
click node_nginx "https://github.com/sanjitdev/surakkha/blob/main/packages/web/nginx.conf"

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_shared toneBlue
class node_api,node_api_auth,node_api_routes,node_ingest,node_subscribers,node_rules,node_audit toneAmber
class node_simulator,node_simulator_control,node_web_spa,node_dashboard,node_web_auth,node_realtime_client,node_admin_simulator toneMint
class node_postgres,node_db_seed,node_compose,node_nginx toneRose
```

## Components

### Shared contracts

Required cross-package boundary.

- [Shared v1 models](https://github.com/sanjitdev/surakkha/blob/main/packages/shared/src/telemetry.ts): contract package · Telemetry, events, auth, incidents, rules, dashboard, and simulator models. · `packages/shared/src/telemetry.ts`

### API process

- [API host](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/index.ts): Node HTTP/WebSocket process · `packages/api/src/index.ts`
- [Auth and authorization](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/auth/router.ts): security layer · JWT routes and centralized RBAC authorization. · `packages/api/src/auth/router.ts`
- [Feature and admin routes](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/devices/router.ts): HTTP API · Devices, latest readings, incidents, and simulator control. · `packages/api/src/devices/router.ts`
- [Telemetry ingest](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/ingest/server.ts): WebSocket pipeline · Validates frames and applies sequencing and rate limits. · `packages/api/src/ingest/server.ts`
- [Realtime subscribers](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/ingest/subscriber.ts): fan-out · `packages/api/src/ingest/subscriber.ts`
- [Rule evaluation](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/rules/engine.ts): alert boundary · Caches rules and evaluates incoming readings. · `packages/api/src/rules/engine.ts`
- [Audit service](https://github.com/sanjitdev/surakkha/blob/main/packages/api/src/audit.ts): audit logging · `packages/api/src/audit.ts`

### Clients

- [Device simulator](https://github.com/sanjitdev/surakkha/blob/main/packages/simulator/src/index.ts): Node process · `packages/simulator/src/index.ts`
- [Simulator control server](https://github.com/sanjitdev/surakkha/blob/main/packages/simulator/src/control/server.ts): control surface · `packages/simulator/src/control/server.ts`
- [React dashboard](https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/main.tsx): browser SPA · `packages/web/src/main.tsx`
- [Dashboard regions](https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/dashboard/Dashboard.tsx): UI composition · Device/map, live-reading, and incident views. · `packages/web/src/dashboard/Dashboard.tsx`
- [Auth and RBAC shell](https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/auth/LoginShell.tsx): UI access layer · `packages/web/src/auth/LoginShell.tsx`
- [Live socket client](https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/realtime/socketClient.ts): WebSocket client · `packages/web/src/realtime/socketClient.ts`
- [Simulator admin UI](https://github.com/sanjitdev/surakkha/blob/main/packages/web/src/admin/simulator/SimulatorPage.tsx): admin page · `packages/web/src/admin/simulator/SimulatorPage.tsx`

### Data and infrastructure

- [Postgres 15](https://github.com/sanjitdev/surakkha/blob/main/packages/db/prisma/schema.prisma): system of record · Sole durable store for telemetry, devices, users, rules, and incidents. · `packages/db/prisma/schema.prisma`
- [Database seed data](https://github.com/sanjitdev/surakkha/blob/main/packages/db/prisma/seed.ts): Prisma seed · Initial devices, users, thresholds, and rules. · `packages/db/prisma/seed.ts`
- [Docker Compose](https://github.com/sanjitdev/surakkha/blob/main/docker-compose.yml): local deployment · Runs API, web, simulator, and health-gated Postgres. · `docker-compose.yml`
- [Nginx web server](https://github.com/sanjitdev/surakkha/blob/main/packages/web/nginx.conf): static host · `packages/web/nginx.conf`

## Connections

- Device simulator → Simulator control server: hosts
- Device simulator → Shared v1 models: uses contracts
- Device simulator → Telemetry ingest: telemetry WebSocket
- React dashboard → Dashboard regions: renders
- React dashboard → Auth and RBAC shell: enforces access
- React dashboard → Simulator admin UI: admin view
- Dashboard regions → Live socket client: live updates
- Live socket client → Realtime subscribers: WebSocket
- Simulator admin UI → Feature and admin routes: control requests
- React dashboard → Shared v1 models: uses models
- API host → Auth and authorization: hosts
- API host → Feature and admin routes: hosts
- API host → Telemetry ingest: hosts
- Feature and admin routes → Auth and authorization: authorizes
- Feature and admin routes → Simulator control server: controls
- Feature and admin routes → Postgres 15: queries
- Feature and admin routes → Audit service: audits
- Telemetry ingest → Postgres 15: persists readings
- Telemetry ingest → Rule evaluation: accepted readings
- Telemetry ingest → Realtime subscribers: publishes
- Rule evaluation → Postgres 15: loads rules and stores incidents
- API host → Shared v1 models: uses contracts
- Database seed data → Postgres 15: initializes
- Nginx web server → React dashboard: serves build
- Docker Compose → Postgres 15: runs
- Docker Compose → API host: runs
- Docker Compose → Device simulator: runs
- Docker Compose → Nginx web server: runs
