-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcleaning_task_routes.py
More file actions
474 lines (394 loc) · 15.3 KB
/
Copy pathcleaning_task_routes.py
File metadata and controls
474 lines (394 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
from typing import Optional
from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.logging import get_logger
from app.db.session import get_db
from app.module.cleaning.schema import (
CleaningTaskDto,
CreateCleaningTaskRequest,
CleaningResultDto,
CleaningTaskLog,
)
from app.module.cleaning.service import CleaningTaskService
from app.module.shared.schema import StandardResponse, PaginatedData
logger = get_logger(__name__)
router = APIRouter(prefix="/cleaning/tasks", tags=["Cleaning Tasks"])
def _get_operator_service():
"""Get operator service"""
from app.module.operator.service import OperatorService
from app.module.operator.repository import (
OperatorRepository,
CategoryRelationRepository,
OperatorReleaseRepository,
)
from app.module.operator.parsers import ParserHolder
from app.module.shared.file_service import FileService
from app.module.shared.chunk_upload_repository import ChunkUploadRepository
return OperatorService(
operator_repo=OperatorRepository(None),
category_relation_repo=CategoryRelationRepository(None),
operator_release_repo=OperatorReleaseRepository(None),
parser_holder=ParserHolder(),
file_service=FileService(ChunkUploadRepository()),
)
def _get_task_service(db: AsyncSession) -> CleaningTaskService:
"""Get cleaning task service instance"""
from app.module.cleaning.service import (
CleaningTaskScheduler,
CleaningTaskValidator,
)
from app.module.cleaning.service.cleaning_task_scheduler import get_scheduler
from app.module.cleaning.repository import (
CleaningTaskRepository,
CleaningResultRepository,
OperatorInstanceRepository,
)
from app.module.dataset.service import DatasetManagementService
from app.module.shared.common.lineage import LineageService
scheduler = get_scheduler() # 使用全局单例,保持轮询状态跨请求共享
operator_service = _get_operator_service()
dataset_service = DatasetManagementService(db)
lineage_service = LineageService(db)
task_repo = CleaningTaskRepository(None)
return CleaningTaskService(
task_repo=task_repo,
result_repo=CleaningResultRepository(None),
operator_instance_repo=OperatorInstanceRepository(None),
operator_service=operator_service,
scheduler=scheduler,
validator=CleaningTaskValidator(task_repo=task_repo, template_repo=None),
dataset_service=dataset_service,
lineage_service=lineage_service,
)
@router.get(
"",
response_model=StandardResponse[PaginatedData[CleaningTaskDto]],
summary="查询清洗任务列表",
description="根据参数查询清洗任务列表(支持分页、状态过滤、关键词搜索)",
tags=["mcp"],
)
async def get_cleaning_tasks(
page: int = 0,
size: int = 10,
status: Optional[str] = None,
keyword: Optional[str] = None,
db: AsyncSession = Depends(get_db),
):
"""Query cleaning tasks"""
task_service = _get_task_service(db)
tasks = await task_service.get_tasks(db, status, keyword, page, size)
count = await task_service.count_tasks(db, status, keyword)
total_pages = (count + size - 1) // size if size > 0 else 0
return StandardResponse(
code="0",
message="success",
data=PaginatedData(
page=page,
size=size,
total_elements=count,
total_pages=total_pages,
content=tasks,
),
)
@router.post(
"",
response_model=StandardResponse[CleaningTaskDto],
summary="创建清洗任务",
description="根据模板ID或算子列表创建清洗任务",
tags=["mcp"],
)
async def create_cleaning_task(
request: CreateCleaningTaskRequest,
db: AsyncSession = Depends(get_db),
):
"""Create cleaning task"""
task_service = _get_task_service(db)
task = await task_service.create_task(db, request)
await db.commit()
await task_service.execute_task(db, task.id)
await db.commit()
return StandardResponse(code="0", message="success", data=task)
@router.get(
"/{task_id}",
response_model=StandardResponse[CleaningTaskDto],
summary="获取清洗任务详情",
description="根据ID获取清洗任务详细信息",
)
async def get_cleaning_task(
task_id: str,
db: AsyncSession = Depends(get_db),
):
"""Get cleaning task by ID"""
task_service = _get_task_service(db)
task = await task_service.get_task(db, task_id)
return StandardResponse(code="0", message="success", data=task)
@router.delete(
"/{task_id}",
response_model=StandardResponse[str],
summary="删除清洗任务",
description="删除指定的清洗任务",
)
async def delete_cleaning_task(
task_id: str,
db: AsyncSession = Depends(get_db),
):
"""Delete cleaning task"""
task_service = _get_task_service(db)
await task_service.delete_task(db, task_id)
await db.commit()
return StandardResponse(code="0", message="success", data=task_id)
@router.post(
"/{task_id}/stop",
response_model=StandardResponse[str],
summary="停止清洗任务",
description="停止正在运行的清洗任务",
)
async def stop_cleaning_task(
task_id: str,
db: AsyncSession = Depends(get_db),
):
"""Stop cleaning task"""
task_service = _get_task_service(db)
await task_service.stop_task(db, task_id)
await db.commit()
return StandardResponse(code="0", message="success", data=task_id)
@router.post(
"/{task_id}/execute",
response_model=StandardResponse[str],
summary="执行清洗任务",
description="重新执行清洗任务",
)
async def execute_cleaning_task(
task_id: str,
db: AsyncSession = Depends(get_db),
):
"""Execute cleaning task"""
task_service = _get_task_service(db)
await task_service.execute_task(db, task_id)
await db.commit()
return StandardResponse(code="0", message="success", data=task_id)
@router.get(
"/{task_id}/result",
response_model=StandardResponse[list[CleaningResultDto]],
summary="获取清洗任务结果",
description="获取指定清洗任务的执行结果",
)
async def get_cleaning_task_results(
task_id: str,
db: AsyncSession = Depends(get_db),
):
"""Get cleaning task results"""
task_service = _get_task_service(db)
results = await task_service.get_task_results(db, task_id)
return StandardResponse(code="0", message="success", data=results)
@router.get(
"/{task_id}/result/download",
summary="下载清洗任务结果文件压缩包",
description="下载指定清洗任务的源文件和处理后文件压缩包",
)
async def download_cleaning_result_files(
task_id: str,
db: AsyncSession = Depends(get_db),
):
"""Download cleaning task result files (both src and dest)"""
import zipfile
import io
from pathlib import Path
from fastapi.responses import StreamingResponse
from app.core.exception import BusinessError, ErrorCodes
task_service = _get_task_service(db)
task = await task_service.get_task(db, task_id)
results = await task_service.get_task_results(db, task_id)
if not results:
raise BusinessError(ErrorCodes.NOT_FOUND, f"Cleaning task {task_id} not found")
src_path = Path("/dataset") / task.src_dataset_id
dest_path = Path("/dataset") / task.dest_dataset_id
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w") as zipf:
for file_record in results:
if file_record.src_name:
src_path = src_path / file_record.src_name
if src_path.exists():
zipf.write(src_path, arcname=f"src/{file_record.src_name}")
if file_record.dest_name:
dest_path = dest_path / file_record.dest_name
if dest_path.exists():
zipf.write(dest_path, arcname=f"dest/{file_record.dest_name}")
zip_buffer.seek(0)
filename = f"task_{task_id}_{file_record.src_name}.zip"
return StreamingResponse(
zip_buffer,
media_type="application/zip",
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
)
@router.get(
"/{task_id}/log/stream",
summary="流式获取清洗任务日志",
description="通过SSE流式获取清洗任务日志",
)
async def stream_cleaning_task_log(
task_id: str,
retry_count: int = 0,
db: AsyncSession = Depends(get_db),
):
"""Stream cleaning task log via SSE"""
import asyncio
import json
import os
import re
from pathlib import Path
FLOW_PATH = "/flow"
task_service = _get_task_service(db)
task = await task_service.task_repo.find_task_by_id(db, task_id)
if not task:
async def error_generator():
yield f"data: {json.dumps({'level': 'ERROR', 'message': 'Task not found'}, ensure_ascii=False)}\n\n"
return StreamingResponse(error_generator(), media_type="text/event-stream")
log_path = Path(f"{FLOW_PATH}/{task_id}/output.log")
if retry_count > 0:
log_path = Path(f"{FLOW_PATH}/{task_id}/output.log.{retry_count}")
# 防止路径穿越:规范化后校验仍在 /flow 下
log_path = log_path.resolve()
if not str(log_path).startswith(FLOW_PATH + "/"):
logger.warning(f"Path traversal attempt in stream_log: task_id={task_id}")
async def traversal_error_generator():
yield f"data: {json.dumps({'level': 'ERROR', 'message': 'Invalid task_id'}, ensure_ascii=False)}\n\n"
return StreamingResponse(traversal_error_generator(), media_type="text/event-stream")
standard_level_pattern = re.compile(
r"\b(DEBUG|Debug|INFO|Info|WARN|Warn|WARNING|Warning|ERROR|Error|FATAL|Fatal)\b"
)
exception_suffix_pattern = re.compile(r"\b\w+(Warning|Error|Exception)\b")
def get_log_level(line: str, default_level: str) -> str:
if not line or not line.strip():
return default_level
std_match = standard_level_pattern.search(line)
if std_match:
return std_match.group(1).upper()
ex_match = exception_suffix_pattern.search(line)
if ex_match:
match = ex_match.group(1).upper()
if match == "WARNING":
return "WARN"
if match in ["ERROR", "EXCEPTION"]:
return "ERROR"
return default_level
async def log_generator():
last_position = 0
last_level = "INFO"
heartbeat_counter = 0
HEARTBEAT_INTERVAL = 10
while True:
try:
current_task = await task_service.task_repo.find_task_by_id(db, task_id)
is_task_finished = current_task and current_task.status in [
"COMPLETED",
"FAILED",
"STOPPED",
]
if log_path.exists():
with open(log_path, "r", encoding="utf-8") as f:
f.seek(last_position)
new_content = f.read()
if new_content:
for line in new_content.splitlines(keepends=True):
last_level = get_log_level(line, last_level)
log_entry = json.dumps(
{"level": last_level, "message": line.rstrip()},
ensure_ascii=False,
)
yield f"data: {log_entry}\n\n"
last_position = f.tell()
heartbeat_counter = 0
else:
heartbeat_counter += 1
f.seek(0, 2)
current_size = f.tell()
if current_size < last_position:
last_position = 0
else:
heartbeat_counter += 1
if is_task_finished:
yield f"data: {json.dumps({'level': 'INFO', 'message': '[END_OF_STREAM]'}, ensure_ascii=False)}\n\n"
break
if heartbeat_counter >= HEARTBEAT_INTERVAL:
yield f"data: {json.dumps({'level': 'DEBUG', 'message': '[HEARTBEAT]'}, ensure_ascii=False)}\n\n"
heartbeat_counter = 0
await asyncio.sleep(1)
except Exception as e:
logger.error(f"Error reading log file: {e}")
await asyncio.sleep(2)
return StreamingResponse(
log_generator(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
},
)
@router.get(
"/{task_id}/log/{retry_count}",
response_model=StandardResponse[list[CleaningTaskLog]],
summary="获取清洗任务日志",
description="获取指定清洗任务的执行日志",
)
async def get_cleaning_task_log(
task_id: str,
retry_count: int,
db: AsyncSession = Depends(get_db),
):
"""Get cleaning task log"""
task_service = _get_task_service(db)
logs = await task_service.get_task_log(db, task_id, retry_count)
return StandardResponse(code="0", message="success", data=logs)
@router.get(
"/{task_id}/log/{retry_count}/download",
summary="下载清洗任务日志文件",
description="下载指定清洗任务的日志文件",
)
async def download_cleaning_task_log(
task_id: str,
retry_count: int,
db: AsyncSession = Depends(get_db),
):
"""Download cleaning task log file"""
from pathlib import Path
from fastapi.responses import FileResponse
FLOW_PATH = "/flow"
task_service = _get_task_service(db)
task = await task_service.task_repo.find_task_by_id(db, task_id)
if not task:
from app.core.exception import BusinessError, ErrorCodes
raise BusinessError(ErrorCodes.CLEANING_TASK_NOT_FOUND, task_id)
log_path = Path(f"{FLOW_PATH}/{task_id}/output.log")
if retry_count > 0:
log_path = Path(f"{FLOW_PATH}/{task_id}/output.log.{retry_count}")
# 防止路径穿越:规范化后校验仍在 /flow 下
log_path = log_path.resolve()
if not str(log_path).startswith(FLOW_PATH + "/"):
logger.warning(f"Path traversal attempt in download_log: task_id={task_id}")
from app.core.exception import BusinessError, ErrorCodes
raise BusinessError(
ErrorCodes.CLEANING_TASK_LOG_NOT_FOUND,
f"Invalid task_id: {task_id}",
)
if not log_path.exists():
from app.core.exception import BusinessError, ErrorCodes
raise BusinessError(
ErrorCodes.CLEANING_TASK_LOG_NOT_FOUND,
f"Log file not found for task {task_id}, retry {retry_count}",
)
# Generate filename with task name and retry count
import re
task_name = task.name or "未命名任务"
safe_task_name = re.sub(
r"[^\w\u4e00-\u9fff\-]", "_", task_name
) # Keep alphanumeric, Chinese, and hyphens
run_number = retry_count + 1 # retry_count is 0-indexed, so add 1 for display
filename = f"{safe_task_name}_第{run_number}次运行.log"
return FileResponse(
path=log_path,
media_type="text/plain",
filename=filename,
)