This commit is contained in:
Your Name
2026-04-17 15:13:46 +08:00
parent fe87c7b343
commit 0e096947fc
19 changed files with 791 additions and 151 deletions

View File

@@ -19,6 +19,39 @@ from app.services.worker_control_service import send_worker_command, start_worke
router = APIRouter(tags=["detect"])
def _build_detect_action_result(
*,
action: str,
ok: bool,
message: str,
poll_after_seconds: int = 2,
refresh_status: bool = True,
data: dict | None = None,
) -> dict:
result = {
"action": action,
"poll_after_seconds": poll_after_seconds,
"refresh_status": refresh_status,
**(data or {}),
}
lowered = str(message or "").strip().lower()
if "ui_level" not in result:
if not ok:
result["ui_level"] = "warning" if any(keyword in lowered for keyword in ("当前没有", "无需", "未运行", "未启动")) else "error"
elif any(keyword in str(message or "") for keyword in ("命令已发送", "已发送检测启动请求", "控制指令")):
result["ui_level"] = "warning"
else:
result["ui_level"] = "success"
if "poll_schedule_seconds" not in result:
if ok:
result["poll_schedule_seconds"] = [1, max(2, poll_after_seconds)]
elif poll_after_seconds > 0:
result["poll_schedule_seconds"] = [poll_after_seconds]
else:
result["poll_schedule_seconds"] = []
return result
def _build_settings_summary(settings_payload: dict) -> dict:
thread_count_resolution = resolve_thread_count(settings_payload=settings_payload)
return {
@@ -65,15 +98,16 @@ def detect_job_detail(job_id: int) -> ApiResponse:
def start_detect() -> ApiResponse:
job_summary = create_detect_job_if_needed(limit=1000, created_by="api")
if not job_summary:
result = _build_detect_action_result(
action="start",
ok=False,
message="当前没有可创建的检测任务",
data={"job": None},
)
return ApiResponse(
code=0,
message="当前没有可创建的检测任务",
data={
"action": "start",
"job": None,
"poll_after_seconds": 2,
"refresh_status": True,
},
data=result,
)
cycle_token = uuid4().hex[:10]
append_detect_job_event(
@@ -91,6 +125,12 @@ def start_detect() -> ApiResponse:
ok, message = start_worker()
if not ok:
result = _build_detect_action_result(
action="start",
ok=False,
message=message,
data={"job": job_summary},
)
append_detect_job_event(
job_summary["job_id"],
event_type="job_dispatch_failed",
@@ -101,12 +141,7 @@ def start_detect() -> ApiResponse:
return ApiResponse(
code=1,
message=message,
data={
"action": "start",
"job": job_summary,
"poll_after_seconds": 2,
"refresh_status": True,
},
data=result,
)
command_ok, command_message = send_worker_command(
@@ -140,16 +175,14 @@ def start_detect() -> ApiResponse:
progress=snapshot.get("progress", {}),
settings_summary=settings_summary,
)
return ApiResponse(
code=0 if command_ok else 1,
message=f"{message}{command_message}" if command_ok else command_message,
data={
"action": "start",
"job": job_summary,
"poll_after_seconds": 2,
"refresh_status": True,
},
response_message = f"{message}{command_message}" if command_ok else command_message
result = _build_detect_action_result(
action="start",
ok=command_ok,
message=response_message,
data={"job": job_summary},
)
return ApiResponse(code=0 if command_ok else 1, message=response_message, data=result)
@router.post("/detect/stop", response_model=ApiResponse)
@@ -193,12 +226,5 @@ def stop_detect() -> ApiResponse:
settings_summary=settings_summary,
active_job=active_job,
)
return ApiResponse(
code=0 if ok else 1,
message=message,
data={
"action": "stop",
"poll_after_seconds": 2,
"refresh_status": True,
},
)
result = _build_detect_action_result(action="stop", ok=ok, message=message)
return ApiResponse(code=0 if ok else 1, message=message, data=result)