# abcd771321-crypto/crypto-prediction architecture

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

- Interactive diagram: https://gitdiagram.com/abcd771321-crypto/crypto-prediction
- Repository: https://github.com/abcd771321-crypto/crypto-prediction

## Overview

##### Project Type and Purpose
- This is a monolithic full-stack web application built on Flask.
- It serves server-rendered HTML pages with interactive Plotly charts and offers ML-driven price predictions (Linear, RF, SVR, LSTM), currency conversion, comparison, and technical indicators for cryptocurrencies.
- Data sources include local CSV files (historical data) and a remote CoinGecko-style API.

##### Top-Level Components
- **Client (Browser)**
- Requests HTML pages, submits forms.
- **Flask Application (WSGI process)**
- App factory (`create_app()`), configuration via `.env`, security headers, logging.
- Blueprints (`routes.py`) handling URL groups: main, detail, technical, predict, convert, compare, update.
- **Service Layer (`services.py`)**
- `load_crypto_data()` reads CSV and merges with cached JSON.
- Lazy singletons for in-memory data and currency converter.
- `get_crypto_data()`, `update_prices_in_place()` for safe in-memory price updates.
- **Model Layer (`models/`)**
- **API Client (`api_client.py`)**: `requests.Session` with Retry and timeout, disk-cache of JSON responses.
- **Prediction Models (`prediction_models.py`)**: data preparation, ensemble fallback logic.
- **Technical Indicators (`technical_indicators.py`)**: computes RSI, MACD, Bollinger Bands, etc.
- **Crypto Comparison & Converter**.
- **Data Stores**
- CSV files under `coin files csv/` (source of historical prices).
- JSON caches under `crypto_analysis/cache/` (latest prices, descriptions, historical 30-day data).
- **Templates & Static**
- HTML templates (`templates/`) for each page.
- Static assets (`static/`) for CSS/JS (Plotly, day/night toggle).
- **Configuration & Deployment**
- Environment variables via `.env` (SECRET_KEY, FLASK_DEBUG, API_HTTP_TIMEOUT).
- `Procfile` for Heroku/Gunicorn.
- Logging to `crypto_analysis/app.log` with rotating handler.

##### Interactions and Data Flow
- Browser → HTTP request → Flask (Gunicorn)
- Flask routes invoke Service Layer → loads CSV or reads JSON cache
- Service may call Model Layer → for predictions or indicators
- Model Layer may call API Client → external API → updates JSON cache on disk
- Service returns data → Flask renders template with Plotly graphs
- Response → Browser

##### Architectural Patterns & Design Principles
- **App Factory**: isolates configuration and app creation.
- **Blueprints**: modular routing by feature.
- **Layered Architecture**: Presentation (Blueprints + Templates), Application (Services), Domain (Models), Infrastructure (File system cache, external API).
- **Lazy Singletons**: for in-memory data and converter to minimize startup cost.
- **Retry/Timeout**: ensures robust external HTTP calls.
- **Fallback Ensemble Logic**: resilience in prediction (LSTM → RF → Linear).

##### Essential Elements
- **Client** (Browser)
- **Web Server** (Gunicorn/Flask)
- **Flask App Factory** & **Blueprints**
- **Service Layer**
- **Model Layer** (API Client, Prediction, Technical Indicators)
- **Data Stores** (CSV files, JSON cache)
- **External API**
- **Templates** & **Static Assets**
- **Config (.env)** & **Logging**

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        Client["Client (Browser)"]:::external
        Templates["HTML Templates"]:::presentation
        Static["Static Assets"]:::presentation
    end

    %% Web Server & Application Layer
    subgraph "Web Server & Application"
        direction TB
        Gunicorn["Gunicorn/Flask WSGI"]:::app
        AppFactory["Flask App Factory & Configuration"]:::app
        Blueprints["Blueprints (routes)"]:::app
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        Service["Service Layer"]:::service
    end

    %% Domain / Model Layer
    subgraph "Domain / Models"
        direction TB
        APIClient["API Client"]:::domain
        Prediction["Prediction Models"]:::domain
        Technical["Technical Indicators"]:::domain
        Comparison["Crypto Comparison Logic"]:::domain
        Converter["Currency Converter Logic"]:::domain
    end

    %% Infrastructure & Data Stores
    subgraph "Infrastructure & Data Stores"
        direction TB
        CSV["Historical CSV Data Store"]:::infra
        JSONCache["JSON Cache Data Store"]:::infra
        ExternalAPI["External Crypto API"]:::external
        ConfigDeploy["Config & Deployment (.env, Procfile)"]:::infra
        Logging["Logging Output"]:::infra
    end

    %% Cross-layer Data Flow
    Client -->|"GET /crypto/<symbol>"| Gunicorn
    Gunicorn --> AppFactory
    AppFactory --> Blueprints
    Blueprints -->|"invoke service"| Service
    Service -->|"load_csv()"| CSV
    Service -->|"read/write cache"| JSONCache
    Service -->|"predict_crypto_price()"| Prediction
    Service -->|"compute_indicators()"| Technical
    Service -->|"compare_cryptos()"| Comparison
    Service -->|"convert_currency()"| Converter
    Prediction -->|"calls API"| APIClient
    APIClient -->|"HTTP Request / Retry"| ExternalAPI
    APIClient -->|"update cache"| JSONCache
    Blueprints -->|"render"| Templates
    Blueprints -->|"serve"| Static
    Templates --> Client
    Static --> Client

    %% Cross-cutting
    ConfigDeploy --> Gunicorn
    ConfigDeploy --> AppFactory
    Gunicorn --> Logging
    Service --> Logging
    APIClient --> Logging

    %% Click Events
    click AppFactory "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/app.py"
    click Blueprints "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/routes.py"
    click Service "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/services.py"
    click APIClient "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/models/api_client.py"
    click Prediction "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/models/prediction_models.py"
    click Technical "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/models/technical_indicators.py"
    click Comparison "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/models/crypto_comparison.py"
    click Converter "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/models/currency_converter.py"
    click CSV "https://github.com/abcd771321-crypto/crypto-prediction/tree/main/coin files csv/"
    click JSONCache "https://github.com/abcd771321-crypto/crypto-prediction/tree/main/crypto_analysis/cache/"
    click Templates "https://github.com/abcd771321-crypto/crypto-prediction/tree/main/crypto_analysis/templates/"
    click Static "https://github.com/abcd771321-crypto/crypto-prediction/tree/main/crypto_analysis/static/"
    click ConfigDeploy "https://github.com/abcd771321-crypto/crypto-prediction/tree/main/Procfile"
    click Logging "https://github.com/abcd771321-crypto/crypto-prediction/blob/main/crypto_analysis/app.log"

    %% Styles
    classDef presentation fill:#D0E6FF,stroke:#0366D6,color:#0366D6;
    classDef app fill:#DFFFE0,stroke:#2E8B57,color:#2E8B57;
    classDef service fill:#E0FFE0,stroke:#228B22,color:#228B22;
    classDef domain fill:#FFE8C0,stroke:#CD8500,color:#CD8500;
    classDef infra fill:#F0F0F0,stroke:#808080,color:#808080;
    classDef external fill:#FFE0E0,stroke:#D63031,color:#D63031;
```
