优化(账号自动化): 履约保护登录配额与重登优先级

This commit is contained in:
yml
2026-11-25 22:13:21 +08:00
parent 0943b7a362
commit 9bdd2f4e1a
25 changed files with 1510 additions and 193 deletions
@@ -11,6 +11,7 @@ from sqlalchemy.orm import Session
from ..models import Account, LoginAttempt, LoginQuotaSetting
from .account_lifecycle import MANUAL_KINDS, MANUAL_STATES
from .account_protection_service import load_protection_snapshot
from .account_value import is_high_value_expr
CK_AVERAGE_LIFETIME_DAYS = 7.38
@@ -25,6 +26,19 @@ MAX_DAILY_ATTEMPTS = 5000
ASSUMED_ACTIVE_HOURS = 12
def _fit_priority_budgets(limits: list[int], available: int) -> list[int]:
"""把 P0-P3 预算压进全局可用名额,按优先级从低到高削减。"""
fitted = list(limits)
excess = max(0, sum(fitted) - max(0, int(available)))
for index in range(len(fitted) - 1, -1, -1):
if excess <= 0:
break
cut = min(fitted[index], excess)
fitted[index] -= cut
excess -= cut
return fitted
def _automatable_accounts(db: Session):
return db.query(Account).filter(
Account.auto_managed.is_(True),
@@ -55,6 +69,7 @@ def _recent_login_outcomes(
"onboarding",
"relogin_high_value",
"relogin_ordinary",
"relogin_protected",
"lease_refresh",
)
),
@@ -83,6 +98,8 @@ def _current_quota(db: Session) -> dict[str, int]:
"hourly_attempt_limit": 100,
"per_account_daily_limit": 2,
"onboarding_daily_limit": 150,
"relogin_protected_daily_limit": 500,
"lease_refresh_daily_limit": 300,
"relogin_high_value_daily_limit": 300,
"relogin_ordinary_daily_limit": 500,
"manual_daily_limit": 300,
@@ -95,6 +112,12 @@ def _current_quota(db: Session) -> dict[str, int]:
"hourly_attempt_limit": int(row.hourly_attempt_limit),
"per_account_daily_limit": int(row.per_account_daily_limit),
"onboarding_daily_limit": int(row.onboarding_daily_limit),
"relogin_protected_daily_limit": int(
getattr(row, "relogin_protected_daily_limit", 500)
),
"lease_refresh_daily_limit": int(
getattr(row, "lease_refresh_daily_limit", 300)
),
"relogin_high_value_daily_limit": int(
row.relogin_high_value_daily_limit
),
@@ -134,6 +157,36 @@ def recommend_login_budget(
invalid_total = int(invalid_query.count() or 0)
invalid_high = _count_high_value(db, invalid_query)
invalid_ordinary = max(0, invalid_total - invalid_high)
protection = load_protection_snapshot(db)
protected_ids = protection.relogin_ids
if protected_ids:
protected_valid = int(
valid_query.filter(Account.id.in_(protected_ids)).count() or 0
)
protected_invalid = int(
invalid_query.filter(Account.id.in_(protected_ids)).count() or 0
)
nonprotected_valid_high = int(
_count_high_value(
db,
valid_query.filter(~Account.id.in_(protected_ids)),
)
or 0
)
nonprotected_invalid_high = int(
_count_high_value(
db,
invalid_query.filter(~Account.id.in_(protected_ids)),
)
or 0
)
else:
protected_valid = 0
protected_invalid = 0
nonprotected_valid_high = valid_high
nonprotected_invalid_high = invalid_high
nonprotected_valid = max(0, valid_total - protected_valid)
nonprotected_invalid = max(0, invalid_total - protected_invalid)
imported_total = int(
_automatable_accounts(db).filter(Account.status == "imported").count() or 0
)
@@ -154,40 +207,76 @@ def recommend_login_budget(
)
core_attempts = math.ceil(required_success / effective_rate)
manual_reserve = max(30, math.ceil(core_attempts * 0.1))
relogin_success = math.ceil(
(
invalid_total / RELOGIN_RECOVERY_DAYS
+ valid_total / CK_AVERAGE_LIFETIME_DAYS
)
/ effective_rate
)
onboarding_attempts = math.ceil(onboarding_pressure / effective_rate)
daily_attempts = max(
MIN_DAILY_ATTEMPTS,
core_attempts + manual_reserve,
relogin_success + onboarding_attempts + manual_reserve,
nonprotected_relogin_pressure_high = (
nonprotected_valid_high / CK_AVERAGE_LIFETIME_DAYS
+ nonprotected_invalid_high / RELOGIN_RECOVERY_DAYS
)
daily_attempts = min(daily_attempts, MAX_DAILY_ATTEMPTS)
hourly_attempts = max(1, math.ceil(daily_attempts / ASSUMED_ACTIVE_HOURS))
relogin_pressure_high = (
valid_high / CK_AVERAGE_LIFETIME_DAYS
+ invalid_high / RELOGIN_RECOVERY_DAYS
)
relogin_pressure_total = (
valid_total / CK_AVERAGE_LIFETIME_DAYS
+ invalid_total / RELOGIN_RECOVERY_DAYS
nonprotected_relogin_pressure_total = (
nonprotected_valid / CK_AVERAGE_LIFETIME_DAYS
+ nonprotected_invalid / RELOGIN_RECOVERY_DAYS
)
high_share = (
relogin_pressure_high / relogin_pressure_total
if relogin_pressure_total > 0
nonprotected_relogin_pressure_high
/ nonprotected_relogin_pressure_total
if nonprotected_relogin_pressure_total > 0
else 0.5
)
nonprotected_relogin_attempts = math.ceil(
nonprotected_relogin_pressure_total / effective_rate
)
relogin_high_limit = max(
0,
math.ceil(relogin_success * high_share),
math.ceil(nonprotected_relogin_attempts * high_share),
)
relogin_ordinary_limit = max(0, relogin_success - relogin_high_limit)
relogin_ordinary_limit = max(
0,
nonprotected_relogin_attempts - relogin_high_limit,
)
relogin_protected_limit = max(
50,
math.ceil(
(protected_invalid / RELOGIN_RECOVERY_DAYS) / effective_rate
),
)
lease_refresh_limit = max(
50,
math.ceil(
(protected_valid / CK_AVERAGE_LIFETIME_DAYS) / effective_rate
),
)
raw_relogin_limits = [
relogin_protected_limit,
lease_refresh_limit,
relogin_high_limit,
relogin_ordinary_limit,
]
planned_attempts = (
sum(raw_relogin_limits) + onboarding_attempts + manual_reserve
)
daily_attempts = min(
MAX_DAILY_ATTEMPTS,
max(
MIN_DAILY_ATTEMPTS,
core_attempts + manual_reserve,
planned_attempts,
),
)
available_relogin_attempts = max(
0,
daily_attempts - onboarding_attempts - manual_reserve,
)
(
relogin_protected_limit,
lease_refresh_limit,
relogin_high_limit,
relogin_ordinary_limit,
) = _fit_priority_budgets(
raw_relogin_limits,
available_relogin_attempts,
)
hourly_attempts = max(1, math.ceil(daily_attempts / ASSUMED_ACTIVE_HOURS))
if sample < MIN_RECENT_SAMPLE or recent_success_rate < 70:
concurrency = 3
@@ -201,6 +290,8 @@ def recommend_login_budget(
"hourly_attempt_limit": hourly_attempts,
"per_account_daily_limit": 2,
"onboarding_daily_limit": onboarding_attempts,
"relogin_protected_daily_limit": relogin_protected_limit,
"lease_refresh_daily_limit": lease_refresh_limit,
"relogin_high_value_daily_limit": relogin_high_limit,
"relogin_ordinary_daily_limit": relogin_ordinary_limit,
"manual_daily_limit": manual_reserve,
@@ -217,6 +308,8 @@ def recommend_login_budget(
"invalid_backlog": invalid_total,
"invalid_high_value": invalid_high,
"invalid_ordinary": invalid_ordinary,
"protected_valid_accounts": protected_valid,
"protected_invalid_backlog": protected_invalid,
"imported_backlog": imported_total,
"recent_sample": sample,
"recent_success_rate": recent_success_rate,