# jiangyang118/datacenter architecture

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

- Interactive diagram: https://gitdiagram.com/jiangyang118/datacenter
- Repository: https://github.com/jiangyang118/datacenter

## Overview

##### Project type and purpose
- This is a full-stack PHP web application built on ThinkPHP 5.0.
- It exposes both a web UI (in `public/ksdist` and `application/index`) and a REST-ful API (`application/api`), plus background job scheduling (`application/cron`) and asynchronous workers (`application/worker`).
- Its domain: synchronizing and processing biomedical device data (HeartRate, Wattbike, Centaur, etc.), storing results in a database, and optionally pushing files to Aliyun OSS.

a. Clients & Entry Points
– Web Browser / SPA: loads `public/dist/index.html` or `public/ksdist/…` assets.
– API Consumers: mobile apps or other services calling endpoints under `/public/index.php/api/...`.
– Cron Scheduler: OS cron invoking shell scripts under `application/cron/shell` or running `application/cron/Sync*.php` via CLI.
b. Web Server Layer
– Nginx / Apache serving `public/` directory, forwarding PHP requests to PHP-FPM.
– `.htaccess` and `router.php` enable rewrite rules.
c. ThinkPHP Core
– `thinkphp/` directory: framework bootstrap, routing, DI, caching, security, console/CLI support.
– `public/index.php` or `server.php` loads ThinkPHP, then dispatches to modules.
d. Application Modules
– API Module (`application/api/controller` & `/logic`): controllers implement device-specific sync and data ingestion logic.
– Web Module (`application/index/controller`): user-facing pages, dashboards, manual sync triggers.
– Common & Base (`application/common`, `application/base`): shared utilities (BaiduFace OCR, QwRobot), base controller/logic classes.
– Models (`application/model`): ORM definitions mapping to MySQL tables under `application/db`.
e. Background Processing
– Cron Jobs (`application/cron/command` & `/shell`): schedule data pulls from devices or databases (Centaur, Polar, KX21N, etc.).
– Queue & Worker (`application/extra/queue.php` + `application/worker`): cron or API may push tasks into an internal queue. Worker controllers consume tasks to perform heavy processing (e.g., file parsing, statistical calculations). Shell scripts under `application/worker/shell` orchestrate starting/restarting worker processes.
f. Data Stores & External Services
– MySQL: persistent data storage for configuration, device results, pipeline logs.
– Aliyun OSS (`extend/aliyun-oss-php-sdk`): optional blob storage for large files or report exports.
– Redis / Memcached: optional cache (configured via `application/config/*`).
– Third-party APIs: Baidu Face, MQTT channels, Inbody770, etc.

##### Relationships and data flows
- Browser → Web Server (HTTP) → PHP-FPM → ThinkPHP router → Web Controllers → Models → MySQL → Response → Browser
- Mobile/API Clients → Web Server → PHP-FPM → ThinkPHP router → API Controllers → Logic → Models → MySQL → JSON → Clients
- Cron Scheduler (OS cron) → Shell scripts → CLI `think` or PHP CLI → Cron Commands → Logic → Models / Queue → Worker
- Workers → internal queue → Worker Controllers → Logic → Models → database or external OSS

##### Architectural patterns and design principles
– MVC (Controllers in `application/*/controller`, Models in `application/model`, Views in `public/ksdist` or ThinkPHP’s template engine)
– Layered architecture (Controllers → Logic/Service → Models/DAO → Database)
– Asynchronous processing (Cron + Queue + Worker) for long-running tasks
– Dependency injection and PSR-4 autoloading via Composer
– Configuration management per environment (`application/config/dev|test|prod`)

##### Essential details
– Show environment configurations feeding into the ThinkPHP bootstrap (dev/test/prod).
– Include Composer/vendor layer under the core.
– Mark the queue layer (even if in-app).
– Don’t over-split; group similar device controllers under “Device Sync API” or “Device Workers.”

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% OS Cron
    Cron("OS Cron"):::cron
    CronShell["Cron Shell Scripts"]:::cron
    click CronShell "https://github.com/jiangyang118/datacenter/tree/main/application/cron/shell/"
    CronCmd["Cron Commands"]:::cron
    click CronCmd "https://github.com/jiangyang118/datacenter/tree/main/application/cron/command"

    Cron -->|invoke| CronShell
    CronShell -->|runs| CronCmd

    %% Clients
    Browser["Browser / SPA Client"]:::external
    click Browser "https://github.com/jiangyang118/datacenter/blob/main/public/dist/index.html"
    Mobile["API Clients"]:::external
    CLI["CLI (think)"]:::external
    click CLI "https://github.com/jiangyang118/datacenter/tree/main/think"

    Browser -->|HTTP| NG
    Mobile -->|HTTP| NG
    CLI -->|invoke| CronCmd

    %% Web Tier
    subgraph "Web Tier"
        NG["Nginx + PHP-FPM"]:::web
        click NG "https://github.com/jiangyang118/datacenter/blob/main/public/nginx.htaccess"
        Think["ThinkPHP Core"]:::web
        click Think "https://github.com/jiangyang118/datacenter/tree/main/thinkphp/"

        NG -->|HTTP| Think

        subgraph "Modules"
            API["API Module"]:::web
            click API "https://github.com/jiangyang118/datacenter/tree/main/application/api/controller/"
            WebMod["Web Module"]:::web
            click WebMod "https://github.com/jiangyang118/datacenter/tree/main/application/index/controller/"
            Common["Common Utilities & Base"]:::web
            click Common "https://github.com/jiangyang118/datacenter/blob/main/application/common/BaiduFace.php"
            Models["Models (ORM)"]:::web
            click Models "https://github.com/jiangyang118/datacenter/tree/main/application/model/"
        end

        Think --> API
        Think --> WebMod
        Think --> Common
        Think --> Models
    end

    %% Queue Layer
    subgraph "Queue Layer"
        Queue["Internal Queue"]:::datastore
    end

    %% Worker Tier
    subgraph "Worker Tier"
        WorkerShell["Worker Shell Scripts"]:::worker
        click WorkerShell "https://github.com/jiangyang118/datacenter/tree/main/application/worker/shell/"
        WorkerCtrl["Worker Controllers"]:::worker
        click WorkerCtrl "https://github.com/jiangyang118/datacenter/tree/main/application/worker/controller/"
    end

    %% Data Tier
    subgraph "Data Tier"
        MySQL["MySQL DB"]:::datastore
        Redis["Redis / Memcached"]:::datastore
        OSS["Aliyun OSS"]:::external
        ThirdParty["Third-party APIs"]:::external
    end

    %% Data Flows
    API -->|"DB read/write"| MySQL
    WebMod -->|"DB read/write"| MySQL
    API -->|"cache"| Redis
    WebMod -->|"cache"| Redis

    CronCmd -->|"enqueue tasks"| Queue
    API -->|"enqueue tasks"| Queue

    Queue -->|"consume"| WorkerCtrl
    WorkerCtrl -->|"DB write"| MySQL
    WorkerCtrl -->|"push files"| OSS
    WorkerCtrl -->|"call"| ThirdParty

    %% Styles
    classDef web fill:#E0F7FA,stroke:#006064,color:#006064
    classDef cron fill:#FFF3E0,stroke:#E65100,color:#E65100
    classDef worker fill:#E8F5E9,stroke:#2E7D32,color:#2E7D32
    classDef external fill:#ECEFF1,stroke:#607D8B,color:#37474F
    classDef datastore fill:#FFFDE7,stroke:#F9A825,color:#F9A825
```
