# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "Client Layer"
        WEB[Web Browser<br/>React SPA]
        MOBILE[InvenTree<br/>Mobile App]
        API_CLIENT[External<br/>API Client]
    end

    subgraph "Load Balancer / Reverse Proxy"
        NGINX[Nginx / Caddy]
    end

    subgraph "Application Server"
        GUNICORN[Gunicorn<br/>WSGI Server]

        subgraph "Django Application"
            MW[Middleware Chain]
            URL[URL Dispatcher]
            DRF[Django REST Framework]
            VIEWS[Views / ViewSets]
            SERIAL[Serializers]
            MODELS[Django Models]
        end

        subgraph "Background Workers"
            DJANGO_Q[Django-Q<br/>Worker Process]
            SCHEDULER[Task Scheduler]
        end
    end

    subgraph "Data Layer"
        DB[(PostgreSQL<br/>MySQL<br/>SQLite)]
        REDIS[(Redis<br/>Cache/Broker)]
    end

    subgraph "External Services"
        SMTP[Email Server<br/>SMTP/ESP]
        SENTRY[Sentry<br/>Error Tracking]
        SSO[SSO Provider<br/>OAuth2/OIDC]
        LDAP[LDAP Server]
        S3[S3 Storage<br/>Media Files]
        OTEL[OpenTelemetry<br/>Collector]
    end

    WEB --> NGINX
    MOBILE --> NGINX
    API_CLIENT --> NGINX

    NGINX --> GUNICORN
    GUNICORN --> MW
    MW --> URL
    URL --> DRF
    DRF --> VIEWS
    VIEWS --> SERIAL
    SERIAL --> MODELS

    MODELS --> DB
    VIEWS --> REDIS

    DJANGO_Q --> DB
    DJANGO_Q --> REDIS
    SCHEDULER --> DJANGO_Q

    VIEWS --> SMTP
    VIEWS --> SSO
    VIEWS --> LDAP
    MW --> SENTRY
    MW --> OTEL
    MODELS --> S3
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "Presentation Layer"
        subgraph "React Frontend"
            PAGES[Pages]
            COMPONENTS[Components]
            TABLES[Tables]
            FORMS[Forms]
            HOOKS[Custom Hooks]
            STATES[Zustand Store]
        end
    end

    subgraph "API Gateway Layer"
        subgraph "Django Middleware"
            SEC_MW[SecurityMiddleware]
            SESSION_MW[SessionMiddleware]
            CORS_MW[CorsMiddleware]
            AUTH_MW[AuthenticationMiddleware]
            AUTH_REQ_MW[AuthRequiredMiddleware]
            MFA_MW[Check2FAMiddleware]
        end
    end

    subgraph "Application Service Layer"
        subgraph "Django REST Framework"
            VIEWSETS[ViewSets]
            SERIALIZERS[Serializers]
            PERMISSIONS[Permissions]
            FILTERS[Filters]
            PAGINATION[Pagination]
        end
    end

    subgraph "Domain Layer"
        subgraph "Django Apps"
            PART_APP[Part App]
            STOCK_APP[Stock App]
            ORDER_APP[Order App]
            BUILD_APP[Build App]
            COMPANY_APP[Company App]
            REPORT_APP[Report App]
            USERS_APP[Users App]
            PLUGIN_APP[Plugin App]
            COMMON_APP[Common App]
        end
    end

    subgraph "Infrastructure Layer"
        ORM[Django ORM]
        CACHE[Cache Backend]
        STORAGE[Storage Backend]
        EMAIL[Email Backend]
        PLUGIN_REG[Plugin Registry]
    end

    PAGES --> COMPONENTS
    PAGES --> TABLES
    PAGES --> FORMS
    COMPONENTS --> HOOKS
    HOOKS --> STATES

    SEC_MW --> SESSION_MW
    SESSION_MW --> CORS_MW
    CORS_MW --> AUTH_MW
    AUTH_MW --> AUTH_REQ_MW
    AUTH_REQ_MW --> MFA_MW

    VIEWSETS --> SERIALIZERS
    SERIALIZERS --> PERMISSIONS
    VIEWSETS --> FILTERS
    VIEWSETS --> PAGINATION

    PART_APP --> ORM
    STOCK_APP --> ORM
    ORDER_APP --> ORM
    BUILD_APP --> ORM
    COMPANY_APP --> ORM
    REPORT_APP --> ORM
    USERS_APP --> ORM
    PLUGIN_APP --> PLUGIN_REG
    COMMON_APP --> CACHE
    REPORT_APP --> STORAGE
