# drift-labs/v2-risk-dashboard architecture

> Architecture diagram of the GitHub repository drift-labs/v2-risk-dashboard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/drift-labs/v2-risk-dashboard
- Repository: https://github.com/drift-labs/v2-risk-dashboard

## Overview

##### Main Components
- Backend API Server:
– Indicate tasks such as snapshot processing (backend/tasks) which show asynchronous or background operations.
- Frontend Application:
– Represent pages or modules under src/page (asset_liability, deposits, health, etc.).
- Cache Generation Process:
– Represent a separate process or “Cache Generator” (triggered by gen.sh) that pre-computes the heavy endpoints.
- Shared Services/Libraries:
– Include the “shared” directory components and any common utilities (backend/utils, src/lib, etc.) that are used across the application.
- Infrastructure and Deployment Components:
– Docker: Represent Docker components (Dockerfile-backend, Dockerfile-frontend & docker-compose.yaml) indicating containerization.

##### Relationships and Interactions
- Directional flow from the Cache Generation Process to the Backend API, indicating that once the CPU-heavy data is computed, it is cached and then served by the API endpoints.
- Backend API to middleware components that manage caching and service readiness, highlighting data flow between these layers.
- That shared libraries and utilities are used across both the backend and frontend.

##### Architectural Patterns/Design Principles
- Separation of concerns:
– Frontend (UI/Presentation) in one container/process.
– Backend (API and data processing) in another.
- Use of background tasks for heavy computation and caching mechanisms.
- Deployment strategies (Docker containers, CI/CD pipelines, and Kubernetes) that allow clean separation and scaling.

##### Technologies and Frameworks
- Backend: Python, Gunicorn for the server, custom API modules, and middleware for caching and health checks.
- Frontend: Streamlit for the UI.
- Deployment: Docker (Dockerfiles and docker-compose), GitHub Actions for CI/CD, and Kubernetes for production deployment.
- Utility scripts: gen.sh and various scripts in backend/scripts to handle cache generation.

