# lalabuy948/phoenixanalytics architecture

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

- Interactive diagram: https://gitdiagram.com/lalabuy948/phoenixanalytics
- Repository: https://github.com/lalabuy948/phoenixanalytics

## Overview

- Phoenix Application Integration:
- Analytics Plug (PhoenixAnalytics.Plugs.RequestTracker) inserted into endpoint (as per the README instructions).
- Routing and Controller components in the “web” directory (both traditional controllers and LiveView components such as the dashboard and charts).
- Data Processing and Storage:
- Services and Queries (in lib/phoenix_analytics/services and lib/phoenix_analytics/queries) which handle batching, telemetry, caching, and the building of stats/charts.
- Entities and migrations (in lib/phoenix_analytics/entities and priv/repo) that define data schemas and migration scripts.
- Storage Options:
- DuckDB for lightweight, separate analytics storage (used by default to avoid interference with the main database).
- Postgres (optionally enabled in configuration) including caching and connection management if running in a dynamic/distributed environment.
- Frontend & Dashboard:
- React components (in assets/react) and LiveView (in lib/phoenix_analytics/web/live) that together build the dashboard UI and charting components.
- Static assets and configuration for Tailwind CSS alongside JavaScript files which style and animate the dashboard.
- External Integration / Example Applications:
- Example projects (e.g., examples/duck_only, examples/duck_postgres, examples/duck_postgres_in_memory) that show how to wire up the analytics plug, manage configuration, and launch a server.

##### Relationships and interactions
- The RequestTracker plug collects relevant user and request data, then passes the data to the analytics service layer.
- The Services (such as the Batcher and Cache modules) process the data asynchronously and invoke queries that populate the analytics database.
- The Query layer handles retrieving data to build statistics and charts. This data is then served to the dashboard.

##### Architectural patterns and design principles
- Separation of Concerns: The analytics logic is clearly separated from the primary application logic via the plug and independent storage (via configuration options).
- Modularity: The project is organized into distinct modules (web, services, queries, entities) making it easier to extend or replace components.
- Extensibility: Options in the configuration (like in_memory flag, caching options, multiple storage choices) allow the same core plug-and-play tool to be tailored for different Phoenix deployment scenarios.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Flow
    HTTP["HTTP Request"]:::web
    WebLayer["Web Layer (Controllers, LiveView, Routing)"]:::web
    Plug["Analytics Plug (PhoenixAnalytics.Plugs.RequestTracker)"]:::web
    Services["Data Processing & Services (Batching,Cache,Telemetry,PubSub)"]:::processing
    DuckDB["DuckDB Storage"]:::storage
    Postgres["Postgres Storage (Optional)"]:::storage
    Queries["Data Query Layer (Statistics & Charts)"]:::processing
    LiveView["LiveView Dashboard UI"]:::ui
    ReactUI["React Charting Components"]:::ui
    Entities["Data Entities (Schemas)"]:::entity
    Migrations["Database Migrations/Seeds"]:::entity

    %% External Examples Subgraph
    subgraph "Example Deployments"
        duck_only["duck_only"]
        duck_postgres["duck_postgres"]
        duck_in_memory["duck_postgres_in_memory"]
    end

    %% Connections
    HTTP -->|"routes"| WebLayer
    WebLayer -->|"inserts"| Plug
    Plug -->|"collects"| Services
    Services -->|"writes_to"| DuckDB
    Services -->|"writes_to"| Postgres
    DuckDB -->|"queries"| Queries
    Postgres -->|"queries"| Queries
    Queries -->|"renders"| LiveView
    Queries -->|"renders"| ReactUI
    Services -.->|"uses"| Entities
    Services -.->|"triggers"| Migrations
    duck_only ---|"integration"| WebLayer
    duck_postgres ---|"integration"| WebLayer
    duck_in_memory ---|"integration"| WebLayer

    %% Click Events
    click Plug "https://github.com/lalabuy948/phoenixanalytics/blob/master/lib/phoenix_analytics/plugs/request_tracker.ex"
    click WebLayer "https://github.com/lalabuy948/phoenixanalytics/blob/master/lib/phoenix_analytics/web/router.ex"
    click LiveView "https://github.com/lalabuy948/phoenixanalytics/tree/master/lib/phoenix_analytics/web/live"
    click ReactUI "https://github.com/lalabuy948/phoenixanalytics/tree/master/assets/react"
    click Services "https://github.com/lalabuy948/phoenixanalytics/tree/master/lib/phoenix_analytics/services"
    click Queries "https://github.com/lalabuy948/phoenixanalytics/tree/master/lib/phoenix_analytics/queries"
    click Entities "https://github.com/lalabuy948/phoenixanalytics/tree/master/lib/phoenix_analytics/entities"
    click Migrations "https://github.com/lalabuy948/phoenixanalytics/tree/master/priv/repo"
    click duck_only "https://github.com/lalabuy948/phoenixanalytics/tree/master/examples/duck_only"
    click duck_postgres "https://github.com/lalabuy948/phoenixanalytics/tree/master/examples/duck_postgres"
    click duck_in_memory "https://github.com/lalabuy948/phoenixanalytics/tree/master/examples/duck_postgres_in_memory"

    %% Styling Classes
    classDef web fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef processing fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef storage fill:#FFD700,stroke:#000,stroke-width:1px;
    classDef ui fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef entity fill:#D3D3D3,stroke:#000,stroke-width:1px;
```
