# jayleecn/btc_alerts architecture

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

- Interactive diagram: https://gitdiagram.com/jayleecn/btc_alerts
- Repository: https://github.com/jayleecn/btc_alerts

## Overview

##### Project’s Purpose and Type
- This project is a scheduled data‐collection and notification service for BTC market metrics. It gathers BTC metrics (MVRV Z-score and Pi Multiple) every morning, then posts alerts to Twitter and, if thresholds are met, to a Google email group.
- It integrates with third-party services (Twitter API 2.0 and Google Groups) and uses GitHub Actions for scheduling and Vercel for serverless deployment. Essentially, it’s a small full-stack (or serverless backend) application focused on scheduled integrations rather than serving a client-facing UI.

##### Analyzing the File Structure
- Top-Level Files/Directories:
- .github/workflows: Contains GitHub Actions YAML files (schedule.yml and keep-alive.yml) responsible for triggering the daily job.
- api/index.js: The main code for collecting data and posting alerts. (Note that the README explains that index.js was relocated here to work as a Vercel Serverless Function.)
- server.js and index.html: Possibly used for local testing or additional web serving aspects.
- vercel.json: Configures Vercel’s behavior – particularly that files under /api are automatically interpreted as serverless functions.
- package.json and package-lock.json: Define dependencies and scripts.
- These indicate a design that uses serverless functions (via Vercel) combined with scheduled triggering via GitHub Actions.

##### Insights from the README
- Describes the schedule (daily at 8 AM) and the external services (Twitter and Google Groups).
- Explains the adjustments made: loading environment variables with dotenv, switching Twitter API versions, and required permission settings.
- Highlights deployment nuances like moving index.js to the /api folder to accommodate Vercel’s serverless functions.
- Notes configuration changes needed in GitHub Actions (public repository requirement) for the scheduler.

a. Main Components:
- GitHub Actions Scheduler: Represents the automated triggers (using schedule.yml and keep-alive.yml) that start the daily execution.
- Vercel Deployment Layer: Illustrates that the project is deployed on Vercel which hosts the serverless function.
- Serverless Function (/api/index.js): The execution core that collects BTC metrics and interfaces with external APIs.
- External Services:
- BTC Data Provider: (Implicit) the source of BTC metrics.
- Twitter API (v2.0): For posting tweets.
- Google Groups (Email Alerts): For sending extra notifications if conditions are met.
- Supporting Components:
- Dotenv Initialization: Depicted as part of the startup process in the serverless function.
- Environment Variables: Critical for API keys and configuration.
- Optional Local Testing Component: server.js and index.html might be used for local validations.

b. Relationships and Interactions:
- GitHub Actions → Vercel (triggering the serverless function).
- Vercel (serverless function) → BTC Data Provider for fetching metrics.
- Vercel (serverless function) → Twitter API for posting tweets.
- Vercel (serverless function) → Google Groups for sending email alerts when criteria are met.
- Additionally, indicate feedback loops (e.g., responses from the APIs, error logging, etc.) if applicable.

c. Architectural Patterns & Design Principles:
- Serverless Architecture: Emphasize that the core processing logic is hosted as a serverless function on Vercel.
- Scheduled/Triggered Processing: Use GitHub Actions as an external scheduler.
- Separation of Concerns: The code clearly separates tasks (data collection vs. notification) and isolates configuration (via dotenv and vercel.json).
- Use of Environment Variables for secure configuration.

d. Relevant Technologies and Libraries:
- Node.js: The runtime environment.
- dotenv: For loading environment variables.
- Twitter API v2.0 Client Library: For posting tweets.
- Vercel Serverless Functions: For hosting the API.
- GitHub Actions: For orchestrating scheduled tasks.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% GitHub Actions Workflows Subgraph
    subgraph "GitHub Actions Scheduler"
        GH1["schedule.yml"]:::scheduler
        GH2["keep-alive.yml"]:::scheduler
    end

    %% Vercel Deployment Layer Node
    Vercel["Vercel Deployment Layer"]:::deployment

    %% Serverless Function Node
    Serverless["Serverless Function (api/index.js)"]:::serverless

    %% External Services Subgraph
    subgraph "External Services"
        BTC["BTC Data Provider"]:::external
        Twitter["Twitter API v2.0"]:::external
        GoogleGroups["Google Groups Email Notification"]:::external
    end

    %% Local Testing Subgraph
    subgraph "Local Testing Components"
        LocalServer["server.js"]:::testing
        LocalHTML["index.html"]:::testing
    end

    %% Connections
    GH1 -->|"triggers"| Vercel
    GH2 -->|"triggers"| Vercel
    Vercel -->|"executes"| Serverless
    Serverless -->|"fetches data from"| BTC
    Serverless -->|"posts tweets to"| Twitter
    Serverless -->|"sends emails to"| GoogleGroups
    Serverless ---|"optional testing"| LocalServer
    Serverless ---|"optional testing"| LocalHTML

    %% Click Events
    click GH1 "https://github.com/jayleecn/btc_alerts/blob/main/.github/workflows/schedule.yml"
    click GH2 "https://github.com/jayleecn/btc_alerts/blob/main/.github/workflows/keep-alive.yml"
    click Vercel "https://github.com/jayleecn/btc_alerts/blob/main/vercel.json"
    click Serverless "https://github.com/jayleecn/btc_alerts/blob/main/api/index.js"
    click LocalServer "https://github.com/jayleecn/btc_alerts/blob/main/server.js"
    click LocalHTML "https://github.com/jayleecn/btc_alerts/blob/main/index.html"

    %% Styles
    classDef scheduler fill:#E8F6FF,stroke:#0277BD,stroke-width:2px;
    classDef deployment fill:#ECF0F1,stroke:#455A64,stroke-width:2px;
    classDef serverless fill:#FFF3E0,stroke:#EF6C00,stroke-width:2px;
    classDef external fill:#E8F5E9,stroke:#2E7D32,stroke-width:2px;
    classDef testing fill:#F3E5F5,stroke:#6A1B9A,stroke-width:2px;
```
