# mohithkudumu/campus-carbon-pulse architecture

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

- Interactive diagram: https://gitdiagram.com/mohithkudumu/campus-carbon-pulse
- Repository: https://github.com/mohithkudumu/campus-carbon-pulse

## Overview

#### Purpose and Entry Points
Campus Carbon Pulse is a campus carbon-emissions digital twin: it forecasts 24 hours of per-building emissions, displays them on an interactive map, records threshold alerts, and generates sustainability insights. Browser entry is `src/main.tsx`, routing/app composition begins in `src/App.tsx`, and the main dashboard is `src/pages/Index.tsx`. The backend API application enters through `backend/main.py`.

#### Frontend Dashboard and State Flow
The React/TypeScript frontend runs under Vite and renders the operational dashboard from `src/pages/Index.tsx`. User time selection flows through `src/components/TimeSlider.tsx`; the selected hour drives backend emissions retrieval and updates map, aggregate analytics, and alert-oriented UI. `src/pages/Insights.tsx` separately requests and renders the AI report.

#### Geospatial Digital Twin
`src/components/CampusMap.tsx` uses MapLibre GL to render campus building GeoJSON as 3D structures, with color/state derived from forecast emissions. The map’s structural source is `public/campus.json`; backend-side `backend/campus.json` is the server-managed counterpart, described as receiving live-data injection. The map boundary is GeoJSON/building identifiers: forecast results must match these IDs.

#### Analytics and Alert Operations
`src/components/AnalyticsPanel.tsx` presents campus totals, trends, and historical charts, while `src/components/AlertsBell.tsx` surfaces unresolved threshold violations and supports resolution. Backend routes in `backend/main.py` expose hour-specific forecasts, historical aggregates, unresolved alerts, and alert resolution. This is a REST-over-HTTP boundary with CORS between Vite’s browser runtime and FastAPI.

#### Forecast Generation Pipeline
On FastAPI startup, `backend/main.py` initializes persistence and invokes forecast generation in `backend/forecast.py`. The forecaster loads one TensorFlow/Keras LSTM and one joblib scaler per monitored building from `backend/models/`, applies them to historical inputs from `backend/snuc_carbon_year_2025.csv`, and produces 24 hourly predictions. Outputs are written both as a file cache, `backend/emissions.json`, and to SQLite for API retrieval.

#### Persistence, Thresholds, and Historical Data
SQLite is accessed through `backend/database.py` and stored in `backend/database.db`. It persists predicted emissions, peak alerts with resolution status, and daily cached AI insight payloads. Forecast generation is also the alert-production boundary: values exceeding configured thresholds create `peak_alerts` records; frontend resolution mutates only alert status through the API.

#### AI Insights and External Services
`backend/main.py` serves the insight workflow, with supporting legacy logic in `backend/insights.py`. It composes emission-derived context, calls Google Gemini (requiring `GEMINI_API_KEY` from `.env`), and caches the structured response by date in SQLite to limit API consumption. Major runtimes are React/Vite in Node.js, FastAPI/Uvicorn in Python, TensorFlow/Keras for inference, and local SQLite; MapLibre and Gemini are the material external services.

