优化(账号自动化): 完善受保护账号重登筛选与登录配额分配
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from sqlalchemy import and_, func, or_, select
|
||||
|
||||
@@ -12,6 +13,7 @@ from ..models import (
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
DouyuExternalOrder,
|
||||
LoginAttempt,
|
||||
LoginTask,
|
||||
User,
|
||||
)
|
||||
@@ -34,6 +36,31 @@ ACTIVE_COLLAB_STATUSES: tuple[str, ...] = (
|
||||
POOL_OWNER_ROLES: tuple[str, ...] = ("super_admin", "operation", "support")
|
||||
CK_FRESH_SECONDS = 24 * 60 * 60
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES = ("pending", "running")
|
||||
BUSINESS_TZ = ZoneInfo("Asia/Shanghai")
|
||||
PROTECTED_NON_RECOVERABLE_KINDS: tuple[str, ...] = tuple(
|
||||
sorted(kind for kind in MANUAL_KINDS if kind != "captcha")
|
||||
)
|
||||
|
||||
|
||||
def quota_day_for(now: datetime | None = None):
|
||||
"""按业务时区(Asia/Shanghai)换算自然日,与登录预算分桶一致。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
aware = moment if moment.tzinfo else moment.replace(tzinfo=UTC)
|
||||
return aware.astimezone(BUSINESS_TZ).date()
|
||||
|
||||
|
||||
def in_flight_login_exists():
|
||||
"""SQL 子查询:账号是否存在在途登录或检测任务。"""
|
||||
return (
|
||||
select(LoginTask.id)
|
||||
.where(LoginTask.account_id == Account.id)
|
||||
.where(
|
||||
func.coalesce(LoginTask.attempt_status, "").in_(
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
)
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def pool_owner_ids():
|
||||
@@ -54,17 +81,7 @@ def cookie_holding_filter():
|
||||
|
||||
def cookie_operable_filter():
|
||||
"""持有 CK,且该账号没有在途登录/检测尝试。"""
|
||||
in_flight = (
|
||||
select(LoginTask.id)
|
||||
.where(LoginTask.account_id == Account.id)
|
||||
.where(
|
||||
func.coalesce(LoginTask.attempt_status, "").in_(
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
)
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
return and_(cookie_holding_filter(), ~in_flight)
|
||||
return and_(cookie_holding_filter(), ~in_flight_login_exists())
|
||||
|
||||
|
||||
def valid_ck_filter(*, checked_since: datetime | None = None):
|
||||
@@ -119,6 +136,126 @@ def relogin_failure_kind_filter(
|
||||
)
|
||||
|
||||
|
||||
def protected_relogin_eligible_filter(
|
||||
*,
|
||||
protected_ids: set[int] | frozenset[int],
|
||||
max_attempts: int,
|
||||
now: datetime | None = None,
|
||||
cookie_operable: bool = True,
|
||||
due_only: bool = True,
|
||||
suppressed_ids: set[int] | frozenset[int] = frozenset(),
|
||||
):
|
||||
"""履约保护账号(P0)自动恢复准入条件。
|
||||
|
||||
覆盖 CK 失效(invalid)、检测异常(error)和缺 CK(without_cookie),
|
||||
仅排除永久人工问题(credential / must_bind_mobile);昨日已达上限的保护账号
|
||||
在进入新自然日后重新获得当日尝试额度。
|
||||
"""
|
||||
ids = sorted({int(value) for value in protected_ids})
|
||||
if not ids:
|
||||
return Account.id == -1
|
||||
cap = max(1, int(max_attempts))
|
||||
moment = now or datetime.now(UTC)
|
||||
quota_day = quota_day_for(moment)
|
||||
attempts_today = (
|
||||
select(func.count(LoginAttempt.id))
|
||||
.where(
|
||||
LoginAttempt.account_id == Account.id,
|
||||
LoginAttempt.quota_day == quota_day,
|
||||
LoginAttempt.status != "skipped",
|
||||
)
|
||||
.correlate(Account)
|
||||
.scalar_subquery()
|
||||
)
|
||||
has_prior_day_attempt = (
|
||||
select(LoginAttempt.id)
|
||||
.where(
|
||||
LoginAttempt.account_id == Account.id,
|
||||
LoginAttempt.quota_day < quota_day,
|
||||
LoginAttempt.status != "skipped",
|
||||
)
|
||||
.correlate(Account)
|
||||
.exists()
|
||||
)
|
||||
filters = [
|
||||
Account.id.in_(ids),
|
||||
automatable_account_filter(),
|
||||
Account.username != "",
|
||||
Account.password != "",
|
||||
or_(
|
||||
Account.ck_check_status.in_(("invalid", "error")),
|
||||
Account.cookie.is_(None),
|
||||
Account.cookie == "",
|
||||
),
|
||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
||||
PROTECTED_NON_RECOVERABLE_KINDS
|
||||
),
|
||||
attempts_today < cap,
|
||||
or_(Account.relogin_attempts < cap, has_prior_day_attempt),
|
||||
]
|
||||
if cookie_operable:
|
||||
filters.append(~in_flight_login_exists())
|
||||
if due_only:
|
||||
filters.append(
|
||||
or_(
|
||||
Account.relogin_next_at.is_(None),
|
||||
Account.relogin_next_at <= moment,
|
||||
)
|
||||
)
|
||||
suppressed = sorted({int(value) for value in suppressed_ids})
|
||||
if suppressed:
|
||||
filters.append(~Account.id.in_(suppressed))
|
||||
return and_(*filters)
|
||||
|
||||
|
||||
def relogin_eligible_account_filter(
|
||||
*,
|
||||
protected_ids: set[int] | frozenset[int],
|
||||
max_attempts: int,
|
||||
now: datetime | None = None,
|
||||
cookie_operable: bool = True,
|
||||
due_only: bool = True,
|
||||
suppressed_ids: set[int] | frozenset[int] = frozenset(),
|
||||
):
|
||||
"""CK 自动重登全库候选准入条件(P0 履约保护 + 普通/高价值库存)。"""
|
||||
cap = max(1, int(max_attempts))
|
||||
moment = now or datetime.now(UTC)
|
||||
ordinary_filters = [
|
||||
Account.ck_check_status == "invalid",
|
||||
automation_account_filter(cookie_operable=cookie_operable),
|
||||
Account.relogin_attempts < cap,
|
||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
||||
tuple(sorted(MANUAL_KINDS))
|
||||
),
|
||||
]
|
||||
if due_only:
|
||||
ordinary_filters.append(
|
||||
or_(
|
||||
Account.relogin_next_at.is_(None),
|
||||
Account.relogin_next_at <= moment,
|
||||
)
|
||||
)
|
||||
suppressed = sorted({int(value) for value in suppressed_ids})
|
||||
if suppressed:
|
||||
ordinary_filters.append(~Account.id.in_(suppressed))
|
||||
ordinary = and_(*ordinary_filters)
|
||||
|
||||
ids = sorted({int(value) for value in protected_ids})
|
||||
if not ids:
|
||||
return ordinary
|
||||
return or_(
|
||||
and_(~Account.id.in_(ids), ordinary),
|
||||
protected_relogin_eligible_filter(
|
||||
protected_ids=ids,
|
||||
max_attempts=cap,
|
||||
now=moment,
|
||||
cookie_operable=cookie_operable,
|
||||
due_only=due_only,
|
||||
suppressed_ids=suppressed,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def assigned_to_pool_filter():
|
||||
return or_(
|
||||
Account.assigned_to.is_(None),
|
||||
|
||||
@@ -5,11 +5,13 @@ from __future__ import annotations
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import func, or_, select
|
||||
from sqlalchemy import and_, case, func, or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import Account, AutomationJob, LoginTask
|
||||
from ...account_lifecycle import MANUAL_KINDS, MANUAL_STATES, STATUS_MANUAL
|
||||
from ...account_protection_service import load_protection_snapshot
|
||||
from ...account_query_filters import protected_relogin_eligible_filter
|
||||
from ...login_automation import run_login_batch_with_fallback, system_user_id
|
||||
|
||||
JOB_KEY = "account_onboarding"
|
||||
@@ -26,50 +28,61 @@ def _in_flight_login_exists():
|
||||
)
|
||||
|
||||
|
||||
def _onboarding_eligibility_filter(db: Session, *, now: datetime | None = None):
|
||||
protection = load_protection_snapshot(db)
|
||||
ordinary = and_(
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_((*MANUAL_STATES, STATUS_MANUAL)),
|
||||
# 缺账号/密码的账号首登必失败,不能占样本名额。
|
||||
Account.username != "",
|
||||
Account.password != "",
|
||||
# 已经判定为极验/凭证问题的普通账号不再自动首登。
|
||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
||||
tuple(sorted(MANUAL_KINDS))
|
||||
),
|
||||
or_(Account.cookie.is_(None), Account.cookie == ""),
|
||||
~_in_flight_login_exists(),
|
||||
)
|
||||
relogin_ids = protection.relogin_ids
|
||||
if not relogin_ids:
|
||||
return ordinary, protection
|
||||
protected = and_(
|
||||
or_(Account.cookie.is_(None), Account.cookie == ""),
|
||||
protected_relogin_eligible_filter(
|
||||
protected_ids=relogin_ids,
|
||||
max_attempts=2,
|
||||
now=now,
|
||||
cookie_operable=True,
|
||||
due_only=True,
|
||||
suppressed_ids=protection.suppressed_relogin_ids,
|
||||
),
|
||||
)
|
||||
return or_(ordinary, protected), protection
|
||||
|
||||
|
||||
def build_select_query(
|
||||
db: Session,
|
||||
job: AutomationJob,
|
||||
*,
|
||||
limit: int | None = None,
|
||||
):
|
||||
"""未登录、自动托管、没有在途登录尝试的账号。"""
|
||||
query = (
|
||||
db.query(Account)
|
||||
.filter(
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_((*MANUAL_STATES, STATUS_MANUAL)),
|
||||
# 缺账号/密码的账号首登必失败,不能占样本名额。
|
||||
Account.username != "",
|
||||
Account.password != "",
|
||||
# 已经判定为极验/凭证问题的账号不再自动首登。
|
||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
||||
tuple(sorted(MANUAL_KINDS))
|
||||
),
|
||||
or_(Account.cookie.is_(None), Account.cookie == ""),
|
||||
~_in_flight_login_exists(),
|
||||
"""未登录、自动托管、没有在途登录尝试的账号(履约保护目标优先)。"""
|
||||
eligibility, protection = _onboarding_eligibility_filter(db)
|
||||
ordering = []
|
||||
if protection.relogin_ids:
|
||||
ordering.append(
|
||||
case((Account.id.in_(protection.relogin_ids), 0), else_=1)
|
||||
)
|
||||
.order_by(Account.id.asc())
|
||||
)
|
||||
ordering.append(Account.id.asc())
|
||||
query = db.query(Account).filter(eligibility).order_by(*ordering)
|
||||
batch = max(1, int(limit if limit is not None else job.batch_size or 20))
|
||||
return query.limit(batch)
|
||||
|
||||
|
||||
def count_pending_onboarding(db: Session) -> int:
|
||||
eligibility, _ = _onboarding_eligibility_filter(db)
|
||||
return int(
|
||||
db.query(func.count(Account.id))
|
||||
.filter(
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_((*MANUAL_STATES, STATUS_MANUAL)),
|
||||
Account.username != "",
|
||||
Account.password != "",
|
||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
||||
tuple(sorted(MANUAL_KINDS))
|
||||
),
|
||||
or_(Account.cookie.is_(None), Account.cookie == ""),
|
||||
~_in_flight_login_exists(),
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
db.query(func.count(Account.id)).filter(eligibility).scalar() or 0
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import case, func, or_
|
||||
from sqlalchemy import case, func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....database import WorkerSessionLocal
|
||||
@@ -45,8 +45,8 @@ from ...account_protection_service import (
|
||||
load_protection_snapshot,
|
||||
)
|
||||
from ...account_query_filters import (
|
||||
automation_account_filter,
|
||||
relogin_failure_kind_filter,
|
||||
quota_day_for,
|
||||
relogin_eligible_account_filter,
|
||||
)
|
||||
from ...account_value import is_high_value_expr, value_tier_expr
|
||||
from ...account_work_queue import (
|
||||
@@ -172,36 +172,16 @@ def _eligible_account_query(
|
||||
due_only: bool,
|
||||
):
|
||||
"""自动重登的账号级准入条件,执行、补货和计数共用同一套语义。"""
|
||||
query = (
|
||||
db.query(Account)
|
||||
.filter(Account.ck_check_status == "invalid")
|
||||
.filter(
|
||||
automation_account_filter(cookie_operable=cookie_operable)
|
||||
)
|
||||
# 停用/归档的账号一律不碰(生命周期状态机,见 account_lifecycle)。
|
||||
.filter(Account.relogin_attempts < max_attempts)
|
||||
# 已判定需要人工的类型不再自动重试。空值用 coalesce 兜住历史值。
|
||||
.filter(
|
||||
relogin_failure_kind_filter(
|
||||
protected_ids=protection.relogin_ids,
|
||||
max_attempts=max_attempts,
|
||||
)
|
||||
return db.query(Account).filter(
|
||||
relogin_eligible_account_filter(
|
||||
protected_ids=protection.relogin_ids,
|
||||
max_attempts=max_attempts,
|
||||
now=now,
|
||||
cookie_operable=cookie_operable,
|
||||
due_only=due_only,
|
||||
suppressed_ids=protection.suppressed_relogin_ids,
|
||||
)
|
||||
)
|
||||
if due_only:
|
||||
query = query.filter(
|
||||
or_(
|
||||
Account.relogin_next_at.is_(None),
|
||||
Account.relogin_next_at <= _cutoff(now),
|
||||
)
|
||||
)
|
||||
if protection.suppressed_relogin_ids:
|
||||
# 保护组可以分别关闭自动重登。账号同时属于任一已开启自动重登的组时,
|
||||
# 仍按允许处理;只有完全被抑制的账号才排除。
|
||||
query = query.filter(
|
||||
~Account.id.in_(protection.suppressed_relogin_ids)
|
||||
)
|
||||
return query
|
||||
|
||||
|
||||
def _pending_relogin_work_count(
|
||||
@@ -436,6 +416,7 @@ def settle_fresh_results(
|
||||
now: datetime,
|
||||
max_attempts: int = 2,
|
||||
expected_account_ids: list[int] | tuple[int, ...] | None = None,
|
||||
protected_captcha_retry_ids: set[int] | frozenset[int] | None = None,
|
||||
) -> dict[str, int]:
|
||||
"""在**新会话(新事务)**里读回批次结果并结算。
|
||||
|
||||
@@ -467,6 +448,7 @@ def settle_fresh_results(
|
||||
settle_account_ids,
|
||||
now=now,
|
||||
max_attempts=max_attempts,
|
||||
protected_captcha_retry_ids=protected_captcha_retry_ids,
|
||||
)
|
||||
session.commit()
|
||||
return counters
|
||||
@@ -531,13 +513,16 @@ def evaluate_failure_circuit(
|
||||
counters: dict[str, int],
|
||||
config: dict[str, Any],
|
||||
) -> tuple[bool, int, float]:
|
||||
"""评估硬失败率;预算跳过和邮箱配额满都不属于真实失败样本。"""
|
||||
"""评估硬失败率;预算跳过、邮箱配额满与保护账号极验二次重试不触发全局熔断。"""
|
||||
scanned = max(0, int(counters.get("scanned", 0) or 0))
|
||||
skipped = max(0, int(counters.get("skipped", 0) or 0))
|
||||
attempted = max(0, scanned - skipped)
|
||||
protected_captcha_failures = max(
|
||||
0, int(counters.get("protected_captcha_failures", 0) or 0)
|
||||
)
|
||||
attempted = max(0, scanned - skipped - protected_captcha_failures)
|
||||
invalid = max(0, int(counters.get("invalid", 0) or 0))
|
||||
email_quota = max(0, int(counters.get("email_quota", 0) or 0))
|
||||
hard_failures = max(0, invalid - email_quota)
|
||||
hard_failures = max(0, invalid - email_quota - protected_captcha_failures)
|
||||
minimum = max(
|
||||
1,
|
||||
int(
|
||||
@@ -756,7 +741,7 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
now,
|
||||
max_attempts=max_attempts,
|
||||
protection=protection,
|
||||
limit=adaptive.batch_size,
|
||||
limit=max(adaptive.batch_size, len(protection.relogin_ids)),
|
||||
account_ids_filter=protection.relogin_ids,
|
||||
)
|
||||
work_items = _claim_relogin_work(
|
||||
@@ -796,7 +781,6 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
"skipped": 0,
|
||||
"message": "没有到期的自动重登工作项",
|
||||
}
|
||||
db.commit()
|
||||
claimed_account_ids = [int(item.account_id) for item in work_items]
|
||||
account_map = {
|
||||
int(account.id): account
|
||||
@@ -809,6 +793,20 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
for account_id in claimed_account_ids
|
||||
if account_id in account_map
|
||||
]
|
||||
pre_captcha_retry_ids = frozenset(
|
||||
int(account.id)
|
||||
for account in accounts
|
||||
if int(account.id) in protection.relogin_ids
|
||||
and (account.relogin_failure_kind or "") == "captcha"
|
||||
)
|
||||
_reconcile_protected_new_day_attempts(
|
||||
db,
|
||||
accounts=accounts,
|
||||
protection=protection,
|
||||
max_attempts=max_attempts,
|
||||
now=now,
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# 调度单位是**账号**(CK 在账号上);登录记录只是这次尝试的历史。
|
||||
account_ids = [account.id for account in accounts]
|
||||
@@ -845,6 +843,7 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
now=now,
|
||||
max_attempts=max_attempts,
|
||||
expected_account_ids=account_ids,
|
||||
protected_captcha_retry_ids=pre_captcha_retry_ids,
|
||||
)
|
||||
# 取码失败不再切 iframe 重跑:换通道 = 重新发信,斗鱼验证码是攒批投递的,
|
||||
# 重发会把邮件队列压更长、flush 更晚(实测每账号发信量 1.5 -> 3.3 封后,
|
||||
@@ -918,6 +917,7 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
"manual": counters["manual"],
|
||||
"retry": counters["retry"],
|
||||
"email_quota": counters["email_quota"],
|
||||
"protected_captcha_failures": counters.get("protected_captcha_failures", 0),
|
||||
"fallback_batch_id": fallback_batch_id,
|
||||
"fallback_success": fallback_success,
|
||||
"hard_failures": hard_failures,
|
||||
@@ -936,6 +936,42 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _reconcile_protected_new_day_attempts(
|
||||
db: Session,
|
||||
*,
|
||||
accounts: list[Account],
|
||||
protection: ProtectionSnapshot,
|
||||
max_attempts: int,
|
||||
now: datetime,
|
||||
) -> int:
|
||||
"""新自然日重新恢复前日耗尽次数的保护账号前,把账号累计计数对齐到今日已用次数。"""
|
||||
cap = max(1, int(max_attempts))
|
||||
quota_day = quota_day_for(now)
|
||||
reset_count = 0
|
||||
for account in accounts:
|
||||
if int(account.id) not in protection.relogin_ids:
|
||||
continue
|
||||
if int(account.relogin_attempts or 0) < cap:
|
||||
continue
|
||||
attempts_today = int(
|
||||
db.query(func.count(LoginAttempt.id))
|
||||
.filter(
|
||||
LoginAttempt.account_id == account.id,
|
||||
LoginAttempt.quota_day == quota_day,
|
||||
LoginAttempt.status != "skipped",
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
if attempts_today >= cap:
|
||||
continue
|
||||
account.relogin_attempts = attempts_today
|
||||
account.relogin_next_at = None
|
||||
sync_status(db, account, occurred_at=now)
|
||||
reset_count += 1
|
||||
return reset_count
|
||||
|
||||
|
||||
def settle_results(
|
||||
db: Session,
|
||||
fresh: dict[int, tuple[Account, LoginTask]],
|
||||
@@ -943,6 +979,7 @@ def settle_results(
|
||||
*,
|
||||
now: datetime,
|
||||
max_attempts: int = 2,
|
||||
protected_captcha_retry_ids: set[int] | frozenset[int] | None = None,
|
||||
) -> dict[str, int]:
|
||||
"""批次跑完后结算:成功的清零,失败的按分型决定重试还是转人工。
|
||||
|
||||
@@ -964,8 +1001,10 @@ def settle_results(
|
||||
"retry": 0,
|
||||
"email_quota": 0,
|
||||
"skipped": 0,
|
||||
"protected_captcha_failures": 0,
|
||||
}
|
||||
protected_ids = load_protection_snapshot(db).relogin_ids
|
||||
pre_captcha_ids = frozenset(int(value) for value in (protected_captcha_retry_ids or ()))
|
||||
for account_id in account_ids:
|
||||
entry = fresh.get(account_id)
|
||||
if entry is None:
|
||||
@@ -1009,6 +1048,8 @@ def settle_results(
|
||||
|
||||
counters["invalid"] += 1
|
||||
kind = classify_failure(task.attempt_message)
|
||||
if kind == "captcha" and int(account.id) in pre_captcha_ids:
|
||||
counters["protected_captcha_failures"] += 1
|
||||
account.relogin_failure_kind = kind
|
||||
account.relogin_last_error = (task.attempt_message or "")[:500]
|
||||
account.relogin_attempts = int(account.relogin_attempts or 0) + 1
|
||||
|
||||
@@ -12,6 +12,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_query_filters import protected_relogin_eligible_filter
|
||||
from .account_value import is_high_value_expr
|
||||
|
||||
CK_AVERAGE_LIFETIME_DAYS = 7.38
|
||||
@@ -164,7 +165,19 @@ def recommend_login_budget(
|
||||
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
|
||||
db.query(func.count(Account.id))
|
||||
.filter(
|
||||
protected_relogin_eligible_filter(
|
||||
protected_ids=protected_ids,
|
||||
max_attempts=2,
|
||||
now=moment,
|
||||
cookie_operable=False,
|
||||
due_only=False,
|
||||
suppressed_ids=protection.suppressed_relogin_ids,
|
||||
)
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
nonprotected_valid_high = int(
|
||||
_count_high_value(
|
||||
@@ -234,10 +247,8 @@ def recommend_login_budget(
|
||||
nonprotected_relogin_attempts - relogin_high_limit,
|
||||
)
|
||||
relogin_protected_limit = max(
|
||||
50,
|
||||
math.ceil(
|
||||
(protected_invalid / RELOGIN_RECOVERY_DAYS) / effective_rate
|
||||
),
|
||||
100,
|
||||
math.ceil((protected_invalid * 2) / effective_rate),
|
||||
)
|
||||
lease_refresh_limit = max(
|
||||
50,
|
||||
|
||||
@@ -20,7 +20,7 @@ from .account_lifecycle import DEFAULT_CK_LIFETIME, MANUAL_KINDS
|
||||
from .account_protection_service import ProtectionSnapshot, load_protection_snapshot
|
||||
from .account_query_filters import (
|
||||
automation_account_filter,
|
||||
relogin_failure_kind_filter,
|
||||
protected_relogin_eligible_filter,
|
||||
)
|
||||
from .account_value import is_high_value_account, is_high_value_expr
|
||||
from .douyu_service import LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
@@ -102,6 +102,8 @@ def classify_login_category(
|
||||
or not account.cookie
|
||||
or account.cookie_updated_at is None
|
||||
):
|
||||
if protected and trigger_source == "automation":
|
||||
return CATEGORY_RELOGIN_PROTECTED
|
||||
return CATEGORY_ONBOARDING
|
||||
return CATEGORY_MANUAL
|
||||
|
||||
@@ -261,21 +263,15 @@ def _pending_protected_invalid_query(
|
||||
now: datetime,
|
||||
max_attempts: int = 2,
|
||||
):
|
||||
relogin_ids = protection.relogin_ids
|
||||
if not relogin_ids:
|
||||
return db.query(Account.id).filter(Account.id == -1)
|
||||
return db.query(Account.id).filter(
|
||||
Account.id.in_(relogin_ids),
|
||||
Account.ck_check_status == "invalid",
|
||||
automation_account_filter(cookie_operable=True),
|
||||
relogin_failure_kind_filter(
|
||||
protected_ids=relogin_ids,
|
||||
protected_relogin_eligible_filter(
|
||||
protected_ids=protection.relogin_ids,
|
||||
max_attempts=max_attempts,
|
||||
),
|
||||
or_(
|
||||
Account.relogin_next_at.is_(None),
|
||||
Account.relogin_next_at <= now,
|
||||
),
|
||||
now=now,
|
||||
cookie_operable=True,
|
||||
due_only=True,
|
||||
suppressed_ids=protection.suppressed_relogin_ids,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -524,7 +520,7 @@ def reserve_login_attempt(
|
||||
setting = _setting(db)
|
||||
protection = None
|
||||
is_protected = False
|
||||
if mode == "relogin" and not category:
|
||||
if not category and (mode == "relogin" or trigger_source == "automation"):
|
||||
protection = load_protection_snapshot(db)
|
||||
is_protected = int(account.id) in protection.relogin_ids
|
||||
resolved_category = classify_login_category(
|
||||
|
||||
Reference in New Issue
Block a user