# thanhphuchuynh/t-pubsub architecture

> Architecture diagram of the GitHub repository thanhphuchuynh/t-pubsub, 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/thanhphuchuynh/t-pubsub
- Repository: https://github.com/thanhphuchuynh/t-pubsub

## Overview

##### Main Components
- PubSub Service: The core component found in the pubsub directory. This includes the main service implementation (pubsub.go), batch consumer logic (batch_consumer.go), benchmarks, and reporting.
- Message Store and Data Models: Represented by the model directory (e.g., message.go) which details the immutable data structures for messages.
- Consumers and Batch Consumers: Show separate but connected components for individual message processing (consumer functions) and batch processing, as indicated by both pubsub and batch_consumer code.
- REST API Layer: Illustrate the API endpoints found in the README (endpoints for health, status, benchmark, simulation, and metrics) that interact with the core PubSub service.
- Monitoring & Metrics:
- Prometheus: As a separate component gathering metrics (configured by prometheus/prometheus.yml).
- Grafana: Dashboard visualization setup (grafana/dashboards and provisioning files).
- Docker & Deployment: Include Dockerfile, docker-compose.yml, and run.sh as components that encapsulate the runtime environment and deployment orchestration.

##### Relationships and Interactions
- From API calls coming into the PubSub Service that trigger message publishing and consumer invocation.
- From the message store to consumers and from batch consumers handling collections of messages.
- From the core service to the Metrics Registry then to Prometheus.
- From Prometheus feeding Grafana dashboards.

##### Architectural Patterns & Design Principles
- Functional Programming style:
- That the system returns new instances for operations rather than modifying state.
- REST API endpoints as interfaces for external interaction.
- That benchmarking and simulation are additional functionalities integrated into the system’s capabilities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        API["REST API Server (main.go)"]:::api
        PubSub["PubSub Service (pubsub)"]:::service
        BatchConsumer["Batch Consumers (pubsub/batch_consumer.go)"]:::service
        Message["Data Models (model/message.go)"]:::model
    end

    %% Monitoring Layer
    subgraph "Monitoring Layer"
        Prom["Prometheus (prometheus.yml)"]:::monitoring
        Grafana["Grafana (dashboards & provisioning)"]:::monitoring
    end

    %% Deployment/Infrastructure Layer
    subgraph "Deployment Layer"
        DockerCompose["Docker Compose (docker-compose.yml)"]:::deployment
        Dockerfile["Dockerfile"]:::deployment
        RunScript["Run.sh"]:::deployment
    end

    %% Connections within Application Layer
    API -->|"invokeAPI"|PubSub
    PubSub -->|"processBatch"|BatchConsumer
    PubSub -->|"accessData"|Message

    %% Connections to Monitoring Layer
    PubSub -->|"collectMetrics"|Prom
    Prom -->|"feedGrafana"|Grafana

    %% Deployment orchestration
    DockerCompose -->|"orchestrate"|PubSub
    DockerCompose -->|"orchestrate"|Prom
    DockerCompose -->|"orchestrate"|Grafana

    %% Click Events for Application Layer
    click API "https://github.com/thanhphuchuynh/t-pubsub/blob/main/main.go"
    click PubSub "https://github.com/thanhphuchuynh/t-pubsub/blob/main/pubsub/pubsub.go"
    click BatchConsumer "https://github.com/thanhphuchuynh/t-pubsub/blob/main/pubsub/batch_consumer.go"
    click Message "https://github.com/thanhphuchuynh/t-pubsub/blob/main/model/message.go"

    %% Click Events for Monitoring Layer
    click Prom "https://github.com/thanhphuchuynh/t-pubsub/blob/main/prometheus/prometheus.yml"
    click Grafana "https://github.com/thanhphuchuynh/t-pubsub/tree/main/grafana/"

    %% Click Events for Deployment Layer
    click DockerCompose "https://github.com/thanhphuchuynh/t-pubsub/blob/main/docker-compose.yml"
    click Dockerfile "https://github.com/thanhphuchuynh/t-pubsub/tree/main/Dockerfile"
    click RunScript "https://github.com/thanhphuchuynh/t-pubsub/blob/main/run.sh"

    %% Styles
    classDef api fill:#FFEDB3,stroke:#333,stroke-width:2px
    classDef service fill:#B3E5FC,stroke:#333,stroke-width:2px
    classDef model fill:#C8E6C9,stroke:#333,stroke-width:2px
    classDef monitoring fill:#FFCCBC,stroke:#333,stroke-width:2px
    classDef deployment fill:#D1C4E9,stroke:#333,stroke-width:2px
```
