# インフラ構成図

## 全体システム構成図

```mermaid
graph TB
    subgraph Internet["インターネット"]
        User[ユーザー]
        ExtServices[外部サービス]
    end

    subgraph LoadBalancer["ロードバランサー層"]
        Nginx[Nginx<br/>SSL終端/リバースプロキシ<br/>:80, :443]
    end

    subgraph AppLayer["アプリケーション層"]
        Rails[Rails Web Server<br/>Puma<br/>:3000]
        Sidekiq[Sidekiq Worker<br/>バックグラウンドジョブ]
        ActionCable[ActionCable<br/>WebSocket]
    end

    subgraph DataLayer["データ層"]
        PostgreSQL[(PostgreSQL 16<br/>pgvector拡張<br/>:5432)]
        Redis[(Redis<br/>キャッシュ/キュー/PubSub<br/>:6379)]
    end

    subgraph Storage["ストレージ層"]
        LocalStorage[ローカルストレージ]
        S3[Amazon S3]
        GCS[Google Cloud Storage]
        Azure[Azure Blob Storage]
    end

    User -->|HTTPS| Nginx
    Nginx -->|HTTP| Rails
    Nginx -->|WebSocket| ActionCable

    Rails --> PostgreSQL
    Rails --> Redis
    Rails --> ActionCable

    Sidekiq --> PostgreSQL
    Sidekiq --> Redis

    ActionCable --> Redis

    Rails --> LocalStorage
    Rails -.-> S3
    Rails -.-> GCS
    Rails -.-> Azure

    ExtServices -.->|Webhook/API| Nginx
```

## Docker Compose構成図（本番環境）

```mermaid
graph TB
    subgraph DockerNetwork["Docker Network"]
        subgraph Services["サービス群"]
            Rails[rails<br/>chatwoot/chatwoot:latest<br/>:3000]
            Sidekiq[sidekiq<br/>chatwoot/chatwoot:latest]
        end

        subgraph DataServices["データサービス"]
            PostgreSQL[(postgres<br/>pgvector/pgvector:pg16<br/>:5432)]
            Redis[(redis<br/>redis:alpine<br/>:6379)]
        end
    end

    subgraph Volumes["永続化ボリューム"]
        StorageVol[storage_data]
        PostgresVol[postgres_data]
        RedisVol[redis_data]
    end

    Rails -->|depends_on| PostgreSQL
    Rails -->|depends_on| Redis
    Sidekiq -->|depends_on| PostgreSQL
    Sidekiq -->|depends_on| Redis

    Rails -.-> StorageVol
    PostgreSQL -.-> PostgresVol
    Redis -.-> RedisVol
```

## Docker Compose構成図（開発環境）

```mermaid
graph TB
    subgraph DockerNetwork["Docker Network"]
        subgraph AppServices["アプリケーションサービス"]
            Rails[rails<br/>chatwoot-rails:development<br/>:3000]
            Sidekiq[sidekiq<br/>chatwoot-rails:development]
            Vite[vite<br/>chatwoot-vite:development<br/>:3036]
        end

        subgraph DataServices["データサービス"]
            PostgreSQL[(postgres<br/>pgvector/pgvector:pg16<br/>:5432)]
            Redis[(redis<br/>redis:alpine<br/>:6379)]
        end

        subgraph DevTools["開発ツール"]
            Mailhog[mailhog<br/>:1025, :8025]
        end
    end

    subgraph Volumes["永続化ボリューム"]
        PostgresVol[postgres]
        RedisVol[redis]
        PacksVol[packs]
        NodeModulesVol[node_modules]
        CacheVol[cache]
        BundleVol[bundle]
    end

    Rails -->|depends_on| PostgreSQL
    Rails -->|depends_on| Redis
    Rails -->|depends_on| Vite
    Rails -->|depends_on| Sidekiq
    Rails -->|depends_on| Mailhog

    Sidekiq -->|depends_on| PostgreSQL
    Sidekiq -->|depends_on| Redis
    Sidekiq -->|depends_on| Mailhog

    PostgreSQL -.-> PostgresVol
    Redis -.-> RedisVol
    Rails -.-> PacksVol
    Rails -.-> NodeModulesVol
    Rails -.-> CacheVol
    Rails -.-> BundleVol
```

