# layeddie/office_server architecture

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

- Interactive diagram: https://gitdiagram.com/layeddie/office_server
- Repository: https://github.com/layeddie/office_server

## Overview

##### Main Components

a. Web Layer (Phoenix):
- LiveView Components (office_live, office_list_live)
- WebSocket Handlers (box_comms, browser_image)
- HTTP Controllers
- Router

b. Core Application Layer:
- Device Management System
- Temperature Monitoring System
- Authentication Service
- PubSub System
- Presence Service

c. Data Layer:
- Postgres Database (implied by the presence of Repo and migrations)
- Schema representations (Devices, Temperatures)

d. External Components:
- Browser Clients

##### Component Relationships

- Browser Clients → LiveView + WebSocket Connections
- Web Layer → Core Application (bidirectional)
- Core Application → Database Layer (bidirectional)

##### Special Architectural Elements

Specific architectural patterns:
- PubSub system for real-time updates
- Supervisor tree for monitoring (monitoring_supervisor)
- WebSocket handlers for device communication
- LiveView for real-time UI updates
- Presence tracking for connected devices

##### Key Features to Emphasize

Create callouts or annotations for:
- Temperature monitoring system
- Device management capabilities
- Real-time data flow
- Authentication system
- Chart generation (vegalite integration)

##### Technical Details

A technical stack sidebar showing:
- Elixir/Phoenix framework
- Phoenix LiveView
- WebSocket protocols
- Postgres database
- Tailwind CSS (frontend)

##### Monitoring and alerts

Remember to:
- Keep the main components visually distinct
- Use consistent symbols throughout
- Notes about scalability considerations
- Error handling paths
- Monitoring points

This design should reflect the Elixir/Phoenix architectural patterns while highlighting the specific requirements of an IoT sensor monitoring system.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph External["External Layer"]
        SB["Sensor Boxes"]:::external
        BC["Browser Clients"]:::external
    end

    subgraph Web["Phoenix Web Layer"]
        direction TB
        Router["Router"]:::web
        click Router "https://github.com/layeddie/office_server/blob/main/lib/office_server_web/router.ex"
        
        subgraph LiveView["LiveView Components"]
            OL["Office Live"]:::web
            OLL["Office List Live"]:::web
            click OL "https://github.com/layeddie/office_server/blob/main/lib/office_server_web/live/office_live.ex"
            click OLL "https://github.com/layeddie/office_server/blob/main/lib/office_server_web/live/office_list_live.ex"
        end

        subgraph WS["WebSocket Handlers"]
            BC_Socket["Box Communications Socket"]:::websocket
            BI_Socket["Browser Image Socket"]:::websocket
            click BC_Socket "https://github.com/layeddie/office_server/blob/main/lib/office_server_web/box_comms/socket_handler.ex"
            click BI_Socket "https://github.com/layeddie/office_server/blob/main/lib/office_server_web/browser_image/browser_image_socket.ex"
        end

        Telemetry["Telemetry"]:::support
        click Telemetry "https://github.com/layeddie/office_server/blob/main/lib/office_server_web/telemetry.ex"
    end

    subgraph Core["Core Application Layer"]
        direction TB
        App["Application Core"]:::core
        click App "https://github.com/layeddie/office_server/blob/main/lib/office_server/application.ex"

        subgraph DeviceSystem["Device Management"]
            DM["Device Manager"]:::core
            DD["Device Data"]:::core
            click DM "https://github.com/layeddie/office_server/blob/main/lib/office_server/devices/device.ex"
            click DD "https://github.com/layeddie/office_server/blob/main/lib/office_server/device_data.ex"
        end

        subgraph MonSystem["Monitoring System"]
            MS["Monitoring Supervisor"]:::core
            TM["Temperature Monitor"]:::core
            TC["Temperature Charts"]:::core
            click MS "https://github.com/layeddie/office_server/blob/main/lib/office_server/monitoring/monitoring_supervisor.ex"
            click TM "https://github.com/layeddie/office_server/blob/main/lib/office_server/monitoring/temperature_monitor.ex"
            click TC "https://github.com/layeddie/office_server/blob/main/lib/office_server/temperature_charts.ex"
        end

        Auth["Authentication Service"]:::support
        click Auth "https://github.com/layeddie/office_server/blob/main/lib/office_server/authentication.ex"
        
        PubSub["PubSub System"]:::support
        click PubSub "https://github.com/layeddie/office_server/blob/main/lib/office_server/all_device_pub_sub.ex"
        
        Presence["Presence Service"]:::support
        click Presence "https://github.com/layeddie/office_server/blob/main/lib/office_server_web/presence.ex"
    end

    subgraph Data["Database Layer"]
        DB[("Postgres Database")]:::database
        Repo["Repository"]:::database
        click Repo "https://github.com/layeddie/office_server/blob/main/lib/office_server/repo.ex"
        
        subgraph Schemas["Database Schemas"]
            DS["Device Schema"]:::database
            TS["Temperature Schema"]:::database
            click TS "https://github.com/layeddie/office_server/blob/main/lib/office_server/temperatures/temperature.ex"
        end
    end

    %% External to Web Layer
    SB --> BC_Socket
    BC --> Router
    BC --> LiveView

    %% Web Layer Internal
    Router --> LiveView
    Router --> WS
    BC_Socket --> PubSub
    BI_Socket --> PubSub

    %% Web to Core
    LiveView --> DeviceSystem
    LiveView --> MonSystem
    WS --> Auth

    %% Core Internal
    DeviceSystem --> PubSub
    MonSystem --> PubSub
    PubSub --> Presence

    %% Core to Data
    DeviceSystem --> Repo
    MonSystem --> Repo
    Repo --> DB
    Repo --> Schemas

    %% Monitoring
    Telemetry --> MonSystem

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:2px
    classDef web fill:#3498db,stroke:#333,stroke-width:2px
    classDef websocket fill:#e74c3c,stroke:#333,stroke-width:2px
    classDef core fill:#2ecc71,stroke:#333,stroke-width:2px
    classDef support fill:#9b59b6,stroke:#333,stroke-width:2px
    classDef database fill:#f1c40f,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        E1["External Components"]:::external
        W1["Web Components"]:::web
        WS1["WebSocket Components"]:::websocket
        C1["Core Components"]:::core
        S1["Support Services"]:::support
        D1["Database Components"]:::database
    end
```
