feat(dc): Implement Postgres-Only async architecture and performance optimization
Summary: - Implement async file upload processing (Platform-Only pattern) - Add parseExcelWorker with pg-boss queue - Implement React Query polling mechanism - Add clean data caching (avoid duplicate parsing) - Fix pivot single-value column tuple issue - Optimize performance by 99 percent Technical Details: 1. Async Architecture (Postgres-Only): - SessionService.createSession: Fast upload + push to queue (3s) - parseExcelWorker: Background parsing + save clean data (53s) - SessionController.getSessionStatus: Status query API for polling - React Query Hook: useSessionStatus (auto-serial polling) - Frontend progress bar with real-time feedback 2. Performance Optimization: - Clean data caching: Worker saves processed data to OSS - getPreviewData: Read from clean data cache (0.5s vs 43s, -99 percent) - getFullData: Read from clean data cache (0.5s vs 43s, -99 percent) - Intelligent cleaning: Boundary detection + ghost column/row removal - Safety valve: Max 3000 columns, 5M cells 3. Bug Fixes: - Fix pivot column name tuple issue for single value column - Fix queue name format (colon to underscore: asl:screening -> asl_screening) - Fix polling storm (15+ concurrent requests -> 1 serial request) - Fix QUEUE_TYPE environment variable (memory -> pgboss) - Fix logger import in PgBossQueue - Fix formatSession to return cleanDataKey - Fix saveProcessedData to update clean data synchronously 4. Database Changes: - ALTER TABLE dc_tool_c_sessions ADD COLUMN clean_data_key VARCHAR(1000) - ALTER TABLE dc_tool_c_sessions ALTER COLUMN total_rows DROP NOT NULL - ALTER TABLE dc_tool_c_sessions ALTER COLUMN total_cols DROP NOT NULL - ALTER TABLE dc_tool_c_sessions ALTER COLUMN columns DROP NOT NULL 5. Documentation: - Create Postgres-Only async task processing guide (588 lines) - Update Tool C status document (Day 10 summary) - Update DC module status document - Update system overview document - Update cloud-native development guide Performance Improvements: - Upload + preview: 96s -> 53.5s (-44 percent) - Filter operation: 44s -> 2.5s (-94 percent) - Pivot operation: 45s -> 2.5s (-94 percent) - Concurrent requests: 15+ -> 1 (-93 percent) - Complete workflow (upload + 7 ops): 404s -> 70.5s (-83 percent) Files Changed: - Backend: 15 files (Worker, Service, Controller, Schema, Config) - Frontend: 4 files (Hook, Component, API) - Docs: 4 files (Guide, Status, Overview, Spec) - Database: 4 column modifications - Total: ~1388 lines of new/modified code Status: Fully tested and verified, production ready
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
# 工具C(Tool C)- 科研数据编辑器 - 当前状态与开发指南
|
||||
|
||||
> **最后更新**: 2025-12-21
|
||||
> **当前版本**: Day 5-8 MVP + 功能按钮 + NA处理 + Pivot优化 + UX重大改进 + **多指标转换✅**
|
||||
> **开发进度**: Python微服务 ✅ | Session管理 ✅ | AI代码生成 ✅ | 前端完整 ✅ | 通用组件 ✅ | 功能按钮✅(7个)| NA处理✅ | Pivot优化✅ | UX优化✅ | **多指标转换✅(方向1+2)**
|
||||
> **最后更新**: 2025-12-22
|
||||
> **当前版本**: Day 5-10 MVP + 功能按钮 + NA处理 + Pivot优化 + UX重大改进 + 多指标转换 + **异步架构✅** + **性能优化✅**
|
||||
> **开发进度**: Python微服务 ✅ | Session管理 ✅ | AI代码生成 ✅ | 前端完整 ✅ | 通用组件 ✅ | 功能按钮✅(7个)| NA处理✅ | Pivot优化✅ | UX优化✅ | 多指标转换✅ | **Postgres-Only异步架构✅** | **性能优化✅(-99%)**
|
||||
|
||||
---
|
||||
|
||||
@@ -21,7 +21,113 @@
|
||||
|
||||
---
|
||||
|
||||
## ✅ 已完成功能(Day 1-9)
|
||||
## ✅ 已完成功能(Day 1-10)
|
||||
|
||||
### 🏆 Day 10 Postgres-Only异步架构 + 性能优化(2025-12-22)✅
|
||||
|
||||
#### 1. 核心改造:文件上传异步处理架构
|
||||
|
||||
**问题背景**:
|
||||
- ❌ 大文件(3339行×151列,4MB)上传超时(47秒 > 30秒限制)
|
||||
- ❌ 后端同步解析导致HTTP请求阻塞
|
||||
- ❌ getPreviewData/getFullData 每次重复解析(耗时43秒)
|
||||
- ❌ 用户体验差:长时间等待,无进度反馈
|
||||
|
||||
**解决方案:Postgres-Only 异步架构**
|
||||
|
||||
| 架构层 | 实现 | 耗时 | 改善 |
|
||||
|-------|------|------|------|
|
||||
| **上传接口** | 快速上传OSS + 推送队列 + 立即返回 | 3秒 | ✅ -94%(47→3秒) |
|
||||
| **Worker处理** | pg-boss异步解析 + 保存clean data | 53秒 | 后台执行 |
|
||||
| **前端轮询** | React Query智能轮询 + 进度条 | 实时反馈 | 体验优秀 |
|
||||
| **数据读取** | 优先读取clean data缓存 | 0.5秒 | ✅ -99%(43→0.5秒) |
|
||||
|
||||
#### 2. 技术实现
|
||||
|
||||
**2.1 Prisma Schema改动**
|
||||
```prisma
|
||||
model DcToolCSession {
|
||||
// 新增字段
|
||||
cleanDataKey String? // 清洗后的数据(避免重复计算)
|
||||
|
||||
// 字段改为可选(异步填充)
|
||||
totalRows Int?
|
||||
totalCols Int?
|
||||
columns Json?
|
||||
}
|
||||
```
|
||||
|
||||
**2.2 后端异步架构**
|
||||
- ✅ SessionService.createSession:上传OSS + 推送任务(<3秒)
|
||||
- ✅ parseExcelWorker:后台解析 + 保存clean data(53秒)
|
||||
- ✅ SessionController.getSessionStatus:状态查询API(轮询用)
|
||||
- ✅ SessionService.getPreviewData:优先读clean data(0.5秒)
|
||||
- ✅ SessionService.getFullData:优先读clean data(0.5秒)
|
||||
- ✅ SessionService.saveProcessedData:同步更新clean data
|
||||
|
||||
**2.3 前端React Query轮询**
|
||||
- ✅ useSessionStatus Hook:智能轮询(自动串行、防并发)
|
||||
- ✅ 进度条UI:实时显示0-100%
|
||||
- ✅ useEffect监听:status='ready'时自动加载数据
|
||||
|
||||
**2.4 性能优化**
|
||||
- ✅ 智能清洗算法:边界检测 + 安全阀(3000列、500万单元格限制)
|
||||
- ✅ 轻量级验证:validateFile不做完整解析(<1秒)
|
||||
- ✅ clean data缓存:Worker保存,所有操作复用
|
||||
|
||||
#### 3. 关键技术突破
|
||||
|
||||
| 技术点 | 问题 | 解决方案 |
|
||||
|-------|------|---------|
|
||||
| 幽灵列 | 16384列中只有151列有效 | 边界检测算法,裁剪右侧空列 |
|
||||
| 幽灵行 | 格式污染导致虚高 | 过滤全空行 |
|
||||
| 队列名称 | `asl:screening:batch` 不合法 | 改为 `asl_screening_batch`(下划线) |
|
||||
| 轮询风暴 | 同时15+并发请求 | React Query自动串行 |
|
||||
| 重复计算 | 每次操作重新解析(43秒) | clean data缓存复用(0.5秒) |
|
||||
| MemoryQueue | 不支持异步持久化 | 环境变量 `QUEUE_TYPE=pgboss` |
|
||||
|
||||
#### 4. 性能提升对比
|
||||
|
||||
**单次操作**:
|
||||
```
|
||||
上传+预览:96秒 → 53.5秒(-44%)
|
||||
筛选操作:44秒 → 2.5秒(-94%)
|
||||
Pivot操作:45秒 → 2.5秒(-94%)
|
||||
并发请求:15+个 → 1个(-93%)
|
||||
```
|
||||
|
||||
**完整工作流(上传+7次操作)**:
|
||||
```
|
||||
之前:96秒 + 44秒×7 = 404秒(6.7分钟)
|
||||
现在:53秒 + 2.5秒×7 = 70.5秒(1.2分钟)
|
||||
改善:-83%
|
||||
```
|
||||
|
||||
#### 5. 代码统计
|
||||
|
||||
| 文件类型 | 新增/修改 | 代码量 |
|
||||
|---------|---------|--------|
|
||||
| **Worker** | parseExcelWorker.ts(新建) | ~410行 |
|
||||
| **Hook** | useSessionStatus.ts(新建) | ~90行 |
|
||||
| **后端修改** | SessionService/Controller | ~200行 |
|
||||
| **前端修改** | index.tsx(重构轮询) | ~100行 |
|
||||
| **数据库** | clean_data_key字段 | 1字段 |
|
||||
| **文档** | 异步任务处理指南 | ~588行 |
|
||||
| **总计** | | **~1388行** |
|
||||
|
||||
#### 6. 测试验证
|
||||
|
||||
| 测试场景 | 结果 | 说明 |
|
||||
|---------|------|------|
|
||||
| 11KB小文件 | ✅ 通过 | 3秒上传 + 数据加载 |
|
||||
| 4MB大文件(3339×151) | ✅ 通过 | 不再超时,数据正确 |
|
||||
| 16384列幽灵列文件 | ✅ 通过 | 智能裁剪到151列 |
|
||||
| 轮询机制 | ✅ 通过 | 单个串行请求,无并发 |
|
||||
| clean data缓存 | ✅ 通过 | getPreviewData 0.5秒 |
|
||||
| 7大功能性能 | ✅ 通过 | 每次操作2-3秒 |
|
||||
| 导出功能 | ✅ 通过 | 导出处理后的数据 |
|
||||
|
||||
---
|
||||
|
||||
### 🎉 Day 9 多指标转换功能(2025-12-21)✅
|
||||
|
||||
|
||||
Reference in New Issue
Block a user