Browse Source

多次scan保留

fulian23 4 months ago
parent
commit
017bee65a3
1 changed files with 12 additions and 8 deletions
  1. 12 8
      api/aiRouter.py

+ 12 - 8
api/aiRouter.py

@@ -16,6 +16,11 @@ class RequestBody(BaseModel):
     uuid: str
     repo_url: str
 
+class RequestScan(BaseModel):
+    task_id: str
+    uuid: str
+    repo_url: str
+
 class RequestFile(BaseModel):
     uuid: str
     repo_url: str
@@ -162,7 +167,7 @@ async def process_batch2(local_path,path):
         return {"summary":None}
 
 
-async def analysis(local_path, repo_id):
+async def analysis(local_path, repo_id, task_id):
     file_list = await get_code_files(local_path)
     print(file_list)
     results = []
@@ -172,14 +177,13 @@ async def analysis(local_path, repo_id):
     for result in batch_results:
         if isinstance(result, Exception):
             print(f"处理出错: {result}")
-            await write_to_db({"results": results}, repo_id, 3)
+            await Scan_Tasks.filter(id=task_id).update(state=2, repo_id=repo_id, result={"results": results},
+                                                 scan_end_time=int(time.time()))
         else:
             results.append(result)
-
-    await write_to_db({"results": results}, repo_id, 2)
+    await Scan_Tasks.filter(id=task_id).update(state=2, repo_id=repo_id, result={"results": results},
+                                               scan_end_time=int(time.time()))
     print("扫描完成")
-async def write_to_db(results_dict,repo_id,state):
-    await Scan_Tasks.filter(repo_id=repo_id).update(state=state, result=results_dict,scan_end_time=int(time.time()))
 
 async def commit_task(content,repo_id):
     commit_result=await asyncio.gather(commit_summary(content), return_exceptions=True)
@@ -203,13 +207,13 @@ async def file_task(file_path,repo_id):
 
 
 @airouter.post("/scan")
-async def scan(request: RequestBody, background_tasks: BackgroundTasks):
+async def scan(request: RequestScan, background_tasks: BackgroundTasks):
     local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
     repo = await Repos.get(name=repo_name)
     repo_id = repo.id
     print(f"开始扫描仓库: {repo_name}")
     await Scan_Tasks.filter(repo_id=repo_id).update(state=1, scan_start_time=int(time.time()))
-    background_tasks.add_task(analysis, local_path, repo_id)
+    background_tasks.add_task(analysis, local_path, repo_id, request.task_id)
     return {"code": 200, "msg": "添加扫描任务成功"}
 
 @airouter.post("/summaryCommit")