feat: backup architecture docs, source code, and scripts

This commit is contained in:
Warren
2026-04-25 17:15:45 +08:00
parent 59809dae1f
commit 1f84e5469f
368 changed files with 146329 additions and 261 deletions
@@ -0,0 +1,521 @@
---
document_type: "architecture_design"
service: "MOMENTRY_CORE"
title: "Playground 開發架構隔離規劃"
date: "2026-03-31"
version: "V1.0"
status: "active"
owner: "Warren"
created_by: "OpenCode"
tags:
- "開發架構隔離規劃"
- "playground"
ai_query_hints:
- "查詢 Playground 開發架構隔離規劃 的內容"
- "Playground 開發架構隔離規劃 的主要目的是什麼?"
- "如何操作或實施 Playground 開發架構隔離規劃?"
---
# Playground 開發架構隔離規劃
| 項目 | 內容 |
|------|------|
| 建立者 | Warren |
| 建立時間 | 2026-03-31 |
| 文件版本 | V1.0 |
---
## 版本歷史
| 版本 | 日期 | 目的 | 操作人 | 工具/模型 |
|------|------|------|--------|-----------|
| V1.0 | 2026-03-31 | 創建 Playground 隔離架構規劃 | Warren | OpenCode |
---
## 概述
本文檔說明 Momentry Core Playground(開發環境)的隔離架構規劃,確保開發測試環境與正式生產環境的數據能夠完整隔離,避免測試數據污染生產數據。
Playground 是 `momentry` 專案的開發專用二進制文件(binary),設計用於本地開發和功能測試,與生產環境(Production)使用不同的配置和資源池。
---
## 當前狀態
| 項目 | 狀態 |
|------|------|
| Redis 隔離 | ✅ 已隔離 |
| File System 隔離 | ✅ 已隔離 |
| PostgreSQL Schema 隔離 | 🔄 待實現 |
| MongoDB Database 隔離 | 🔄 待實現 |
| Qdrant Collection 隔離 | 🔄 待實現 |
---
## 隔離架構總覽
### 當前架構(部分隔離)
```
┌─────────────────────────────────────────────────────────────────┐
│ Playground (Development) 現況 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Playground │ │ Production │ │
│ │ Server │ │ Server │ │
│ │ Port:3003 │ │ Port:3002 │ │
│ │ │ │ │ │
│ │ Redis: │ │ Redis: │ │
│ │ momentry_dev│ │ momentry: │◀── 隔離 ✅ │
│ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │
│ │ ┌───────────────┴───────────────┐ │
│ │ │ 共享資源 │ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
│ │ PostgreSQL │ │ MongoDB │ │ Qdrant │ │
│ │ momentry │ │ momentry │ │ momentry_rule1 │ │
│ │ (同一DB) │ │ (同一DB) │ │ (同一collection) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
│ │ │ │ │
│ └────────────────────┴────────────────────┘ │
│ │ ❌ 未隔離 │
│ ▼ │
│ 數據混合污染風險 │
└─────────────────────────────────────────────────────────────────┘
```
### 規劃中的隔離架構
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ Playground 完整隔離架構 │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────┐ ┌─────────────────────────────────┐ │
│ │ Development (Playground) │ │ Production │ │
│ │ Port: 3003 │ │ Port: 3002 │ │
│ │ Binary: debug │ │ Binary: release │ │
│ │ Worker: disabled │ │ Worker: enabled │ │
│ └──────────────┬──────────────┘ └──────────────┬────────────────┘ │
│ │ │ │
│ │ ┌──────────────────────────────────┴──────────────┐ │
│ │ │ 共享基礎設施 │ │
│ │ └──────────────────────────────────┬──────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌────────────────────┐ ┌───────────────────────────────┐ │
│ │ PostgreSQL Schema │ │ PostgreSQL Schema │ │
│ │ (dev schema) │ │ (public schema) │ │
│ ├────────────────────┤ ├───────────────────────────────┤ │
│ │ videos_dev │ │ videos │ │
│ │ chunks_dev │ │ chunks │ │
│ │ pre_chunks_dev │ │ pre_chunks │ │
│ │ frames_dev │ │ frames │ │
│ │ processor_results │ (隔離 ✅) │ processor_results │ │
│ │ file_registry_dev │ │ file_registry │ │
│ │ face_*_dev │ │ face_* │ │
│ ├────────────────────┤ ├───────────────────────────────┤ │
│ │ 可共享: │ │ 可共享: │ │
│ │ api_keys (獨立的) │ │ api_keys │ │
│ │ monitor_* │ │ monitor_* │ │
│ │ backup_* │ │ backup_* │ │
│ └────────────────────┘ └───────────────────────────────┘ │
│ │
│ ┌────────────────────┐ ┌───────────────────────────────┐ │
│ │ MongoDB │ │ MongoDB │ │
│ │ Database: │ │ Database: │ │
│ │ momentry_dev │ (隔離 ✅) │ momentry │ │
│ ├────────────────────┤ ├───────────────────────────────┤ │
│ │ - chunks │ │ - chunks │ │
│ │ - cache │ │ - cache │ │
│ └────────────────────┘ └───────────────────────────────┘ │
│ │
│ ┌────────────────────┐ ┌───────────────────────────────┐ │
│ │ Qdrant │ │ Qdrant │ │
│ │ Collection: │ │ Collection: │ │
│ │ momentry_dev_ │ (隔離 ✅) │ momentry_rule1 │ │
│ │ rule1 │ │ │ │
│ └────────────────────┘ └───────────────────────────────┘ │
│ │
│ ┌────────────────────┐ ┌───────────────────────────────┐ │
│ │ Redis │ │ Redis │ │
│ │ Prefix: │ (已有 ✅) │ Prefix: │ │
│ │ momentry_dev: │ │ momentry: │ │
│ └────────────────────┘ └───────────────────────────────┘ │
│ │
│ ┌────────────────────┐ ┌───────────────────────────────┐ │
│ │ File System │ (已有 ✅) │ File System │ │
│ │ /output_dev │ │ /output │ │
│ │ /backup_dev │ │ /backup/momentry │ │
│ └────────────────────┘ └───────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## 隔離矩陣
| 資源 | Production | Development | 隔離狀態 | 隔離方式 |
|------|-----------|-------------|---------|---------|
| **API Server** | Port 3002 | Port 3003 | ✅ | 環境變數配置 |
| **Redis Prefix** | `momentry:` | `momentry_dev:` | ✅ | 環境變數配置 |
| **File Output** | `/Users/accusys/momentry/output` | `/Users/accusys/momentry/output_dev` | ✅ | 環境變數配置 |
| **File Backup** | `/Users/accusys/momentry/backup/momentry` | `/Users/accusys/momentry/backup/momentry_dev` | ✅ | 環境變數配置 |
| **PostgreSQL** | `public` schema | `dev` schema | ❌ | Schema 隔離 |
| **MongoDB** | `momentry` database | `momentry_dev` database | ❌ | Database 隔離 |
| **Qdrant** | `momentry_rule1` collection | `momentry_dev_rule1` collection | ❌ | Collection 隔離 |
---
## 需要隔離的數據表
### PostgreSQL(使用 Schema 隔離)
#### 需要隔離的表(放入 dev schema)
| 表名 | 說明 |
|------|------|
| `videos` | 視頻記錄 |
| `chunks` | 區塊數據 |
| `pre_chunks` | 預處理區塊 |
| `chunk_vectors` | 向量數據 |
| `frames` | 幀數據 |
| `processor_results` | 處理器結果 |
| `file_registry` | 文件註冊 |
| `file_lifecycle` | 文件生命周期 |
| `face_clusters` | 人臉聚類 |
| `face_detections` | 人臉檢測 |
| `face_identities` | 人臉身份 |
| `face_recognition_results` | 人臉識別結果 |
#### 可共享的表(留在 public schema
| 表名 | 說明 |
|------|------|
| `api_keys` | API 金鑰(使用獨立的 Development API Key |
| `api_key_audit_log` | API 金鑰審計日誌 |
| `api_key_anomalies` | API 金鑰異常 |
| `monitor_*` | 所有監控相關表 |
| `backup_*` | 備份記錄表 |
| `gitea_tokens` | Gitea API Token |
| `n8n_api_keys` | n8n API 金鑰 |
| `node_*` | 節點相關表 |
| `python_*` | Python 版本基線 |
| `storage_*` | 存儲統計表 |
| `v_idle_workflows` | 空閒工作流視圖 |
| `v_recent_anomalies` | 最近異常視圖 |
| `v_service_health` | 服務健康視圖 |
| `v_storage_overview` | 存儲概覽視圖 |
---
## 配置對比
### 環境變數對比
| 變數 | Production (.env) | Development (.env.development) |
|------|------------------|------------------------------|
| `MOMENTRY_SERVER_PORT` | 3002 | 3003 |
| `MOMENTRY_REDIS_PREFIX` | `momentry:` | `momentry_dev:` |
| `MOMENTRY_OUTPUT_DIR` | `/Users/accusys/momentry/output` | `/Users/accusys/momentry/output_dev` |
| `MOMENTRY_BACKUP_DIR` | `/Users/accusys/momentry/backup/momentry` | `/Users/accusys/momentry/backup/momentry_dev` |
| `DATABASE_URL` | `postgres://accusys@localhost:5432/momentr` | `postgres://accusys@localhost:5432/momentry` |
| `MONGODB_URL` | `mongodb://localhost:27017` | `mongodb://localhost:27017` |
| `QDRANT_URL` | `http://localhost:6333` | `http://localhost:6333` |
| `QDRANT_COLLECTION` | `momentry_rule1` | `momentry_dev_rule1` |
| `RUST_LOG` | info | debug |
| `MOMENTRY_WORKER_ENABLED` | true | false |
### 二進制對比
| 屬性 | Production | Playground |
|------|-----------|------------|
| Binary Name | `momentry` | `momentry_playground` |
| Build Mode | release | debug |
| Port | 3002 | 3003 |
| Config File | `.env` | `.env.development` |
---
## 實施步驟
### Step 1: 修改配置檔案
#### 更新 `.env.development`
在現有配置中添加數據庫隔離相關變數:
```bash
# Database Schema (PostgreSQL)
DATABASE_SCHEMA=dev
# MongoDB Database
MONGODB_DATABASE=momentry_dev
# Qdrant Collection
QDRANT_COLLECTION=momentry_dev_rule1
```
---
### Step 2: 修改代碼支持 Schema 切換
#### 2.1 更新 `src/core/config.rs`
添加新的配置項:
```rust
pub static DEV_SCHEMA: Lazy<String> = Lazy::new(|| {
env::var("DATABASE_SCHEMA").unwrap_or_else(|_| "dev".to_string())
});
pub static DEV_DATABASE: Lazy<String> = Lazy::new(|| {
env::var("MONGODB_DATABASE").unwrap_or_else(|_| "momentry_dev".to_string())
});
pub static DEV_QDRANT_COLLECTION: Lazy<String> = Lazy::new(|| {
env::var("QDRANT_COLLECTION").unwrap_or_else(|_| "momentry_dev_rule1".to_string())
});
```
#### 2.2 更新 `src/core/db/postgres_db.rs`
在查詢方法中添加 schema 參數支持:
```rust
// 在連接配置中使用 schema
let schema = DEV_SCHEMA.as_str();
let query = format!("SET search_path TO {}", schema);
sqlx::query(&query).execute(&pool).await?;
```
#### 2.3 更新 `src/core/db/mongodb.rs`
支持數據庫切換:
```rust
let database_name = DEV_DATABASE.as_str();
let database = client.database(database_name);
```
#### 2.4 更新 `src/core/db/qdrant_db.rs`
支持 collection 切換:
```rust
let collection_name = DEV_QDRANT_COLLECTION.as_str();
```
---
### Step 3: 創建開發環境數據庫
#### 3.1 PostgreSQL - 創建 Schema
```bash
# 連接 PostgreSQL
psql -U accusys -d momentry
# 創建 dev schema
CREATE SCHEMA IF NOT EXISTS dev;
# 將現有表的結構復製到 dev schema
CREATE TABLE dev.videos AS SELECT * FROM public.videos WHERE 1=0;
CREATE TABLE dev.chunks AS SELECT * FROM public.chunks WHERE 1=0;
-- ... 其他表
```
#### 3.2 MongoDB - 創建 Database
```bash
# 複製數據到開發數據庫
use admin
db.copyDatabase('momentry', 'momentry_dev')
```
#### 3.3 Qdrant - 創建 Collection
```bash
# 使用 Qdrant API 創建新的 collection
curl -X PUT 'http://localhost:6333/collections/momentry_dev_rule1' \
-H 'api-key: Test3200Test3200Test3200' \
-H 'Content-Type: application/json' \
-d '{
"vectors": {
"size": 1024,
"distance": "Cosine"
}
}'
```
---
### Step 4: 驗證隔離效果
#### 4.1 啟動服務驗證
```bash
# Terminal 1: 啟動 Production Server
cargo run --bin momentry -- server --port 3002
# Terminal 2: 啟動 Playground Server
cargo run --bin momentry_playground -- server --port 3003
```
#### 4.2 數據隔離驗證
```bash
# 驗證 Redis 隔離
redis-cli KEYS "momentry:job:*"
redis-cli KEYS "momentry_dev:job:*"
# 驗證 PostgreSQL Schema
psql -U accusys -d momentry -c "\dt dev.*"
psql -U accusys -d momentry -c "\dt public.*"
# 驗證 MongoDB
mongosh --eval "db.adminCommand('listDatabases')" | grep momentry
# 驗證 Qdrant
curl -s -H "api-key: Test3200Test3200Test3200" \
'http://localhost:6333/collections' | jq '.result[].name'
```
---
## 檔案位置
| 類型 | 路徑 | 說明 |
|------|------|------|
| Production Config | `/Users/accusys/momentry_core_0.1/.env` | 生產環境配置 |
| Development Config | `/Users/accusys/momentry_core_0.1/.env.development` | 開發環境配置 |
| Binary | `/Users/accusys/momentry_core_0.1/src/playground.rs` | Playground 二進制源碼 |
| Config Module | `/Users/accusys/momentry_core_0.1/src/core/config.rs` | 配置模組 |
| PostgreSQL Module | `/Users/accusys/momentry_core_0.1/src/core/db/postgres_db.rs` | PostgreSQL 模組 |
| MongoDB Module | `/Users/accusys/momentry_core_0.1/src/core/db/mongodb.rs` | MongoDB 模組 |
| Qdrant Module | `/Users/accusys/momentry_core_0.1/src/core/db/qdrant_db.rs` | Qdrant 模組 |
---
## 常用指令
### 啟動服務
```bash
# 啟動 Production Server
cargo run --bin momentry -- server
# 啟動 Playground Server
cargo run --bin momentry_playground -- server
# 指定 Port
cargo run --bin momentry_playground -- server --port 3003
# 啟動 Worker (Production)
cargo run --bin momentry -- worker --max-concurrent 2
```
### 驗證隔離
```bash
# 驗證 Redis 隔離
redis-cli KEYS "momentry:*"
redis-cli KEYS "momentry_dev:*"
# 驗證 PostgreSQL Schema
psql -U accusys -d momentry -c "\dt dev.*"
psql -U accusys -d momentry -c "\dt public.*"
# 驗證文件系統隔離
ls -la /Users/accusys/momentry/output/
ls -la /Users/accusys/momentry/output_dev/
```
### 數據庫操作
```bash
# 連接 PostgreSQL
psql -U accusys -d momentry
# 切換 Schema
SET search_path TO dev;
# 列出 Schema 表
\dt
# MongoDB 數據庫列表
mongosh --eval "db.adminCommand('listDatabases')"
# 切換 MongoDB 數據庫
use momentry_dev
db.chunks.countDocuments()
```
---
## 版本資訊
- 版本: V1.0
- 建立日期: 2026-03-31
- 文件更新: 2026-03-31
---
## 相關文件
| 文件 | 說明 |
|------|------|
| `PLAYGROUND_BINARY_IMPLEMENTATION.md` | Playground 二進制實現計劃 |
| `SERVICES.md` | 服務端口分配 |
| `MOMENTRY_CORE_REDIS_KEYS.md` | Redis Key 設計規範 |
| `AGENTS.md` | AI 代理執行指令 |
| `DOCS_STANDARD.md` | 文件創建規範 |
---
## 附錄:AI Agent 友好資訊
### 可用 Tools
| Tool | 用途 |
|------|------|
| `postgres_query` | 執行 PostgreSQL 查詢 |
| `mongodb_*` | MongoDB 操作 |
| `redis_*` | Redis 操作 |
| `qdrant_qdrant-*` | Qdrant 向量數據庫操作 |
### 數據庫 Schema
#### PostgreSQL Tables (dev schema)
```
videos, chunks, pre_chunks, chunk_vectors, frames,
processor_results, file_registry, file_lifecycle,
face_clusters, face_detections, face_identities,
face_recognition_results
```
#### MongoDB Collections
```
momentry_dev: chunks, cache
```
#### Qdrant Collections
```
momentry_dev_rule1
```
### 環境變數
```
MOMENTRY_SERVER_PORT, MOMENTRY_REDIS_PREFIX,
DATABASE_SCHEMA, MONGODB_DATABASE, QDRANT_COLLECTION,
MOMENTRY_OUTPUT_DIR, MOMENTRY_BACKUP_DIR
```