# amuthavanmoorthi/calengine architecture

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

- Interactive diagram: https://gitdiagram.com/amuthavanmoorthi/calengine
- Repository: https://github.com/amuthavanmoorthi/calengine

## Overview

#### Purpose and entry points
BERSn CalEngine is a building-energy calculation platform with a Vue UI, Node.js API/persistence layer, and Python/FastAPI formula engine. Docker Compose is the primary local deployment entry point: `infra/docker/docker-compose.yml`. Service entry points are `services/ui/src/main.ts`, `services/api/src/server.js`, and `services/calc/app/main.py`.

#### Contract-first boundary
Versioned API, input/result JSON Schema, error registry, and event definitions define the inter-service and client contract. The API surface is specified in `contracts/openapi/bersn-api-v1.0.yaml` and calculator contract in `contracts/openapi/bersn-calc-v1.0.yaml`; payload validation semantics are captured by `contracts/jsonschema/bersn_input.schema.json` and `contracts/jsonschema/bersn_result.schema.json`.

#### User interface
The Vue 3/Vite SPA provides login, dashboard, calculation submission, and an API console. Application composition begins at `services/ui/src/App.vue`; primary user flows are implemented in `services/ui/src/views/CalculationPage.vue`, `services/ui/src/views/LoginPage.vue`, and `services/ui/src/auth.ts`. It is a client of the API rather than a calculation runtime.

#### API gateway and persistence
The Node.js API receives client requests, applies route/controller orchestration, authentication, classification, and calculation persistence. Its runtime starts at `services/api/src/server.js`; calculation request flow is centered on `services/api/src/routes/calcRoutes.js` and `services/api/src/controllers/calcController.js`. Database access is isolated through `services/api/src/db.js` and models such as `services/api/src/models/calcModel.js`.

#### Calculation engine
The Python FastAPI service is the authoritative computational boundary. `services/calc/app/main.py` exposes calculation endpoints, which compose formula modules for energy indicators, renewable contributions, weights, score, NZB, and grades. Key calculation stages are represented by `services/calc/app/formulas/eteui_formula.py`, `services/calc/app/formulas/eei_formula_bersn.py`, and `services/calc/app/formulas/grade_formula.py`.

#### Reference-data-driven formulas and traceability
Both API and calculator carry versioned lookup tables, enabling deterministic calculations against published constants and thresholds. The calculation engine loads and normalizes tables through `services/calc/app/utils/table_loader.py` and `services/calc/app/utils/lookup_normalizer.py`; it builds explainability/audit output via `services/calc/app/utils/trace_builder.py`. Core data includes `services/calc/app/data/v1.0/baseline_eui_tableA_v1.0_A1_O6_full.json` and `services/calc/app/data/v1.0/grade_thresholds.json`.

#### Data and infrastructure
PostgreSQL schema/auth evolution is managed as SQL migrations in `database/migrations/postgres/001_init.sql` and `database/migrations/postgres/002_phase1_auth.sql`. Container boundaries are defined by `services/api/Dockerfile`, `services/calc/Dockerfile`, and `infra/docker/docker-compose.yml`; Nginx deployment material resides in `infra/nginx/README.md`. The material external dependencies are PostgreSQL and the HTTP calls between UI, API, and calculator.

#### Primary flow
A user authenticates and submits building input from the SPA to the Node API. The API validates/orchestrates the request, delegates formula computation to the FastAPI calculator, persists applicable calculation/auth records in PostgreSQL, and returns contract-shaped results and classification data to the UI. Contract files are the compatibility boundary; the API-to-calc HTTP interface is the runtime boundary; PostgreSQL is the durability boundary.

