# relaypro-open/dog_trainer architecture

> Architecture diagram of the GitHub repository relaypro-open/dog_trainer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/relaypro-open/dog_trainer
- Repository: https://github.com/relaypro-open/dog_trainer

## Overview

##### Main System Components

a) Core Components:
- dog_trainer (Central Server)
- RethinkDB (Database)
- RabbitMQ (Message Broker)
- dog_agents (Client Components)

b) Supporting Services:
- Certificate Authority System
- AWS EC2 Integration (evident from erlcloud configuration)

##### Key Architectural Patterns

The project follows a distributed architecture with several key patterns:
- Publisher/Subscriber Pattern (via RabbitMQ)
- Supervisor Tree Pattern (Erlang/OTP)
- API-driven architecture (multiple API versions)
- Watcher Pattern (multiple *_watcher.erl files)

**┌─────────────────────────────────────────────────────────────┐**
- Client Layer                            │
- ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐   │
- dog_agent1│  │dog_agent2│  │dog_agent3│  │   ...    │   │
- ┴──────────┴──┴──────────┴──┴──────────┴──┴──────────┴───┘

**┌─────────────────────────────────────────────────────────────┐**
- Communication Layer                         │
- ┌─────────────────────────────────────────────┐           │
- RabbitMQ Cluster               │           │
- (With SSL/TLS & Federation Support)        │           │
- ┴─────────────────────────────────────────────┴───────────┘

**┌─────────────────────────────────────────────────────────────┐**
- Core Server Layer                          │
- ┌─────────────────────────────────────────────┐           │
- dog_trainer                     │           │
- ┌─────────┐  ┌─────────┐  ┌────────────┐  │           │
- API (v1) │  │API (v2) │  │Supervisors │  │           │
- ┘  └─────────┘  └────────────┘  │           │
- ┌─────────┐  ┌─────────┐  ┌────────────┐  │           │
- Watchers │  │Agents   │  │State Mgmt  │  │           │
- ┘  └─────────┘  └────────────┘  │           │
- ┴─────────────────────────────────────────────┴───────────┘

**┌─────────────────────────────────────────────────────────────┐**
- Persistence Layer                          │
- ┌─────────────────────────────────────────────┐           │
- RethinkDB Cluster              │           │
- (With Sharding & Replication)              │           │
- ┴─────────────────────────────────────────────┴───────────┘

##### Important Relationships

- Bidirectional communication between dog_agents and RabbitMQ
- Data flow between dog_trainer and RethinkDB
- Supervision hierarchies within dog_trainer
- Federation relationships between RabbitMQ instances
- Security certificate distribution paths

##### Key Components to Detail

Core Services (from src/ directory):
- API Handlers (api_handler*.erl)
- Database Operations (dog_rethink.erl)
- Configuration Management (dog_config.erl)
- State Management (dog_state.erl)
- File Transfer System (dog_file_transfer*.erl)
- EC2 Integration (dog_ec2*.erl)

##### Configuration and Security

Configuration flow:
- Certificate management
- RabbitMQ virtual hosts
- Database initialization
- Security groups and firewall rules

##### Monitoring and Health

Monitoring components:
- Keepalive system (dog_keepalive_agent.erl)
- Health checks (healthcheck_api_handler.erl)
- Watcher services (multiple *_watcher.erl files)

