优化(登录预算与任务): 建议上限封顶2000、错误中文化及补全跳过触发来源
This commit is contained in:
@@ -34,6 +34,13 @@ from ..services.execution_jobs import (
|
||||
request_execution_cancel,
|
||||
)
|
||||
from ..services.login_failure import classify_failure, failure_kind_label
|
||||
from ..services.login_quota_service import (
|
||||
AUTOMATED_CATEGORIES,
|
||||
CATEGORY_ACCOUNT_CHECK,
|
||||
CATEGORY_MANUAL,
|
||||
humanize_login_message,
|
||||
infer_category_from_message,
|
||||
)
|
||||
from ..services.login_service import batch_registry
|
||||
|
||||
router = APIRouter(prefix="/api/login", tags=["登录任务"])
|
||||
@@ -278,22 +285,47 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]:
|
||||
accounts_map[int(row[0])] = row
|
||||
|
||||
# 登录任务记录本身不带来源;来源在 LoginAttempt(人工/自动化/首次登录/重登)。
|
||||
# 已跳过的任务不会创建 LoginAttempt,需要按同批次 LoginAttempt / ExecutionJob / 消息文案补全来源与分类。
|
||||
batch_ids = {str(t.attempt_batch_id or t.batch_id or "") for t in rows}
|
||||
batch_ids.discard("")
|
||||
attempt_map: dict[tuple[str, int], LoginAttempt] = {}
|
||||
if batch_ids and account_ids:
|
||||
batch_fallback_map: dict[str, tuple[str, str]] = {}
|
||||
if batch_ids:
|
||||
for attempt in (
|
||||
db.query(LoginAttempt)
|
||||
.filter(
|
||||
LoginAttempt.batch_id.in_(batch_ids),
|
||||
LoginAttempt.account_id.in_(account_ids),
|
||||
)
|
||||
.filter(LoginAttempt.batch_id.in_(batch_ids))
|
||||
.order_by(LoginAttempt.id.asc())
|
||||
.all()
|
||||
):
|
||||
attempt_map[(str(attempt.batch_id or ""), int(attempt.account_id))] = (
|
||||
attempt
|
||||
)
|
||||
b_id = str(attempt.batch_id or "")
|
||||
if int(attempt.account_id) in account_ids:
|
||||
attempt_map[(b_id, int(attempt.account_id))] = attempt
|
||||
if b_id and b_id not in batch_fallback_map:
|
||||
batch_fallback_map[b_id] = (
|
||||
str(attempt.trigger_source or ""),
|
||||
str(attempt.category or ""),
|
||||
)
|
||||
|
||||
missing_batch_ids = batch_ids - set(batch_fallback_map.keys())
|
||||
job_origin_map: dict[str, tuple[str, str]] = {}
|
||||
if missing_batch_ids:
|
||||
for job_batch_id, payload in (
|
||||
db.query(ExecutionJob.batch_id, ExecutionJob.payload_json)
|
||||
.filter(ExecutionJob.batch_id.in_(missing_batch_ids))
|
||||
.all()
|
||||
):
|
||||
payload_dict = payload if isinstance(payload, dict) else {}
|
||||
job_trigger = str(payload_dict.get("trigger_source") or "")
|
||||
job_mode = str(payload_dict.get("mode") or "")
|
||||
if job_mode == "check":
|
||||
job_category = CATEGORY_ACCOUNT_CHECK
|
||||
elif job_trigger == "manual":
|
||||
job_category = CATEGORY_MANUAL
|
||||
else:
|
||||
job_category = ""
|
||||
if job_trigger or job_category:
|
||||
job_origin_map[str(job_batch_id or "")] = (job_trigger, job_category)
|
||||
|
||||
creator_ids = {
|
||||
int(value)
|
||||
for value in [
|
||||
@@ -318,6 +350,29 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]:
|
||||
account = accounts_map.get(int(t.account_id))
|
||||
batch_id = str(t.attempt_batch_id or t.batch_id or "")
|
||||
attempt = attempt_map.get((batch_id, int(t.account_id)))
|
||||
raw_message = (t.attempt_message if attempt_status else "") or t.message or ""
|
||||
display_message = humanize_login_message(raw_message)
|
||||
|
||||
if attempt is not None:
|
||||
trigger_source = str(attempt.trigger_source or "")
|
||||
category = str(attempt.category or "")
|
||||
else:
|
||||
batch_trigger, batch_category = batch_fallback_map.get(batch_id, ("", ""))
|
||||
job_trigger, job_category = job_origin_map.get(batch_id, ("", ""))
|
||||
inferred_category = infer_category_from_message(raw_message)
|
||||
category = inferred_category or batch_category or job_category
|
||||
trigger_source = batch_trigger or job_trigger
|
||||
if not trigger_source:
|
||||
if category in AUTOMATED_CATEGORIES:
|
||||
trigger_source = "automation"
|
||||
elif category in {CATEGORY_MANUAL, CATEGORY_ACCOUNT_CHECK}:
|
||||
trigger_source = "manual"
|
||||
elif (
|
||||
"今日登录预算" in display_message
|
||||
or "每小时登录尝试预算" in display_message
|
||||
):
|
||||
trigger_source = "automation"
|
||||
|
||||
result.append(
|
||||
LoginTaskOut(
|
||||
id=t.id,
|
||||
@@ -333,13 +388,11 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]:
|
||||
cookie_updated_at=(account[7] if account else None),
|
||||
status=attempt_status or t.status,
|
||||
cookie="",
|
||||
message=(t.attempt_message if attempt_status else "")
|
||||
or t.message
|
||||
or "",
|
||||
message=display_message,
|
||||
created_by=t.created_by,
|
||||
created_by_username=creator_map.get(int(t.created_by), ""),
|
||||
trigger_source=(str(attempt.trigger_source or "") if attempt else ""),
|
||||
category=(str(attempt.category or "") if attempt else ""),
|
||||
trigger_source=trigger_source,
|
||||
category=category,
|
||||
created_at=t.created_at,
|
||||
finished_at=t.attempt_at or t.finished_at,
|
||||
)
|
||||
@@ -412,10 +465,18 @@ def get_task_logs(
|
||||
)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="登录任务不存在")
|
||||
items = [
|
||||
(
|
||||
{**entry, "message": humanize_login_message(entry.get("message"))}
|
||||
if isinstance(entry, dict) and "message" in entry
|
||||
else entry
|
||||
)
|
||||
for entry in (task.logs or [])
|
||||
]
|
||||
return {
|
||||
"task_id": int(task.id),
|
||||
"account_id": int(task.account_id),
|
||||
"items": task.logs or [],
|
||||
"items": items,
|
||||
}
|
||||
|
||||
|
||||
@@ -523,7 +584,8 @@ def export_tasks(
|
||||
for task in rows:
|
||||
attempt_status = task.attempt_status or ""
|
||||
effective = attempt_status or task.status or ""
|
||||
message = (task.attempt_message if attempt_status else "") or task.message or ""
|
||||
raw_message = (task.attempt_message if attempt_status else "") or task.message or ""
|
||||
message = humanize_login_message(raw_message)
|
||||
kind = classify_failure(message) if effective in {"failed", "error"} else ""
|
||||
username, tag, owner = info.get(task.account_id, ("", "", ""))
|
||||
writer.writerow(
|
||||
|
||||
Reference in New Issue
Block a user