# wenjuinchuah/stock_view_backend architecture

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

- Interactive diagram: https://gitdiagram.com/wenjuinchuah/stock_view_backend
- Repository: https://github.com/wenjuinchuah/stock_view_backend

## Overview

##### Project Type and Purpose
- This repository implements a standalone backend microservice (REST API) for the “Stock View” system.
- It is built with FastAPI (uvicorn ASGI server), SQLAlchemy (models/crud), and orchestrated via Docker Compose. Its core responsibility is to expose endpoints for stock data (price lists, stock details, screeners), persist information in a database, and fetch or scrape external stock data.

##### File Structure Analysis
Top-level artifacts:
- Dockerfile / docker-compose.yml / start.sh – containerization and orchestration
- requirements.txt – Python dependencies
- app/ – application code
├─ api/
├─ endpoints/ – FastAPI route handlers (price_list.py, stock.py, stock_screener.py)
├─ crud/ – database access & business logic (price_list.py, stock_scrapper.py, etc.)
├─ models/ – SQLAlchemy ORM definitions (Stock, PriceList, Screener models)
└─ dependencies/ – shared DI providers (database session)
├─ assets/ – static data (stock_list.csv)
└─ main.py – FastAPI app instantiation and router mounting

##### README Insights
- Confirms use of Python 3.8+, uvicorn, virtualenv, Docker Compose.
- No external services except the database (likely Postgres/MySQL) and possibly public stock-data sources invoked by scrapers.

