Skip to main content

System Architecture

Overviewโ€‹

AirSafeNet uses a 3-layer architecture with a cache-based AI pipeline:

External APIs (Open-Meteo + OpenAQ)
โ†“
AI Server โ€” FastAPI :8000
Ensemble Model | APScheduler 60min | Anomaly | Heatmap
โ†“ REST/JSON
Backend โ€” ASP.NET Core 8 :7276
JWT Auth | Activity | Notifications | AI Assistant
โ†“ REST/JWT โ†˜ PostgreSQL 16
Frontend โ€” React 18 :5173
Dashboard | Activity | Heatmap | Assistant | Guide

Core Principle: Cache-Based Architectureโ€‹

The most important design decision: the AI model never runs per-request.

APScheduler
โ””โ”€โ”€ run_compute() every 60 min
โ”œโ”€โ”€ build_ensemble_forecast_df(days=7) โ†’ forecast_cache.csv
โ”œโ”€โ”€ history_range_payload(days=30) โ†’ history_cache.csv
โ””โ”€โ”€ get_ensemble_current_snapshot() โ†’ current_cache.json

All endpoints READ from cache โ†’ response time < 50ms

Benefits:

  • Zero latency for users โ€” no model inference on request
  • Stable even under high traffic
  • Admin can trigger manual recompute anytime via POST /api/admin/compute

Layer 1 โ€” AI Server (FastAPI)โ€‹

ModuleFilePurpose
API routesapp/api.pyAll FastAPI endpoints
Main modelapp/predict.pyRandom Forest inference
Ensembleapp/ensemble_predict.pyRF + ARIMA + XGBoost
Cache pipelineapp/cache_manager.py60-min compute cycle
Anomalyapp/anomaly_detector.pySpike detection + XAI
Heatmapapp/districts.py22-district parallel compute
Schedulerapp/scheduler.pyAPScheduler job
Featuresapp/features.pyLag, rolling, cyclical
Dataapp/data_loader.pyOpen-Meteo + OpenAQ fetch

Layer 2 โ€” Backend (ASP.NET Core 8)โ€‹

ControllerEndpoint prefixPurpose
AirController/api/airCurrent, forecast, history, explain
DashboardController/api/dashboardSummary, chart, full
ActivityController/api/activityCRUD + risk scoring
AnomalyController/api/anomalyProxy to Python
NotificationController/api/notificationAlert dispatch + history
AssistantController/api/assistantGemini AI chat
AdminController/api/adminCache trigger, status

Layer 3 โ€” Frontend (React 18)โ€‹

PageRouteDescription
Dashboard/dashboardAQI + forecast + anomaly
Activity/activitySmart planner
Heatmap/heatmap22-district SVG map
Assistant/assistantAI chat
Guide/guidePM2.5 education
Preferences/preferencesHealth group settings
Admin/adminCache management

Database Tablesโ€‹

TablePurpose
UsersAccounts + JWT auth
UserPreferencesHealth group, notification settings
UserActivitySchedulesPersonal activity calendar
ChatConversationsAI assistant history
ChatMessagesMessage content + metadata
AlertLogsNotification dispatch records