– Separate components for backend (with internal modules like API, middleware, tasks) and frontend.
– Clearly indicate the process for heavy computations and caching.
– Define integration points (HTTP requests, internal data flows) between components.
- Avoid oversimplifying. Instead, strive for clarity by breaking down the system into as many well-labeled, interacting parts as possible.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Backend Components
    subgraph "Backend Components"
        BackendAPI["Backend API Server"]:::backend
        APIEndpoints["API Endpoints"]:::backend
        CacheMiddleware["Middleware: Cache"]:::backend
        Readiness["Middleware: Readiness"]:::backend
        BackgroundTasks["Background Tasks: Snapshot Processing"]:::backend
    end

    %% Frontend Components
    subgraph "Frontend Components"
        FrontendUI["Frontend – Streamlit UI"]:::frontend
        subgraph "Pages"
            AssetLiability["Asset Liability Page"]:::frontend
            Deposits["Deposits Page"]:::frontend
            Health["Health Page"]:::frontend
            LiquidationCurves["Liquidation Curves"]:::frontend
            MarketInspector["Market Inspector"]:::frontend
            Orderbook["Orderbook"]:::frontend
            PnL["PnL"]:::frontend
            PriceShock["Price Shock Page"]:::frontend
            Swap["Swap Page"]:::frontend
            Vaults["Vaults Page"]:::frontend
            Welcome["Welcome Page"]:::frontend
        end
    end

    %% Cache Generation Process
    subgraph "Cache Generation"
        CacheProcessMain["Cache Script Process"]:::cache
        CacheSupport["Supporting Cache Script"]:::cache
    end

    %% Shared Services
    subgraph "Shared Services"
        SharedLib["Shared Library"]:::shared
        BackendUtils["Backend Utilities"]:::shared
        FrontendUtils["Frontend Utilities"]:::shared
    end

    %% Infrastructure & Deployment
    subgraph "Infrastructure & Deployment"
        DockerBackend["Dockerfile-backend"]:::infra
        DockerFrontend["Dockerfile-frontend"]:::infra
        DockerCompose["Docker Compose"]:::infra
        CICDPipeline["CI/CD Pipeline"]:::infra
        DeploymentConfig["Deployment Config"]:::infra
    end

    %% Relationships between components
    FrontendUI -->|"HTTPRequest"| BackendAPI
    CacheProcessMain -->|"Precompute"| APIEndpoints
    CacheProcessMain -->|"executes"| CacheSupport
    APIEndpoints -->|"uses"| CacheMiddleware
    APIEndpoints -->|"checks"| Readiness
    BackgroundTasks -->|"updates"| APIEndpoints
    BackendAPI -->|"uses"| SharedLib
    BackendAPI -->|"calls"| BackendUtils
    FrontendUI -->|"calls"| FrontendUtils
    FrontendUI -->|"uses"| SharedLib

    DockerBackend -->|"deploys"| BackendAPI
    DockerFrontend -->|"deploys"| FrontendUI
    DockerCompose -->|"orchestrates"| DockerBackend
    DockerCompose -->|"orchestrates"| DockerFrontend
    CICDPipeline -->|"triggers"| DockerCompose
    DeploymentConfig -->|"appliesTo"| BackendAPI

    %% Styles
    classDef backend fill:#ADD8E6,stroke:#0000FF,stroke-width:2px;
    classDef frontend fill:#90EE90,stroke:#008000,stroke-width:2px;
    classDef cache fill:#FFA500,stroke:#FF8C00,stroke-width:2px;
    classDef shared fill:#E6E6FA,stroke:#800080,stroke-width:2px;
    classDef infra fill:#D3D3D3,stroke:#696969,stroke-width:2px;

    %% Click Events for Backend Components
    click BackendAPI "https://github.com/drift-labs/v2-risk-dashboard/blob/master/backend/app.py"
    click APIEndpoints "https://github.com/drift-labs/v2-risk-dashboard/tree/master/backend/api"
    click CacheMiddleware "https://github.com/drift-labs/v2-risk-dashboard/blob/master/backend/middleware/cache_middleware.py"
    click Readiness "https://github.com/drift-labs/v2-risk-dashboard/blob/master/backend/middleware/readiness.py"
    click BackgroundTasks "https://github.com/drift-labs/v2-risk-dashboard/blob/master/backend/tasks/snapshot_watcher.py"

    %% Click Events for Frontend Components
    click FrontendUI "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/main.py"
    click AssetLiability "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/asset_liability.py"
    click Deposits "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/deposits.py"
    click Health "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/health.py"
    click LiquidationCurves "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/liquidation_curves.py"
    click MarketInspector "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/market_inspector.py"
    click Orderbook "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/orderbook.py"
    click PnL "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/pnl.py"
    click PriceShock "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/price_shock.py"
    click Swap "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/swap.py"
    click Vaults "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/vaults.py"
    click Welcome "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/page/welcome.py"

    %% Click Events for Cache Generation Process
    click CacheProcessMain "https://github.com/drift-labs/v2-risk-dashboard/blob/master/gen.sh"
    click CacheSupport "https://github.com/drift-labs/v2-risk-dashboard/blob/master/backend/scripts/generate_ucache.py"

    %% Click Events for Shared Services
    click SharedLib "https://github.com/drift-labs/v2-risk-dashboard/blob/master/shared/__init__.py"
    click BackendUtils "https://github.com/drift-labs/v2-risk-dashboard/blob/master/backend/utils/matrix.py"
    click FrontendUtils "https://github.com/drift-labs/v2-risk-dashboard/blob/master/src/lib/api.py"

    %% Click Events for Infrastructure & Deployment
    click DockerBackend "https://github.com/drift-labs/v2-risk-dashboard/tree/master/Dockerfile-backend"
    click DockerFrontend "https://github.com/drift-labs/v2-risk-dashboard/tree/master/Dockerfile-frontend"
    click DockerCompose "https://github.com/drift-labs/v2-risk-dashboard/blob/master/docker-compose.yaml"
    click CICDPipeline "https://github.com/drift-labs/v2-risk-dashboard/blob/master/.github/workflows/master.yaml"
    click DeploymentConfig "https://github.com/drift-labs/v2-risk-dashboard/blob/master/supervisord.conf"
```
