# pooriat/stockup architecture

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

- Interactive diagram: https://gitdiagram.com/pooriat/stockup
- Repository: https://github.com/pooriat/stockup

## Overview

##### Project type and purpose
- StockUP is a full-stack web application:
- Frontend built with SvelteKit/Vite (TypeScript, components under `frontend/src`)
- Backend built with FastAPI (Python, modules under `backend/app`)
- No on-premise database schema appears—business logic modules call external AI and market-data services.
- Its goal is to present AI-driven stock analysis (via Google Gemini) and interactive charts (via Plotly) in a web dashboard.

##### File structure
- Top-level:
- `.github/` – issue/pr templates and CI workflows (linting)
- `backend/` – Python FastAPI service
- `frontend/` – SvelteKit UI
- `test/` – unit & integration tests
- `backend/app/` layers:
- `main.py` – FastAPI entrypoint with CORS and router mounting
- `routers/` – API endpoints (e.g. `/stock_info`)
- `services/` – domain logic: fetching symbols, wrapping Gemini AI calls, historical data
- `models/` – Pydantic request/response schemas
- `setup/environment.py` – configuration loader (`.env`)
- `frontend/src/`:
- `routes/` – page Svelte components for Home, About, AI Chat, Stock Info, etc.
- `lib/components` – reusable UI (e.g. `StockCharts.svelte`)
- `lib/config.ts` – client-side API base URL

##### Extract architecture insights from README
- Uses Docker for containerization (`backend/Dockerfile`)
- Poetry manages Python dependencies; NPM/Yarn for frontend
- CI via GitHub Actions (`.github/workflows/lint.yaml`), pre-commit hooks
- Testing via pytest and front-end test suite (not shown, but configured)

a. Components
- Client (Browser) / User Interface
- Frontend (SvelteKit app served by Vite or StaticFiles)
- Backend (FastAPI REST API)
- External AI Service (Google Gemini API)
- External Market-Data API (e.g. stock price provider)
- CI/CD (GitHub Actions + pre-commit)
- Container (Docker image)
b. Relationships & data flow
1. User interacts with the SvelteKit UI in the browser.
2. UI sends HTTP requests to FastAPI endpoints (`/api/stock_info`, `/api/ai_chat`, etc.).
3. FastAPI routers receive requests, validate via Pydantic models, and delegate to service modules.
4. Service modules fetch raw market data from an external market-data API.
5. Service modules call Google Gemini via its SDK or HTTP API for analysis/predictions.
6. Services assemble responses and return JSON to the frontend.
7. Frontend receives data and renders charts (Plotly via `StockCharts.svelte`) and UI elements.
8. Entire application is built/tested via CI pipelines and packaged into a Docker image for deployment.
c. Architectural patterns & principles
- Layered architecture: Presentation (Frontend) → API Layer (Routers) → Business Logic (Services) → External Integrations
- Modularization: each service in `backend/app/services` encapsulates one domain task
- Separation of concerns: Pydantic models for validation, routers for HTTP, services for logic
- Containerization: single Dockerfile orchestrates frontend build + FastAPI serving
d. Key technologies
- FastAPI, Uvicorn, Pydantic
- SvelteKit, Vite, TypeScript
- Google Gemini AI SDK
- Plotly (frontend charts)
- Poetry, pytest, pre-commit, ESLint, Prettier

