优化(登录预算与任务): 建议上限封顶2000、错误中文化及补全跳过触发来源
This commit is contained in:
@@ -23,12 +23,13 @@ MIN_RECENT_SAMPLE = 20
|
||||
DEFAULT_SUCCESS_RATE = 70.0
|
||||
MIN_EFFECTIVE_SUCCESS_RATE = 35.0
|
||||
MIN_DAILY_ATTEMPTS = 50
|
||||
MAX_DAILY_ATTEMPTS = 5000
|
||||
MAX_DAILY_ATTEMPTS = 2000
|
||||
MAX_MANUAL_RESERVE = 200
|
||||
ASSUMED_ACTIVE_HOURS = 12
|
||||
|
||||
|
||||
def _fit_priority_budgets(limits: list[int], available: int) -> list[int]:
|
||||
"""把 P0-P3 预算压进全局可用名额,按优先级从低到高削减。"""
|
||||
"""把 P0-P4 预算压进全局可用名额,按优先级从低到高削减。"""
|
||||
fitted = list(limits)
|
||||
excess = max(0, sum(fitted) - max(0, int(available)))
|
||||
for index in range(len(fitted) - 1, -1, -1):
|
||||
@@ -219,7 +220,11 @@ def recommend_login_budget(
|
||||
expected_daily_invalid + recovery_pressure + onboarding_pressure
|
||||
)
|
||||
core_attempts = math.ceil(required_success / effective_rate)
|
||||
manual_reserve = max(30, math.ceil(core_attempts * 0.1))
|
||||
# 人工预留按每日总预算(而非未封顶需求)比例计算并设上限,避免大库存时挤占 P0-P3 自动化预算。
|
||||
raw_manual_reserve = min(
|
||||
MAX_MANUAL_RESERVE,
|
||||
max(30, math.ceil(min(MAX_DAILY_ATTEMPTS, core_attempts) * 0.1)),
|
||||
)
|
||||
onboarding_attempts = math.ceil(onboarding_pressure / effective_rate)
|
||||
nonprotected_relogin_pressure_high = (
|
||||
nonprotected_valid_high / CK_AVERAGE_LIFETIME_DAYS
|
||||
@@ -257,35 +262,36 @@ def recommend_login_budget(
|
||||
),
|
||||
)
|
||||
|
||||
raw_relogin_limits = [
|
||||
raw_priority_limits = [
|
||||
relogin_protected_limit,
|
||||
lease_refresh_limit,
|
||||
relogin_high_limit,
|
||||
relogin_ordinary_limit,
|
||||
onboarding_attempts,
|
||||
]
|
||||
planned_attempts = (
|
||||
sum(raw_relogin_limits) + onboarding_attempts + manual_reserve
|
||||
)
|
||||
planned_attempts = sum(raw_priority_limits) + raw_manual_reserve
|
||||
daily_attempts = min(
|
||||
MAX_DAILY_ATTEMPTS,
|
||||
max(
|
||||
MIN_DAILY_ATTEMPTS,
|
||||
core_attempts + manual_reserve,
|
||||
core_attempts + raw_manual_reserve,
|
||||
planned_attempts,
|
||||
),
|
||||
)
|
||||
available_relogin_attempts = max(
|
||||
0,
|
||||
daily_attempts - onboarding_attempts - manual_reserve,
|
||||
manual_reserve = min(
|
||||
raw_manual_reserve,
|
||||
max(30, math.ceil(daily_attempts * 0.1)),
|
||||
)
|
||||
available_auto_attempts = max(0, daily_attempts - manual_reserve)
|
||||
(
|
||||
relogin_protected_limit,
|
||||
lease_refresh_limit,
|
||||
relogin_high_limit,
|
||||
relogin_ordinary_limit,
|
||||
onboarding_attempts,
|
||||
) = _fit_priority_budgets(
|
||||
raw_relogin_limits,
|
||||
available_relogin_attempts,
|
||||
raw_priority_limits,
|
||||
available_auto_attempts,
|
||||
)
|
||||
hourly_attempts = max(1, math.ceil(daily_attempts / ASSUMED_ACTIVE_HOURS))
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, date, datetime, timedelta
|
||||
import re
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from sqlalchemy import and_, func, or_, select
|
||||
@@ -57,14 +58,104 @@ _TIER_BY_CATEGORY: dict[str, int] = {
|
||||
CATEGORY_ONBOARDING: 4,
|
||||
}
|
||||
|
||||
CATEGORY_LABELS: dict[str, str] = {
|
||||
CATEGORY_ONBOARDING: "首次登录",
|
||||
CATEGORY_RELOGIN_HIGH: "高价值重登",
|
||||
CATEGORY_RELOGIN_ORDINARY: "普通重登",
|
||||
CATEGORY_RELOGIN_PROTECTED: "履约保护重登",
|
||||
CATEGORY_LEASE_REFRESH: "履约快过期续期",
|
||||
CATEGORY_MANUAL: "人工登录",
|
||||
"manual": "人工登录",
|
||||
CATEGORY_ACCOUNT_CHECK: "账号检测",
|
||||
}
|
||||
|
||||
AUTOMATED_CATEGORIES: frozenset[str] = frozenset(
|
||||
{
|
||||
CATEGORY_ONBOARDING,
|
||||
CATEGORY_RELOGIN_HIGH,
|
||||
CATEGORY_RELOGIN_ORDINARY,
|
||||
CATEGORY_RELOGIN_PROTECTED,
|
||||
CATEGORY_LEASE_REFRESH,
|
||||
}
|
||||
)
|
||||
|
||||
_HTTP_POOL_RE = re.compile(
|
||||
r"HTTPS?ConnectionPool\([^\n]*?(?=(?:(旧 Cookie 已保留)|$))"
|
||||
)
|
||||
_QUOTA_KEY_RE = re.compile(
|
||||
r"(relogin_protected|lease_refresh|relogin_high_value|relogin_ordinary|onboarding|manual_login|account_check|all)\s*今日登录预算已用完"
|
||||
)
|
||||
|
||||
BUSINESS_TZ = ZoneInfo("Asia/Shanghai")
|
||||
|
||||
|
||||
def category_label(category: str | None) -> str:
|
||||
"""将登录分类 key 映射为中文展示名。"""
|
||||
key = (category or "").strip()
|
||||
if key == CATEGORY_ALL:
|
||||
return "全局"
|
||||
return CATEGORY_LABELS.get(key, key)
|
||||
|
||||
|
||||
def infer_category_from_message(message: str | None) -> str:
|
||||
"""从跳过或失败文案中提取登录分类 key(兼容历史原始英文 key 与中文标签)。"""
|
||||
text = message or ""
|
||||
if not text:
|
||||
return ""
|
||||
for key, label in CATEGORY_LABELS.items():
|
||||
if key == "manual":
|
||||
continue
|
||||
if key in text or f"{label}今日登录预算已用完" in text:
|
||||
return key
|
||||
return ""
|
||||
|
||||
|
||||
def humanize_login_message(message: str | None) -> str:
|
||||
"""把历史或底层英文登录错误、跳过原因整理为可读中文。"""
|
||||
text = str(message or "")
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
text = _QUOTA_KEY_RE.sub(
|
||||
lambda match: f"{category_label(match.group(1))}今日登录预算已用完",
|
||||
text,
|
||||
)
|
||||
text = text.replace("极验 slide 验证失败: forbidden", "极验滑块验证失败: 触发风控拦截")
|
||||
text = text.replace("极验 slide 验证失败: fail", "极验滑块验证失败: 滑块未通过")
|
||||
text = text.replace("极验 slide 验证失败:", "极验滑块验证失败:")
|
||||
text = text.replace("极验 slide 验证失败", "极验滑块验证失败")
|
||||
|
||||
if (
|
||||
"HTTPSConnectionPool(" in text
|
||||
or "HTTPConnectionPool(" in text
|
||||
or "Max retries exceeded" in text
|
||||
):
|
||||
lower = text.lower()
|
||||
if "proxyerror" in lower or "unable to connect to proxy" in lower:
|
||||
network_reason = "代理连接失败"
|
||||
elif "timed out" in lower or "timeouterror" in lower:
|
||||
network_reason = "网络或代理请求超时"
|
||||
elif "ssl" in lower:
|
||||
network_reason = "SSL 安全连接失败"
|
||||
elif "max retries exceeded" in lower:
|
||||
network_reason = "网络连接失败(重试次数超限)"
|
||||
else:
|
||||
network_reason = "网络连接异常"
|
||||
if _HTTP_POOL_RE.search(text):
|
||||
text = _HTTP_POOL_RE.sub(network_reason, text)
|
||||
else:
|
||||
text = text.replace("Max retries exceeded", "网络连接失败(重试次数超限)")
|
||||
|
||||
text = text.replace("Read timed out", "网络读取超时")
|
||||
return text
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class QuotaDecision:
|
||||
allowed: bool
|
||||
reason: str = ""
|
||||
attempt: LoginAttempt | None = None
|
||||
category: str = ""
|
||||
|
||||
|
||||
def _bucket(now: datetime | None = None) -> tuple[date, int]:
|
||||
@@ -620,7 +711,12 @@ def reserve_login_attempt(
|
||||
if _category_daily_total(db, quota_day, resolved_category) >= _category_limit(
|
||||
setting, resolved_category
|
||||
):
|
||||
return QuotaDecision(False, reason=f"{resolved_category} 今日登录预算已用完")
|
||||
label = category_label(resolved_category)
|
||||
return QuotaDecision(
|
||||
False,
|
||||
reason=f"{label}今日登录预算已用完",
|
||||
category=resolved_category,
|
||||
)
|
||||
|
||||
attempt = LoginAttempt(
|
||||
account_id=int(account.id),
|
||||
@@ -707,7 +803,9 @@ def cleanup_orphan_login_attempts(
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AUTOMATED_CATEGORIES",
|
||||
"CATEGORY_ACCOUNT_CHECK",
|
||||
"CATEGORY_LABELS",
|
||||
"CATEGORY_LEASE_REFRESH",
|
||||
"CATEGORY_MANUAL",
|
||||
"CATEGORY_ONBOARDING",
|
||||
@@ -715,10 +813,13 @@ __all__ = [
|
||||
"CATEGORY_RELOGIN_ORDINARY",
|
||||
"CATEGORY_RELOGIN_PROTECTED",
|
||||
"QuotaDecision",
|
||||
"category_label",
|
||||
"classify_login_category",
|
||||
"cleanup_orphan_login_attempts",
|
||||
"finish_login_attempt",
|
||||
"get_login_quota_usage_summary",
|
||||
"humanize_login_message",
|
||||
"infer_category_from_message",
|
||||
"login_concurrency_limit",
|
||||
"reserve_login_attempt",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user