84 lines
2.9 KiB
Python
84 lines
2.9 KiB
Python
"""后台检测任务"""
|
|
import logging
|
|
|
|
from django.conf import settings
|
|
from django.db import transaction
|
|
|
|
from .checker import run_checks
|
|
from .llm import ai_summarize
|
|
from .models import Submission
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def run_submission(submission_id: int):
|
|
with transaction.atomic():
|
|
submission = Submission.objects.select_for_update().get(pk=submission_id)
|
|
submission.status = "running"
|
|
submission.save(update_fields=["status"])
|
|
file_path = submission.file.path
|
|
|
|
from pathlib import Path
|
|
|
|
archive = Path(file_path)
|
|
root = archive.parent
|
|
llm_override = submission.requirement.llm_config or {}
|
|
spec = submission.requirement.spec
|
|
try:
|
|
report, root = run_checks(archive, spec)
|
|
try:
|
|
ai = ai_summarize(str(root), spec, llm_override)
|
|
except Exception:
|
|
logger.exception("AI 检测失败")
|
|
ai = {"summary": "AI 检测执行失败", "issues": []}
|
|
report["ai_inspection"] = ai
|
|
report["overall"] = _overall(report)
|
|
except Exception as exc:
|
|
logger.exception("检测任务失败")
|
|
report = {"error": str(exc), "ai_inspection": None}
|
|
finally:
|
|
import shutil
|
|
shutil.rmtree(root, ignore_errors=True)
|
|
if root != archive.parent:
|
|
shutil.rmtree(archive.parent, ignore_errors=True)
|
|
|
|
with transaction.atomic():
|
|
submission = Submission.objects.get(pk=submission_id)
|
|
submission.status = "failed" if report.get("error") else "completed"
|
|
submission.report = report
|
|
submission.save(update_fields=["status", "report"])
|
|
|
|
|
|
def _overall(report: dict) -> dict:
|
|
checks = []
|
|
stab = report.get("stability", {})
|
|
if stab.get("enabled"):
|
|
checks.append({
|
|
"name": "稳定度",
|
|
"pass": stab.get("pass"),
|
|
"note": stab.get("reason") or (
|
|
f"基准错误率 {stab.get('base_error_rate')}, 加压崩溃率 {stab.get('rampup_crash_rate')}"
|
|
),
|
|
})
|
|
perf = report.get("performance", {})
|
|
if perf.get("enabled"):
|
|
checks.append({"name": "性能", "pass": None, "note": perf.get("note", "依赖稳定度数据")})
|
|
sec = report.get("security", {})
|
|
if sec.get("enabled"):
|
|
checks.append({"name": "安全", "pass": None, "note": sec.get("note", "由 AI 检测覆盖")})
|
|
ai = report.get("ai_inspection") or {}
|
|
if ai:
|
|
fail_points = [i for i in ai.get("issues", []) if i.get("status") == "fail"]
|
|
checks.append({
|
|
"name": "AI 检测",
|
|
"pass": not fail_points if ai.get("issues") is not None else None,
|
|
"note": ai.get("summary", ""),
|
|
})
|
|
passed = sum(1 for c in checks if c.get("pass") is True)
|
|
decided = sum(1 for c in checks if c.get("pass") is not None)
|
|
return {
|
|
"passed_checks": passed,
|
|
"decided_checks": decided,
|
|
"checks": checks,
|
|
}
|