# kprasanth08/pyalgokite architecture

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

- Interactive diagram: https://gitdiagram.com/kprasanth08/pyalgokite
- Repository: https://github.com/kprasanth08/pyalgokite

## Overview

##### Project type and purpose
- From the file tree and minimal README, this is a Python-based full-stack web application called “PyalgoKite.”
- It provides a dashboard for subscribing to market data feeds (via a Protobuf definition), interacting with the Upstox API (token generation, user profile) and displaying real-time data in the browser.

##### File structure
- Top-level:
- app.py – the web server entry point (likely Flask).
- upstox_service.py – encapsulates the Upstox API/websocket logic.
- generate_token.py, get_profile.py – helpers to authenticate and fetch user profile.
- generate_protobuf.py & MarketDataFeed.proto → MarketDataFeed_pb2.py – pipeline to generate Protobuf classes.
- user_watchlists/ – JSON files for per-user watchlists (simple file-based storage).
- Dockerfile & docker-compose.yml – containerization and orchestration.
- Static assets (CSS/JS) and Jinja2 templates under static/ and templates/ indicate a server-rendered UI plus client-side scripts (including upstox-websocket.js).

##### Available documentation
- README only gives the project name, so we infer architecture from structure and naming conventions.

##### Main components and interactions
a. Frontend (Browser)
- HTML pages (templates/*.html) + CSS/JS (static/)
- A client-side WebSocket or Server-Sent Events connection (static/js/upstox-websocket.js) to receive real-time data.
b. Backend Web Server
- app.py defines HTTP routes: login, profile, dashboard, watchlist management.
- Uses generate_token.py for authentication, get_profile.py for user data.
c. Market Data Service Layer
- upstox_service.py handles subscribing to the Upstox market data WebSocket API, decodes Protobuf messages (MarketDataFeed_pb2.py), and pushes updates to connected clients.
d. Data Storage
- File-based storage for user watchlists under user_watchlists/*.json.
e. External Services
- Upstox REST API (token, profile)
- Upstox WebSocket feed (market data)

##### Architectural patterns and design principles
- MVC‐style separation:
- Views (templates) → Controllers (app.py routes) → Models/Services (upstox_service.py, token/profile generators).
- Service layer isolating external API logic.
- Protobuf code generation decoupled via generate_protobuf.py.

##### Technologies and frameworks
- Python 3 + Flask (implied by app.py and Jinja2 templates)
- Protobuf (MarketDataFeed.proto → MarketDataFeed_pb2.py)
- Docker & docker-compose for containerization
- Static JavaScript for WebSocket client

##### Detail
- You cover all data flows: HTTP, WebSocket, file I/O, Protobuf codegen.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (Browser UI)"
        Templates["templates/*.html"]:::frontend
        CSS["static/css/dashboard.css"]:::frontend
        JS_Main["static/js/main.js"]:::frontend
        JS_Dash["static/js/dashboard.js"]:::frontend
        JS_WS["static/js/upstox-websocket.js"]:::frontend
    end

    %% Backend Runtime
    subgraph "Backend (Runtime)"
        App["Flask Web Server (app.py)"]:::backend
        TokenGen["Token Generator (generate_token.py)"]:::backend
        Profile["Profile Fetcher (get_profile.py)"]:::backend
        subgraph "Market Data Service"
            UpstoxSvc["Upstox Service (upstox_service.py)"]:::service
        end
    end

    %% Protobuf Build-Time
    subgraph "Protobuf Module (Build-Time)" 
        ProtoDef["MarketDataFeed.proto"]:::proto
        ProtoGen["generate_protobuf.py"]:::proto
        ProtoPy["MarketDataFeed_pb2.py"]:::proto
        ProtoClient["static/proto/MarketDataFeed.proto"]:::proto
    end

    %% Storage
    Watchlist["Watchlist Store (user_watchlists/*.json)"]:::storage

    %% External Services
    subgraph "External Services"
        UpstoxREST["Upstox REST API"]:::external
        UpstoxWS["Upstox WebSocket Feed"]:::external
    end

    %% Infrastructure
    Dockerfile["Dockerfile"]:::infra
    Compose["docker-compose.yml"]:::infra
    Requirements["requirements.txt"]:::infra

    %% Data Flows
    Templates -->|renders pages| App
    CSS -->|assets| App
    JS_Main -->|assets| App
    JS_Dash -->|assets| App
    JS_WS -->|assets| App

    App -->|HTTP: login, dashboard| TokenGen
    TokenGen -->|REST call| UpstoxREST
    App -->|HTTP: profile| Profile
    Profile -->|REST call| UpstoxREST

    App -->|file I/O| Watchlist
    App -->|invokes| UpstoxSvc
    UpstoxSvc -->|WebSocket| UpstoxWS
    UpstoxSvc -->|push SSE/WebSocket| JS_WS

    ProtoGen -->|reads| ProtoDef
    ProtoGen -->|generates| ProtoPy
    ProtoGen -->|serves| ProtoClient

    %% Infrastructure links
    Dockerfile -.-> App
    Dockerfile -.-> UpstoxSvc
    Dockerfile -.-> ProtoGen
    Compose -.-> Dockerfile
    Requirements -.-> App

    %% Click events
    click Templates "https://github.com/kprasanth08/pyalgokite/tree/main/templates/"
    click CSS "https://github.com/kprasanth08/pyalgokite/blob/main/static/css/dashboard.css"
    click JS_Main "https://github.com/kprasanth08/pyalgokite/blob/main/static/js/main.js"
    click JS_Dash "https://github.com/kprasanth08/pyalgokite/blob/main/static/js/dashboard.js"
    click JS_WS "https://github.com/kprasanth08/pyalgokite/blob/main/static/js/upstox-websocket.js"
    click App "https://github.com/kprasanth08/pyalgokite/blob/main/app.py"
    click TokenGen "https://github.com/kprasanth08/pyalgokite/blob/main/generate_token.py"
    click Profile "https://github.com/kprasanth08/pyalgokite/blob/main/get_profile.py"
    click UpstoxSvc "https://github.com/kprasanth08/pyalgokite/blob/main/upstox_service.py"
    click ProtoDef "https://github.com/kprasanth08/pyalgokite/blob/main/MarketDataFeed.proto"
    click ProtoGen "https://github.com/kprasanth08/pyalgokite/blob/main/generate_protobuf.py"
    click ProtoPy "https://github.com/kprasanth08/pyalgokite/blob/main/MarketDataFeed_pb2.py"
    click ProtoClient "https://github.com/kprasanth08/pyalgokite/blob/main/static/proto/MarketDataFeed.proto"
    click Watchlist "https://github.com/kprasanth08/pyalgokite/blob/main/user_watchlists/YI9582_watchlist.json"
    click Dockerfile "https://github.com/kprasanth08/pyalgokite/tree/main/Dockerfile"
    click Compose "https://github.com/kprasanth08/pyalgokite/blob/main/docker-compose.yml"
    click Requirements "https://github.com/kprasanth08/pyalgokite/blob/main/requirements.txt"

    %% Styles
    classDef frontend fill:#E8F4FD,stroke:#3B8FC2,color:#034A6E;
    classDef backend fill:#FFF4E5,stroke:#D99E00,color:#6B4C00;
    classDef service fill:#E8FDE8,stroke:#2E8F2E,color:#1B4D1B;
    classDef proto fill:#FDE8F8,stroke:#C23B8F,color:#6E034A;
    classDef storage fill:#FFF0E8,stroke:#D06A3B,color:#6A3B1B;
    classDef external fill:#F0F0F0,stroke:#888888,color:#333333,stroke-dasharray: 5 5;
    classDef infra fill:#EFEFFB,stroke:#6666CC,color:#2E2E6E;
```