Main entry points: [`backend/main.py`](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/main.py) (FastAPI application), [`src/pages/Index.tsx`](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/pages/Index.tsx) (Operations dashboard), [`backend/forecast.py`](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/forecast.py) (Forecast generator), [`src/App.tsx`](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/App.tsx) (App routing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser dashboard"]
  node_browser_entry["React/Vite entry<br/>TypeScript entry<br/>[main.tsx]"]
  node_app["App routing<br/>React composition<br/>[App.tsx]"]
  node_dashboard["Operations dashboard<br/>React page<br/>[Index.tsx]"]
  node_time_slider["Hour selector<br/>React component<br/>[TimeSlider.tsx]"]
  node_campus_map["3D campus map<br/>MapLibre component<br/>[CampusMap.tsx]"]
  node_analytics["Analytics panel<br/>React component<br/>[AnalyticsPanel.tsx]"]
  node_alerts_ui["Alerts bell<br/>React component<br/>[AlertsBell.tsx]"]
  node_insights_page["Insights page<br/>React page<br/>[Insights.tsx]"]
end

subgraph group_backend["FastAPI backend"]
  node_api["FastAPI application<br/>REST API<br/>[main.py]"]
  node_forecast["Forecast generator<br/>LSTM inference<br/>[forecast.py]"]
  node_database_access["Database layer<br/>SQLite access<br/>[database.py]"]
  node_legacy_insights["Insight support<br/>AI workflow<br/>[insights.py]"]
end

subgraph group_data["Local data &amp; persistence"]
  node_building_geojson["Building GeoJSON<br/>map source<br/>[campus.json]"]
  node_server_campus["Server campus data<br/>live-injected GeoJSON<br/>[campus.json]"]
  node_historical_csv["Historical emissions<br/>CSV input"]
  node_models["Building models<br/>Keras models + scalers"]
  node_emissions_cache["Emissions cache<br/>forecast JSON<br/>[emissions.json]"]
  node_sqlite[("Operational database<br/>SQLite<br/>[database.db]")]
end

subgraph group_external["External services"]
  node_gemini{{"Google Gemini<br/>AI API"}}
end

node_browser_entry -->|"mounts"| node_app
node_app -->|"routes to"| node_dashboard
node_app -->|"routes to"| node_insights_page
node_time_slider -->|"selects hour"| node_dashboard
node_dashboard -->|"renders"| node_campus_map
node_dashboard -->|"renders"| node_analytics
node_dashboard -->|"renders"| node_alerts_ui
node_campus_map -->|"loads buildings"| node_building_geojson
node_dashboard -->|"forecast, history, alerts HTTP"| node_api
node_insights_page -->|"insights HTTP"| node_api
node_alerts_ui -->|"resolve alert HTTP"| node_api
node_api -->|"runs at startup"| node_forecast
node_forecast -->|"loads LSTM and scaler"| node_models
node_forecast -->|"reads inputs"| node_historical_csv
node_forecast -->|"writes forecasts"| node_emissions_cache
node_forecast -->|"persists forecasts and alerts"| node_database_access
node_api -->|"queries and updates"| node_database_access
node_database_access -->|"reads/writes"| node_sqlite
node_api -->|"builds insight context"| node_legacy_insights
node_legacy_insights -->|"requests report"| node_gemini
node_api -.->|"manages live data"| node_server_campus

click node_browser_entry "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/main.tsx"
click node_app "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/App.tsx"
click node_dashboard "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/pages/Index.tsx"
click node_time_slider "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/TimeSlider.tsx"
click node_campus_map "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/CampusMap.tsx"
click node_analytics "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/AnalyticsPanel.tsx"
click node_alerts_ui "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/AlertsBell.tsx"
click node_insights_page "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/pages/Insights.tsx"
click node_api "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/main.py"
click node_forecast "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/forecast.py"
click node_database_access "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/database.py"
click node_legacy_insights "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/insights.py"
click node_building_geojson "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/public/campus.json"
click node_server_campus "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/campus.json"
click node_historical_csv "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/snuc_carbon_year_2025.csv"
click node_emissions_cache "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/emissions.json"
click node_sqlite "https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/database.db"

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_browser_entry,node_app,node_dashboard,node_time_slider,node_campus_map,node_analytics,node_alerts_ui,node_insights_page toneBlue
class node_api,node_forecast,node_database_access,node_legacy_insights toneAmber
class node_building_geojson,node_server_campus,node_historical_csv,node_models,node_emissions_cache,node_sqlite toneMint
class node_gemini toneRose
```

## Components

### Browser dashboard

- [React/Vite entry](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/main.tsx): TypeScript entry · `src/main.tsx`
- [App routing](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/App.tsx): React composition · `src/App.tsx`
- [Operations dashboard](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/pages/Index.tsx): React page · `src/pages/Index.tsx`
- [Hour selector](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/TimeSlider.tsx): React component · Selected hour drives forecast retrieval. · `src/components/TimeSlider.tsx`
- [3D campus map](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/CampusMap.tsx): MapLibre component · `src/components/CampusMap.tsx`
- [Analytics panel](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/AnalyticsPanel.tsx): React component · `src/components/AnalyticsPanel.tsx`
- [Alerts bell](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/components/AlertsBell.tsx): React component · `src/components/AlertsBell.tsx`
- [Insights page](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/src/pages/Insights.tsx): React page · `src/pages/Insights.tsx`

### FastAPI backend

- [FastAPI application](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/main.py): REST API · Serves forecasts, history, alerts, and insights. · `backend/main.py`
- [Forecast generator](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/forecast.py): LSTM inference · Startup job produces 24-hour building forecasts and threshold alerts. · `backend/forecast.py`
- [Database layer](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/database.py): SQLite access · `backend/database.py`
- [Insight support](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/insights.py): AI workflow · `backend/insights.py`

### Local data & persistence

- [Building GeoJSON](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/public/campus.json): map source · Building IDs must match forecast results. · `public/campus.json`
- [Server campus data](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/campus.json): live-injected GeoJSON · `backend/campus.json`
- [Historical emissions](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/snuc_carbon_year_2025.csv): CSV input · `backend/snuc_carbon_year_2025.csv`
- **Building models**: Keras models + scalers · One LSTM and scaler per monitored building.
- [Emissions cache](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/emissions.json): forecast JSON · `backend/emissions.json`
- [Operational database](https://github.com/mohithkudumu/campus-carbon-pulse/blob/main/backend/database.db): SQLite · Stores forecasts, peak alerts, and daily insight cache. · `backend/database.db`

### External services

- **Google Gemini**: AI API

## Connections

- React/Vite entry → App routing: mounts
- App routing → Operations dashboard: routes to
- App routing → Insights page: routes to
- Hour selector → Operations dashboard: selects hour
- Operations dashboard → 3D campus map: renders
- Operations dashboard → Analytics panel: renders
- Operations dashboard → Alerts bell: renders
- 3D campus map → Building GeoJSON: loads buildings
- Operations dashboard → FastAPI application: forecast, history, alerts HTTP · CORS REST boundary.
- Insights page → FastAPI application: insights HTTP
- Alerts bell → FastAPI application: resolve alert HTTP
- FastAPI application → Forecast generator: runs at startup
- Forecast generator → Building models: loads LSTM and scaler
- Forecast generator → Historical emissions: reads inputs
- Forecast generator → Emissions cache: writes forecasts
- Forecast generator → Database layer: persists forecasts and alerts
- FastAPI application → Database layer: queries and updates
- Database layer → Operational database: reads/writes
- FastAPI application → Insight support: builds insight context
- Insight support → Google Gemini: requests report
- FastAPI application → Server campus data: manages live data