## ネットワーク構成図

```mermaid
graph LR
    subgraph External["外部ネットワーク"]
        Internet((Internet))
    end

    subgraph DMZ["DMZ"]
        Nginx[Nginx<br/>:80/:443]
    end

    subgraph Internal["内部ネットワーク"]
        Rails[Rails<br/>:3000]
        Sidekiq[Sidekiq]
        PostgreSQL[(PostgreSQL<br/>:5432)]
        Redis[(Redis<br/>:6379)]
    end

    Internet -->|HTTPS :443| Nginx
    Internet -->|HTTP :80| Nginx
    Nginx -->|HTTP :3000| Rails

    Rails <-->|TCP :5432| PostgreSQL
    Rails <-->|TCP :6379| Redis
    Sidekiq <-->|TCP :5432| PostgreSQL
    Sidekiq <-->|TCP :6379| Redis
```

## Sidekiqキュー構成図

```mermaid
graph TD
    subgraph Queues["Sidekiqキュー（優先度順）"]
        Critical[critical<br/>最優先]
        High[high<br/>高優先度]
        Medium[medium<br/>中優先度]
        Default[default<br/>標準]
        Mailers[mailers<br/>メール送信]
        ActionMailbox[action_mailbox_routing<br/>受信メール]
        Low[low<br/>低優先度]
        Scheduled[scheduled_jobs<br/>定期実行]
        Deferred[deferred<br/>遅延]
        Purgable[purgable<br/>削除可能]
        Housekeeping[housekeeping<br/>メンテナンス]
        AsyncDB[async_database_migration<br/>非同期DB]
        BulkReindex[bulk_reindex_low<br/>大量インデックス]
        ASAnalysis[active_storage_analysis<br/>ファイル解析]
        ASPurge[active_storage_purge<br/>ファイル削除]
        AMIncineration[action_mailbox_incineration<br/>メール削除]
    end

    subgraph Workers["Sidekiqワーカー"]
        Worker1[Worker Process 1]
        Worker2[Worker Process 2]
        WorkerN[Worker Process N]
    end

    subgraph Backend["バックエンド"]
        Redis[(Redis)]
    end

    Critical --> Worker1
    High --> Worker1
    Medium --> Worker2
    Default --> Worker2
    Mailers --> WorkerN
    Low --> WorkerN

    Worker1 <--> Redis
    Worker2 <--> Redis
    WorkerN <--> Redis
```

## Redis接続プール構成図

```mermaid
graph TB
    subgraph Application["アプリケーション"]
        Rails[Rails Web]
        Sidekiq[Sidekiq Worker]
        ActionCable[ActionCable]
    end

    subgraph ConnectionPools["Redisコネクションプール"]
        Alfred["$alfred<br/>ConnectionPool<br/>サイズ: REDIS_ALFRED_SIZE(5)<br/>用途: Round Robin, Email, Presence"]
        Velma["$velma<br/>ConnectionPool<br/>サイズ: 5<br/>用途: Rack Attack"]
        SidekiqPool[Sidekiq Redis Pool<br/>用途: ジョブキュー]
        CablePool[ActionCable Redis Pool<br/>用途: WebSocket PubSub]
    end

    subgraph Redis["Redis"]
        RedisMain[(Redis Server<br/>:6379)]
    end

    Rails --> Alfred
    Rails --> Velma
    Sidekiq --> SidekiqPool
    ActionCable --> CablePool

    Alfred --> RedisMain
    Velma --> RedisMain
    SidekiqPool --> RedisMain
    CablePool --> RedisMain
```

## 高可用性構成図（Redis Sentinel）

