From 25adc1017ff493d135f024b08604c93d631cfaf9 Mon Sep 17 00:00:00 2001 From: Accusys Date: Sun, 27 Sep 2026 13:35:08 +0800 Subject: [PATCH] docs: complete re-scan analysis with solution recommendation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 分析結果: - 前端: Studio 有 register + process 組合邏輯(autoScan) - 後端: Core 的 force register 不會自動 process - 問題: 缺少一鍵 re-scan 功能 根本原因: 1. Auto-pipeline 默認關閉 2. Force re-register 條件不滿足 3. 前後端協作不完整 解決方案對比: - 方案 A: 後端實現 Re-scan API(推薦) - 方案 B: 前端組合調用 - 方案 C: 修改 auto-pipeline 邏輯 建議: 方案 A - 理由: 業務流程明確、易於維護、職責分離 - 實現: Core team 1-2天 + Studio team 0.5天 相關文件: - 前端: ~/momentry_studio/src/views/LibraryView.vue - 後端: src/api/files.rs, src/worker/job_worker.rs --- .../2026-09-27_re_scan_analysis.md | 357 ++++++++++++++++++ 1 file changed, 357 insertions(+) create mode 100644 docs_v1.0/M4_workspace/2026-09-27_re_scan_analysis.md diff --git a/docs_v1.0/M4_workspace/2026-09-27_re_scan_analysis.md b/docs_v1.0/M4_workspace/2026-09-27_re_scan_analysis.md new file mode 100644 index 00000000..d8f86bb7 --- /dev/null +++ b/docs_v1.0/M4_workspace/2026-09-27_re_scan_analysis.md @@ -0,0 +1,357 @@ +--- +title: Re-scan 問題完整分析報告 +version: 1.0 +date: 2026-09-27 +author: OpenCode +status: analysis_complete +--- + +## 執行摘要 + +**問題**: Re-scan 程序缺少 Deep Scan 步驟 + +**根本原因**: 前後端協作不完整 + +**建議方案**: 後端實現 Re-scan API,前端調用 + +--- + +## 當前實現分析 + +### Studio (前端) 實現 + +**位置**: `~/momentry_studio/src/views/LibraryView.vue` + +#### doRegister 函數 + +```typescript +async function doRegister(f: any) { + // 1. 調用 register API + const result = await apiCall('register_file', { filePath }) + + // 2. 等待 register 完成 + const finalStatus = await pollCommandStatus(result.command_id) + + // 3. 如果 autoScanAfterRegister 開啟,調用 process API + if (procs.length && autoScanAfterRegister.value) { + await apiCall('process_file', { fileUuid, processors: procs }) + } +} +``` + +#### autoScan 設置 + +**位置**: `~/momentry_studio/src/stores/autoScan.ts` + +```typescript +// 默認值:false(不自動 process) +const savedAutoScan = localStorage.getItem(AUTO_SCAN_KEY) +const autoScanEnabled = ref(savedAutoScan === 'true') +``` + +--- + +### Core (後端) 實現 + +#### Register API + +**位置**: `src/api/files.rs` + +```rust +// force=true 時的行為 +if req.force { + // 1. 創建 unregister command + create_command("unregister", ...) + + // 2. 創建 register command + create_command("register", ...) + + // ❌ 沒有創建 process command +} +``` + +#### Worker 處理 + +**位置**: `src/worker/job_worker.rs` + +```rust +// 處理 register command +async fn process_register_command(&self, cmd: &CommandJob) -> Result<()> { + let resp = register_single_file(...).await; + + // 自動 pipeline 條件 + if resp.success + && !resp.already_exists // ← 限制條件 + && resp.file_type == Some("video") + && get_auto_pipeline_enabled() // ← 默認 false + { + // 創建 monitor_job 並觸發 pipeline + } +} +``` + +--- + +## 問題診斷 + +### 1. 用戶期望 vs 實際行為 + +| 操作 | 用戶期望 | 實際行為 | 問題 | +|------|----------|----------|------| +| **Register (新文件)** | Register → Process | Register ✅ | 需要手動開啟 autoScan | +| **Force Register (已存在文件)** | Unregister → Register → Process | Unregister → Register | ❌ 缺少 Process | +| **Re-scan** | Unregister → Register → Process | 需要手動執行 3 步 | ❌ 沒有一鍵操作 | + +--- + +### 2. 為什麼 Force Register 不會自動 Process? + +#### 原因 A: Auto-Pipeline 默認關閉 + +```rust +// src/core/config.rs +pub static RUNTIME_AUTO_PIPELINE_ENABLED: Lazy> = + Lazy::new(|| RwLock::new(false)); +``` + +#### 原因 B: Force Re-register 條件不滿足 + +```rust +// src/worker/job_worker.rs +if resp.success + && !resp.already_exists // ← force re-register 時為 false +{ + // 不會執行 +} +``` + +--- + +### 3. Studio 為什麼沒有 Re-scan? + +#### 當前功能 + +- ✅ Register (新文件) +- ✅ Unregister (移除文件) +- ✅ Process (處理文件) +- ❌ **Re-scan (一鍵重新掃描)** + +#### 原因 + +Studio 只有單獨的 `unregister`、`register`、`process` 操作,沒有組合成 `re-scan`。 + +--- + +## 解決方案對比 + +### 方案 A: 後端實現 Re-scan API(推薦) + +#### 實現方式 + +**新增 API**: `POST /api/v1/re-scan` + +```rust +async fn re_scan( + State(state): State, + Json(req): Json, +) -> Result, StatusCode> { + let file_uuid = req.file_uuid; + + // Step 1: 創建 unregister command + state.db.create_command("unregister", ...).await?; + + // Step 2: 創建 register command + state.db.create_command("register", ...).await?; + + // Step 3: 創建 process command + state.db.create_command("process", ...).await?; + + Ok(Json(ReScanResponse { ... })) +} +``` + +#### 優點 + +1. **業務邏輯封裝**: Re-scan 是一個完整的業務流程 +2. **易於維護**: 後端統一管理流程 +3. **易於測試**: 單一 API,容易測試 +4. **前端簡化**: 只需一個 API 調用 +5. **日誌追蹤**: 統一的日誌和錯誤處理 + +#### 缺點 + +1. 需要新增 API +2. 需要新增 command 處理邏輯 + +#### 負責團隊 + +**Core team**(後端) + +--- + +### 方案 B: 前端組合調用 + +#### 實現方式 + +**Studio 新增**: "Re-scan" 按鈕 + +```typescript +async function doReScan(f: any) { + // Step 1: Unregister + await apiCall('unregister_file', { fileUuid: f.file_uuid }) + + // Step 2: Register + const result = await apiCall('register_file', { filePath: f.file_path }) + await pollCommandStatus(result.command_id) + + // Step 3: Process + await apiCall('process_file', { fileUuid: result.file_uuid, processors: null }) +} +``` + +#### 優點 + +1. 不需要修改後端 +2. 前端邏輯靈活 + +#### 缺點 + +1. **業務邏輯分散**: 流程分散在前端 +2. **不易維護**: 多個 API 調用組合 +3. **不易測試**: 需要測試多個 API 的協作 +4. **錯誤處理複雜**: 每個步驟都可能失敗 +5. **前端複雜度增加**: 需要處理中間狀態 + +#### 負責團隊 + +**Studio team**(前端) + +--- + +### 方案 C: 修改 Auto-Pipeline 邏輯 + +#### 實現方式 + +修改 `process_register_command` 的觸發條件: + +```rust +if resp.success + && (resp.force || !resp.already_exists) // ← 新增 force 條件 + && resp.file_type == Some("video") + && get_auto_pipeline_enabled() +{ + // 創建 monitor_job 並觸發 pipeline +} +``` + +#### 優點 + +1. 最小改動 +2. 保持現有 API + +#### 缺點 + +1. **依賴 auto_pipeline 設置**: 用戶需要手動開啟 +2. **語義不明確**: force register 的行為不夠清晰 +3. **不夠靈活**: 無法單獨控制 re-scan 行為 + +#### 負責團隊 + +**Core team**(後端) + +--- + +## 建議 + +### 推薦方案: A (後端實現 Re-scan API) + +#### 理由 + +1. **業務流程明確**: Re-scan 是一個完整的業務流程,應該封裝在後端 +2. **職責分離**: 後端負責業務邏輯,前端負責 UI 展示 +3. **易於維護**: 單一 API,統一管理 +4. **易於測試**: 單元測試和集成測試更容易 +5. **用戶體驗**: 一鍵操作,簡單明了 + +--- + +### 實現計劃 + +#### Phase 1: Core team 實現 Re-scan API + +**時間**: 1-2 天 + +1. 新增 `ReScanRequest` 和 `ReScanResponse` 結構體 +2. 新增 `POST /api/v1/re-scan` 路由 +3. 實現 `re_scan` 函數: + - 創建 unregister command + - 創建 register command + - 創建 process command +4. 添加單元測試和集成測試 +5. 更新 API 文檔 + +--- + +#### Phase 2: Studio team 更新 UI + +**時間**: 0.5 天 + +1. 添加 "Re-scan" 按鈕到右鍵菜單 +2. 調用 `/api/v1/re-scan` API +3. 顯示進度和結果 + +--- + +### 團隊協作 + +| 團隊 | 負責內容 | 時間 | +|------|----------|------| +| **Core team** | 實現 Re-scan API | 1-2 天 | +| **Studio team** | 更新 UI | 0.5 天 | + +**總時間**: 1.5-2.5 天 + +--- + +## 附錄 + +### 相關文件 + +#### 後端 (Core) + +- `/Users/accusys/momentry_core/src/api/files.rs` - Register API +- `/Users/accusys/momentry_core/src/api/processing.rs` - Process API +- `/Users/accusys/momentry_core/src/worker/job_worker.rs` - Command 處理 +- `/Users/accusys/momentry_core/src/core/config.rs` - Auto-Pipeline 配置 + +#### 前端 (Studio) + +- `/Users/accusys/momentry_studio/src/views/LibraryView.vue` - 文件管理 UI +- `/Users/accusys/momentry_studio/src/stores/autoScan.ts` - Auto-Scan 設置 +- `/Users/accusys/momentry_studio/src/api/index.ts` - API 調用 + +--- + +### 測試場景 + +1. **新文件 Re-scan**: + - 已處理的文件,想要重新處理 + - 預期:刪除舊數據,重新處理 + +2. **Force Register 後 Re-scan**: + - Force register 後沒有處理 + - 使用 Re-scan API 觸發處理 + +3. **錯誤處理**: + - Unregister 失敗 + - Register 失敗 + - Process 失敗 + +--- + +## 狀態 + +- **分析日期**: 2026-09-27 +- **優先級**: High +- **建議方案**: 方案 A (後端實現 Re-scan API) +- **下一步**: 等待團隊確認方案