feat(account-check): 增加履约账号 CK 保护目标

This commit is contained in:
yml
2026-11-22 09:57:03 +08:00
parent 492288e84f
commit f2041f2444
10 changed files with 1151 additions and 23 deletions
@@ -18,6 +18,7 @@ from ..models import (
LoginAttempt,
)
from .account_capability_service import AVAILABILITY_AVAILABLE, AVAILABILITY_LIMITED
from .account_protection_service import evaluate_protection_targets
from .account_supply_service import evaluate_supply_targets
TREND_DAYS = 7
@@ -367,6 +368,42 @@ def account_platform_status(
}
)
protection_targets = evaluate_protection_targets(db, now=moment)
for item in protection_targets:
if not item["enabled"]:
continue
affected = (
int(item["ck_invalid"])
+ int(item["ck_error"])
+ int(item["without_cookie"])
)
if affected:
alerts.append(
{
"code": "account_protection_ck_affected",
"severity": "critical",
"group_key": item["group_key"],
"count": affected,
"message": (
f"{item['label']}有 {affected} 个账号 CK 失效、"
"检测异常或缺少 CK"
),
}
)
if int(item["target_shortage"]) > 0:
alerts.append(
{
"code": "account_protection_target_shortage",
"severity": "warning",
"group_key": item["group_key"],
"count": int(item["target_shortage"]),
"message": (
f"{item['label']}有效 CK 距目标还差 "
f"{item['target_shortage']} 个"
),
}
)
if not inventories:
alerts.append(
{
@@ -479,6 +516,7 @@ def account_platform_status(
"fresh_available": fresh_available,
"fresh_limited": fresh_limited,
},
"protection_targets": protection_targets,
"supply_targets": supply_targets,
"alerts": alerts,
"trends": trends,
@@ -0,0 +1,341 @@
"""斗鱼履约账号 CK 保护目标。
保护目标不是新的账号价值分类,也不固定挑选若干账号。成员始终由当前履约
关系动态计算:
- ``presale_bound``:仍在履约中的预售任务所分配的账号;
- ``collab_bound``:仍在进行中的斗鱼外部协作单所绑定的账号。
配置保存在 ``ck_health.config["protection_groups"]``。这样不需要扩容账号表,
也不会污染高价值/普通的库存供给口径。目标数量用于预警缺口;只要组已启用,
当前全部绑定账号都会获得更短的 CK 巡检间隔。
"""
from __future__ import annotations
from dataclasses import dataclass
from datetime import UTC, datetime
from typing import Any
from sqlalchemy.orm import Session
from ..models import (
Account,
AutomationJob,
DouyuExternalOrder,
PlatformFulfillmentTask,
)
CK_HEALTH_JOB_KEY = "ck_health"
PROTECTION_CONFIG_KEY = "protection_groups"
DEFAULT_PROBE_INTERVAL_MINUTES = 60
MIN_PROBE_INTERVAL_MINUTES = 15
MAX_PROBE_INTERVAL_MINUTES = 24 * 60
MAX_TARGET_COUNT = 100_000
# 与 douyu_collab_service.COLLAB_LEASE_STATUSES 保持一致。这里只放状态值,
# 避免服务层为了两个常量导入整个协作业务模块。
COLLAB_ACTIVE_STATUSES = (
"incomplete",
"to_recharge",
"recharged",
"scripted",
"points_error",
"role_error",
"failed",
)
GROUP_DEFINITIONS: dict[str, dict[str, Any]] = {
"presale_bound": {
"label": "预售绑定账号",
"description": "仍在履约中的斗鱼预售任务所分配的全部账号",
"enabled": True,
"target_count": 0,
"probe_interval_minutes": DEFAULT_PROBE_INTERVAL_MINUTES,
"auto_relogin": True,
},
"collab_bound": {
"label": "协作单绑定账号",
"description": "未归档且仍在进行中的斗鱼外部协作单所绑定账号",
"enabled": True,
"target_count": 0,
"probe_interval_minutes": DEFAULT_PROBE_INTERVAL_MINUTES,
"auto_relogin": True,
},
}
def _clamp_int(value: Any, default: int, minimum: int, maximum: int) -> int:
try:
parsed = int(value)
except (TypeError, ValueError):
parsed = default
return max(minimum, min(parsed, maximum))
def normalize_group_config(
group_key: str,
raw: dict[str, Any] | None,
) -> dict[str, Any]:
definition = GROUP_DEFINITIONS[group_key]
value = raw or {}
return {
"key": group_key,
"label": definition["label"],
"description": definition["description"],
"enabled": bool(value.get("enabled", definition["enabled"])),
"target_count": _clamp_int(
value.get("target_count"),
int(definition["target_count"]),
0,
MAX_TARGET_COUNT,
),
"probe_interval_minutes": _clamp_int(
value.get("probe_interval_minutes"),
int(definition["probe_interval_minutes"]),
MIN_PROBE_INTERVAL_MINUTES,
MAX_PROBE_INTERVAL_MINUTES,
),
"auto_relogin": bool(
value.get("auto_relogin", definition["auto_relogin"])
),
}
def normalize_protection_config(
config: dict[str, Any] | None,
) -> dict[str, dict[str, Any]]:
raw_groups = (config or {}).get(PROTECTION_CONFIG_KEY)
if not isinstance(raw_groups, dict):
raw_groups = {}
return {
key: normalize_group_config(
key,
raw_groups.get(key) if isinstance(raw_groups.get(key), dict) else None,
)
for key in GROUP_DEFINITIONS
}
def _presale_account_ids(db: Session) -> set[int]:
rows = (
db.query(PlatformFulfillmentTask.account_id)
.filter(
PlatformFulfillmentTask.sale_mode == "presale",
PlatformFulfillmentTask.account_id.isnot(None),
PlatformFulfillmentTask.execution_status.notin_(
("completed", "cancelled")
),
)
.distinct()
.all()
)
return {int(row[0]) for row in rows if row[0] is not None}
def _collab_account_ids(db: Session) -> set[int]:
rows = (
db.query(DouyuExternalOrder.account_id)
.filter(
DouyuExternalOrder.account_id.isnot(None),
DouyuExternalOrder.archived_at.is_(None),
DouyuExternalOrder.status.in_(COLLAB_ACTIVE_STATUSES),
)
.distinct()
.all()
)
return {int(row[0]) for row in rows if row[0] is not None}
@dataclass(frozen=True)
class ProtectionSnapshot:
groups: dict[str, dict[str, Any]]
group_account_ids: dict[str, frozenset[int]]
@property
def monitored_ids(self) -> frozenset[int]:
ids: set[int] = set()
for key, group in self.groups.items():
if group["enabled"]:
ids.update(self.group_account_ids.get(key, ()))
return frozenset(ids)
@property
def relogin_ids(self) -> frozenset[int]:
ids: set[int] = set()
for key, group in self.groups.items():
if group["enabled"] and group["auto_relogin"]:
ids.update(self.group_account_ids.get(key, ()))
return frozenset(ids)
@property
def suppressed_relogin_ids(self) -> frozenset[int]:
return self.monitored_ids - self.relogin_ids
def probe_intervals_by_account(self) -> dict[int, int]:
intervals: dict[int, int] = {}
for key, group in self.groups.items():
if not group["enabled"]:
continue
seconds = int(group["probe_interval_minutes"]) * 60
for account_id in self.group_account_ids.get(key, ()):
current = intervals.get(account_id)
intervals[account_id] = (
seconds if current is None else min(current, seconds)
)
return intervals
def load_protection_snapshot(
db: Session,
*,
job_config: dict[str, Any] | None = None,
) -> ProtectionSnapshot:
if job_config is None:
job = db.get(AutomationJob, CK_HEALTH_JOB_KEY)
job_config = job.config if job is not None else None
groups = normalize_protection_config(job_config)
return ProtectionSnapshot(
groups=groups,
group_account_ids={
"presale_bound": frozenset(_presale_account_ids(db)),
"collab_bound": frozenset(_collab_account_ids(db)),
},
)
def update_protection_group(
db: Session,
*,
group_key: str,
enabled: bool,
target_count: int,
probe_interval_minutes: int,
auto_relogin: bool,
) -> tuple[AutomationJob, dict[str, dict[str, Any]]]:
if group_key not in GROUP_DEFINITIONS:
raise KeyError(group_key)
job = db.get(AutomationJob, CK_HEALTH_JOB_KEY)
if job is None:
raise LookupError("CK 自动巡检作业尚未初始化")
groups = normalize_protection_config(job.config)
groups[group_key] = normalize_group_config(
group_key,
{
"enabled": enabled,
"target_count": target_count,
"probe_interval_minutes": probe_interval_minutes,
"auto_relogin": auto_relogin,
},
)
job.config = {
**(job.config or {}),
PROTECTION_CONFIG_KEY: groups,
}
job.updated_at = datetime.now(UTC)
return job, groups
def evaluate_protection_targets(
db: Session,
*,
job_config: dict[str, Any] | None = None,
now: datetime | None = None,
) -> list[dict[str, Any]]:
moment = now or datetime.now(UTC)
snapshot = load_protection_snapshot(db, job_config=job_config)
all_ids = {
account_id
for ids in snapshot.group_account_ids.values()
for account_id in ids
}
accounts = (
db.query(Account).filter(Account.id.in_(all_ids)).all() if all_ids else []
)
account_map = {int(account.id): account for account in accounts}
rows: list[dict[str, Any]] = []
for key, group in snapshot.groups.items():
account_ids = snapshot.group_account_ids.get(key, frozenset())
group_accounts = [
account_map[account_id]
for account_id in account_ids
if account_id in account_map
]
status_counts = {
"ck_valid": 0,
"ck_invalid": 0,
"ck_error": 0,
"ck_unchecked": 0,
"without_cookie": 0,
}
for account in group_accounts:
status = str(account.ck_check_status or "")
if status == "valid":
status_counts["ck_valid"] += 1
elif status == "invalid":
status_counts["ck_invalid"] += 1
elif status == "error":
status_counts["ck_error"] += 1
else:
status_counts["ck_unchecked"] += 1
if not account.cookie:
status_counts["without_cookie"] += 1
target = int(group["target_count"])
shortage = max(target - status_counts["ck_valid"], 0)
probe_seconds = int(group["probe_interval_minutes"]) * 60
due_at = moment.timestamp() - probe_seconds
due_count = sum(
1
for account in group_accounts
if account.ck_checked_at is None
or (
account.ck_checked_at.replace(tzinfo=UTC)
if account.ck_checked_at.tzinfo is None
else account.ck_checked_at
).timestamp()
<= due_at
)
needs_attention = bool(
group["enabled"]
and (
status_counts["ck_invalid"]
or status_counts["ck_error"]
or status_counts["without_cookie"]
or shortage
)
)
rows.append(
{
"group_key": key,
"label": group["label"],
"description": group["description"],
"enabled": bool(group["enabled"]),
"target_count": target,
"probe_interval_minutes": int(group["probe_interval_minutes"]),
"auto_relogin": bool(group["auto_relogin"]),
"bound_count": len(group_accounts),
**status_counts,
"target_shortage": shortage,
"probe_due_count": due_count,
"needs_attention": needs_attention,
}
)
return rows
__all__ = [
"CK_HEALTH_JOB_KEY",
"COLLAB_ACTIVE_STATUSES",
"GROUP_DEFINITIONS",
"PROTECTION_CONFIG_KEY",
"ProtectionSnapshot",
"evaluate_protection_targets",
"load_protection_snapshot",
"normalize_group_config",
"normalize_protection_config",
"update_protection_group",
]
@@ -13,16 +13,18 @@ from concurrent.futures import as_completed
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import and_, func, or_
from sqlalchemy import and_, case, func, or_
from sqlalchemy.orm import Session
from ....models import Account, AutomationJob
from ...account_lifecycle import MANUAL_STATES
from ...account_lifecycle import MANUAL_STATES, WORK_LOGIN_RELOGIN
from ...account_protection_service import load_protection_snapshot
from ...account_value import (
HIGH_VALUE_TIER_MAX,
high_value_recheck_seconds,
value_tier_expr,
)
from ...account_work_queue import enqueue_account_work
from ...cookie_check_service import (
check_douyu_cookie,
)
@@ -64,7 +66,40 @@ def _candidates_query(db: Session, job: AutomationJob, now: datetime):
# 到点的高价值号先占本轮名额,剩下的再按「最久没查」填。
hv_cutoff = now - timedelta(seconds=high_value_recheck_seconds(interval))
tier = value_tier_expr()
return (
protection = load_protection_snapshot(
db,
job_config=getattr(job, "config", None),
)
protected_ids = protection.monitored_ids
due_filter = or_(
Account.ck_checked_at.is_(None),
and_(tier <= HIGH_VALUE_TIER_MAX, Account.ck_checked_at < hv_cutoff),
and_(tier > HIGH_VALUE_TIER_MAX, Account.ck_checked_at < cutoff),
)
if protected_ids:
protected_due = []
for group_key, group in protection.groups.items():
group_ids = protection.group_account_ids.get(group_key, frozenset())
if not group["enabled"] or not group_ids:
continue
group_cutoff = now - timedelta(
seconds=int(group["probe_interval_minutes"]) * 60
)
protected_due.append(
and_(
Account.id.in_(group_ids),
or_(
Account.ck_checked_at.is_(None),
Account.ck_checked_at < group_cutoff,
),
)
)
due_filter = or_(
*protected_due,
due_filter,
)
query = (
db.query(Account)
.filter(cookie_operable_filter())
.filter(Account.auto_managed.is_(True))
@@ -73,20 +108,24 @@ def _candidates_query(db: Session, job: AutomationJob, now: datetime):
.filter(Account.ck_check_status.notin_(("invalid", "manual")))
# 生命周期状态机:停用/归档的账号不参与任何自动化(见 account_lifecycle)。
.filter(Account.status.notin_(MANUAL_STATES))
.filter(
or_(
Account.ck_checked_at.is_(None),
and_(tier <= HIGH_VALUE_TIER_MAX, Account.ck_checked_at < hv_cutoff),
and_(tier > HIGH_VALUE_TIER_MAX, Account.ck_checked_at < cutoff),
)
)
.order_by(
.filter(due_filter)
)
if protected_ids:
query = query.order_by(
case((Account.id.in_(protected_ids), 0), else_=1),
tier.asc(),
Account.ck_checked_at.is_(None).desc(),
Account.ck_checked_at.asc(),
Account.id.asc(),
)
)
else:
query = query.order_by(
tier.asc(),
Account.ck_checked_at.is_(None).desc(),
Account.ck_checked_at.asc(),
Account.id.asc(),
)
return query
def build_select_query(db: Session, job: AutomationJob, now: datetime):
@@ -132,6 +171,12 @@ def _detect(account: Account) -> dict[str, Any]:
def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
now = datetime.now(UTC)
protection = load_protection_snapshot(
db,
job_config=getattr(job, "config", None),
)
monitored_ids = protection.monitored_ids
relogin_ids = protection.relogin_ids
accounts = _select_accounts(db, job, now)
if not accounts:
earliest_checked = (
@@ -160,16 +205,21 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
results[account.id] = future.result()
# 只写检测结果三件套;cookie / status / attempt_* 一律不动。
valid = invalid = errors = 0
valid = invalid = errors = protected_scanned = protected_invalid = 0
for index, account in enumerate(accounts, start=1):
result = results.get(account.id)
if result is None:
continue
verdict = str(result.get("status") or "error")
is_protected = int(account.id) in monitored_ids
if is_protected:
protected_scanned += 1
if verdict == "valid":
valid += 1
elif verdict == "invalid":
invalid += 1
if is_protected:
protected_invalid += 1
else:
errors += 1
# 自动巡检不额外刷 account_probe 事件;统一落库仍会处理 CK 状态变化
@@ -198,6 +248,18 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
record_event=False,
commit=False,
)
if verdict == "invalid" and int(account.id) in relogin_ids:
# 保护组失效账号优先进入重登队列。on_ck_verdict 会登记默认
# priority=30,这里把已存在的同类型待办降到 10,不重复建行。
enqueue_account_work(
db,
account_id=int(account.id),
work_type=WORK_LOGIN_RELOGIN,
priority=10,
max_attempts=20,
due_at=now,
reason="履约保护账号 CK 失效,优先自动重登",
)
if index % FLUSH_EVERY == 0:
db.commit()
@@ -216,6 +278,11 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
message = f"本轮检测 {len(accounts)} 条,剩余待检 {remaining} 条"
if errors:
message += f";非鉴权类失败 {errors} 条({error_ratio:.0%})"
if protected_scanned:
message += (
f";履约保护账号 {protected_scanned} 条"
f",失效 {protected_invalid} 条"
)
if paused:
message += ";失败占比过高,已自动暂停作业"
return {
@@ -230,5 +297,7 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
"errors": errors,
"error_ratio": round(error_ratio, 4),
"paused": paused,
"protected_scanned": protected_scanned,
"protected_invalid": protected_invalid,
},
}
@@ -19,7 +19,7 @@ from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import func, or_
from sqlalchemy import case, func, or_
from sqlalchemy.orm import Session
from ....database import WorkerSessionLocal
@@ -40,6 +40,10 @@ from ...account_lifecycle import (
on_login_success,
sync_status,
)
from ...account_protection_service import (
ProtectionSnapshot,
load_protection_snapshot,
)
from ...account_value import value_tier_expr
from ...account_work_queue import (
claim_account_work,
@@ -102,6 +106,7 @@ def build_select_query(
max_attempts: int = 2,
system_user_id: int | None = None,
batch_limit: int | None = None,
protection: ProtectionSnapshot | None = None,
):
"""构造本轮重登候选的查询(与取行分开,便于按方言编译做回归)。
@@ -134,17 +139,33 @@ def build_select_query(
tuple(sorted(MANUAL_KINDS))
)
)
snapshot = protection or load_protection_snapshot(db)
suppressed_ids = snapshot.suppressed_relogin_ids
if suppressed_ids:
# 保护组可以分别关闭自动重登。账号同时属于任一已开启自动重登的组时,
# 仍按允许处理;只有完全被抑制的账号才排除。
query = query.filter(~Account.id.in_(suppressed_ids))
relogin_ids = snapshot.relogin_ids
if system_user_id is not None:
pass # 预留:将来若需要限制到某个归属人,在这里加过滤
# 名额先给第 1、2 档(两套还能换 / 还剩一套 / 积分≥300)。
# 同一档里仍按退避时间,避免把还没到点的高价值号提前拉起来。
limit = int(batch_limit if batch_limit is not None else job.batch_size or 0)
return query.order_by(
value_tier_expr().asc(),
ordering = [
Account.relogin_next_at.is_(None).desc(),
Account.relogin_next_at.asc(),
Account.id.asc(),
).limit(max(limit, 1))
]
if relogin_ids:
ordering.insert(
0,
case((Account.id.in_(relogin_ids), 0), else_=1),
)
ordering.insert(
1 if relogin_ids else 0,
value_tier_expr().asc(),
)
return query.order_by(*ordering).limit(max(limit, 1))
def _load_batch_state(
@@ -208,7 +229,8 @@ def count_pending_relogin(db: Session, *, max_attempts: int = 2) -> int:
count 只会得到批次上限(页面上显示恒等于 10),完全看不出积压。这里也**不
按退避窗口过滤** —— 退避是「什么时候试」的细节,不影响「还有多少要处理」。
"""
return int(
snapshot = load_protection_snapshot(db)
query = (
db.query(func.count(Account.id))
.filter(Account.ck_check_status == "invalid")
.filter(cookie_holding_filter())
@@ -221,9 +243,10 @@ def count_pending_relogin(db: Session, *, max_attempts: int = 2) -> int:
tuple(sorted(MANUAL_KINDS))
)
)
.scalar()
or 0
)
if snapshot.suppressed_relogin_ids:
query = query.filter(~Account.id.in_(snapshot.suppressed_relogin_ids))
return int(query.scalar() or 0)
def _system_user_id(db: Session) -> int | None:
@@ -453,12 +476,14 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
now=now,
pending=pending,
)
protection = load_protection_snapshot(db)
accounts = build_select_query(
db,
job,
now,
max_attempts=max_attempts,
batch_limit=adaptive.batch_size,
protection=protection,
).all()
if not accounts:
db.commit()
@@ -466,11 +491,12 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
selected_account_ids = tuple(int(account.id) for account in accounts)
for account in accounts:
priority = 10 if int(account.id) in protection.relogin_ids else 30
enqueue_account_work(
db,
account_id=int(account.id),
work_type=WORK_LOGIN_RELOGIN,
priority=30,
priority=priority,
max_attempts=20,
due_at=now,
reason="CK 失效,等待自动重登",