# thomas131104/regressionapi architecture

> Architecture diagram of the GitHub repository thomas131104/regressionapi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thomas131104/regressionapi
- Repository: https://github.com/thomas131104/regressionapi

## Overview

##### Project type and purpose
- This is a backend‐only RESTful API (“Regression API”) built with FastAPI that exposes multiple endpoints for running and evaluating regression models (Linear, Ridge, Lasso, ElasticNet, SVM, Decision Tree, Random Forest, KNN, etc.).
- It persists request results to MongoDB, automatically manages DB connections, and includes automated tests via pytest.

##### File structure
- Top level:
- main.py – entry point launching the FastAPI app (Hypercorn/Uvicorn)
- app/ – contains application code
- run/ – OS‐specific startup scripts
- render.yaml, requirements.txt, pytest.ini – config and dependencies
- test/ – pytest unit and integration tests
- app/ substructure:
- app/app.py – builds the FastAPI instance
- app/lifespan.py – startup/shutdown events for DB lifecycle
- app/middleware.py – any request/response middleware
- app/router/ – REST route handlers grouped by feature: best_model, option, history, stack_model, utils
- app/schemas/ – Pydantic models defining request/response payloads
- app/utils/machine_learning/ – core ML logic separated by phase: preprocessing, training, classification, running specific model flows
- app/database/ – MongoDB connection and CRUD operations

##### Extract insights from README
- REST endpoints under /regression/option, /regression/best-model-without-prediction, /regression/{...}/history
- Core features: model training, prediction, scoring (RMSE, MAE, R²), history persistence (with an auto-deletion policy), environment‐based DB config
- Tech stack: Python, FastAPI, Pydantic, MongoDB, pytest, Hypercorn/Uvicorn

a. Components
1. External Client(s) (Postman, frontend, other services)
2. HTTP Server (Hypercorn/Uvicorn process running main.py → app.app)
3. FastAPI Application
- Routers (controllers)
- Middleware
- Lifespan handlers
4. Business Logic Layer (service) in app/utils/machine_learning
5. Data Schemas (Pydantic models in app/schemas)
6. Persistence Layer (MongoDB driver in app/database/database.py)
7. MongoDB Database
8. Test Suite (pytest) – separate but helpful to show CI or local testing
9. Configuration/Env (.env → URL for MongoDB)
b. Relationships & data flows
1. Client → HTTP request → HTTP Server
2. Server passes through Middleware → Router
3. Router validates and parses JSON payload into Pydantic schema
4. Router invokes ML service methods (preprocessing, training, predicting)
5. ML services return results to Router
6. Router invokes Persistence layer to insert or query history in MongoDB
7. Persistence layer issues commands to MongoDB and returns stored documents
8. Router constructs response schema and returns JSON → Client
c. Architectural patterns & principles
- Layered architecture (Controller → Service → Repository)
- Separation of concerns (routers vs. schemas vs. business logic vs. database access)
- Environment‐driven configuration and automatic resource management via lifespan
- Testability via dependency injection (FastAPI’s dependency system)
d. Key technologies & libraries
- FastAPI (web framework)
- Pydantic (data validation / serialization)
- Hypercorn / Uvicorn (ASGI server)
- pymongo / motor (MongoDB driver)
- scikit-learn (model implementations, metrics)
- pytest (unit/integration testing)

##### Additional notes
- Initialization (lifespan) flow separately if desired: connect to MongoDB at startup, disconnect at shutdown

## Diagram (Mermaid)