Remember to emphasize the high-availability aspects of the system, showing how RethinkDB clustering and RabbitMQ federation work together to provide a robust, distributed firewall management solution.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    subgraph Clients
        A1[dog_agent1]
        A2[dog_agent2]
        A3[dog_agent3]
        A4[dog_agent_n]
    end

    %% Communication Layer
    subgraph MessageBroker
        RMQ["RabbitMQ Cluster<br/>SSL/TLS & Federation"]:::messagebroker
    end

    %% Core Server Layer
    subgraph CoreServer
        DTA["Dog Trainer App"]:::core
        click DTA "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_trainer_app.erl"
        
        subgraph Supervisors
            MSuper["Main Supervisor"]:::supervisor
            click MSuper "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_trainer_sup.erl"
            FTSuper["File Transfer Supervisor"]:::supervisor
            click FTSuper "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_file_transfer_sup.erl"
        end

        subgraph APIs
            APIv1["API v1"]:::api
            click APIv1 "https://github.com/relaypro-open/dog_trainer/blob/main/src/api_handler.erl"
            APIv2["API v2"]:::api
            click APIv2 "https://github.com/relaypro-open/dog_trainer/blob/main/src/api_handler_v2.erl"
            Router["API Router"]:::api
            click Router "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_api_router.erl"
        end

        subgraph CoreServices
            Config["Configuration Manager"]:::service
            click Config "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_config.erl"
            State["State Manager"]:::service
            click State "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_state.erl"
            FileTransfer["File Transfer System"]:::service
            click FileTransfer "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_file_transfer.erl"
        end

        subgraph Watchers
            GWatch["Group Watcher"]:::watcher
            click GWatch "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_group_watcher.erl"
            HWatch["Host Watcher"]:::watcher
            click HWatch "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_host_config_watcher.erl"
            PWatch["Profile Watcher"]:::watcher
            click PWatch "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_profile_watcher.erl"
            SWatch["Service Watcher"]:::watcher
            click SWatch "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_service_watcher.erl"
        end

        subgraph Monitoring
            Health["Health Checks"]:::monitor
            click Health "https://github.com/relaypro-open/dog_trainer/blob/main/src/healthcheck_api_handler.erl"
            KAlive["Keepalive Agent"]:::monitor
            click KAlive "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_keepalive_agent.erl"
        end

        subgraph EC2Integration
            EC2SG["EC2 Security Groups"]:::aws
            click EC2SG "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_ec2_sg.erl"
            EC2Update["EC2 Update Agent"]:::aws
            click EC2Update "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_ec2_update_agent.erl"
        end

        subgraph Schemas
            ExtSchema["External Schema"]:::schema
            click ExtSchema "https://github.com/relaypro-open/dog_trainer/blob/main/priv/schema/external/external-schema.json"
            GroupSchema["Group Schema"]:::schema
            click GroupSchema "https://github.com/relaypro-open/dog_trainer/blob/main/priv/schema/group/group-schema.json"
            HostSchema["Host Schema"]:::schema
            click HostSchema "https://github.com/relaypro-open/dog_trainer/blob/main/priv/schema/host/host-schema.json"
            ServiceSchema["Service Schema"]:::schema
            click ServiceSchema "https://github.com/relaypro-open/dog_trainer/blob/main/priv/schema/service/service-schema.json"
        end
    end

    %% Persistence Layer
    subgraph Database
        RDB["RethinkDB Cluster"]:::database
        DBOps["Database Operations"]:::database
        click DBOps "https://github.com/relaypro-open/dog_trainer/blob/main/src/dog_rethink.erl"
    end

    %% Security
    subgraph Security
        CA["Certificate Authority"]:::security
        click CA "https://github.com/relaypro-open/dog_trainer/blob/main/csc_certs.sh"
        Config_Dir["Configuration Files"]:::security
        click Config_Dir "https://github.com/relaypro-open/dog_trainer/tree/main/config/"
    end

    %% Relationships
    A1 & A2 & A3 & A4 --> RMQ
    RMQ --> DTA
    DTA --> RMQ
    DTA --> MSuper
    MSuper --> FTSuper
    Router --> APIv1 & APIv2
    DBOps --> RDB
    CoreServices --> DBOps
    Watchers --> CoreServices
    EC2Integration --> CoreServices
    Monitoring --> CoreServices
    Schemas --> APIs

    %% Styles
    classDef core fill:#a8d1ff,stroke:#333,stroke-width:2px
    classDef supervisor fill:#ffb366,stroke:#333,stroke-width:2px
    classDef api fill:#90EE90,stroke:#333,stroke-width:2px
    classDef service fill:#b19cd9,stroke:#333,stroke-width:2px
    classDef watcher fill:#ffcc99,stroke:#333,stroke-width:2px
    classDef monitor fill:#ff9999,stroke:#333,stroke-width:2px
    classDef aws fill:#ffd699,stroke:#333,stroke-width:2px
    classDef schema fill:#c2f0c2,stroke:#333,stroke-width:2px
    classDef database fill:#ffdb4d,stroke:#333,stroke-width:2px
    classDef messagebroker fill:#85e085,stroke:#333,stroke-width:2px
    classDef security fill:#ff8080,stroke:#333,stroke-width:2px
```