a. Main Components
- Client(s) – e.g., Web frontend, Postman
- FastAPI App – ASGI server (uvicorn)
– Router Layer: app/main.py registers endpoints
– Endpoints (app/api/endpoints/*.py)
– CRUD Layer (app/api/crud/*.py)
– Models (app/api/models/*.py)
– Dependencies (DB session factory)
- Database – SQL database container (PostgreSQL or MySQL)
- External Data Sources – HTTP stock APIs or scraping targets
- Static Asset – stock_list.csv loader

b. Interactions
1. Client → HTTP(S) → Uvicorn/ASGI
2. Router dispatches to endpoint function
3. Endpoint invokes CRUD services (business logic)
4. CRUD layer reads/writes via SQLAlchemy ORM to Database
5. For real-time data: CRUD calls scrapers / external HTTP APIs
6. Responses flow back up to client

c. Architectural Patterns
- Layered Architecture (Presentation → Controller/Router → Service/CRUD → Data)
- Dependency Injection via FastAPI’s Depends (database sessions, configs)
- Containerization (Docker image + Compose network)

d. Key Technologies
- Python 3.8+, FastAPI, Uvicorn
- SQLAlchemy ORM
- Docker & Docker Compose
- External HTTP libraries (e.g., requests or httpx for scrapers)

##### Detail Checklist
- Three endpoint groups (price_list, stock, stock_screener) under the Router.
- Dependency Injector for DB sessions.
- Static CSV asset loader.
- Docker Compose context: network and volume mappings.
- Call out any background tasks or cron-style scrapers if applicable.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Docker Compose Context
    subgraph "Docker Compose" 
        direction TB
        AppContainer["StockView App"]:::container
        DBContainer["Database (PostgreSQL)"]:::db
    end

    %% Client
    Client["Client (Web Frontend / Postman)"]:::client

    %% App Container Components
    subgraph "App Container"
        direction TB
        Uvicorn["Uvicorn ASGI Server"]:::appserver
        Main["FastAPI App & Router"]:::appserver
        subgraph "Router Layer"
            direction TB
            PriceListEP["Price List Endpoint"]:::endpoint
            StockEP["Stock Endpoint"]:::endpoint
            ScreenerEP["Stock Screener Endpoint"]:::endpoint
        end
        subgraph "CRUD Layer"
            direction TB
            PriceListCRUD["PriceList CRUD"]:::crud
            StockCRUD["Stock CRUD"]:::crud
            ScrapperCRUD["Stock Scrapper"]:::crud
            ScreenerCRUD["Stock Screener CRUD"]:::crud
            UtilsCRUD["Utils"]:::crud
        end
        subgraph "Models"
            direction TB
            BaseModel["base.py"]:::model
            PriceListModel["price_list.py"]:::model
            StockModel["stock.py"]:::model
            IndicatorModel["stock_indicator.py"]:::model
            ScreenerModel["stock_screener.py"]:::model
            TickerModel["stock_ticker.py"]:::model
        end
        Dependency["DB Session Provider"]:::dependency
        AssetLoader["Static CSV Loader"]:::asset
    end

    %% External and Database
    External["External Stock Data APIs / Scraping Targets"]:::external
    CSVFile["stock_list.csv"]:::asset

    %% Connections
    Client -->|"HTTP(S) request"| Uvicorn
    Uvicorn --> Main
    Main -->|"router dispatch"| PriceListEP
    Main -->|"router dispatch"| StockEP
    Main -->|"router dispatch"| ScreenerEP

    PriceListEP -->|"calls"| PriceListCRUD
    StockEP -->|"calls"| StockCRUD
    ScreenerEP -->|"calls"| ScreenerCRUD

    PriceListCRUD -->|"uses ORM"| BaseModel
    PriceListCRUD --> PriceListModel

    StockCRUD --> StockModel
    StockCRUD --> IndicatorModel
    StockCRUD --> TickerModel

    ScreenerCRUD --> ScreenerModel
    ScrapperCRUD -->|"fetch real-time"| External

    UtilsCRUD --> Dependency
    Dependency -->|"provides session"| PriceListCRUD
    Dependency --> StockCRUD
    Dependency --> ScreenerCRUD
    Dependency --> ScrapperCRUD

    AssetLoader --> CSVFile
    AssetLoader --> PriceListCRUD

    PriceListCRUD -->|"SQL"| DBContainer
    StockCRUD -->|"SQL"| DBContainer
    ScreenerCRUD -->|"SQL"| DBContainer

    ScrapperCRUD -->|"HTTP"| External

    AppContainer --> DBContainer
    AppContainer --> External

    Client --- AppContainer

    %% Click Events
    click AppContainer "https://github.com/wenjuinchuah/stock_view_backend/blob/main/docker-compose.yml"
    click AppContainer "https://github.com/wenjuinchuah/stock_view_backend/tree/main/Dockerfile"
    click AppContainer "https://github.com/wenjuinchuah/stock_view_backend/blob/main/start.sh"
    click CSVFile "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/assets/stock_list.csv"
    click Main "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/main.py"
    click PriceListEP "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/endpoints/price_list.py"
    click StockEP "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/endpoints/stock.py"
    click ScreenerEP "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/endpoints/stock_screener.py"
    click PriceListCRUD "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/crud/price_list.py"
    click StockCRUD "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/crud/stock.py"
    click ScrapperCRUD "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/crud/stock_scrapper.py"
    click ScreenerCRUD "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/crud/stock_screener.py"
    click UtilsCRUD "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/crud/utils.py"
    click BaseModel "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/models/base.py"
    click PriceListModel "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/models/price_list.py"
    click StockModel "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/models/stock.py"
    click IndicatorModel "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/models/stock_indicator.py"
    click ScreenerModel "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/models/stock_screener.py"
    click TickerModel "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/models/stock_ticker.py"
    click Dependency "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/api/dependencies/database.py"
    click CSVFile "https://github.com/wenjuinchuah/stock_view_backend/blob/main/app/assets/stock_list.csv"

    %% Styles
    classDef container fill:#FFE4B5,stroke:#E69A00,stroke-width:2px
    classDef client fill:#B3E5FC,stroke:#0288D1,stroke-width:2px
    classDef appserver fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px
    classDef endpoint fill:#FFF9C4,stroke:#F9A825,stroke-width:2px
    classDef crud fill:#E1BEE7,stroke:#6A1B9A,stroke-width:2px
    classDef model fill:#FFCDD2,stroke:#C62828,stroke-width:2px
    classDef dependency fill:#D1C4E9,stroke:#512DA8,stroke-width:2px
    classDef asset fill:#CFD8DC,stroke:#546E7A,stroke-width:2px
    classDef db fill:#B2DFDB,stroke:#00796B,stroke-width:2px
    classDef external fill:#FFE0B2,stroke:#EF6C00,stroke-width:2px
```