##### Detail
- If the SvelteKit app is prerendered/static, show the build step in the Dockerfile stage.
- Tests runner in CI: “pytest”, “ESLint/Prettier”, “pre-commit hooks.”

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        BrowserUI["Browser (UI)"]:::ui
        SvelteKitFE["SvelteKit Frontend"]:::ui
        ConfigTS["API Config (config.ts)"]:::ui
        StockCharts["StockCharts.svelte"]:::ui
        subgraph "Build Config"
            direction TB
            ViteConfig["vite.config.ts"]:::ui
            SvelteConfig["svelte.config.js"]:::ui
            TSConfig["tsconfig.json"]:::ui
        end
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        FastAPIEntrypoint["main.py (FastAPI)"]:::backend
        Routers["API Routers"]:::backend
        subgraph "Services" 
            direction TB
            AllSymbols["all_symbols.py"]:::backend
            StockInfoService["stock_info.py"]:::backend
            GeminiTechAnalysis["gemini_technical_analysis.py"]:::backend
            GeminiWithHistory["gemini_with_history.py"]:::backend
        end
        Models["Pydantic Models"]:::backend
        ConfigLoader["environment.py"]:::backend
    end

    %% External Dependencies
    subgraph "External APIs"
        direction TB
        Gemini["Google Gemini AI"]:::external
        MarketData["Market-Data API"]:::external
    end

    %% Infrastructure
    subgraph "Infrastructure"
        direction TB
        CI_CD["CI/CD (GitHub Actions)"]:::infra
        PreCommit["pre-commit hooks"]:::infra
        DockerContainer["Docker Container"]:::infra
        PoetryDeps["pyproject.toml"]:::infra
        UnitTests["Unit Tests"]:::infra
        IntegrationTests["Integration Tests"]:::infra
    end

    %% Data Flows
    BrowserUI -->|"Loads UI"| SvelteKitFE
    ConfigTS -->|Used by| SvelteKitFE
    SvelteKitFE -->|GET /api/stock_info<br/>POST /api/ai_chat| FastAPIEntrypoint
    FastAPIEntrypoint -->|"Routes to"| Routers
    Routers -->|"Calls"| AllSymbols
    Routers -->|"Calls"| StockInfoService
    Routers -->|"Calls"| GeminiTechAnalysis
    Routers -->|"Calls"| GeminiWithHistory
    Routers -->|"Validates via"| Models
    Routers -->|"Uses config"| ConfigLoader
    AllSymbols -->|"Fetches data from"| MarketData
    StockInfoService -->|"Fetches data from"| MarketData
    GeminiTechAnalysis -->|"Calls"| Gemini
    GeminiWithHistory -->|"Calls"| Gemini
    MarketData -->|"Returns data to"| AllSymbols
    MarketData -->|"Returns data to"| StockInfoService
    Gemini -->|"Returns analysis to"| GeminiTechAnalysis
    Gemini -->|"Returns analysis to"| GeminiWithHistory
    AllSymbols -->|"Returns JSON to"| Routers
    StockInfoService -->|"Returns JSON to"| Routers
    GeminiTechAnalysis -->|"Returns JSON to"| Routers
    GeminiWithHistory -->|"Returns JSON to"| Routers
    Routers -->|"Responds"| SvelteKitFE
    SvelteKitFE -->|"Renders charts (Plotly)"| BrowserUI

    %% Infrastructure Flows
    CI_CD -->|"Runs tests"| UnitTests
    CI_CD -->|"Runs tests"| IntegrationTests
    CI_CD -->|"Checks code"| PreCommit
    CI_CD -->|"Builds image"| DockerContainer
    DockerContainer -->|"Contains"| SvelteKitFE
    DockerContainer -->|"Contains"| FastAPIEntrypoint
    PoetryDeps -->|"Used by"| DockerContainer

    %% Click Events
    click BrowserUI "https://github.com/pooriat/stockup/tree/develop/frontend/src/routes/"
    click SvelteKitFE "https://github.com/pooriat/stockup/tree/develop/frontend/"
    click StockCharts "https://github.com/pooriat/stockup/blob/develop/frontend/src/lib/components/StockCharts.svelte"
    click ConfigTS "https://github.com/pooriat/stockup/blob/develop/frontend/src/lib/config.ts"
    click ViteConfig "https://github.com/pooriat/stockup/blob/develop/frontend/vite.config.ts"
    click SvelteConfig "https://github.com/pooriat/stockup/blob/develop/frontend/svelte.config.js"
    click TSConfig "https://github.com/pooriat/stockup/blob/develop/frontend/tsconfig.json"
    click FastAPIEntrypoint "https://github.com/pooriat/stockup/blob/develop/backend/app/main.py"
    click Routers "https://github.com/pooriat/stockup/tree/develop/backend/app/routers/"
    click AllSymbols "https://github.com/pooriat/stockup/blob/develop/backend/app/services/all_symbols.py"
    click StockInfoService "https://github.com/pooriat/stockup/blob/develop/backend/app/services/stock_info.py"
    click GeminiTechAnalysis "https://github.com/pooriat/stockup/blob/develop/backend/app/services/gemini_technical_analysis.py"
    click GeminiWithHistory "https://github.com/pooriat/stockup/blob/develop/backend/app/services/gemini_with_history.py"
    click Models "https://github.com/pooriat/stockup/tree/develop/backend/app/models/"
    click ConfigLoader "https://github.com/pooriat/stockup/blob/develop/backend/app/setup/environment.py"
    click DockerContainer "https://github.com/pooriat/stockup/tree/develop/backend/Dockerfile"
    click PoetryDeps "https://github.com/pooriat/stockup/blob/develop/backend/pyproject.toml"
    click CI_CD "https://github.com/pooriat/stockup/blob/develop/.github/workflows/lint.yaml"
    click PreCommit "https://github.com/pooriat/stockup/blob/develop/.pre-commit-config.yaml"
    click UnitTests "https://github.com/pooriat/stockup/blob/develop/test/unit/apis/test_stock_info.py"
    click IntegrationTests "https://github.com/pooriat/stockup/tree/develop/test/integration/"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0066CC,color:#000
    classDef backend fill:#DFF7DF,stroke:#009933,color:#000
    classDef external fill:#FFE6CC,stroke:#FF9900,color:#000
    classDef infra fill:#EEEEEE,stroke:#888888,color:#000
```
