# amogh2212/vahan architecture

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

- Interactive diagram: https://gitdiagram.com/amogh2212/vahan
- Repository: https://github.com/amogh2212/vahan

## Overview

#### Purpose and entry points
This is an India vehicle-registration analytics system: it ingests VAHAN/TDC data into SQL storage, serves an interactive dashboard and reporting views, supports natural-language/tracked queries, and produces RTO-level EV and contextual-factor insights. Runtime entry point: `server.mjs`; browser entry points: `public/index.html`, `public/app.js`. Operational entry points are script-driven, notably `scripts/import-to-db.mjs`, `scripts/vahan-scraper.mjs`, and `scripts/run-rto-reports.mjs`.

#### HTTP application boundary
`server.mjs` composes the HTTP service. Routing/security are separated into `lib/http-router.mjs` and `lib/http-security.mjs`; authentication is in `lib/auth.mjs`. The server exposes application/query/report functionality to static browser clients under `public/`, rather than embedding presentation logic in the data pipelines. `routes/rto-daily-routes.mjs` is a distinct route module for daily RTO data.

#### Data acquisition and persistence
VAHAN scraping is an external-system boundary with retry/failure evidence retained locally. `scripts/vahan-scraper.mjs` collects data; canonical outputs include `data/vahan/vahan_fuel_monthly.csv` and `data/vahan/vahan_state_maker_fuel.csv`; failed source responses are preserved in `data/vahan/failures/`. Import jobs transfer acquired data into the relational model defined by `db/schema.sql`, via `scripts/import-to-db.mjs` and `scripts/import-to-neon.mjs`.

#### Query and dashboard layer
The query subsystem turns user-facing requests into normalized, validated data access: `lib/query-engine.mjs`, `lib/query-normalization.mjs`, and `lib/db.mjs`. Its public contract is documented in `docs/query-contract-v1.md`, with a machine-readable corpus at `data/query-tests/dashboard-query-contract-v1.json`. Main dashboard/map/compare clients consume this backend through `public/app.js`, `public/map.js`, and `public/compare.js`. This is a key correctness boundary: user/AI phrasing should be normalized before database querying.

#### Reporting and RTO intelligence
Domain services produce structured reports above raw registrations: national/monthly/OEM views in `lib/registrations.mjs`, `lib/maker-registrations.mjs`, and `lib/monthly-sales-report.mjs`; RTO reporting and insights in `lib/rto-reports.mjs`, `lib/rto-insights.mjs`, and `lib/rto-daily-snapshots.mjs`. Corresponding browser report pages live under `public/reports/`, including `public/reports/rto-reports.js` and `public/reports/monthly-sales.js`. Daily snapshot execution is operationalized by `scripts/run-rto-daily-snapshots.mjs`.

#### RTO factor enrichment pipeline
A separate enrichment pipeline associates RTO outcomes with external contextual factors, source evidence, events, validation, and generated narrative. Its stages are represented by `lib/rto-factor-source-collector.mjs`, `lib/rto-factor-events.mjs`, `lib/rto-factor-validation.mjs`, and `lib/rto-factor-narrative.mjs`; orchestration is in `lib/rto-factor-agent.mjs` and `lib/rto-factor-daily-automation.mjs`. Source registry data is versioned at `data/rto-factor-source-registry.json`. This boundary should preserve provenance and validation separately from derived explanations.

#### Runtimes, infrastructure, and automation
The application is Node.js/ESM (`.mjs`) with a SQL database abstraction. Repository artifacts indicate local PostgreSQL plus Neon deployment/migration support (`scripts/configure-local-postgres.mjs`, `scripts/apply-neon-schema.mjs`), and Cockroach compatibility checking (`.env.cockroach.test.example`, `scripts/cockroach-test-runner.mjs`). GitHub Actions schedule tracked-query, RTO-daily, and production-risk workflows under `.github/workflows/`. External integrations include the VAHAN source system, optional local/hosted AI query providers suggested by `scripts/check-ollama.mjs` and `scripts/groq-query-provider-unit-check.mjs`, and Telegram notifications through `lib/telegram-bot.mjs`.

