feat(dc/tool-c): Day 2 - Session管理与数据处理完成
核心功能: - 数据库: 创建dc_tool_c_sessions表 (12字段, 3索引) - 服务层: SessionService (383行), DataProcessService (303行) - 控制器: SessionController (300行, 6个API端点) - 路由: 新增6个Session管理路由 - 测试: 7个API测试全部通过 (100%) 技术亮点: - 零落盘架构: Excel内存解析, OSS存储 - Session管理: 10分钟过期, 心跳延长机制 - 云原生规范: storage/logger/prisma全平台复用 - 完整测试: 上传/预览/完整数据/删除/心跳 文件清单: - backend/prisma/schema.prisma (新增DcToolCSession模型) - backend/prisma/migrations/create_tool_c_session.sql - backend/scripts/create-tool-c-table.mjs - backend/src/modules/dc/tool-c/services/ (SessionService, DataProcessService) - backend/src/modules/dc/tool-c/controllers/SessionController.ts - backend/src/modules/dc/tool-c/routes/index.ts - backend/test-tool-c-day2.mjs - docs/03-业务模块/DC-数据清洗整理/00-工具C当前状态与开发指南.md - docs/03-业务模块/DC-数据清洗整理/06-开发记录/2025-12-06_工具C_Day2开发完成总结.md 代码统计: ~1900行 测试结果: 7/7 通过 (100%) 云原生规范: 完全符合
This commit is contained in:
171
backend/src/modules/dc/tool-c/README.md
Normal file
171
backend/src/modules/dc/tool-c/README.md
Normal file
@@ -0,0 +1,171 @@
|
||||
# 工具C (Tool C) - 科研数据编辑器
|
||||
|
||||
## 📁 项目结构
|
||||
|
||||
```
|
||||
tool-c/
|
||||
├── services/
|
||||
│ └── PythonExecutorService.ts # Python代码执行服务
|
||||
├── controllers/
|
||||
│ └── TestController.ts # 测试控制器(Day 1)
|
||||
├── routes/
|
||||
│ └── index.ts # 路由定义
|
||||
└── README.md # 本文件
|
||||
```
|
||||
|
||||
## ⚙️ 环境变量配置
|
||||
|
||||
在 `backend/.env` 文件中添加以下配置:
|
||||
|
||||
```bash
|
||||
# Python微服务地址
|
||||
EXTRACTION_SERVICE_URL=http://localhost:8000
|
||||
```
|
||||
|
||||
**说明**:
|
||||
- 默认值:`http://localhost:8000`
|
||||
- Python微服务需要先启动才能使用工具C
|
||||
- 启动命令:`cd extraction_service && .\venv\Scripts\activate && uvicorn main:app --host 0.0.0.0 --port 8000`
|
||||
|
||||
## 🚀 API端点(Day 1 测试)
|
||||
|
||||
### 1. 测试Python服务健康检查
|
||||
|
||||
```
|
||||
GET /api/v1/dc/tool-c/test/health
|
||||
```
|
||||
|
||||
**响应**:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"message": "Python服务正常",
|
||||
"healthy": true
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 测试代码验证
|
||||
|
||||
```
|
||||
POST /api/v1/dc/tool-c/test/validate
|
||||
```
|
||||
|
||||
**请求体**:
|
||||
```json
|
||||
{
|
||||
"code": "df['age_group'] = df['age'] > 60"
|
||||
}
|
||||
```
|
||||
|
||||
**响应**:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"data": {
|
||||
"valid": true,
|
||||
"errors": [],
|
||||
"warnings": []
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 测试代码执行
|
||||
|
||||
```
|
||||
POST /api/v1/dc/tool-c/test/execute
|
||||
```
|
||||
|
||||
**请求体**:
|
||||
```json
|
||||
{
|
||||
"data": [
|
||||
{"age": 25},
|
||||
{"age": 65}
|
||||
],
|
||||
"code": "df['old'] = df['age'] > 60"
|
||||
}
|
||||
```
|
||||
|
||||
**响应**:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"data": {
|
||||
"success": true,
|
||||
"result_data": [
|
||||
{"age": 25, "old": false},
|
||||
{"age": 65, "old": true}
|
||||
],
|
||||
"output": "",
|
||||
"error": null,
|
||||
"execution_time": 0.004,
|
||||
"result_shape": [2, 2]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## ✅ Day 1 完成情况
|
||||
|
||||
- [x] 创建Python微服务(dc_executor.py)
|
||||
- [x] 添加AST安全检查
|
||||
- [x] 实现Pandas代码执行
|
||||
- [x] 创建FastAPI端点(/api/dc/validate, /api/dc/execute)
|
||||
- [x] 创建Node.js服务(PythonExecutorService.ts)
|
||||
- [x] 创建测试控制器和路由
|
||||
- [x] 验证功能正常工作
|
||||
|
||||
## 📝 使用示例
|
||||
|
||||
### 启动Python微服务
|
||||
|
||||
```bash
|
||||
cd extraction_service
|
||||
.\venv\Scripts\activate
|
||||
python main.py
|
||||
```
|
||||
|
||||
### 启动Node.js后端
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
npm run dev
|
||||
```
|
||||
|
||||
### 测试API
|
||||
|
||||
```bash
|
||||
# 1. 健康检查
|
||||
curl http://localhost:3000/api/v1/dc/tool-c/test/health
|
||||
|
||||
# 2. 代码验证
|
||||
curl -X POST http://localhost:3000/api/v1/dc/tool-c/test/validate \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"code":"df[\"x\"] = 1"}'
|
||||
|
||||
# 3. 代码执行
|
||||
curl -X POST http://localhost:3000/api/v1/dc/tool-c/test/execute \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"data":[{"age":25},{"age":65}],"code":"df[\"old\"] = df[\"age\"] > 60"}'
|
||||
```
|
||||
|
||||
## 🔐 安全特性
|
||||
|
||||
- **AST静态检查**:拦截危险模块导入(os, sys, subprocess等)
|
||||
- **超时保护**:代码执行超时30秒自动终止
|
||||
- **沙箱环境**:限制可用的内置函数
|
||||
- **错误处理**:完整的异常捕获和错误信息
|
||||
|
||||
## 📚 技术栈
|
||||
|
||||
- **Python后端**: FastAPI + Pandas + AST
|
||||
- **Node.js后端**: Fastify + Axios + TypeScript
|
||||
- **通信方式**: HTTP REST API
|
||||
- **数据格式**: JSON
|
||||
|
||||
## 🎯 下一步(Day 2)
|
||||
|
||||
- [ ] Session管理(数据库 + OSS)
|
||||
- [ ] 数据处理服务
|
||||
- [ ] AI代码生成服务(LLMFactory集成)
|
||||
- [ ] 前端基础框架搭建
|
||||
|
||||
Reference in New Issue
Block a user