# chainstacklabs/compare-dashboard-functions architecture

> Architecture diagram of the GitHub repository chainstacklabs/compare-dashboard-functions, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chainstacklabs/compare-dashboard-functions
- Repository: https://github.com/chainstacklabs/compare-dashboard-functions

## Overview

##### Main Components
- Global Serverless Functions: Representing the functions that collect metrics every minute.
- Region-Specific Functions (e.g., EU only): Handling tasks like state updates (30-minute intervals) and measuring transaction landing rate (15-minute intervals).
- Blob Storage – Represents Vercel’s Blob Storage used to store state data. This component will interact with the functions.
- Grafana Cloud – The destination for pushing the collected metrics. Note that it is running a Prometheus instance for visualization.

##### Detail the Relationships and Interactions
- Blob Storage sends/stores and receives data to/from the Collector functions.
- The Collector functions interact bidirectionally with the RPC nodes to fetch performance metrics.
- Convert the collected metrics and send them to Grafana Cloud.
- The State update and Transaction (TX) functions also interact with Blob Storage and the RPC nodes.
- That while some serverless functions (within the Global category) are collecting metrics continuously, others (like state updates) run at longer intervals and are deployed in specific regions.

##### Architectural Patterns and Design Principles
- Modular Structure: The file tree is organized into distinct directories such as “api” (with subdirectories for read, write, and support), “common” (for shared utilities and state management), and “metrics” (for individual metric implementations). Emphasize that this decoupling supports clear separation of concerns.
- Serverless and Multi-region Deployment: Illustrate the Vercel Functions deployed in distinct regions (e.g., Singapore, EU, US-West) and how the environment variables are configured for multi-region consistency.
- Extensibility: The project is designed to easily add new metrics. The “common”, “factory”, and “metric” related modules along with tests encourage modularity and extensibility.

##### Relevant Technologies, Frameworks, and Libraries
- Python: As indicated by the use of Python scripts, virtual environment setup, and requirements.txt.
- Vercel Functions: For serverless deployment along with Blob Storage integration.
- Grafana Cloud & Prometheus: For metrics storage and visualization.
- Blockchain Specific Modules: Within the “api/read” and “api/write” directories, there are separate handlers for Ethereum, Solana, TON, and in some cases “Base” – each indicating blockchain-specific logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Services
    subgraph "RPC Nodes"
        ETH_RPC["Ethereum RPC Node"]:::rpc
        SOL_RPC["Solana RPC Node"]:::rpc
        TON_RPC["TON RPC Node"]:::rpc
        BASE_RPC["Base RPC Node"]:::rpc
    end

    %% Vercel Platform
    subgraph "Vercel Platform"
        %% Global Serverless Functions
        subgraph "Global Serverless Functions"
            ETH_COL["Ethereum Collector"]:::function
            SOL_COL["Solana Collector"]:::function
            TON_COL["TON Collector"]:::function
            BASE_COL["Base Collector"]:::function

            ETH_MET["Ethereum Metrics Module"]:::function
            SOL_MET["Solana Metrics Module"]:::function
            TON_MET["TON Metrics Module"]:::function
        end

        %% Region-Specific Functions (EU Only)
        subgraph "Region-Specific Functions (EU Only)"
            STATE_UPD["State Updater"]:::function
            TX_MET["Transaction Metrics"]:::function
        end

        BLOB_STORE["Blob Storage"]:::storage
        SHARED_UTIL["Shared Utilities & Factories"]:::utility
    end

    %% External Destination
    GRAFANA["Grafana Cloud (Prometheus)"]:::external

    %% Data Flows
    %% Global Collectors interact with RPC Nodes (bidirectional)
    ETH_COL <-->|"fetch/response"| ETH_RPC
    SOL_COL <-->|"fetch/response"| SOL_RPC
    TON_COL <-->|"fetch/response"| TON_RPC
    BASE_COL <-->|"fetch/response"| BASE_RPC

    %% Global Metrics Modules (used with collectors)
    ETH_COL -->|"process"| ETH_MET
    SOL_COL -->|"process"| SOL_MET
    TON_COL -->|"process"| TON_MET

    %% Blob Storage Interactions with Functions (state retrieval and update)
    BLOB_STORE <-->|"state read/write"| STATE_UPD
    BLOB_STORE <-->|"state read/write"| TX_MET
    BLOB_STORE <-->|"state access"| ETH_COL
    BLOB_STORE <-->|"state access"| SOL_COL
    BLOB_STORE <-->|"state access"| TON_COL
    BLOB_STORE <-->|"state access"| BASE_COL

    %% Push metrics to Grafana Cloud
    ETH_COL -->|"push metrics"| GRAFANA
    SOL_COL -->|"push metrics"| GRAFANA
    TON_COL -->|"push metrics"| GRAFANA
    BASE_COL -->|"push metrics"| GRAFANA
    TX_MET -->|"push metrics"| GRAFANA

    %% Shared Utilities assist processing functions
    SHARED_UTIL ---|"internal modules"| ETH_COL
    SHARED_UTIL ---|"internal modules"| SOL_COL
    SHARED_UTIL ---|"internal modules"| TON_COL
    SHARED_UTIL ---|"internal modules"| BASE_COL
    SHARED_UTIL ---|"internal modules"| STATE_UPD
    SHARED_UTIL ---|"internal modules"| TX_MET

    %% Click Events
    click ETH_COL "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/api/read/ethereum.py"
    click SOL_COL "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/api/read/solana.py"
    click TON_COL "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/api/read/ton.py"
    click BASE_COL "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/api/read/base.py"
    click STATE_UPD "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/api/support/update_state.py"
    click TX_MET "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/metrics/solana_landing_rate.py"
    click BLOB_STORE "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/common/state/blob_storage.py"
    click ETH_MET "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/metrics/ethereum.py"
    click SOL_MET "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/metrics/solana.py"
    click TON_MET "https://github.com/chainstacklabs/compare-dashboard-functions/blob/master/metrics/ton.py"
    click SHARED_UTIL "https://github.com/chainstacklabs/compare-dashboard-functions/tree/master/common/"

    %% Class Styles
    classDef function fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef storage fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef rpc fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef utility fill:#e2e3e5,stroke:#d6d8db,stroke-width:2px;
```