Main entry points: [`lib/http-router.mjs`](https://github.com/amogh2212/vahan/blob/main/lib/http-router.mjs) (Routing, security & auth), [`lib/db.mjs`](https://github.com/amogh2212/vahan/blob/main/lib/db.mjs) (Database abstraction), [`server.mjs`](https://github.com/amogh2212/vahan/blob/main/server.mjs) (Node HTTP server), [`lib/rto-daily-snapshots.mjs`](https://github.com/amogh2212/vahan/blob/main/lib/rto-daily-snapshots.mjs) (Daily RTO snapshots)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["HTTP &amp; browser"]
  node_server["Node HTTP server<br/>application entry<br/>[server.mjs]"]
  node_http["Routing, security &amp; auth<br/>HTTP boundary<br/>[http-router.mjs]"]
  node_daily_routes["Daily RTO routes<br/>route module"]
  node_dashboard["Dashboard clients<br/>browser clients<br/>[app.js]"]
end

subgraph group_data["Acquisition &amp; SQL"]
  node_scraper["VAHAN scraper<br/>ingestion job<br/>[vahan-scraper.mjs]"]
  node_vahan{{"VAHAN source<br/>external data source"}}
  node_vahan_files["VAHAN canonical files<br/>CSV dataset"]
  node_importer["Database import jobs<br/>ETL jobs<br/>[import-to-db.mjs]"]
  node_schema["Relational schema<br/>SQL schema<br/>[schema.sql]"]
  node_database[("SQL database<br/>PostgreSQL / Neon")]
end

subgraph group_query["Query &amp; reporting"]
  node_query_engine["Query engine<br/>query service<br/>[query-engine.mjs]"]
  node_normalizer["Query normalization<br/>validation layer"]
  node_db_access["Database abstraction<br/>data access layer<br/>[db.mjs]"]
  node_reports["Registration reports<br/>domain services<br/>[registrations.mjs]"]
end

subgraph group_rto["RTO intelligence"]
  node_rto_reports["RTO reports &amp; insights<br/>domain services<br/>[rto-reports.mjs]"]
  node_snapshots["Daily RTO snapshots<br/>snapshot service"]
  node_factor_agent["RTO factor agent<br/>enrichment orchestrator"]
  node_factor_collector["Factor source collector<br/>evidence collection"]
  node_factor_validation["Factor validation<br/>validation stage"]
  node_factor_narrative["Factor narrative<br/>explanation stage"]
end

subgraph group_ops["Automation &amp; integrations"]
  node_automation["Scheduled workflows<br/>GitHub Actions"]
  node_telegram{{"Telegram notifications<br/>external notification<br/>[telegram-bot.mjs]"}}
end

node_server -->|"delegates requests"| node_http
node_server -->|"mounts"| node_daily_routes
node_server -->|"serves"| node_dashboard
node_http -->|"query API"| node_query_engine
node_http -->|"report API"| node_reports
node_daily_routes -->|"daily data API"| node_snapshots
node_dashboard -->|"requests data"| node_http
node_vahan -->|"registration data"| node_scraper
node_scraper -->|"writes CSV and failures"| node_vahan_files
node_vahan_files -->|"input"| node_importer
node_schema -->|"defines"| node_database
node_importer -->|"loads"| node_database
node_query_engine -->|"normalizes requests"| node_normalizer
node_normalizer -->|"validated query"| node_db_access
node_db_access -->|"SQL access"| node_database
node_reports -->|"aggregates registrations"| node_db_access
node_rto_reports -->|"queries RTO outcomes"| node_db_access
node_snapshots -->|"persists daily state"| node_database
node_factor_agent -->|"collects evidence"| node_factor_collector
node_factor_collector -->|"source evidence"| node_factor_validation
node_factor_validation -->|"validated factors"| node_factor_narrative
node_factor_narrative -->|"contextual insights"| node_rto_reports
node_automation -->|"runs daily"| node_snapshots
node_automation -->|"runs enrichment"| node_factor_agent
node_automation -->|"notifies"| node_telegram

click node_server "https://github.com/amogh2212/vahan/blob/main/server.mjs"
click node_http "https://github.com/amogh2212/vahan/blob/main/lib/http-router.mjs"
click node_daily_routes "https://github.com/amogh2212/vahan/blob/main/routes/rto-daily-routes.mjs"
click node_dashboard "https://github.com/amogh2212/vahan/blob/main/public/app.js"
click node_scraper "https://github.com/amogh2212/vahan/blob/main/scripts/vahan-scraper.mjs"
click node_vahan_files "https://github.com/amogh2212/vahan/blob/main/data/vahan/vahan_fuel_monthly.csv"
click node_importer "https://github.com/amogh2212/vahan/blob/main/scripts/import-to-db.mjs"
click node_schema "https://github.com/amogh2212/vahan/blob/main/db/schema.sql"
click node_query_engine "https://github.com/amogh2212/vahan/blob/main/lib/query-engine.mjs"
click node_normalizer "https://github.com/amogh2212/vahan/blob/main/lib/query-normalization.mjs"
click node_db_access "https://github.com/amogh2212/vahan/blob/main/lib/db.mjs"
click node_reports "https://github.com/amogh2212/vahan/blob/main/lib/registrations.mjs"
click node_rto_reports "https://github.com/amogh2212/vahan/blob/main/lib/rto-reports.mjs"
click node_snapshots "https://github.com/amogh2212/vahan/blob/main/lib/rto-daily-snapshots.mjs"
click node_factor_agent "https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-agent.mjs"
click node_factor_collector "https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-source-collector.mjs"
click node_factor_validation "https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-validation.mjs"
click node_factor_narrative "https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-narrative.mjs"
click node_telegram "https://github.com/amogh2212/vahan/blob/main/lib/telegram-bot.mjs"

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_server,node_http,node_daily_routes,node_dashboard toneBlue
class node_scraper,node_vahan,node_vahan_files,node_importer,node_schema,node_database toneAmber
class node_query_engine,node_normalizer,node_db_access,node_reports toneMint
class node_rto_reports,node_snapshots,node_factor_agent,node_factor_collector,node_factor_validation,node_factor_narrative toneRose
class node_automation,node_telegram toneIndigo
```

## Components

### HTTP & browser

- [Node HTTP server](https://github.com/amogh2212/vahan/blob/main/server.mjs): application entry · Composes the service and static browser delivery. · `server.mjs`
- [Routing, security & auth](https://github.com/amogh2212/vahan/blob/main/lib/http-router.mjs): HTTP boundary · `lib/http-router.mjs`
- [Daily RTO routes](https://github.com/amogh2212/vahan/blob/main/routes/rto-daily-routes.mjs): route module · `routes/rto-daily-routes.mjs`
- [Dashboard clients](https://github.com/amogh2212/vahan/blob/main/public/app.js): browser clients · `public/app.js`

### Acquisition & SQL

- [VAHAN scraper](https://github.com/amogh2212/vahan/blob/main/scripts/vahan-scraper.mjs): ingestion job · Retains failed source responses as evidence. · `scripts/vahan-scraper.mjs`
- **VAHAN source**: external data source
- [VAHAN canonical files](https://github.com/amogh2212/vahan/blob/main/data/vahan/vahan_fuel_monthly.csv): CSV dataset · `data/vahan/vahan_fuel_monthly.csv`
- [Database import jobs](https://github.com/amogh2212/vahan/blob/main/scripts/import-to-db.mjs): ETL jobs · `scripts/import-to-db.mjs`
- [Relational schema](https://github.com/amogh2212/vahan/blob/main/db/schema.sql): SQL schema · `db/schema.sql`
- **SQL database**: PostgreSQL / Neon

### Query & reporting

- [Query engine](https://github.com/amogh2212/vahan/blob/main/lib/query-engine.mjs): query service · `lib/query-engine.mjs`
- [Query normalization](https://github.com/amogh2212/vahan/blob/main/lib/query-normalization.mjs): validation layer · Normalizes user and AI phrasing before database access. · `lib/query-normalization.mjs`
- [Database abstraction](https://github.com/amogh2212/vahan/blob/main/lib/db.mjs): data access layer · `lib/db.mjs`
- [Registration reports](https://github.com/amogh2212/vahan/blob/main/lib/registrations.mjs): domain services · Produces national, monthly, and OEM reporting views. · `lib/registrations.mjs`

### RTO intelligence

- [RTO reports & insights](https://github.com/amogh2212/vahan/blob/main/lib/rto-reports.mjs): domain services · `lib/rto-reports.mjs`
- [Daily RTO snapshots](https://github.com/amogh2212/vahan/blob/main/lib/rto-daily-snapshots.mjs): snapshot service · `lib/rto-daily-snapshots.mjs`
- [RTO factor agent](https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-agent.mjs): enrichment orchestrator · Coordinates provenance, validation, and derived narrative. · `lib/rto-factor-agent.mjs`
- [Factor source collector](https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-source-collector.mjs): evidence collection · `lib/rto-factor-source-collector.mjs`
- [Factor validation](https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-validation.mjs): validation stage · `lib/rto-factor-validation.mjs`
- [Factor narrative](https://github.com/amogh2212/vahan/blob/main/lib/rto-factor-narrative.mjs): explanation stage · `lib/rto-factor-narrative.mjs`

### Automation & integrations

- **Scheduled workflows**: GitHub Actions · Runs tracked-query, RTO-daily, and production-risk automation.
- [Telegram notifications](https://github.com/amogh2212/vahan/blob/main/lib/telegram-bot.mjs): external notification · `lib/telegram-bot.mjs`

## Connections

- Node HTTP server → Routing, security & auth: delegates requests
- Node HTTP server → Daily RTO routes: mounts
- Node HTTP server → Dashboard clients: serves
- Routing, security & auth → Query engine: query API
- Routing, security & auth → Registration reports: report API
- Daily RTO routes → Daily RTO snapshots: daily data API
- Dashboard clients → Routing, security & auth: requests data
- VAHAN source → VAHAN scraper: registration data
- VAHAN scraper → VAHAN canonical files: writes CSV and failures
- VAHAN canonical files → Database import jobs: input
- Relational schema → SQL database: defines
- Database import jobs → SQL database: loads
- Query engine → Query normalization: normalizes requests
- Query normalization → Database abstraction: validated query
- Database abstraction → SQL database: SQL access
- Registration reports → Database abstraction: aggregates registrations
- RTO reports & insights → Database abstraction: queries RTO outcomes
- Daily RTO snapshots → SQL database: persists daily state
- RTO factor agent → Factor source collector: collects evidence
- Factor source collector → Factor validation: source evidence
- Factor validation → Factor narrative: validated factors
- Factor narrative → RTO reports & insights: contextual insights
- Scheduled workflows → Daily RTO snapshots: runs daily
- Scheduled workflows → RTO factor agent: runs enrichment
- Scheduled workflows → Telegram notifications: notifies