```mermaid
graph TB
    subgraph Application["アプリケーション"]
        App[Rails/Sidekiq]
    end

    subgraph Sentinels["Redis Sentinel群"]
        Sentinel1[Sentinel 1<br/>:26379]
        Sentinel2[Sentinel 2<br/>:26379]
        Sentinel3[Sentinel 3<br/>:26379]
    end

    subgraph RedisCluster["Redis Cluster"]
        Master[(Redis Master<br/>mymaster)]
        Replica1[(Redis Replica 1)]
        Replica2[(Redis Replica 2)]
    end

    App -->|監視問い合わせ| Sentinel1
    App -->|監視問い合わせ| Sentinel2
    App -->|監視問い合わせ| Sentinel3

    Sentinel1 -.->|監視| Master
    Sentinel2 -.->|監視| Master
    Sentinel3 -.->|監視| Master

    Master -->|レプリケーション| Replica1
    Master -->|レプリケーション| Replica2

    App -->|読み書き| Master
```

## 外部サービス連携図

```mermaid
graph TB
    subgraph Chatwoot["Chatwoot"]
        Rails[Rails Application]
    end

    subgraph Storage["ストレージサービス"]
        S3[Amazon S3]
        GCS[Google Cloud Storage]
        Azure[Azure Blob Storage]
        Minio[Minio/S3互換]
    end

    subgraph Messaging["メッセージングチャネル"]
        Facebook[Facebook Messenger]
        Twitter[Twitter/X]
        Slack[Slack]
        LINE[LINE]
        Twilio[Twilio SMS/Voice]
    end

    subgraph AI["AI/ML サービス"]
        OpenAI[OpenAI API]
        Dialogflow[Google Dialogflow]
        GoogleTranslate[Google Translate]
    end

    subgraph Monitoring["監視サービス"]
        Sentry[Sentry]
        NewRelic[New Relic]
        Datadog[Datadog]
        ElasticAPM[Elastic APM]
        Scout[Scout APM]
    end

    subgraph Notification["通知サービス"]
        FCM[Firebase Cloud Messaging]
        WebPush[Web Push / VAPID]
        SMTP[SMTP Server]
    end

    subgraph Payment["決済サービス"]
        Stripe[Stripe]
    end

    subgraph Search["検索サービス"]
        OpenSearch[OpenSearch/Elasticsearch]
    end

    Rails -.-> S3
    Rails -.-> GCS
    Rails -.-> Azure
    Rails -.-> Minio

    Rails -.-> Facebook
    Rails -.-> Twitter
    Rails -.-> Slack
    Rails -.-> LINE
    Rails -.-> Twilio

    Rails -.-> OpenAI
    Rails -.-> Dialogflow
    Rails -.-> GoogleTranslate

    Rails -.-> Sentry
    Rails -.-> NewRelic
    Rails -.-> Datadog
    Rails -.-> ElasticAPM
    Rails -.-> Scout

    Rails -.-> FCM
    Rails -.-> WebPush
    Rails -.-> SMTP

    Rails -.-> Stripe

    Rails -.-> OpenSearch
```

## Systemdサービス構成図

```mermaid
graph TB
    subgraph SystemdTargets["Systemd Targets"]
        ChatwootTarget[chatwoot.target]
        WebTarget[chatwoot-web.target]
        WorkerTarget[chatwoot-worker.target]
    end

    subgraph Services["Systemd Services"]
        WebService[chatwoot-web.1.service<br/>Rails Server :3000]
        WorkerService[chatwoot-worker.1.service<br/>Sidekiq Worker<br/>MemoryMax: 1.2G]
    end

    ChatwootTarget --> WebTarget
    ChatwootTarget --> WorkerTarget

    WebTarget --> WebService
    WorkerTarget --> WorkerService
```

## デプロイメントフロー図

```mermaid
sequenceDiagram
    participant Dev as 開発者
    participant CI as CI/CD
    participant Registry as Container Registry
    participant Server as 本番サーバー
    participant DB as PostgreSQL

    Dev->>CI: コードプッシュ
    CI->>CI: テスト実行
    CI->>CI: Dockerイメージビルド
    CI->>Registry: イメージプッシュ

    CI->>Server: デプロイトリガー
    Server->>Registry: イメージプル
    Server->>DB: rails db:chatwoot_prepare
    Server->>Server: コンテナ再起動
    Server->>Server: ヘルスチェック
```