Main entry points: [`services/api/src/controllers/calcController.js`](https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/controllers/calcController.js) (Calculation orchestration), [`services/calc/app/main.py`](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/main.py) (Calculator API), [`services/calc/app/formulas/eteui_formula.py`](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/formulas/eteui_formula.py) (Formula pipeline), [`services/api/src/server.js`](https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/server.js) (API server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_contracts["Versioned contracts"]
  node_contracts["API and payload contracts<br/>OpenAPI + JSON Schema"]
  node_calc_contract["Calculator API contract<br/>OpenAPI"]
end

subgraph group_ui["Vue SPA"]
  node_ui_entry["SPA entry<br/>Vue/Vite entry<br/>[main.ts]"]
  node_ui_app["App composition<br/>Vue application<br/>[App.vue]"]
  node_ui_flows["Login and calculation flows<br/>Vue views/auth"]
end

subgraph group_api["Node API"]
  node_api_server["API server<br/>Node.js runtime<br/>[server.js]"]
  node_api_calc["Calculation orchestration<br/>routes and controller<br/>[calcController.js]"]
  node_api_persistence["Calculation persistence<br/>DB access and models<br/>[calcModel.js]"]
end

subgraph group_calc["Calculation engine"]
  node_calc_main["Calculator API<br/>Python FastAPI<br/>[main.py]"]
  node_formula_pipeline["Formula pipeline<br/>energy and score formulas<br/>[eteui_formula.py]"]
  node_grade_formula["Grade calculation<br/>formula stage<br/>[grade_formula.py]"]
  node_table_loading["Lookup loading and normalization<br/>reference-data loader<br/>[table_loader.py]"]
  node_trace_output["Audit trace builder<br/>explainability output<br/>[trace_builder.py]"]
end

subgraph group_data["Data and deployment"]
  node_compose{{"Docker Compose<br/>local deployment<br/>[docker-compose.yml]"}}
  node_reference_data["Versioned reference tables<br/>calculation constants"]
  node_postgres[("PostgreSQL<br/>durable storage")]
  node_migrations["Database migrations<br/>SQL schema evolution<br/>[001_init.sql]"]
end

node_compose -->|"runs"| node_ui_entry
node_compose -->|"runs"| node_api_server
node_compose -->|"runs"| node_calc_main
node_ui_entry -->|"boots"| node_ui_app
node_ui_app -->|"hosts"| node_ui_flows
node_ui_flows -->|"HTTP requests"| node_api_server
node_contracts -.->|"client contract"| node_ui_flows
node_contracts -.->|"validates and serves"| node_api_server
node_api_server -->|"routes requests"| node_api_calc
node_api_calc -->|"calculation HTTP"| node_calc_main
node_calc_contract -.->|"delegation contract"| node_api_calc
node_calc_contract -.->|"endpoint contract"| node_calc_main
node_api_calc -->|"persists requests/results"| node_api_persistence
node_api_persistence -->|"reads and writes"| node_postgres
node_migrations -->|"evolves schema"| node_postgres
node_calc_main -->|"executes"| node_formula_pipeline
node_formula_pipeline -->|"derives grade"| node_grade_formula
node_formula_pipeline -->|"uses lookups"| node_table_loading
node_table_loading -->|"loads"| node_reference_data
node_formula_pipeline -->|"emits audit detail"| node_trace_output
node_calc_main -->|"result and trace"| node_api_calc

click node_compose "https://github.com/amuthavanmoorthi/calengine/blob/main/infra/docker/docker-compose.yml"
click node_contracts "https://github.com/amuthavanmoorthi/calengine/blob/main/contracts/openapi/bersn-api-v1.0.yaml"
click node_calc_contract "https://github.com/amuthavanmoorthi/calengine/blob/main/contracts/openapi/bersn-calc-v1.0.yaml"
click node_ui_entry "https://github.com/amuthavanmoorthi/calengine/blob/main/services/ui/src/main.ts"
click node_ui_app "https://github.com/amuthavanmoorthi/calengine/blob/main/services/ui/src/App.vue"
click node_ui_flows "https://github.com/amuthavanmoorthi/calengine/blob/main/services/ui/src/views/CalculationPage.vue"
click node_api_server "https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/server.js"
click node_api_calc "https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/controllers/calcController.js"
click node_api_persistence "https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/models/calcModel.js"
click node_calc_main "https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/main.py"
click node_formula_pipeline "https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/formulas/eteui_formula.py"
click node_grade_formula "https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/formulas/grade_formula.py"
click node_table_loading "https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/utils/table_loader.py"
click node_trace_output "https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/utils/trace_builder.py"
click node_reference_data "https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/data/v1.0/baseline_eui_tableA_v1.0_A1_O6_full.json"
click node_migrations "https://github.com/amuthavanmoorthi/calengine/blob/main/database/migrations/postgres/001_init.sql"

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_contracts,node_calc_contract toneBlue
class node_ui_entry,node_ui_app,node_ui_flows toneAmber
class node_api_server,node_api_calc,node_api_persistence toneMint
class node_calc_main,node_formula_pipeline,node_grade_formula,node_table_loading,node_trace_output toneRose
class node_compose,node_reference_data,node_postgres,node_migrations toneIndigo
```

## Components

### Versioned contracts

Compatibility boundary for clients and services.

- [API and payload contracts](https://github.com/amuthavanmoorthi/calengine/blob/main/contracts/openapi/bersn-api-v1.0.yaml): OpenAPI + JSON Schema · Versioned request, result, error, and event definitions. · `contracts/openapi/bersn-api-v1.0.yaml`
- [Calculator API contract](https://github.com/amuthavanmoorthi/calengine/blob/main/contracts/openapi/bersn-calc-v1.0.yaml): OpenAPI · `contracts/openapi/bersn-calc-v1.0.yaml`

### Vue SPA

- [SPA entry](https://github.com/amuthavanmoorthi/calengine/blob/main/services/ui/src/main.ts): Vue/Vite entry · `services/ui/src/main.ts`
- [App composition](https://github.com/amuthavanmoorthi/calengine/blob/main/services/ui/src/App.vue): Vue application · `services/ui/src/App.vue`
- [Login and calculation flows](https://github.com/amuthavanmoorthi/calengine/blob/main/services/ui/src/views/CalculationPage.vue): Vue views/auth · `services/ui/src/views/CalculationPage.vue`

### Node API

- [API server](https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/server.js): Node.js runtime · `services/api/src/server.js`
- [Calculation orchestration](https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/controllers/calcController.js): routes and controller · Validates, classifies, delegates, and shapes calculation responses. · `services/api/src/controllers/calcController.js`
- [Calculation persistence](https://github.com/amuthavanmoorthi/calengine/blob/main/services/api/src/models/calcModel.js): DB access and models · `services/api/src/models/calcModel.js`

### Calculation engine

- [Calculator API](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/main.py): Python FastAPI · Authoritative computational boundary. · `services/calc/app/main.py`
- [Formula pipeline](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/formulas/eteui_formula.py): energy and score formulas · Computes indicators, renewable contribution, score, NZB, and grades. · `services/calc/app/formulas/eteui_formula.py`
- [Grade calculation](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/formulas/grade_formula.py): formula stage · `services/calc/app/formulas/grade_formula.py`
- [Lookup loading and normalization](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/utils/table_loader.py): reference-data loader · `services/calc/app/utils/table_loader.py`
- [Audit trace builder](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/utils/trace_builder.py): explainability output · `services/calc/app/utils/trace_builder.py`

### Data and deployment

- [Docker Compose](https://github.com/amuthavanmoorthi/calengine/blob/main/infra/docker/docker-compose.yml): local deployment · Primary local service entry point. · `infra/docker/docker-compose.yml`
- [Versioned reference tables](https://github.com/amuthavanmoorthi/calengine/blob/main/services/calc/app/data/v1.0/baseline_eui_tableA_v1.0_A1_O6_full.json): calculation constants · Published baselines and grade thresholds provide deterministic inputs. · `services/calc/app/data/v1.0/baseline_eui_tableA_v1.0_A1_O6_full.json`
- **PostgreSQL**: durable storage · Stores authentication and calculation records.
- [Database migrations](https://github.com/amuthavanmoorthi/calengine/blob/main/database/migrations/postgres/001_init.sql): SQL schema evolution · `database/migrations/postgres/001_init.sql`

## Connections

- Docker Compose → SPA entry: runs
- Docker Compose → API server: runs
- Docker Compose → Calculator API: runs
- SPA entry → App composition: boots
- App composition → Login and calculation flows: hosts
- Login and calculation flows → API server: HTTP requests
- API and payload contracts → Login and calculation flows: client contract
- API and payload contracts → API server: validates and serves
- API server → Calculation orchestration: routes requests
- Calculation orchestration → Calculator API: calculation HTTP
- Calculator API contract → Calculation orchestration: delegation contract
- Calculator API contract → Calculator API: endpoint contract
- Calculation orchestration → Calculation persistence: persists requests/results
- Calculation persistence → PostgreSQL: reads and writes
- Database migrations → PostgreSQL: evolves schema
- Calculator API → Formula pipeline: executes
- Formula pipeline → Grade calculation: derives grade
- Formula pipeline → Lookup loading and normalization: uses lookups
- Lookup loading and normalization → Versioned reference tables: loads
- Formula pipeline → Audit trace builder: emits audit detail
- Calculator API → Calculation orchestration: result and trace
