feat: add translation billing stats and remove Export/Settings pages

- Add TranslationLog model to track translation API usage per task
- Integrate Dify API actual price (total_price) into translation stats
- Display translation statistics in admin dashboard with per-task costs
- Remove unused Export and Settings pages to simplify frontend
- Add GET /api/v2/admin/translation-stats endpoint

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
egg
2025-12-12 17:38:12 +08:00
parent d20751d56b
commit 65abd51d60
21 changed files with 682 additions and 662 deletions

View File

@@ -0,0 +1,62 @@
# Change: 簡化前端頁面並新增翻譯計費功能
## Why
目前前端存在多個冗餘的頁面和功能,需要精簡以改善使用者體驗和維護性:
1. Tasks 頁面的 JSON/MD 下載功能已不再需要(僅保留 PDF 下載)
2. Export 頁面功能與 Tasks 頁面重疊,且複雜度不符實際需求
3. Settings 頁面僅管理導出規則,而導出功能即將移除
同時,系統已整合 Dify 翻譯服務,需要在管理員儀表板中新增翻譯計費追蹤功能,以便監控 API Token 使用量和成本。
## What Changes
### 1. 移除 Tasks 頁面的 JSON/MD 下載按鈕(前端)
- 已在 TaskDetailPage 移除,確認 ExportPage 中的相關功能一併移除
- 保留 apiV2.ts 中的 API 方法(維持後端相容性)
### 2. 移除 Export 頁面(前端)
- 移除 `frontend/src/pages/ExportPage.tsx`
- 從 App.tsx 路由配置移除 `/export` 路由
- 從 Layout.tsx 導航選單移除 Export 連結
- 移除 i18n 中 export 相關翻譯(可選,不影響功能)
### 3. 移除 Settings 頁面(前端)
- 移除 `frontend/src/pages/SettingsPage.tsx`
- 從 App.tsx 路由配置移除 `/settings` 路由
- 從 Layout.tsx 導航選單移除 Settings 連結
- 後端 Export Rules API 保留(不影響現有資料)
### 4. 新增翻譯計費功能(前端 + 後端)
#### 後端新增:
-`AdminService` 新增 `get_translation_statistics()` 方法
- 新增 API 端點 `GET /api/v2/admin/translation-stats`
- 返回結構:
- 總翻譯任務數
- 總 Token 使用量input_tokens, output_tokens
- 各語言翻譯統計
- 預估成本(基於配置的 Token 價格)
#### 前端新增:
- 在 AdminDashboardPage 新增「翻譯統計」卡片
- 顯示總 Token 使用量、翻譯次數、預估成本
- 顯示各目標語言的翻譯分佈
## Impact
- Affected specs: frontend-ui (修改), backend-api (修改)
- Affected code:
- **前端移除**
- `frontend/src/pages/ExportPage.tsx`
- `frontend/src/pages/SettingsPage.tsx`
- `frontend/src/App.tsx` (路由)
- `frontend/src/components/Layout.tsx` (導航)
- **後端新增**
- `backend/app/services/admin_service.py` (翻譯統計方法)
- `backend/app/routers/admin.py` (新 API 端點)
- `backend/app/schemas/admin.py` (回應結構)
- **前端新增**
- `frontend/src/pages/AdminDashboardPage.tsx` (翻譯統計元件)
- `frontend/src/services/apiV2.ts` (新 API 呼叫)
- `frontend/src/types/apiV2.ts` (新類型)

View File

@@ -0,0 +1,22 @@
# Spec Delta: backend-api
## ADDED Requirements
### Requirement: Translation Statistics Endpoint
The system SHALL provide a new admin API endpoint for translation usage statistics across all users.
#### Scenario: Admin requests translation statistics
- GIVEN the admin is authenticated
- WHEN GET /api/v2/admin/translation-stats is called
- THEN the response contains:
- total_translations: number of translation jobs
- total_input_tokens: sum of input tokens used
- total_output_tokens: sum of output tokens used
- estimated_cost: calculated cost based on token pricing
- by_language: breakdown of translations by target language
- recent_translations: list of recent translation activities
#### Scenario: Non-admin user requests translation statistics
- GIVEN a regular user is authenticated
- WHEN GET /api/v2/admin/translation-stats is called
- THEN the response is 403 Forbidden

View File

@@ -0,0 +1,31 @@
# Spec Delta: frontend-ui
## REMOVED Requirements
- REQ-FE-EXPORT: Export Page - The export page for batch exporting task results is removed.
- REQ-FE-SETTINGS: Settings Page - The settings page for managing export rules is removed.
## ADDED Requirements
### Requirement: Translation Statistics in Admin Dashboard
The admin dashboard SHALL display translation usage statistics and estimated costs.
#### Scenario: Admin views translation statistics
- GIVEN the user is logged in as admin
- WHEN the user views the admin dashboard
- THEN the page displays a translation statistics card showing:
- Total translation count
- Total token usage (input + output tokens)
- Estimated cost based on token pricing
- Breakdown by target language
## MODIFIED Requirements
### Requirement: Navigation Menu Updated
The navigation menu SHALL be updated to remove Export and Settings links.
#### Scenario: User views navigation menu
- GIVEN the user is logged in
- WHEN the user views the sidebar navigation
- THEN the menu shows: Upload, Processing, Results, Task History, Admin (if admin)
- AND the menu does NOT show: Export, Settings

View File

@@ -0,0 +1,39 @@
# Tasks: 簡化前端頁面並新增翻譯計費功能
## 1. 移除 Export 頁面
- [x] 1.1 從 App.tsx 移除 `/export` 路由
- [x] 1.2 從 Layout.tsx 導航選單移除 Export 連結
- [x] 1.3 刪除 `frontend/src/pages/ExportPage.tsx`
## 2. 移除 Settings 頁面
- [x] 2.1 從 App.tsx 移除 `/settings` 路由
- [x] 2.2 從 Layout.tsx 導航選單移除 Settings 連結
- [x] 2.3 刪除 `frontend/src/pages/SettingsPage.tsx`
## 3. 後端翻譯統計 API
- [x] 3.1 新增 `TranslationLog` model 和 migration
- [x] 3.2 在 `admin_service.py` 新增 `get_translation_statistics()` 方法
- [x] 3.3 在 `admin.py` router 新增 `GET /admin/translation-stats` 端點
- [x] 3.4 修改翻譯流程在完成時寫入統計到資料庫
## 4. 前端翻譯統計顯示
- [x] 4.1 在 `apiV2.ts` 新增 `getTranslationStats()` API 呼叫
- [x] 4.2 在 `types/apiV2.ts` 新增翻譯統計類型定義
- [x] 4.3 在 `AdminDashboardPage.tsx` 新增翻譯統計卡片
## 5. i18n 翻譯
- [ ] 5.1 新增翻譯統計相關中文翻譯 (暫時使用硬編碼)
- [ ] 5.2 新增翻譯統計相關英文翻譯 (暫時使用硬編碼)
## 6. 測試與驗證
- [x] 6.1 驗證 Export/Settings 頁面路由已移除
- [x] 6.2 驗證導航選單已更新
- [x] 6.3 驗證 TypeScript 編譯通過
- [ ] 6.4 測試翻譯統計 API 回傳正確資料 (需要實際翻譯測試)
- [ ] 6.5 測試管理員儀表板顯示翻譯統計 (需要實際測試)