```

## バックエンドモジュール依存関係図

```mermaid
graph LR
    subgraph "Core Module"
        INVENTREE[InvenTree<br/>Core]
        COMMON[common]
        USERS[users]
        GENERIC[generic]
    end

    subgraph "Domain Modules"
        PART[part]
        STOCK[stock]
        ORDER[order]
        BUILD[build]
        COMPANY[company]
    end

    subgraph "Support Modules"
        REPORT[report]
        PLUGIN[plugin]
        MACHINE[machine]
        IMPORTER[importer]
        DATA_EXPORT[data_exporter]
    end

    %% Core dependencies
    INVENTREE --> COMMON
    INVENTREE --> USERS
    INVENTREE --> GENERIC

    %% Domain dependencies
    PART --> COMMON
    PART --> COMPANY

    STOCK --> COMMON
    STOCK --> PART
    STOCK --> COMPANY

    ORDER --> COMMON
    ORDER --> PART
    ORDER --> STOCK
    ORDER --> COMPANY

    BUILD --> COMMON
    BUILD --> PART
    BUILD --> STOCK

    COMPANY --> COMMON

    %% Support dependencies
    REPORT --> PART
    REPORT --> STOCK
    REPORT --> ORDER
    REPORT --> BUILD
    REPORT --> COMPANY

    PLUGIN --> INVENTREE

    MACHINE --> COMMON
    MACHINE --> PLUGIN

    IMPORTER --> COMMON
    IMPORTER --> PART
    IMPORTER --> STOCK
    IMPORTER --> COMPANY

    DATA_EXPORT --> COMMON
```

## フロントエンドコンポーネント構成図

```mermaid
graph TB
    subgraph "Entry Point"
        MAIN[main.tsx]
        APP[App.tsx]
        ROUTER[router.tsx]
    end

    subgraph "Layout Components"
        LAYOUT[Layout]
        NAV[Navigation]
        SIDEBAR[Sidebar]
        HEADER[Header]
    end

    subgraph "Page Components"
        HOME[Home]
        PART_PAGES[Part Pages]
        STOCK_PAGES[Stock Pages]
        ORDER_PAGES[Order Pages]
        BUILD_PAGES[Build Pages]
        SETTINGS_PAGES[Settings Pages]
        AUTH_PAGES[Auth Pages]
    end

    subgraph "Shared Components"
        TABLES_C[Tables]
        FORMS_C[Forms]
        MODALS[Modals]
        BUTTONS[Buttons]
        CARDS[Cards]
    end

    subgraph "State Management"
        ZUSTAND[Zustand Stores]
        TQ[TanStack Query]
        CONTEXTS[React Contexts]
    end

    subgraph "Utilities"
        HOOKS_U[Custom Hooks]
        FUNCTIONS[Helper Functions]
        TYPES[TypeScript Types]
    end

    MAIN --> APP
    APP --> ROUTER
    ROUTER --> LAYOUT

    LAYOUT --> NAV
    LAYOUT --> SIDEBAR
    LAYOUT --> HEADER

    ROUTER --> HOME
    ROUTER --> PART_PAGES
    ROUTER --> STOCK_PAGES
    ROUTER --> ORDER_PAGES
    ROUTER --> BUILD_PAGES
    ROUTER --> SETTINGS_PAGES
    ROUTER --> AUTH_PAGES

    PART_PAGES --> TABLES_C
    PART_PAGES --> FORMS_C
    STOCK_PAGES --> TABLES_C
    STOCK_PAGES --> FORMS_C
    ORDER_PAGES --> TABLES_C
    ORDER_PAGES --> FORMS_C

    TABLES_C --> ZUSTAND
    FORMS_C --> ZUSTAND
    TABLES_C --> TQ

    ZUSTAND --> HOOKS_U
    TQ --> HOOKS_U
    HOOKS_U --> FUNCTIONS
    FUNCTIONS --> TYPES
```

## 認証・認可フロー図

```mermaid
sequenceDiagram
    participant C as Client
    participant MW as Middleware
    participant AUTH as Auth Backend
    participant DB as Database
    participant EXT as External IdP

    %% Session Authentication Flow
    rect rgb(200, 230, 200)
        Note over C,DB: Session Authentication (Web)
        C->>MW: POST /api/auth/login
        MW->>AUTH: Authenticate User
        AUTH->>DB: Verify Credentials
        DB-->>AUTH: User Data
        AUTH-->>MW: Auth Success
        MW->>DB: Create Session
        MW-->>C: Set-Cookie: sessionid
    end

    %% Token Authentication Flow
    rect rgb(200, 200, 230)
        Note over C,DB: Token Authentication (API)
        C->>MW: GET /api/part/ (Authorization: Token xxx)
        MW->>AUTH: ApiTokenAuthentication
        AUTH->>DB: Lookup Token
        DB-->>AUTH: Token + User
        AUTH-->>MW: Request.user = User
        MW-->>C: API Response
    end

    %% SSO Authentication Flow
    rect rgb(230, 200, 200)
        Note over C,EXT: SSO Authentication
        C->>MW: GET /accounts/login/google/
        MW->>EXT: Redirect to Google
        EXT-->>C: Auth Code
        C->>MW: Callback with Code
        MW->>EXT: Exchange Code for Token
        EXT-->>MW: User Info
        MW->>DB: Create/Update User
        MW-->>C: Session Created
    end

    %% MFA Flow
    rect rgb(230, 230, 200)
        Note over C,DB: MFA Verification
        C->>MW: Any Request (MFA Required)
        MW->>MW: Check2FAMiddleware
        MW-->>C: 401 (mfa_register required)
        C->>MW: POST /api/auth/2fa/authenticate
        MW->>DB: Verify TOTP Code
        DB-->>MW: MFA Verified
        MW-->>C: Session Updated
    end
