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:
33
backend/prisma/migrations/create_tool_c_session.sql
Normal file
33
backend/prisma/migrations/create_tool_c_session.sql
Normal file
@@ -0,0 +1,33 @@
|
||||
-- 创建 Tool C Session 表
|
||||
-- 日期: 2025-12-06
|
||||
-- 用途: 科研数据编辑器会话管理
|
||||
|
||||
CREATE TABLE IF NOT EXISTS dc_schema.dc_tool_c_sessions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id VARCHAR(255) NOT NULL,
|
||||
file_name VARCHAR(500) NOT NULL,
|
||||
file_key VARCHAR(500) NOT NULL,
|
||||
|
||||
-- 数据元信息
|
||||
total_rows INTEGER NOT NULL,
|
||||
total_cols INTEGER NOT NULL,
|
||||
columns JSONB NOT NULL,
|
||||
encoding VARCHAR(50),
|
||||
file_size INTEGER NOT NULL,
|
||||
|
||||
-- 时间戳
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP NOT NULL
|
||||
);
|
||||
|
||||
-- 创建索引
|
||||
CREATE INDEX IF NOT EXISTS idx_dc_tool_c_sessions_user_id ON dc_schema.dc_tool_c_sessions(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_dc_tool_c_sessions_expires_at ON dc_schema.dc_tool_c_sessions(expires_at);
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE dc_schema.dc_tool_c_sessions IS 'Tool C (科研数据编辑器) Session会话表';
|
||||
COMMENT ON COLUMN dc_schema.dc_tool_c_sessions.file_key IS 'OSS存储路径: dc/tool-c/sessions/{timestamp}-{fileName}';
|
||||
COMMENT ON COLUMN dc_schema.dc_tool_c_sessions.columns IS '列名数组 ["age", "gender", "diagnosis"]';
|
||||
COMMENT ON COLUMN dc_schema.dc_tool_c_sessions.expires_at IS '过期时间(创建后10分钟)';
|
||||
|
||||
@@ -846,3 +846,30 @@ model DCExtractionItem {
|
||||
@@map("dc_extraction_items")
|
||||
@@schema("dc_schema")
|
||||
}
|
||||
|
||||
// ==================== DC数据清洗模块 - Tool C (科研数据编辑器) ====================
|
||||
|
||||
// Tool C Session 会话表
|
||||
model DcToolCSession {
|
||||
id String @id @default(uuid())
|
||||
userId String @map("user_id")
|
||||
fileName String @map("file_name")
|
||||
fileKey String @map("file_key") // OSS存储key: dc/tool-c/sessions/{timestamp}-{fileName}
|
||||
|
||||
// 数据元信息
|
||||
totalRows Int @map("total_rows")
|
||||
totalCols Int @map("total_cols")
|
||||
columns Json @map("columns") // ["age", "gender", "diagnosis"] 列名数组
|
||||
encoding String? @map("encoding") // 文件编码 utf-8, gbk等
|
||||
fileSize Int @map("file_size") // 文件大小(字节)
|
||||
|
||||
// 时间戳
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
expiresAt DateTime @map("expires_at") // 过期时间(创建后10分钟)
|
||||
|
||||
@@index([userId])
|
||||
@@index([expiresAt])
|
||||
@@map("dc_tool_c_sessions")
|
||||
@@schema("dc_schema")
|
||||
}
|
||||
|
||||
138
backend/scripts/create-tool-c-table.mjs
Normal file
138
backend/scripts/create-tool-c-table.mjs
Normal file
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* 创建 Tool C Session 表
|
||||
*
|
||||
* 执行方式:node scripts/create-tool-c-table.mjs
|
||||
*/
|
||||
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function createToolCTable() {
|
||||
console.log('========================================');
|
||||
console.log('开始创建 Tool C Session 表');
|
||||
console.log('========================================\n');
|
||||
|
||||
try {
|
||||
// 1. 检查表是否已存在
|
||||
console.log('[1/4] 检查表是否已存在...');
|
||||
const checkResult = await prisma.$queryRawUnsafe(`
|
||||
SELECT EXISTS (
|
||||
SELECT FROM information_schema.tables
|
||||
WHERE table_schema = 'dc_schema'
|
||||
AND table_name = 'dc_tool_c_sessions'
|
||||
) as exists
|
||||
`);
|
||||
|
||||
const tableExists = checkResult[0].exists;
|
||||
|
||||
if (tableExists) {
|
||||
console.log('✅ 表已存在: dc_schema.dc_tool_c_sessions');
|
||||
console.log('\n如需重新创建,请手动执行: DROP TABLE dc_schema.dc_tool_c_sessions CASCADE;\n');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('✅ 表不存在,准备创建\n');
|
||||
|
||||
// 2. 创建表
|
||||
console.log('[2/4] 创建表 dc_tool_c_sessions...');
|
||||
await prisma.$executeRawUnsafe(`
|
||||
CREATE TABLE dc_schema.dc_tool_c_sessions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id VARCHAR(255) NOT NULL,
|
||||
file_name VARCHAR(500) NOT NULL,
|
||||
file_key VARCHAR(500) NOT NULL,
|
||||
|
||||
total_rows INTEGER NOT NULL,
|
||||
total_cols INTEGER NOT NULL,
|
||||
columns JSONB NOT NULL,
|
||||
encoding VARCHAR(50),
|
||||
file_size INTEGER NOT NULL,
|
||||
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP NOT NULL
|
||||
)
|
||||
`);
|
||||
console.log('✅ 表创建成功\n');
|
||||
|
||||
// 3. 创建索引
|
||||
console.log('[3/4] 创建索引...');
|
||||
await prisma.$executeRawUnsafe(`
|
||||
CREATE INDEX idx_dc_tool_c_sessions_user_id ON dc_schema.dc_tool_c_sessions(user_id)
|
||||
`);
|
||||
await prisma.$executeRawUnsafe(`
|
||||
CREATE INDEX idx_dc_tool_c_sessions_expires_at ON dc_schema.dc_tool_c_sessions(expires_at)
|
||||
`);
|
||||
console.log('✅ 索引创建成功\n');
|
||||
|
||||
// 4. 添加注释
|
||||
console.log('[4/4] 添加表注释...');
|
||||
await prisma.$executeRawUnsafe(`
|
||||
COMMENT ON TABLE dc_schema.dc_tool_c_sessions IS 'Tool C (科研数据编辑器) Session会话表'
|
||||
`);
|
||||
await prisma.$executeRawUnsafe(`
|
||||
COMMENT ON COLUMN dc_schema.dc_tool_c_sessions.file_key IS 'OSS存储路径: dc/tool-c/sessions/{timestamp}-{fileName}'
|
||||
`);
|
||||
await prisma.$executeRawUnsafe(`
|
||||
COMMENT ON COLUMN dc_schema.dc_tool_c_sessions.columns IS '列名数组 ["age", "gender", "diagnosis"]'
|
||||
`);
|
||||
await prisma.$executeRawUnsafe(`
|
||||
COMMENT ON COLUMN dc_schema.dc_tool_c_sessions.expires_at IS '过期时间(创建后10分钟)'
|
||||
`);
|
||||
console.log('✅ 注释添加成功\n');
|
||||
|
||||
// 5. 验证表创建
|
||||
console.log('========================================');
|
||||
console.log('验证表结构');
|
||||
console.log('========================================\n');
|
||||
|
||||
const columns = await prisma.$queryRawUnsafe(`
|
||||
SELECT column_name, data_type, is_nullable
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = 'dc_schema'
|
||||
AND table_name = 'dc_tool_c_sessions'
|
||||
ORDER BY ordinal_position
|
||||
`);
|
||||
|
||||
console.log('表结构:');
|
||||
console.table(columns);
|
||||
|
||||
const indexes = await prisma.$queryRawUnsafe(`
|
||||
SELECT indexname, indexdef
|
||||
FROM pg_indexes
|
||||
WHERE schemaname = 'dc_schema'
|
||||
AND tablename = 'dc_tool_c_sessions'
|
||||
`);
|
||||
|
||||
console.log('\n索引:');
|
||||
console.table(indexes);
|
||||
|
||||
console.log('\n========================================');
|
||||
console.log('🎉 Tool C Session 表创建成功!');
|
||||
console.log('========================================\n');
|
||||
console.log('表名: dc_schema.dc_tool_c_sessions');
|
||||
console.log(`列数: ${columns.length}`);
|
||||
console.log(`索引数: ${indexes.length}\n`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('\n❌ 创建表失败:', error.message);
|
||||
console.error('\n详细错误:');
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// 执行
|
||||
createToolCTable()
|
||||
.then(() => {
|
||||
console.log('脚本执行完成');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('脚本执行失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
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集成)
|
||||
- [ ] 前端基础框架搭建
|
||||
|
||||
299
backend/src/modules/dc/tool-c/controllers/SessionController.ts
Normal file
299
backend/src/modules/dc/tool-c/controllers/SessionController.ts
Normal file
@@ -0,0 +1,299 @@
|
||||
/**
|
||||
* Session控制器
|
||||
*
|
||||
* API端点:
|
||||
* - POST /sessions/upload 上传Excel文件创建Session
|
||||
* - GET /sessions/:id 获取Session信息
|
||||
* - GET /sessions/:id/preview 获取预览数据(前100行)
|
||||
* - GET /sessions/:id/full 获取完整数据
|
||||
* - DELETE /sessions/:id 删除Session
|
||||
* - POST /sessions/:id/heartbeat 更新心跳
|
||||
*
|
||||
* @module SessionController
|
||||
*/
|
||||
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { MultipartFile } from '@fastify/multipart';
|
||||
import { logger } from '../../../../common/logging/index.js';
|
||||
import { sessionService } from '../services/SessionService.js';
|
||||
import { dataProcessService } from '../services/DataProcessService.js';
|
||||
|
||||
// ==================== 请求参数类型定义 ====================
|
||||
|
||||
interface SessionIdParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
// ==================== 控制器 ====================
|
||||
|
||||
export class SessionController {
|
||||
/**
|
||||
* 上传Excel文件创建Session
|
||||
*
|
||||
* POST /api/v1/dc/tool-c/sessions/upload
|
||||
*/
|
||||
async upload(request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
logger.info('[SessionController] 收到文件上传请求');
|
||||
|
||||
// 1. 获取multipart数据
|
||||
const data = await request.file();
|
||||
|
||||
if (!data) {
|
||||
return reply.code(400).send({
|
||||
success: false,
|
||||
error: '未找到上传的文件',
|
||||
});
|
||||
}
|
||||
|
||||
const file = data as MultipartFile;
|
||||
const fileName = file.filename;
|
||||
|
||||
logger.info(`[SessionController] 文件名: ${fileName}`);
|
||||
|
||||
// 2. 读取文件到Buffer
|
||||
const fileBuffer = await file.toBuffer();
|
||||
|
||||
// 3. 验证文件
|
||||
const validation = dataProcessService.validateFile(fileBuffer, fileName);
|
||||
if (!validation.valid) {
|
||||
return reply.code(400).send({
|
||||
success: false,
|
||||
error: validation.error,
|
||||
});
|
||||
}
|
||||
|
||||
// 4. 获取用户ID(从请求中提取,实际部署时从JWT获取)
|
||||
// TODO: 从JWT token中获取userId
|
||||
const userId = (request as any).userId || 'test-user-001';
|
||||
|
||||
// 5. 创建Session
|
||||
const session = await sessionService.createSession(
|
||||
userId,
|
||||
fileName,
|
||||
fileBuffer
|
||||
);
|
||||
|
||||
logger.info(`[SessionController] Session创建成功: ${session.id}`);
|
||||
|
||||
// 6. 返回Session信息
|
||||
return reply.code(201).send({
|
||||
success: true,
|
||||
message: 'Session创建成功',
|
||||
data: {
|
||||
sessionId: session.id,
|
||||
fileName: session.fileName,
|
||||
fileSize: dataProcessService.formatFileSize(session.fileSize),
|
||||
totalRows: session.totalRows,
|
||||
totalCols: session.totalCols,
|
||||
columns: session.columns,
|
||||
expiresAt: session.expiresAt,
|
||||
createdAt: session.createdAt,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionController] 文件上传失败: ${error.message}`);
|
||||
return reply.code(500).send({
|
||||
success: false,
|
||||
error: error.message || '文件上传失败,请重试',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Session信息(只含元数据)
|
||||
*
|
||||
* GET /api/v1/dc/tool-c/sessions/:id
|
||||
*/
|
||||
async getSession(
|
||||
request: FastifyRequest<{ Params: SessionIdParams }>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
logger.info(`[SessionController] 获取Session: ${id}`);
|
||||
|
||||
const session = await sessionService.getSession(id);
|
||||
|
||||
return reply.code(200).send({
|
||||
success: true,
|
||||
data: {
|
||||
sessionId: session.id,
|
||||
fileName: session.fileName,
|
||||
fileSize: dataProcessService.formatFileSize(session.fileSize),
|
||||
totalRows: session.totalRows,
|
||||
totalCols: session.totalCols,
|
||||
columns: session.columns,
|
||||
encoding: session.encoding,
|
||||
expiresAt: session.expiresAt,
|
||||
createdAt: session.createdAt,
|
||||
updatedAt: session.updatedAt,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionController] 获取Session失败: ${error.message}`);
|
||||
|
||||
const statusCode = error.message.includes('不存在') || error.message.includes('过期')
|
||||
? 404
|
||||
: 500;
|
||||
|
||||
return reply.code(statusCode).send({
|
||||
success: false,
|
||||
error: error.message || '获取Session失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览数据(前100行)
|
||||
*
|
||||
* GET /api/v1/dc/tool-c/sessions/:id/preview
|
||||
*/
|
||||
async getPreviewData(
|
||||
request: FastifyRequest<{ Params: SessionIdParams }>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
logger.info(`[SessionController] 获取预览数据: ${id}`);
|
||||
|
||||
const result = await sessionService.getPreviewData(id);
|
||||
|
||||
return reply.code(200).send({
|
||||
success: true,
|
||||
data: {
|
||||
sessionId: result.id,
|
||||
fileName: result.fileName,
|
||||
totalRows: result.totalRows,
|
||||
totalCols: result.totalCols,
|
||||
columns: result.columns,
|
||||
previewRows: result.previewData.length,
|
||||
previewData: result.previewData,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionController] 获取预览数据失败: ${error.message}`);
|
||||
|
||||
const statusCode = error.message.includes('不存在') || error.message.includes('过期')
|
||||
? 404
|
||||
: 500;
|
||||
|
||||
return reply.code(statusCode).send({
|
||||
success: false,
|
||||
error: error.message || '获取预览数据失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整数据
|
||||
*
|
||||
* GET /api/v1/dc/tool-c/sessions/:id/full
|
||||
*/
|
||||
async getFullData(
|
||||
request: FastifyRequest<{ Params: SessionIdParams }>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
logger.info(`[SessionController] 获取完整数据: ${id}`);
|
||||
|
||||
const data = await sessionService.getFullData(id);
|
||||
|
||||
return reply.code(200).send({
|
||||
success: true,
|
||||
data: {
|
||||
sessionId: id,
|
||||
totalRows: data.length,
|
||||
data,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionController] 获取完整数据失败: ${error.message}`);
|
||||
|
||||
const statusCode = error.message.includes('不存在') || error.message.includes('过期')
|
||||
? 404
|
||||
: 500;
|
||||
|
||||
return reply.code(statusCode).send({
|
||||
success: false,
|
||||
error: error.message || '获取完整数据失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Session
|
||||
*
|
||||
* DELETE /api/v1/dc/tool-c/sessions/:id
|
||||
*/
|
||||
async deleteSession(
|
||||
request: FastifyRequest<{ Params: SessionIdParams }>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
logger.info(`[SessionController] 删除Session: ${id}`);
|
||||
|
||||
await sessionService.deleteSession(id);
|
||||
|
||||
return reply.code(200).send({
|
||||
success: true,
|
||||
message: 'Session删除成功',
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionController] 删除Session失败: ${error.message}`);
|
||||
return reply.code(500).send({
|
||||
success: false,
|
||||
error: error.message || '删除Session失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新心跳(延长过期时间)
|
||||
*
|
||||
* POST /api/v1/dc/tool-c/sessions/:id/heartbeat
|
||||
*/
|
||||
async updateHeartbeat(
|
||||
request: FastifyRequest<{ Params: SessionIdParams }>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const { id } = request.params;
|
||||
|
||||
logger.info(`[SessionController] 更新心跳: ${id}`);
|
||||
|
||||
const newExpiresAt = await sessionService.updateHeartbeat(id);
|
||||
|
||||
return reply.code(200).send({
|
||||
success: true,
|
||||
message: '心跳更新成功',
|
||||
data: {
|
||||
sessionId: id,
|
||||
expiresAt: newExpiresAt,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionController] 更新心跳失败: ${error.message}`);
|
||||
|
||||
const statusCode = error.message.includes('不存在')
|
||||
? 404
|
||||
: 500;
|
||||
|
||||
return reply.code(statusCode).send({
|
||||
success: false,
|
||||
error: error.message || '更新心跳失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 导出单例实例 ====================
|
||||
|
||||
export const sessionController = new SessionController();
|
||||
|
||||
130
backend/src/modules/dc/tool-c/controllers/TestController.ts
Normal file
130
backend/src/modules/dc/tool-c/controllers/TestController.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* 工具C测试控制器
|
||||
*
|
||||
* 用于Day 1验证Python服务集成
|
||||
*
|
||||
* @module TestController
|
||||
*/
|
||||
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { logger } from '../../../../common/logging/index.js';
|
||||
import { pythonExecutorService } from '../services/PythonExecutorService.js';
|
||||
|
||||
// ==================== 请求体类型定义 ====================
|
||||
|
||||
interface ValidateRequest {
|
||||
code: string;
|
||||
}
|
||||
|
||||
interface ExecuteRequest {
|
||||
data: Record<string, any>[];
|
||||
code: string;
|
||||
}
|
||||
|
||||
// ==================== 控制器 ====================
|
||||
|
||||
export class TestController {
|
||||
/**
|
||||
* 测试Python服务健康检查
|
||||
*
|
||||
* GET /api/dc/tool-c/test/health
|
||||
*/
|
||||
async testHealth(request: FastifyRequest, reply: FastifyReply) {
|
||||
try {
|
||||
logger.info('[Test] 测试Python服务健康检查');
|
||||
|
||||
const isHealthy = await pythonExecutorService.healthCheck();
|
||||
|
||||
return reply.code(200).send({
|
||||
success: true,
|
||||
message: isHealthy ? 'Python服务正常' : 'Python服务异常',
|
||||
healthy: isHealthy,
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[Test] 健康检查失败: ${error.message}`);
|
||||
return reply.code(500).send({
|
||||
success: false,
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试代码验证
|
||||
*
|
||||
* POST /api/dc/tool-c/test/validate
|
||||
*/
|
||||
async testValidate(
|
||||
request: FastifyRequest<{ Body: ValidateRequest }>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const { code } = request.body;
|
||||
|
||||
if (!code) {
|
||||
return reply.code(400).send({
|
||||
success: false,
|
||||
error: '缺少必需参数: code',
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(`[Test] 测试代码验证,长度: ${code.length} 字符`);
|
||||
|
||||
const result = await pythonExecutorService.validateCode(code);
|
||||
|
||||
return reply.code(200).send({
|
||||
success: true,
|
||||
data: result,
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[Test] 代码验证失败: ${error.message}`);
|
||||
return reply.code(500).send({
|
||||
success: false,
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试代码执行
|
||||
*
|
||||
* POST /api/dc/tool-c/test/execute
|
||||
*/
|
||||
async testExecute(
|
||||
request: FastifyRequest<{ Body: ExecuteRequest }>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const { data, code } = request.body;
|
||||
|
||||
if (!data || !code) {
|
||||
return reply.code(400).send({
|
||||
success: false,
|
||||
error: '缺少必需参数: data 或 code',
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(
|
||||
`[Test] 测试代码执行: 数据行数=${data.length}, 代码长度=${code.length} 字符`
|
||||
);
|
||||
|
||||
const result = await pythonExecutorService.executeCode(data, code);
|
||||
|
||||
return reply.code(200).send({
|
||||
success: result.success,
|
||||
data: result,
|
||||
});
|
||||
} catch (error: any) {
|
||||
logger.error(`[Test] 代码执行失败: ${error.message}`);
|
||||
return reply.code(500).send({
|
||||
success: false,
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 导出单例实例 ====================
|
||||
|
||||
export const testController = new TestController();
|
||||
|
||||
61
backend/src/modules/dc/tool-c/routes/index.ts
Normal file
61
backend/src/modules/dc/tool-c/routes/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 工具C路由定义
|
||||
*
|
||||
* @module routes
|
||||
*/
|
||||
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { testController } from '../controllers/TestController.js';
|
||||
import { sessionController } from '../controllers/SessionController.js';
|
||||
|
||||
export async function toolCRoutes(fastify: FastifyInstance) {
|
||||
// ==================== 测试路由(Day 1) ====================
|
||||
|
||||
// 测试Python服务健康检查
|
||||
fastify.get('/test/health', {
|
||||
handler: testController.testHealth.bind(testController),
|
||||
});
|
||||
|
||||
// 测试代码验证
|
||||
fastify.post('/test/validate', {
|
||||
handler: testController.testValidate.bind(testController),
|
||||
});
|
||||
|
||||
// 测试代码执行
|
||||
fastify.post('/test/execute', {
|
||||
handler: testController.testExecute.bind(testController),
|
||||
});
|
||||
|
||||
// ==================== Session管理路由(Day 2) ====================
|
||||
|
||||
// 上传Excel文件创建Session
|
||||
fastify.post('/sessions/upload', {
|
||||
handler: sessionController.upload.bind(sessionController),
|
||||
});
|
||||
|
||||
// 获取Session信息(元数据)
|
||||
fastify.get('/sessions/:id', {
|
||||
handler: sessionController.getSession.bind(sessionController),
|
||||
});
|
||||
|
||||
// 获取预览数据(前100行)
|
||||
fastify.get('/sessions/:id/preview', {
|
||||
handler: sessionController.getPreviewData.bind(sessionController),
|
||||
});
|
||||
|
||||
// 获取完整数据
|
||||
fastify.get('/sessions/:id/full', {
|
||||
handler: sessionController.getFullData.bind(sessionController),
|
||||
});
|
||||
|
||||
// 删除Session
|
||||
fastify.delete('/sessions/:id', {
|
||||
handler: sessionController.deleteSession.bind(sessionController),
|
||||
});
|
||||
|
||||
// 更新心跳(延长10分钟)
|
||||
fastify.post('/sessions/:id/heartbeat', {
|
||||
handler: sessionController.updateHeartbeat.bind(sessionController),
|
||||
});
|
||||
}
|
||||
|
||||
302
backend/src/modules/dc/tool-c/services/DataProcessService.ts
Normal file
302
backend/src/modules/dc/tool-c/services/DataProcessService.ts
Normal file
@@ -0,0 +1,302 @@
|
||||
/**
|
||||
* 数据处理服务
|
||||
*
|
||||
* 功能:
|
||||
* - Excel文件解析
|
||||
* - 文件验证
|
||||
* - 列类型推断(可选)
|
||||
*
|
||||
* @module DataProcessService
|
||||
*/
|
||||
|
||||
import * as xlsx from 'xlsx';
|
||||
import { logger } from '../../../../common/logging/index.js';
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
|
||||
interface ParsedExcelData {
|
||||
data: any[];
|
||||
columns: string[];
|
||||
totalRows: number;
|
||||
totalCols: number;
|
||||
}
|
||||
|
||||
interface ValidationResult {
|
||||
valid: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
interface ColumnType {
|
||||
name: string;
|
||||
type: 'number' | 'string' | 'date' | 'boolean' | 'mixed';
|
||||
sampleValues: any[];
|
||||
}
|
||||
|
||||
// ==================== 配置常量 ====================
|
||||
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
||||
const SUPPORTED_EXTENSIONS = ['.xlsx', '.xls', '.csv'];
|
||||
|
||||
// ==================== 数据处理服务 ====================
|
||||
|
||||
export class DataProcessService {
|
||||
/**
|
||||
* 解析Excel文件
|
||||
*
|
||||
* @param buffer - 文件Buffer
|
||||
* @returns 解析后的数据
|
||||
*/
|
||||
parseExcel(buffer: Buffer): ParsedExcelData {
|
||||
try {
|
||||
logger.info('[DataProcessService] 开始解析Excel文件');
|
||||
|
||||
// 1. 读取Excel文件(内存操作)
|
||||
const workbook = xlsx.read(buffer, { type: 'buffer' });
|
||||
|
||||
// 2. 获取第一个工作表
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
if (!sheetName) {
|
||||
throw new Error('Excel文件中没有工作表');
|
||||
}
|
||||
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
|
||||
// 3. 转换为JSON格式
|
||||
const data = xlsx.utils.sheet_to_json(sheet);
|
||||
|
||||
if (data.length === 0) {
|
||||
throw new Error('Excel文件没有数据');
|
||||
}
|
||||
|
||||
// 4. 提取元数据
|
||||
const totalRows = data.length;
|
||||
const columns = Object.keys(data[0] || {});
|
||||
const totalCols = columns.length;
|
||||
|
||||
if (totalCols === 0) {
|
||||
throw new Error('Excel文件没有列');
|
||||
}
|
||||
|
||||
logger.info(
|
||||
`[DataProcessService] Excel解析成功: ${totalRows}行 x ${totalCols}列`,
|
||||
{ columns }
|
||||
);
|
||||
|
||||
return {
|
||||
data,
|
||||
columns,
|
||||
totalRows,
|
||||
totalCols,
|
||||
};
|
||||
} catch (error: any) {
|
||||
logger.error(`[DataProcessService] Excel解析失败: ${error.message}`);
|
||||
throw new Error(`Excel文件解析失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证文件
|
||||
*
|
||||
* @param buffer - 文件Buffer
|
||||
* @param fileName - 文件名
|
||||
* @returns 验证结果
|
||||
*/
|
||||
validateFile(buffer: Buffer, fileName: string): ValidationResult {
|
||||
try {
|
||||
logger.info('[DataProcessService] 验证文件', { fileName, size: buffer.length });
|
||||
|
||||
// 1. 检查文件大小
|
||||
if (buffer.length === 0) {
|
||||
return {
|
||||
valid: false,
|
||||
error: '文件为空',
|
||||
};
|
||||
}
|
||||
|
||||
if (buffer.length > MAX_FILE_SIZE) {
|
||||
const sizeMB = (buffer.length / 1024 / 1024).toFixed(2);
|
||||
return {
|
||||
valid: false,
|
||||
error: `文件大小超过限制(最大10MB),当前: ${sizeMB}MB`,
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 检查文件扩展名
|
||||
const ext = fileName.toLowerCase().substring(fileName.lastIndexOf('.'));
|
||||
if (!SUPPORTED_EXTENSIONS.includes(ext)) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `不支持的文件格式: ${ext},仅支持 .xlsx, .xls, .csv`,
|
||||
};
|
||||
}
|
||||
|
||||
// 3. 尝试解析文件
|
||||
try {
|
||||
const parsed = this.parseExcel(buffer);
|
||||
|
||||
// 检查行数
|
||||
if (parsed.totalRows > 50000) {
|
||||
logger.warn('[DataProcessService] 文件行数较多,可能影响性能', {
|
||||
rows: parsed.totalRows,
|
||||
});
|
||||
}
|
||||
|
||||
// 检查列数
|
||||
if (parsed.totalCols > 100) {
|
||||
logger.warn('[DataProcessService] 文件列数较多', {
|
||||
cols: parsed.totalCols,
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `文件内容无法解析: ${error.message}`,
|
||||
};
|
||||
}
|
||||
|
||||
logger.info('[DataProcessService] 文件验证通过', { fileName });
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
};
|
||||
} catch (error: any) {
|
||||
logger.error(`[DataProcessService] 文件验证失败: ${error.message}`);
|
||||
return {
|
||||
valid: false,
|
||||
error: `文件验证失败: ${error.message}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 推断列类型(可选功能,Day 3优化)
|
||||
*
|
||||
* @param data - 数据数组
|
||||
* @returns 列类型信息
|
||||
*/
|
||||
inferColumnTypes(data: any[]): ColumnType[] {
|
||||
try {
|
||||
logger.info('[DataProcessService] 推断列类型');
|
||||
|
||||
if (data.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const columns = Object.keys(data[0] || {});
|
||||
const columnTypes: ColumnType[] = [];
|
||||
|
||||
for (const columnName of columns) {
|
||||
// 取前10行样本值
|
||||
const sampleValues = data.slice(0, 10).map((row) => row[columnName]);
|
||||
|
||||
// 推断类型
|
||||
const types = new Set(sampleValues.map((val) => this.getValueType(val)));
|
||||
|
||||
let inferredType: ColumnType['type'] = 'string';
|
||||
if (types.size === 1) {
|
||||
inferredType = Array.from(types)[0];
|
||||
} else if (types.size > 1) {
|
||||
inferredType = 'mixed';
|
||||
}
|
||||
|
||||
columnTypes.push({
|
||||
name: columnName,
|
||||
type: inferredType,
|
||||
sampleValues,
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(`[DataProcessService] 列类型推断完成: ${columnTypes.length}列`);
|
||||
|
||||
return columnTypes;
|
||||
} catch (error: any) {
|
||||
logger.error(`[DataProcessService] 列类型推断失败: ${error.message}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值类型
|
||||
*
|
||||
* @param value - 值
|
||||
* @returns 类型
|
||||
*/
|
||||
private getValueType(value: any): ColumnType['type'] {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return 'string';
|
||||
}
|
||||
|
||||
if (typeof value === 'number') {
|
||||
return 'number';
|
||||
}
|
||||
|
||||
if (typeof value === 'boolean') {
|
||||
return 'boolean';
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
return 'date';
|
||||
}
|
||||
|
||||
// 尝试解析为日期
|
||||
const datePattern = /^\d{4}-\d{2}-\d{2}$/;
|
||||
if (typeof value === 'string' && datePattern.test(value)) {
|
||||
return 'date';
|
||||
}
|
||||
|
||||
// 尝试解析为数字
|
||||
if (typeof value === 'string' && !isNaN(Number(value))) {
|
||||
return 'number';
|
||||
}
|
||||
|
||||
return 'string';
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文件大小
|
||||
*
|
||||
* @param bytes - 字节数
|
||||
* @returns 格式化后的大小
|
||||
*/
|
||||
formatFileSize(bytes: number): string {
|
||||
if (bytes < 1024) {
|
||||
return `${bytes} B`;
|
||||
} else if (bytes < 1024 * 1024) {
|
||||
return `${(bytes / 1024).toFixed(2)} KB`;
|
||||
} else {
|
||||
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成文件摘要信息
|
||||
*
|
||||
* @param buffer - 文件Buffer
|
||||
* @param fileName - 文件名
|
||||
* @returns 文件摘要
|
||||
*/
|
||||
generateFileSummary(buffer: Buffer, fileName: string) {
|
||||
try {
|
||||
const parsed = this.parseExcel(buffer);
|
||||
const columnTypes = this.inferColumnTypes(parsed.data);
|
||||
|
||||
return {
|
||||
fileName,
|
||||
fileSize: this.formatFileSize(buffer.length),
|
||||
totalRows: parsed.totalRows,
|
||||
totalCols: parsed.totalCols,
|
||||
columns: parsed.columns,
|
||||
columnTypes,
|
||||
sampleData: parsed.data.slice(0, 5), // 前5行样本
|
||||
};
|
||||
} catch (error: any) {
|
||||
logger.error(`[DataProcessService] 生成文件摘要失败: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 导出单例实例 ====================
|
||||
|
||||
export const dataProcessService = new DataProcessService();
|
||||
|
||||
176
backend/src/modules/dc/tool-c/services/PythonExecutorService.ts
Normal file
176
backend/src/modules/dc/tool-c/services/PythonExecutorService.ts
Normal file
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Python代码执行服务
|
||||
*
|
||||
* 功能:
|
||||
* - 调用Python微服务执行Pandas代码
|
||||
* - AST安全验证
|
||||
* - 超时控制
|
||||
* - 错误处理
|
||||
*
|
||||
* @module PythonExecutorService
|
||||
*/
|
||||
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
import { logger } from '../../../../common/logging/index.js';
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
|
||||
interface ValidateCodeRequest {
|
||||
code: string;
|
||||
}
|
||||
|
||||
interface ValidateCodeResponse {
|
||||
valid: boolean;
|
||||
errors: string[];
|
||||
warnings: string[];
|
||||
}
|
||||
|
||||
interface ExecuteCodeRequest {
|
||||
data: Record<string, any>[];
|
||||
code: string;
|
||||
}
|
||||
|
||||
interface ExecuteCodeResponse {
|
||||
success: boolean;
|
||||
result_data: Record<string, any>[] | null;
|
||||
output: string;
|
||||
error: string | null;
|
||||
execution_time: number;
|
||||
result_shape?: [number, number];
|
||||
}
|
||||
|
||||
// ==================== 配置常量 ====================
|
||||
|
||||
const EXTRACTION_SERVICE_URL = process.env.EXTRACTION_SERVICE_URL || 'http://localhost:8000';
|
||||
const DEFAULT_TIMEOUT = 30000; // 30秒超时
|
||||
|
||||
// ==================== Python执行器服务 ====================
|
||||
|
||||
export class PythonExecutorService {
|
||||
private client: AxiosInstance;
|
||||
|
||||
constructor() {
|
||||
// 创建axios实例
|
||||
this.client = axios.create({
|
||||
baseURL: EXTRACTION_SERVICE_URL,
|
||||
timeout: DEFAULT_TIMEOUT,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`PythonExecutorService initialized: ${EXTRACTION_SERVICE_URL}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证Pandas代码安全性(AST检查)
|
||||
*
|
||||
* @param code - Pandas代码
|
||||
* @returns 验证结果
|
||||
*/
|
||||
async validateCode(code: string): Promise<ValidateCodeResponse> {
|
||||
try {
|
||||
logger.info(`验证代码安全性,长度: ${code.length} 字符`);
|
||||
|
||||
const response = await this.client.post<ValidateCodeResponse>(
|
||||
'/api/dc/validate',
|
||||
{ code } as ValidateCodeRequest
|
||||
);
|
||||
|
||||
logger.info(
|
||||
`代码验证完成: valid=${response.data.valid}, ` +
|
||||
`errors=${response.data.errors.length}, warnings=${response.data.warnings.length}`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
logger.error(`代码验证失败: ${error.message}`);
|
||||
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (error.response) {
|
||||
throw new Error(`验证失败 (${error.response.status}): ${error.response.data?.detail || error.message}`);
|
||||
} else if (error.code === 'ECONNREFUSED') {
|
||||
throw new Error('无法连接到Python微服务,请确保服务已启动');
|
||||
} else if (error.code === 'ECONNABORTED') {
|
||||
throw new Error('代码验证超时');
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`代码验证失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Pandas代码
|
||||
*
|
||||
* @param data - JSON格式的数据(数组对象)
|
||||
* @param code - Pandas代码(操作df变量)
|
||||
* @returns 执行结果
|
||||
*/
|
||||
async executeCode(
|
||||
data: Record<string, any>[],
|
||||
code: string
|
||||
): Promise<ExecuteCodeResponse> {
|
||||
try {
|
||||
logger.info(
|
||||
`执行Pandas代码: 数据行数=${data.length}, 代码长度=${code.length} 字符`
|
||||
);
|
||||
|
||||
const response = await this.client.post<ExecuteCodeResponse>(
|
||||
'/api/dc/execute',
|
||||
{ data, code } as ExecuteCodeRequest,
|
||||
{ timeout: DEFAULT_TIMEOUT } // 执行可能较慢,使用完整超时
|
||||
);
|
||||
|
||||
if (response.data.success) {
|
||||
logger.info(
|
||||
`代码执行成功: ` +
|
||||
`结果shape=${JSON.stringify(response.data.result_shape)}, ` +
|
||||
`耗时=${response.data.execution_time.toFixed(3)}秒`
|
||||
);
|
||||
} else {
|
||||
logger.warn(`代码执行失败: ${response.data.error}`);
|
||||
}
|
||||
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
logger.error(`代码执行失败: ${error.message}`);
|
||||
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (error.response) {
|
||||
throw new Error(`执行失败 (${error.response.status}): ${error.response.data?.detail || error.message}`);
|
||||
} else if (error.code === 'ECONNREFUSED') {
|
||||
throw new Error('无法连接到Python微服务,请确保服务已启动');
|
||||
} else if (error.code === 'ECONNABORTED') {
|
||||
throw new Error('代码执行超时(>30秒)');
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`代码执行失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 健康检查(测试Python服务连接)
|
||||
*
|
||||
* @returns 服务是否正常
|
||||
*/
|
||||
async healthCheck(): Promise<boolean> {
|
||||
try {
|
||||
const response = await this.client.get('/api/health', { timeout: 5000 });
|
||||
const isHealthy = response.status === 200 && response.data.status === 'healthy';
|
||||
|
||||
logger.info(`Python服务健康检查: ${isHealthy ? '正常' : '异常'}`);
|
||||
|
||||
return isHealthy;
|
||||
} catch (error: any) {
|
||||
logger.error(`Python服务健康检查失败: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 导出单例实例 ====================
|
||||
|
||||
export const pythonExecutorService = new PythonExecutorService();
|
||||
|
||||
382
backend/src/modules/dc/tool-c/services/SessionService.ts
Normal file
382
backend/src/modules/dc/tool-c/services/SessionService.ts
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* Session管理服务
|
||||
*
|
||||
* 功能:
|
||||
* - 创建Session(上传Excel到OSS)
|
||||
* - 获取Session信息
|
||||
* - 获取预览/完整数据(从OSS)
|
||||
* - 删除Session
|
||||
* - 更新心跳
|
||||
*
|
||||
* @module SessionService
|
||||
*/
|
||||
|
||||
import { storage } from '../../../../common/storage/index.js';
|
||||
import { logger } from '../../../../common/logging/index.js';
|
||||
import { prisma } from '../../../../config/database.js';
|
||||
import * as xlsx from 'xlsx';
|
||||
|
||||
// ==================== 类型定义 ====================
|
||||
|
||||
interface SessionData {
|
||||
id: string;
|
||||
userId: string;
|
||||
fileName: string;
|
||||
fileKey: string;
|
||||
totalRows: number;
|
||||
totalCols: number;
|
||||
columns: string[];
|
||||
encoding: string | null;
|
||||
fileSize: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
expiresAt: Date;
|
||||
}
|
||||
|
||||
interface PreviewDataResponse extends SessionData {
|
||||
previewData: any[];
|
||||
}
|
||||
|
||||
// ==================== 配置常量 ====================
|
||||
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
||||
const SESSION_EXPIRE_MINUTES = 10; // Session过期时间:10分钟
|
||||
const PREVIEW_ROWS = 100; // 预览行数
|
||||
|
||||
// ==================== Session管理服务 ====================
|
||||
|
||||
export class SessionService {
|
||||
/**
|
||||
* 创建Session
|
||||
*
|
||||
* @param userId - 用户ID
|
||||
* @param fileName - 原始文件名
|
||||
* @param fileBuffer - 文件Buffer
|
||||
* @returns Session信息
|
||||
*/
|
||||
async createSession(
|
||||
userId: string,
|
||||
fileName: string,
|
||||
fileBuffer: Buffer
|
||||
): Promise<SessionData> {
|
||||
try {
|
||||
logger.info(`[SessionService] 创建Session: userId=${userId}, fileName=${fileName}`);
|
||||
|
||||
// 1. 验证文件大小
|
||||
if (fileBuffer.length > MAX_FILE_SIZE) {
|
||||
throw new Error(`文件大小超过限制(最大10MB),当前: ${(fileBuffer.length / 1024 / 1024).toFixed(2)}MB`);
|
||||
}
|
||||
|
||||
// 2. 内存解析Excel(不落盘,符合云原生规范)
|
||||
logger.info('[SessionService] 解析Excel文件...');
|
||||
let workbook: xlsx.WorkBook;
|
||||
try {
|
||||
workbook = xlsx.read(fileBuffer, { type: 'buffer' });
|
||||
} catch (error: any) {
|
||||
throw new Error(`Excel文件解析失败: ${error.message}`);
|
||||
}
|
||||
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
if (!sheetName) {
|
||||
throw new Error('Excel文件中没有工作表');
|
||||
}
|
||||
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
const data = xlsx.utils.sheet_to_json(sheet);
|
||||
|
||||
if (data.length === 0) {
|
||||
throw new Error('Excel文件没有数据');
|
||||
}
|
||||
|
||||
// 3. 提取元数据
|
||||
const totalRows = data.length;
|
||||
const totalCols = Object.keys(data[0] || {}).length;
|
||||
const columns = Object.keys(data[0] || {});
|
||||
|
||||
logger.info(`[SessionService] 解析完成: ${totalRows}行 x ${totalCols}列`);
|
||||
|
||||
// 4. 上传到OSS(使用平台storage服务)
|
||||
const timestamp = Date.now();
|
||||
const fileKey = `dc/tool-c/sessions/${userId}/${timestamp}-${fileName}`;
|
||||
|
||||
logger.info(`[SessionService] 上传到OSS: ${fileKey}`);
|
||||
await storage.upload(fileKey, fileBuffer);
|
||||
logger.info('[SessionService] OSS上传成功');
|
||||
|
||||
// 5. 保存Session到数据库(只存元数据,符合云原生规范)
|
||||
const expiresAt = new Date(Date.now() + SESSION_EXPIRE_MINUTES * 60 * 1000);
|
||||
|
||||
const session = await prisma.dcToolCSession.create({
|
||||
data: {
|
||||
userId,
|
||||
fileName,
|
||||
fileKey,
|
||||
totalRows,
|
||||
totalCols,
|
||||
columns: columns, // Prisma会自动转换为JSONB
|
||||
encoding: 'utf-8', // 默认utf-8,后续可扩展检测
|
||||
fileSize: fileBuffer.length,
|
||||
expiresAt,
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`[SessionService] Session创建成功: ${session.id}`);
|
||||
|
||||
return this.formatSession(session);
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionService] 创建Session失败: ${error.message}`, { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Session信息(只含元数据)
|
||||
*
|
||||
* @param sessionId - Session ID
|
||||
* @returns Session信息
|
||||
*/
|
||||
async getSession(sessionId: string): Promise<SessionData> {
|
||||
try {
|
||||
logger.info(`[SessionService] 获取Session: ${sessionId}`);
|
||||
|
||||
const session = await prisma.dcToolCSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
throw new Error('Session不存在');
|
||||
}
|
||||
|
||||
// 检查是否过期
|
||||
if (new Date() > session.expiresAt) {
|
||||
logger.warn(`[SessionService] Session已过期: ${sessionId}`);
|
||||
throw new Error('Session已过期,请重新上传文件');
|
||||
}
|
||||
|
||||
logger.info(`[SessionService] Session获取成功: ${sessionId}`);
|
||||
|
||||
return this.formatSession(session);
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionService] 获取Session失败: ${error.message}`, { sessionId });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览数据(前100行)
|
||||
*
|
||||
* @param sessionId - Session ID
|
||||
* @returns Session信息 + 预览数据
|
||||
*/
|
||||
async getPreviewData(sessionId: string): Promise<PreviewDataResponse> {
|
||||
try {
|
||||
logger.info(`[SessionService] 获取预览数据: ${sessionId}`);
|
||||
|
||||
// 1. 获取Session信息
|
||||
const session = await this.getSession(sessionId);
|
||||
|
||||
// 2. 从OSS下载文件到内存
|
||||
logger.info(`[SessionService] 从OSS下载文件: ${session.fileKey}`);
|
||||
const buffer = await storage.download(session.fileKey);
|
||||
|
||||
// 3. 内存解析Excel(不落盘)
|
||||
const workbook = xlsx.read(buffer, { type: 'buffer' });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
const data = xlsx.utils.sheet_to_json(sheet);
|
||||
|
||||
// 4. 返回前100行
|
||||
const previewData = data.slice(0, PREVIEW_ROWS);
|
||||
|
||||
logger.info(`[SessionService] 预览数据获取成功: ${previewData.length}行`);
|
||||
|
||||
return {
|
||||
...session,
|
||||
previewData,
|
||||
};
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionService] 获取预览数据失败: ${error.message}`, { sessionId });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整数据
|
||||
*
|
||||
* @param sessionId - Session ID
|
||||
* @returns 完整数据数组
|
||||
*/
|
||||
async getFullData(sessionId: string): Promise<any[]> {
|
||||
try {
|
||||
logger.info(`[SessionService] 获取完整数据: ${sessionId}`);
|
||||
|
||||
// 1. 获取Session信息
|
||||
const session = await this.getSession(sessionId);
|
||||
|
||||
// 2. 从OSS下载文件到内存
|
||||
logger.info(`[SessionService] 从OSS下载文件: ${session.fileKey}`);
|
||||
const buffer = await storage.download(session.fileKey);
|
||||
|
||||
// 3. 内存解析Excel
|
||||
const workbook = xlsx.read(buffer, { type: 'buffer' });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
const data = xlsx.utils.sheet_to_json(sheet);
|
||||
|
||||
logger.info(`[SessionService] 完整数据获取成功: ${data.length}行`);
|
||||
|
||||
return data;
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionService] 获取完整数据失败: ${error.message}`, { sessionId });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Session
|
||||
*
|
||||
* @param sessionId - Session ID
|
||||
*/
|
||||
async deleteSession(sessionId: string): Promise<void> {
|
||||
try {
|
||||
logger.info(`[SessionService] 删除Session: ${sessionId}`);
|
||||
|
||||
// 1. 获取Session信息
|
||||
const session = await prisma.dcToolCSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
logger.warn(`[SessionService] Session不存在: ${sessionId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 删除OSS文件
|
||||
try {
|
||||
logger.info(`[SessionService] 删除OSS文件: ${session.fileKey}`);
|
||||
await storage.delete(session.fileKey);
|
||||
logger.info('[SessionService] OSS文件删除成功');
|
||||
} catch (error: any) {
|
||||
logger.warn(`[SessionService] OSS文件删除失败: ${error.message}`);
|
||||
// 继续执行,删除数据库记录
|
||||
}
|
||||
|
||||
// 3. 删除数据库记录
|
||||
await prisma.dcToolCSession.delete({
|
||||
where: { id: sessionId },
|
||||
});
|
||||
|
||||
logger.info(`[SessionService] Session删除成功: ${sessionId}`);
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionService] 删除Session失败: ${error.message}`, { sessionId });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新心跳(延长过期时间)
|
||||
*
|
||||
* @param sessionId - Session ID
|
||||
* @returns 新的过期时间
|
||||
*/
|
||||
async updateHeartbeat(sessionId: string): Promise<Date> {
|
||||
try {
|
||||
logger.info(`[SessionService] 更新心跳: ${sessionId}`);
|
||||
|
||||
// 检查Session是否存在
|
||||
const session = await prisma.dcToolCSession.findUnique({
|
||||
where: { id: sessionId },
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
throw new Error('Session不存在');
|
||||
}
|
||||
|
||||
// 更新过期时间
|
||||
const newExpiresAt = new Date(Date.now() + SESSION_EXPIRE_MINUTES * 60 * 1000);
|
||||
|
||||
await prisma.dcToolCSession.update({
|
||||
where: { id: sessionId },
|
||||
data: {
|
||||
expiresAt: newExpiresAt,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`[SessionService] 心跳更新成功: ${sessionId}, 新过期时间: ${newExpiresAt.toISOString()}`);
|
||||
|
||||
return newExpiresAt;
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionService] 更新心跳失败: ${error.message}`, { sessionId });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理过期Session(定时任务使用)
|
||||
*
|
||||
* @returns 清理的Session数量
|
||||
*/
|
||||
async cleanExpiredSessions(): Promise<number> {
|
||||
try {
|
||||
logger.info('[SessionService] 开始清理过期Session...');
|
||||
|
||||
// 查询所有过期的Session
|
||||
const expiredSessions = await prisma.dcToolCSession.findMany({
|
||||
where: {
|
||||
expiresAt: {
|
||||
lt: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`[SessionService] 发现${expiredSessions.length}个过期Session`);
|
||||
|
||||
// 删除过期Session
|
||||
let cleanedCount = 0;
|
||||
for (const session of expiredSessions) {
|
||||
try {
|
||||
await this.deleteSession(session.id);
|
||||
cleanedCount++;
|
||||
} catch (error: any) {
|
||||
logger.warn(`[SessionService] 清理Session失败: ${session.id}`, { error });
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`[SessionService] 清理完成: ${cleanedCount}/${expiredSessions.length}个`);
|
||||
|
||||
return cleanedCount;
|
||||
} catch (error: any) {
|
||||
logger.error(`[SessionService] 清理过期Session失败: ${error.message}`, { error });
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化Session数据
|
||||
*
|
||||
* @param session - Prisma Session对象
|
||||
* @returns 格式化的Session数据
|
||||
*/
|
||||
private formatSession(session: any): SessionData {
|
||||
return {
|
||||
id: session.id,
|
||||
userId: session.userId,
|
||||
fileName: session.fileName,
|
||||
fileKey: session.fileKey,
|
||||
totalRows: session.totalRows,
|
||||
totalCols: session.totalCols,
|
||||
columns: session.columns as string[],
|
||||
encoding: session.encoding,
|
||||
fileSize: session.fileSize,
|
||||
createdAt: session.createdAt,
|
||||
updatedAt: session.updatedAt,
|
||||
expiresAt: session.expiresAt,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 导出单例实例 ====================
|
||||
|
||||
export const sessionService = new SessionService();
|
||||
|
||||
382
backend/test-tool-c-day2.mjs
Normal file
382
backend/test-tool-c-day2.mjs
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* Tool C Day 2 API测试脚本
|
||||
*
|
||||
* 测试内容:
|
||||
* 1. 创建测试Excel文件
|
||||
* 2. 上传文件创建Session
|
||||
* 3. 获取Session信息
|
||||
* 4. 获取预览数据
|
||||
* 5. 获取完整数据
|
||||
* 6. 更新心跳
|
||||
* 7. 删除Session
|
||||
*
|
||||
* 执行方式:node test-tool-c-day2.mjs
|
||||
*/
|
||||
|
||||
import FormData from 'form-data';
|
||||
import axios from 'axios';
|
||||
import * as XLSX from 'xlsx';
|
||||
import { Buffer } from 'buffer';
|
||||
|
||||
const BASE_URL = 'http://localhost:3000';
|
||||
const API_PREFIX = '/api/v1/dc/tool-c';
|
||||
|
||||
// ==================== 辅助函数 ====================
|
||||
|
||||
function printSection(title) {
|
||||
console.log('\n' + '='.repeat(70));
|
||||
console.log(` ${title}`);
|
||||
console.log('='.repeat(70) + '\n');
|
||||
}
|
||||
|
||||
function printSuccess(message) {
|
||||
console.log('✅ ' + message);
|
||||
}
|
||||
|
||||
function printError(message) {
|
||||
console.log('❌ ' + message);
|
||||
}
|
||||
|
||||
function printInfo(message) {
|
||||
console.log('ℹ️ ' + message);
|
||||
}
|
||||
|
||||
// ==================== 创建测试Excel文件 ====================
|
||||
|
||||
function createTestExcelFile() {
|
||||
printSection('创建测试Excel文件');
|
||||
|
||||
// 创建医疗数据
|
||||
const testData = [
|
||||
{ patient_id: 'P001', name: '张三', age: 25, gender: '男', diagnosis: '感冒', sbp: 120, dbp: 80 },
|
||||
{ patient_id: 'P002', name: '李四', age: 65, gender: '女', diagnosis: '高血压', sbp: 150, dbp: 95 },
|
||||
{ patient_id: 'P003', name: '王五', age: 45, gender: '男', diagnosis: '糖尿病', sbp: 135, dbp: 85 },
|
||||
{ patient_id: 'P004', name: '赵六', age: 70, gender: '女', diagnosis: '冠心病', sbp: 160, dbp: 100 },
|
||||
{ patient_id: 'P005', name: '钱七', age: 35, gender: '男', diagnosis: '胃炎', sbp: 110, dbp: 70 },
|
||||
{ patient_id: 'P006', name: '孙八', age: 55, gender: '女', diagnosis: '肺炎', sbp: 125, dbp: 82 },
|
||||
{ patient_id: 'P007', name: '周九', age: 48, gender: '男', diagnosis: '肝炎', sbp: 130, dbp: 88 },
|
||||
{ patient_id: 'P008', name: '吴十', age: 62, gender: '女', diagnosis: '关节炎', sbp: 145, dbp: 92 },
|
||||
];
|
||||
|
||||
// 创建工作簿
|
||||
const ws = XLSX.utils.json_to_sheet(testData);
|
||||
const wb = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
|
||||
|
||||
// 生成Buffer
|
||||
const excelBuffer = XLSX.write(wb, { type: 'buffer', bookType: 'xlsx' });
|
||||
|
||||
printSuccess(`测试文件创建成功: ${testData.length}行 x ${Object.keys(testData[0]).length}列`);
|
||||
printInfo(`文件大小: ${(excelBuffer.length / 1024).toFixed(2)} KB`);
|
||||
|
||||
return {
|
||||
buffer: excelBuffer,
|
||||
fileName: 'test-medical-data.xlsx',
|
||||
expectedRows: testData.length,
|
||||
expectedCols: Object.keys(testData[0]).length,
|
||||
};
|
||||
}
|
||||
|
||||
// ==================== API测试函数 ====================
|
||||
|
||||
async function testUploadFile(excelData) {
|
||||
printSection('测试1: 上传Excel文件创建Session');
|
||||
|
||||
try {
|
||||
const form = new FormData();
|
||||
form.append('file', excelData.buffer, {
|
||||
filename: excelData.fileName,
|
||||
contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
});
|
||||
|
||||
printInfo(`上传文件: ${excelData.fileName}`);
|
||||
|
||||
const response = await axios.post(
|
||||
`${BASE_URL}${API_PREFIX}/sessions/upload`,
|
||||
form,
|
||||
{
|
||||
headers: form.getHeaders(),
|
||||
timeout: 10000,
|
||||
}
|
||||
);
|
||||
|
||||
if (response.status === 201 && response.data.success) {
|
||||
printSuccess('文件上传成功');
|
||||
console.log('响应数据:', JSON.stringify(response.data.data, null, 2));
|
||||
|
||||
const { sessionId, totalRows, totalCols, columns } = response.data.data;
|
||||
|
||||
// 验证数据
|
||||
if (totalRows === excelData.expectedRows && totalCols === excelData.expectedCols) {
|
||||
printSuccess(`数据验证通过: ${totalRows}行 x ${totalCols}列`);
|
||||
} else {
|
||||
printError(`数据不匹配: 预期${excelData.expectedRows}行x${excelData.expectedCols}列, 实际${totalRows}行x${totalCols}列`);
|
||||
}
|
||||
|
||||
printInfo(`Session ID: ${sessionId}`);
|
||||
printInfo(`列名: ${columns.join(', ')}`);
|
||||
|
||||
return sessionId;
|
||||
} else {
|
||||
printError('上传失败: ' + JSON.stringify(response.data));
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
printError('上传异常: ' + (error.response?.data?.error || error.message));
|
||||
if (error.response) {
|
||||
console.log('错误详情:', JSON.stringify(error.response.data, null, 2));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function testGetSession(sessionId) {
|
||||
printSection('测试2: 获取Session信息');
|
||||
|
||||
try {
|
||||
printInfo(`Session ID: ${sessionId}`);
|
||||
|
||||
const response = await axios.get(
|
||||
`${BASE_URL}${API_PREFIX}/sessions/${sessionId}`,
|
||||
{ timeout: 5000 }
|
||||
);
|
||||
|
||||
if (response.status === 200 && response.data.success) {
|
||||
printSuccess('Session信息获取成功');
|
||||
console.log('Session信息:', JSON.stringify(response.data.data, null, 2));
|
||||
return true;
|
||||
} else {
|
||||
printError('获取失败');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
printError('获取异常: ' + (error.response?.data?.error || error.message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function testGetPreviewData(sessionId) {
|
||||
printSection('测试3: 获取预览数据(前100行)');
|
||||
|
||||
try {
|
||||
printInfo(`Session ID: ${sessionId}`);
|
||||
|
||||
const response = await axios.get(
|
||||
`${BASE_URL}${API_PREFIX}/sessions/${sessionId}/preview`,
|
||||
{ timeout: 10000 }
|
||||
);
|
||||
|
||||
if (response.status === 200 && response.data.success) {
|
||||
printSuccess('预览数据获取成功');
|
||||
|
||||
const { totalRows, previewRows, previewData } = response.data.data;
|
||||
printInfo(`总行数: ${totalRows}, 预览行数: ${previewRows}`);
|
||||
printInfo(`预览数据前3行:`);
|
||||
console.log(JSON.stringify(previewData.slice(0, 3), null, 2));
|
||||
|
||||
return true;
|
||||
} else {
|
||||
printError('获取预览数据失败');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
printError('获取预览数据异常: ' + (error.response?.data?.error || error.message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function testGetFullData(sessionId) {
|
||||
printSection('测试4: 获取完整数据');
|
||||
|
||||
try {
|
||||
printInfo(`Session ID: ${sessionId}`);
|
||||
|
||||
const response = await axios.get(
|
||||
`${BASE_URL}${API_PREFIX}/sessions/${sessionId}/full`,
|
||||
{ timeout: 10000 }
|
||||
);
|
||||
|
||||
if (response.status === 200 && response.data.success) {
|
||||
printSuccess('完整数据获取成功');
|
||||
|
||||
const { totalRows, data } = response.data.data;
|
||||
printInfo(`总行数: ${totalRows}`);
|
||||
printInfo(`完整数据前2行:`);
|
||||
console.log(JSON.stringify(data.slice(0, 2), null, 2));
|
||||
|
||||
return true;
|
||||
} else {
|
||||
printError('获取完整数据失败');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
printError('获取完整数据异常: ' + (error.response?.data?.error || error.message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function testUpdateHeartbeat(sessionId) {
|
||||
printSection('测试5: 更新心跳');
|
||||
|
||||
try {
|
||||
printInfo(`Session ID: ${sessionId}`);
|
||||
|
||||
const response = await axios.post(
|
||||
`${BASE_URL}${API_PREFIX}/sessions/${sessionId}/heartbeat`,
|
||||
{},
|
||||
{ timeout: 5000 }
|
||||
);
|
||||
|
||||
if (response.status === 200 && response.data.success) {
|
||||
printSuccess('心跳更新成功');
|
||||
console.log('新过期时间:', response.data.data.expiresAt);
|
||||
return true;
|
||||
} else {
|
||||
printError('心跳更新失败');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
printError('心跳更新异常: ' + (error.response?.data?.error || error.message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function testDeleteSession(sessionId) {
|
||||
printSection('测试6: 删除Session');
|
||||
|
||||
try {
|
||||
printInfo(`Session ID: ${sessionId}`);
|
||||
|
||||
const response = await axios.delete(
|
||||
`${BASE_URL}${API_PREFIX}/sessions/${sessionId}`,
|
||||
{ timeout: 5000 }
|
||||
);
|
||||
|
||||
if (response.status === 200 && response.data.success) {
|
||||
printSuccess('Session删除成功');
|
||||
return true;
|
||||
} else {
|
||||
printError('Session删除失败');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
printError('Session删除异常: ' + (error.response?.data?.error || error.message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function testGetDeletedSession(sessionId) {
|
||||
printSection('测试7: 验证Session已删除');
|
||||
|
||||
try {
|
||||
printInfo(`尝试获取已删除的Session: ${sessionId}`);
|
||||
|
||||
const response = await axios.get(
|
||||
`${BASE_URL}${API_PREFIX}/sessions/${sessionId}`,
|
||||
{ timeout: 5000 }
|
||||
);
|
||||
|
||||
printError('Session仍然存在(不应该)');
|
||||
return false;
|
||||
} catch (error) {
|
||||
if (error.response?.status === 404) {
|
||||
printSuccess('Session已正确删除(返回404)');
|
||||
return true;
|
||||
} else {
|
||||
printError('未预期的错误: ' + error.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 主测试函数 ====================
|
||||
|
||||
async function runAllTests() {
|
||||
console.log('\n' + '🚀'.repeat(35));
|
||||
console.log(' Tool C Day 2 API测试');
|
||||
console.log('🚀'.repeat(35));
|
||||
|
||||
const results = {};
|
||||
|
||||
try {
|
||||
// 创建测试文件
|
||||
const excelData = createTestExcelFile();
|
||||
|
||||
// 测试1: 上传文件
|
||||
const sessionId = await testUploadFile(excelData);
|
||||
results['上传文件'] = !!sessionId;
|
||||
|
||||
if (!sessionId) {
|
||||
printError('上传失败,后续测试无法继续');
|
||||
return results;
|
||||
}
|
||||
|
||||
// 等待1秒
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
// 测试2: 获取Session信息
|
||||
results['获取Session'] = await testGetSession(sessionId);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
// 测试3: 获取预览数据
|
||||
results['获取预览数据'] = await testGetPreviewData(sessionId);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
// 测试4: 获取完整数据
|
||||
results['获取完整数据'] = await testGetFullData(sessionId);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
// 测试5: 更新心跳
|
||||
results['更新心跳'] = await testUpdateHeartbeat(sessionId);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
// 测试6: 删除Session
|
||||
results['删除Session'] = await testDeleteSession(sessionId);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
// 测试7: 验证删除
|
||||
results['验证删除'] = await testGetDeletedSession(sessionId);
|
||||
|
||||
} catch (error) {
|
||||
printError('测试过程中发生异常: ' + error.message);
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// 汇总结果
|
||||
printSection('测试结果汇总');
|
||||
|
||||
let passed = 0;
|
||||
let total = 0;
|
||||
|
||||
for (const [testName, result] of Object.entries(results)) {
|
||||
total++;
|
||||
if (result) {
|
||||
passed++;
|
||||
console.log(`${testName.padEnd(20)}: ✅ 通过`);
|
||||
} else {
|
||||
console.log(`${testName.padEnd(20)}: ❌ 失败`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n' + '-'.repeat(70));
|
||||
console.log(`总计: ${passed}/${total} 通过 (${((passed/total)*100).toFixed(1)}%)`);
|
||||
console.log('-'.repeat(70));
|
||||
|
||||
if (passed === total) {
|
||||
console.log('\n🎉 所有测试通过!Day 2 Session管理功能完成!\n');
|
||||
} else {
|
||||
console.log(`\n⚠️ 有 ${total - passed} 个测试失败,请检查\n`);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行测试
|
||||
runAllTests()
|
||||
.then(() => {
|
||||
console.log('测试完成');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('测试失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user