```mermaid
graph TB
    EXTERNAL["External Client(s)"]:::external
    CONFIG["Configuration (.env, render.yaml)"]:::config
    EXTERNAL --> SERVER
    CONFIG --> SERVER

    SERVER["HTTP Server (main.py)"]:::api
    click SERVER "https://github.com/thomas131104/regressionapi/blob/master/main.py"

    SERVER --> APP
    APP["FastAPI Application"]:::api
    click APP "https://github.com/thomas131104/regressionapi/blob/master/app/app.py"

    subgraph "App Lifecycle"
        START["Lifespan Handlers\n(db connect/disconnect)"]:::api
        MIDDLE["Middleware"]:::api
        click START "https://github.com/thomas131104/regressionapi/blob/master/app/lifespan.py"
        click MIDDLE "https://github.com/thomas131104/regressionapi/blob/master/app/middleware.py"
    end

    APP --> START
    APP --> MIDDLE
    MIDDLE --> MAIN_ROUTER

    subgraph "Routers (/regression)" 
        MAIN_ROUTER["Main Router"]:::api
        click MAIN_ROUTER "https://github.com/thomas131104/regressionapi/blob/master/app/router/main.py"
        OPTION["/regression/option"]:::api
        BEST["/regression/best_model"]:::api
        STACK["/regression/stack_model"]:::api
        HISTORY["/regression/history"]:::api
        UTILROUT["Utility Routes"]:::api

        click OPTION "https://github.com/thomas131104/regressionapi/blob/master/app/router/option.py"
        click BEST "https://github.com/thomas131104/regressionapi/blob/master/app/router/best_model.py"
        click STACK "https://github.com/thomas131104/regressionapi/blob/master/app/router/stack_model.py"
        click HISTORY "https://github.com/thomas131104/regressionapi/blob/master/app/router/history.py"
        click UTILROUT "https://github.com/thomas131104/regressionapi/blob/master/app/router/utils.py"
    end

    MAIN_ROUTER --> OPTION
    MAIN_ROUTER --> BEST
    MAIN_ROUTER --> STACK
    MAIN_ROUTER --> HISTORY
    MAIN_ROUTER --> UTILROUT

    OPTION --> SCHEMAS
    BEST --> SCHEMAS
    STACK --> SCHEMAS
    HISTORY --> SCHEMAS
    UTILROUT --> SCHEMAS

    subgraph "Schemas (Pydantic Models)"
        OPTION_S["Option Schemas"]:::schema
        BEST_S["Best Model Schemas"]:::schema
        STACK_S["Stack Model Schemas"]:::schema
        UTIL_S["Shared/Utils Schemas"]:::schema

        click OPTION_S "https://github.com/thomas131104/regressionapi/blob/master/app/schemas/option.py"
        click BEST_S "https://github.com/thomas131104/regressionapi/blob/master/app/schemas/best_model.py"
        click STACK_S "https://github.com/thomas131104/regressionapi/blob/master/app/schemas/stack_model.py"
        click UTIL_S "https://github.com/thomas131104/regressionapi/blob/master/app/schemas/utils.py"
    end

    SCHEMAS --> SERVICES

    subgraph "Service Layer (ML Logic)"
        PREP["Data Preprocessing"]:::service
        TRAIN["Model Training/Evaluation"]:::service
        INDEX["Model Index/Registry"]:::service
        RUN_OPT["Run Option Flow"]:::service
        RUN_BEST["Run Best Model Flow"]:::service
        RUN_STACK["Run Stack Model Flow"]:::service
        CLSF["Classification Logic"]:::service
        LOGIC["Additional ML Notes"]:::service

        click PREP "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/data_preprocessing.py"
        click TRAIN "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/model_training.py"
        click INDEX "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/model_index.py"
        click RUN_OPT "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/run_option.py"
        click RUN_BEST "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/run_best_model.py"
        click RUN_STACK "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/run_stack_model.py"
        click CLSF "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/classify.py"
        click LOGIC "https://github.com/thomas131104/regressionapi/blob/master/app/utils/machine_learning/logic_stack.txt"
    end

    SERVICES --> PERSISTENCE

    subgraph "Persistence Layer"
        DBPKG["DB Package Init"]:::service
        DBMOD["MongoDB Access (database.py)"]:::service
        click DBPKG "https://github.com/thomas131104/regressionapi/blob/master/app/database/__init__.py"
        click DBMOD "https://github.com/thomas131104/regressionapi/blob/master/app/database/database.py"
    end

    PERSISTENCE --> DB

    DB["MongoDB Database"]:::db

    DB --> MAIN_ROUTER

    APP --> TEST_SUITE

    subgraph "Testing & CI"
        TEST_SUITE["pytest Suite"]:::test
        click TEST_SUITE "https://github.com/thomas131104/regressionapi/tree/master/test/"
    end

    APP --> SCRIPTS

    subgraph "Scripts & Config"
        REQ["requirements.txt"]:::script
        REND["render.yaml"]:::script
        PYINI["pytest.ini"]:::script
        LINUX["run/linux.sh"]:::script
        WIN["run/windows.ps1"]:::script

        click REQ "https://github.com/thomas131104/regressionapi/blob/master/requirements.txt"
        click REND "https://github.com/thomas131104/regressionapi/blob/master/render.yaml"
        click PYINI "https://github.com/thomas131104/regressionapi/blob/master/pytest.ini"
        click LINUX "https://github.com/thomas131104/regressionapi/blob/master/run/linux.sh"
        click WIN "https://github.com/thomas131104/regressionapi/blob/master/run/windows.ps1"
    end

    classDef api fill:#c3dbf0,stroke:#2196F3,stroke-width:2px
    classDef service fill:#d7f0d3,stroke:#4CAF50,stroke-width:2px
    classDef db fill:#ffe5b4,stroke:#FF9800,stroke-width:2px
    classDef config fill:#eeeeee,stroke:#9E9E9E,stroke-width:2px
    classDef external fill:#e1bee7,stroke:#9C27B0,stroke-width:2px
    classDef schema fill:#f0f4c3,stroke:#CDDC39,stroke-width:2px
    classDef test fill:#fff9c4,stroke:#FFEB3B,stroke-width:2px
    classDef script fill:#f5f5f5,stroke:#607D8B,stroke-width:2px
```