```

## データフロー図

```mermaid
flowchart TB
    subgraph "Request Flow"
        R1[HTTP Request] --> R2[Gunicorn]
        R2 --> R3[Middleware Chain]
        R3 --> R4[URL Routing]
        R4 --> R5[DRF ViewSet]
        R5 --> R6[Serializer]
        R6 --> R7[Model/ORM]
        R7 --> R8[(Database)]
        R8 --> R7
        R7 --> R6
        R6 --> R5
        R5 --> R9[HTTP Response]
    end

    subgraph "Background Task Flow"
        T1[Task Trigger] --> T2[schedule_task]
        T2 --> T3[Django-Q Queue]
        T3 --> T4[(Redis/ORM Broker)]
        T4 --> T5[Worker Process]
        T5 --> T6[Task Execution]
        T6 --> T7[(Database)]
        T6 --> T8[External Service]
        T6 --> T9[Result/Notification]
    end

    subgraph "Plugin Integration"
        P1[Plugin Registry] --> P2[Load Plugins]
        P2 --> P3[Register Mixins]
        P3 --> P4[URL Endpoints]
        P3 --> P5[Event Handlers]
        P3 --> P6[Schedule Tasks]
        P3 --> P7[Report Templates]
    end
```

## デプロイメント構成図

```mermaid
graph TB
    subgraph "Production Environment"
        subgraph "Load Balancer"
            LB[Nginx / Traefik]
        end

        subgraph "Application Cluster"
            APP1[InvenTree<br/>Container 1]
            APP2[InvenTree<br/>Container 2]
            WORKER1[Django-Q<br/>Worker 1]
            WORKER2[Django-Q<br/>Worker 2]
        end

        subgraph "Data Services"
            PG[(PostgreSQL<br/>Primary)]
            PG_REP[(PostgreSQL<br/>Replica)]
            REDIS_C[(Redis<br/>Cluster)]
        end

        subgraph "Storage"
            S3_MEDIA[S3 / MinIO<br/>Media Storage]
            S3_BACKUP[S3 / MinIO<br/>Backup Storage]
        end

        subgraph "Monitoring"
            SENTRY_S[Sentry]
            OTEL_C[OpenTelemetry<br/>Collector]
            PROMETHEUS[Prometheus]
            GRAFANA[Grafana]
        end
    end

    subgraph "External Services"
        CDN[CDN<br/>Static Assets]
        MAIL[Email Service<br/>SMTP/ESP]
        SSO_P[SSO Provider]
    end

    LB --> APP1
    LB --> APP2

    APP1 --> PG
    APP2 --> PG
    APP1 --> REDIS_C
    APP2 --> REDIS_C

    WORKER1 --> PG
    WORKER2 --> PG
    WORKER1 --> REDIS_C
    WORKER2 --> REDIS_C

    PG --> PG_REP

    APP1 --> S3_MEDIA
    APP2 --> S3_MEDIA
    WORKER1 --> S3_BACKUP

    APP1 --> SENTRY_S
    APP1 --> OTEL_C
    OTEL_C --> PROMETHEUS
    PROMETHEUS --> GRAFANA

    LB --> CDN
    WORKER1 --> MAIL
    APP1 --> SSO_P
```

## プラグインシステム構成図

```mermaid
graph TB
    subgraph "Plugin System"
        REG[Plugin Registry]

        subgraph "Plugin Base Classes"
            BASE[InvenTreePlugin]
            BARCODE_MX[BarcodeMixin]
            REPORT_MX[ReportMixin]
            SCHEDULE_MX[ScheduleMixin]
            EVENT_MX[EventMixin]
            URL_MX[UrlsMixin]
            NAV_MX[NavigationMixin]
            APP_MX[AppMixin]
            SETTINGS_MX[SettingsMixin]
        end

        subgraph "Builtin Plugins"
            CORE_BARCODE[Core Barcode]
            CORE_LABEL[Core Label]
            CORE_REPORT[Core Report]
        end

        subgraph "Custom Plugins"
            CUSTOM1[Custom Plugin 1]
            CUSTOM2[Custom Plugin 2]
        end
    end

    subgraph "Integration Points"
        API_EXT[API Extensions]
        UI_EXT[UI Extensions]
        EVENT_HOOK[Event Hooks]
        TASK_HOOK[Scheduled Tasks]
    end

    REG --> BASE
    BASE --> BARCODE_MX
    BASE --> REPORT_MX
    BASE --> SCHEDULE_MX
    BASE --> EVENT_MX
    BASE --> URL_MX
    BASE --> NAV_MX
    BASE --> APP_MX
    BASE --> SETTINGS_MX

    BARCODE_MX --> CORE_BARCODE
    REPORT_MX --> CORE_LABEL
    REPORT_MX --> CORE_REPORT

    BASE --> CUSTOM1
    BASE --> CUSTOM2

    URL_MX --> API_EXT
    NAV_MX --> UI_EXT
    EVENT_MX --> EVENT_HOOK
    SCHEDULE_MX --> TASK_HOOK
```
