Browse Source

新增文件总结

fulian23 4 months ago
parent
commit
b3d171fe6c
2 changed files with 65 additions and 5 deletions
  1. 53 5
      api/aiRouter.py
  2. 12 0
      models/aiModels.py

+ 53 - 5
api/aiRouter.py

@@ -8,7 +8,7 @@ from git import Repo
 from http import HTTPStatus
 from http import HTTPStatus
 from dashscope import Application
 from dashscope import Application
 
 
-from models.aiModels import Scan_Tasks, Commit_Summary_Tasks
+from models.aiModels import Scan_Tasks, Commit_Summary_Tasks, File_Summary_Tasks
 from models.gitModels import Repos
 from models.gitModels import Repos
 
 
 airouter = APIRouter()
 airouter = APIRouter()
@@ -16,11 +16,34 @@ class RequestBody(BaseModel):
     uuid: str
     uuid: str
     repo_url: str
     repo_url: str
 
 
+class RequestFile(BaseModel):
+    uuid: str
+    repo_url: str
+    file_path: str
+
 def generate_repo_path(uuid, repo_url):
 def generate_repo_path(uuid, repo_url):
     repo_name = repo_url.split("/")[-1].replace(".git", "")
     repo_name = repo_url.split("/")[-1].replace(".git", "")
     base_path = os.path.join(path, uuid)
     base_path = os.path.join(path, uuid)
     return os.path.join(base_path, repo_name), repo_name
     return os.path.join(base_path, repo_name), repo_name
 
 
+async def file_summary(content):
+    response = Application.call(
+        # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
+        api_key=ai_key,
+        app_id='5e3df30ad8bf44b68209e1ef089ff15c',
+        prompt=content)
+    if response.status_code == HTTPStatus.OK:
+        try:
+            json_data = json.loads(response.output.text)
+            print(json_data)
+        except json.JSONDecodeError:
+            print("返回内容不是有效的 JSON 格式!")
+            json_data = {"summary": []}
+    else:
+        print(f"请求失败: {response.message}")
+        json_data = {"summary": []}
+    return json_data
+
 async def commit_summary(content):
 async def commit_summary(content):
     response = Application.call(
     response = Application.call(
         # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
         # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
@@ -166,6 +189,16 @@ async def commit_task(content,repo_id):
         print("提交成功")
         print("提交成功")
         await Commit_Summary_Tasks.filter(repo_id=repo_id).update(result=commit_result,end_time=int(time.time()))
         await Commit_Summary_Tasks.filter(repo_id=repo_id).update(result=commit_result,end_time=int(time.time()))
 
 
+async def file_task(file_path,repo_id):
+    with open(file_path, 'r', encoding="utf8") as f:
+        content = f.read()
+    file_result = await asyncio.gather(file_summary(content), return_exceptions=True)
+    if isinstance(file_result, Exception):
+        print(f"提交出错: {file_result}")
+    else:
+        print("提交成功")
+        await File_Summary_Tasks.filter(repo_id=repo_id).update(result=file_result, end_time=int(time.time()))
+
 
 
 
 
 
 
@@ -177,16 +210,31 @@ async def scan(request: RequestBody, background_tasks: BackgroundTasks):
     print(f"开始扫描仓库: {repo_name}")
     print(f"开始扫描仓库: {repo_name}")
     await Scan_Tasks.filter(repo_id=repo_id).update(state=1, scan_start_time=int(time.time()))
     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)
-    return {"code": 200, "meg": "添加扫描任务成功"}
-@airouter.post("/summary")
-async def summary(request: RequestBody, background_tasks: BackgroundTasks):
+    return {"code": 200, "msg": "添加扫描任务成功"}
+
+@airouter.post("/summaryCommit")
+async def summaryCommit(request: RequestBody, background_tasks: BackgroundTasks):
     local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
     local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
     repo=await Repos.get(name=repo_name)
     repo=await Repos.get(name=repo_name)
     repo_id=repo.id
     repo_id=repo.id
     await Commit_Summary_Tasks.filter(repo_id=repo_id).update(start_time=int(time.time()))
     await Commit_Summary_Tasks.filter(repo_id=repo_id).update(start_time=int(time.time()))
     commit_content = Repo(local_path).git.log('-1', '-p', '--pretty=format:%h %s')
     commit_content = Repo(local_path).git.log('-1', '-p', '--pretty=format:%h %s')
     background_tasks.add_task(commit_task,commit_content,repo_id)
     background_tasks.add_task(commit_task,commit_content,repo_id)
-    return {"code": 200, "meg": "添加提交任务成功"}
+    return {"code": 200, "msg": "添加提交任务成功"}
+
+@airouter.post("/summaryFile")
+async def summaryFile(request: RequestFile,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
+    await File_Summary_Tasks.filter(repo_id=repo_id).update(start_time=int(time.time()))
+    background_tasks.add_task(file_task, request.file_path, repo_id)
+    return {"code": 200, "msg": "添加提交任务成功"}
+
+
+
+
+
 
 
 
 
 
 

+ 12 - 0
models/aiModels.py

@@ -22,5 +22,17 @@ class Commit_Summary_Tasks(Model):
     repo_id = fields.IntField()
     repo_id = fields.IntField()
     repo_hash = fields.CharField(max_length=40)
     repo_hash = fields.CharField(max_length=40)
 
 
+class File_Summary_Tasks(Model):
+    id = fields.IntField(pk=True)
+    create_user = fields.CharField(max_length=36)
+    create_time = fields.BigIntField()
+    start_time = fields.BigIntField()
+    end_time = fields.BigIntField()
+    result = fields.JSONField()
+    repo_id = fields.IntField()
+    repo_hash = fields.CharField(max_length=40)
+    file_path = fields.CharField(max_length=128)
+
+