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
+323
View File
@@ -0,0 +1,323 @@
"""履约保护账号的动态成员、目标和巡检/重登优先级测试。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from web.backend.models import (
Account,
AutomationJob,
DouyuExternalOrder,
PlatformFulfillmentTask,
User,
)
from web.backend.routers.douyu import update_account_protection_target
from web.backend.schemas import DouyuAccountProtectionTargetUpdate
from web.backend.services.account_observability import account_platform_status
from web.backend.services.account_protection_service import (
PROTECTION_CONFIG_KEY,
evaluate_protection_targets,
load_protection_snapshot,
update_protection_group,
)
from web.backend.services.automation.jobs import ck_health, ck_relogin
NOW = datetime.now(UTC).replace(microsecond=0)
def _account(
name: str,
*,
ck_status: str = "valid",
checked_at: datetime | None = None,
points: int = 0,
) -> Account:
return Account(
username=name,
password="p",
email=f"{name}@example.com",
email_password="ep",
cookie="acf_uid=100",
cookie_updated_at=NOW,
ck_check_status=ck_status,
ck_checked_at=checked_at or NOW,
status="active",
auto_managed=True,
points=points,
)
def _presale_task(account: Account, *, execution_status: str = "bound") -> PlatformFulfillmentTask:
return PlatformFulfillmentTask(
source_order_id=f"order-{account.id or account.username}",
source_order_item_id=f"item-{account.id or account.username}",
sale_mode="presale",
account_id=account.id,
binding_status=(
"bound" if execution_status not in {"completed", "cancelled"} else "not_started"
),
execution_status=execution_status,
)
def _collab_order(
account: Account,
*,
order_no: str | None = None,
status: str = "incomplete",
archived_at: datetime | None = None,
) -> DouyuExternalOrder:
return DouyuExternalOrder(
order_no=order_no or f"collab-{account.username}",
account_id=account.id,
account_username=account.username,
status=status,
archived_at=archived_at,
)
def _health_job(config: dict | None = None) -> AutomationJob:
return AutomationJob(
key=ck_health.JOB_KEY,
interval_seconds=6 * 3600,
batch_size=200,
concurrency=3,
config=config,
)
def test_protection_membership_uses_active_fulfillment_relationships(db):
shared = _account("protection-shared")
presale_only = _account("protection-presale")
collab_only = _account("protection-collab")
finished = _account("protection-finished")
db.add_all([shared, presale_only, collab_only, finished])
db.flush()
db.add_all(
[
_presale_task(shared),
_presale_task(presale_only),
_presale_task(finished, execution_status="completed"),
_collab_order(shared),
_collab_order(collab_only),
_collab_order(finished, status="done"),
_collab_order(
finished,
order_no="collab-finished-archived",
archived_at=NOW,
),
]
)
db.commit()
snapshot = load_protection_snapshot(db, job_config=None)
assert snapshot.group_account_ids["presale_bound"] == frozenset(
{shared.id, presale_only.id}
)
assert snapshot.group_account_ids["collab_bound"] == frozenset(
{shared.id, collab_only.id}
)
assert snapshot.monitored_ids == frozenset(
{shared.id, presale_only.id, collab_only.id}
)
def test_protection_target_reports_ck_health_and_shortage(db):
valid = _account("protection-valid")
invalid = _account("protection-invalid", ck_status="invalid")
db.add_all([valid, invalid])
db.flush()
db.add_all([_presale_task(valid), _presale_task(invalid)])
job = _health_job(
{
PROTECTION_CONFIG_KEY: {
"presale_bound": {
"enabled": True,
"target_count": 3,
"probe_interval_minutes": 30,
"auto_relogin": True,
}
}
}
)
db.add(job)
db.commit()
rows = evaluate_protection_targets(db, job_config=job.config, now=NOW)
presale = next(row for row in rows if row["group_key"] == "presale_bound")
status = account_platform_status(db, now=NOW)
assert presale["bound_count"] == 2
assert presale["ck_valid"] == 1
assert presale["ck_invalid"] == 1
assert presale["target_shortage"] == 2
assert presale["needs_attention"] is True
assert {
alert["code"] for alert in status["alerts"]
} >= {
"account_protection_ck_affected",
"account_protection_target_shortage",
}
def test_ck_health_probes_protected_account_at_its_shorter_interval_first(db):
protected = _account(
"protection-due",
checked_at=NOW - timedelta(minutes=90),
)
regular_due = _account(
"protection-regular-due",
checked_at=NOW - timedelta(days=2),
)
regular_due.points = None
regular_due.gold_balance = 0
regular_due.elite_opened = False
regular_due.overview_queried_at = NOW
regular_fresh = _account(
"protection-regular-fresh",
checked_at=NOW - timedelta(minutes=30),
)
db.add_all([protected, regular_due, regular_fresh])
db.flush()
db.add(_presale_task(protected))
job = _health_job(
{
PROTECTION_CONFIG_KEY: {
"presale_bound": {
"enabled": True,
"target_count": 1,
"probe_interval_minutes": 60,
"auto_relogin": True,
}
}
}
)
db.add(job)
db.commit()
selected = ck_health._select_accounts(db, job, NOW)
assert [account.id for account in selected] == [protected.id, regular_due.id]
def test_ck_relogin_prioritizes_protected_invalid_account(db):
protected = _account(
"protection-relogin-first",
ck_status="invalid",
)
high_value = _account(
"protection-high-value",
ck_status="invalid",
points=1000,
)
db.add_all([protected, high_value])
db.flush()
db.add(_presale_task(protected))
db.add(_health_job())
relogin_job = AutomationJob(
key=ck_relogin.JOB_KEY,
interval_seconds=6 * 3600,
batch_size=2,
concurrency=1,
)
db.commit()
selected = ck_relogin.build_select_query(
db,
relogin_job,
NOW,
batch_limit=2,
).all()
assert [account.id for account in selected] == [protected.id, high_value.id]
def test_ck_relogin_can_be_disabled_for_a_protection_group(db):
protected = _account("protection-no-auto-relogin", ck_status="invalid")
ordinary = _account("protection-ordinary-auto-relogin", ck_status="invalid")
db.add_all([protected, ordinary])
db.flush()
db.add(_presale_task(protected))
db.add(
_health_job(
{
PROTECTION_CONFIG_KEY: {
"presale_bound": {
"enabled": True,
"target_count": 1,
"probe_interval_minutes": 60,
"auto_relogin": False,
}
}
}
)
)
relogin_job = AutomationJob(
key=ck_relogin.JOB_KEY,
interval_seconds=6 * 3600,
batch_size=10,
concurrency=1,
)
db.commit()
selected = ck_relogin.build_select_query(
db,
relogin_job,
NOW,
batch_limit=10,
).all()
assert [account.id for account in selected] == [ordinary.id]
def test_update_protection_group_preserves_other_job_config(db):
job = _health_job({"unrelated": {"keep": True}})
db.add(job)
db.commit()
updated, groups = update_protection_group(
db,
group_key="collab_bound",
enabled=False,
target_count=25,
probe_interval_minutes=90,
auto_relogin=False,
)
db.commit()
assert updated.config["unrelated"] == {"keep": True}
assert groups["collab_bound"]["target_count"] == 25
assert updated.config[PROTECTION_CONFIG_KEY]["collab_bound"]["enabled"] is False
def test_protection_target_update_route_returns_current_status(db):
user = User(
username="protection-admin",
password_hash="h",
role="super_admin",
is_active=True,
)
account = _account("protection-route")
db.add_all([user, account])
db.flush()
db.add_all([_presale_task(account), _health_job()])
db.commit()
result = update_account_protection_target(
"presale_bound",
DouyuAccountProtectionTargetUpdate(
enabled=True,
target_count=5,
probe_interval_minutes=45,
auto_relogin=True,
),
db=db,
current=user,
)
assert result["group_key"] == "presale_bound"
assert result["bound_count"] == 1
assert result["target_count"] == 5
assert result["target_shortage"] == 4
assert result["probe_interval_minutes"] == 45
+54
View File
@@ -48,6 +48,8 @@ from ..models import (
from ..permissions import user_has_permission
from ..schemas import (
CookieReloginRequest,
DouyuAccountProtectionTargetOut,
DouyuAccountProtectionTargetUpdate,
DouyuConfigOut,
DouyuConfigUpdate,
DouyuExchangeResultImageOut,
@@ -63,12 +65,17 @@ from ..schemas import (
)
from ..services.account_binding import bind_payload
from ..services.account_observability import account_platform_status
from ..services.account_protection_service import (
evaluate_protection_targets,
update_protection_group,
)
from ..services.account_supply_service import (
evaluate_supply_target,
update_supply_target,
upsert_supply_target,
)
from ..services.audit_service import record_audit
from ..services.automation.scheduler import ensure_jobs
from ..services.business_events import record_recharge
from ..services.douyu_activity_query import activity_client, query_activity_goods
from ..services.douyu_batch_launcher import (
@@ -735,6 +742,53 @@ def get_account_platform_status(
return account_platform_status(db)
@router.put(
"/account-protection-targets/{group_key}",
response_model=DouyuAccountProtectionTargetOut,
)
def update_account_protection_target(
group_key: str,
req: DouyuAccountProtectionTargetUpdate,
db: Session = Depends(get_db),
current: User = Depends(require_permission("douyu:config")),
):
"""更新履约保护组的 CK 目标与巡检/重登策略。"""
ensure_jobs(db)
before_rows = evaluate_protection_targets(db)
before = next(
(row for row in before_rows if row["group_key"] == group_key),
None,
)
try:
job, groups = update_protection_group(
db,
group_key=group_key,
enabled=req.enabled,
target_count=req.target_count,
probe_interval_minutes=req.probe_interval_minutes,
auto_relogin=req.auto_relogin,
)
except KeyError:
raise HTTPException(status_code=404, detail="履约保护目标不存在") from None
except LookupError as exc:
raise HTTPException(status_code=409, detail=str(exc)) from None
after_config = job.config
record_audit(
db,
current,
action="douyu:account-protection-target:update",
target=f"automation_job:{job.key}:{group_key}",
detail={
"before": before,
"after": groups[group_key],
},
)
db.commit()
rows = evaluate_protection_targets(db, job_config=after_config)
return next(row for row in rows if row["group_key"] == group_key)
@router.get("/accounts")
def list_task_accounts(
search: str = Query(""),
+26
View File
@@ -1630,6 +1630,32 @@ class DouyuSupplyTargetOut(BaseModel):
updated_at: datetime | None = None
class DouyuAccountProtectionTargetUpdate(BaseModel):
enabled: bool = True
target_count: int = Field(0, ge=0, le=100_000)
probe_interval_minutes: int = Field(60, ge=15, le=1440)
auto_relogin: bool = True
class DouyuAccountProtectionTargetOut(BaseModel):
group_key: str
label: str
description: str
enabled: bool
target_count: int
probe_interval_minutes: int
auto_relogin: bool
bound_count: int
ck_valid: int
ck_invalid: int
ck_error: int
ck_unchecked: int
without_cookie: int
target_shortage: int
probe_due_count: int
needs_attention: bool
class PlatformFulfillmentMappingUpsert(BaseModel):
internal_sku_key: str = Field(..., min_length=1, max_length=255)
campaign_key: str = Field(default="douyu-current", min_length=1, max_length=128)
@@ -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 失效,等待自动重登",
+10
View File
@@ -1,5 +1,7 @@
import api from './client';
import type {
DouyuAccountProtectionTarget,
DouyuAccountProtectionTargetUpdate,
DouyuAccountPlatformStatus,
DouyuConfig,
DouyuExchangeResultImage,
@@ -22,6 +24,14 @@ export const douyuApi = {
taskTypes: () => api.get<Record<string, string>, Record<string, string>>('/douyu/task-types'),
getAccountPlatformStatus: () =>
api.get<DouyuAccountPlatformStatus, DouyuAccountPlatformStatus>('/douyu/account-platform-status'),
updateAccountProtectionTarget: (
groupKey: DouyuAccountProtectionTarget['group_key'],
data: DouyuAccountProtectionTargetUpdate,
) =>
api.put<DouyuAccountProtectionTarget, DouyuAccountProtectionTarget>(
`/douyu/account-protection-targets/${groupKey}`,
data,
),
listSupplyTargets: () =>
api.get<DouyuSupplyTarget[], DouyuSupplyTarget[]>('/douyu/supply-targets'),
createSupplyTarget: (data: DouyuSupplyTargetUpsert) =>
+28
View File
@@ -772,6 +772,32 @@ export interface DouyuSupplyTargetUpdate {
enabled: boolean;
}
export interface DouyuAccountProtectionTarget {
group_key: 'presale_bound' | 'collab_bound';
label: string;
description: string;
enabled: boolean;
target_count: number;
probe_interval_minutes: number;
auto_relogin: boolean;
bound_count: number;
ck_valid: number;
ck_invalid: number;
ck_error: number;
ck_unchecked: number;
without_cookie: number;
target_shortage: number;
probe_due_count: number;
needs_attention: boolean;
}
export interface DouyuAccountProtectionTargetUpdate {
enabled: boolean;
target_count: number;
probe_interval_minutes: number;
auto_relogin: boolean;
}
export interface DouyuPlatformAlert {
code: string;
severity: 'critical' | 'warning' | 'info';
@@ -781,6 +807,7 @@ export interface DouyuPlatformAlert {
value_class?: string;
shortage?: number;
count?: number;
group_key?: string;
}
export interface DouyuQueryTrendDay {
@@ -852,6 +879,7 @@ export interface DouyuAccountPlatformStatus {
fresh_available: number;
fresh_limited: number;
};
protection_targets: DouyuAccountProtectionTarget[];
supply_targets: DouyuSupplyTargetStatus[];
alerts: DouyuPlatformAlert[];
trends: DouyuPlatformTrends;
@@ -20,9 +20,15 @@ import {
Typography,
} from 'antd';
import type { TableProps } from 'antd';
import { EditOutlined, PlusOutlined, ReloadOutlined } from '@ant-design/icons';
import {
EditOutlined,
PlusOutlined,
ReloadOutlined,
SafetyCertificateOutlined,
} from '@ant-design/icons';
import {
douyuApi,
type DouyuAccountProtectionTarget,
type DouyuAccountPlatformStatus,
type DouyuSupplyTarget,
} from '../../api/modules';
@@ -54,6 +60,13 @@ interface TargetFormValues {
enabled: boolean;
}
interface ProtectionFormValues {
enabled: boolean;
target_count: number;
probe_interval_minutes: number;
auto_relogin: boolean;
}
interface TrendRow {
date: string;
queryTotal: number;
@@ -68,6 +81,7 @@ export default function AccountPlatformStatusPanel() {
const { can } = usePermissions();
const canManage = can('douyu:config');
const [form] = Form.useForm<TargetFormValues>();
const [protectionForm] = Form.useForm<ProtectionFormValues>();
const [status, setStatus] = useState<DouyuAccountPlatformStatus | null>(null);
const [targets, setTargets] = useState<DouyuSupplyTarget[]>([]);
const [loading, setLoading] = useState(false);
@@ -75,6 +89,10 @@ export default function AccountPlatformStatusPanel() {
const [modalOpen, setModalOpen] = useState(false);
const [saving, setSaving] = useState(false);
const [editing, setEditing] = useState<DouyuSupplyTarget | null>(null);
const [protectionModalOpen, setProtectionModalOpen] = useState(false);
const [protectionSaving, setProtectionSaving] = useState(false);
const [editingProtection, setEditingProtection] =
useState<DouyuAccountProtectionTarget | null>(null);
const load = useCallback(async () => {
setLoading(true);
@@ -218,6 +236,119 @@ export default function AccountPlatformStatusPanel() {
}
};
const openProtectionEdit = (target: DouyuAccountProtectionTarget) => {
setEditingProtection(target);
protectionForm.setFieldsValue({
enabled: target.enabled,
target_count: target.target_count,
probe_interval_minutes: target.probe_interval_minutes,
auto_relogin: target.auto_relogin,
});
setProtectionModalOpen(true);
};
const saveProtectionTarget = async () => {
if (!editingProtection) return;
const values = await protectionForm.validateFields();
setProtectionSaving(true);
try {
await douyuApi.updateAccountProtectionTarget(editingProtection.group_key, {
enabled: Boolean(values.enabled),
target_count: Number(values.target_count || 0),
probe_interval_minutes: Number(values.probe_interval_minutes || 60),
auto_relogin: Boolean(values.auto_relogin),
});
message.success('履约保护目标已更新');
setProtectionModalOpen(false);
await load();
} catch (saveError: unknown) {
message.error(getErrorMessage(saveError));
} finally {
setProtectionSaving(false);
}
};
const protectionColumns: TableProps<DouyuAccountProtectionTarget>['columns'] = [
{
title: '保护分类',
width: 190,
render: (_, row) => (
<Space size={6}>
<SafetyCertificateOutlined />
<Space size={0} orientation="vertical">
<Text strong>{row.label}</Text>
<Text type="secondary" style={{ fontSize: 12 }}>{row.description}</Text>
</Space>
</Space>
),
},
{
title: '当前绑定',
dataIndex: 'bound_count',
width: 90,
},
{
title: '有效 CK / 目标',
width: 130,
render: (_, row) => (
<Space size={4}>
<Text type={row.ck_invalid + row.ck_error > 0 ? 'danger' : undefined}>
{row.ck_valid}
</Text>
<Text type="secondary">/</Text>
<Text>{row.target_count || '未设'}</Text>
</Space>
),
},
{
title: '失效 / 异常',
width: 110,
render: (_, row) => {
const affected = row.ck_invalid + row.ck_error + row.without_cookie;
return affected > 0
? <Text type="danger">{affected}</Text>
: <Text type="success">0</Text>;
},
},
{
title: '巡检间隔',
width: 100,
render: (_, row) => `${row.probe_interval_minutes} 分钟`,
},
{
title: '自动重登',
width: 90,
render: (_, row) => (
row.auto_relogin
? <Tag color="blue">优先</Tag>
: <Tag>不处理</Tag>
),
},
{
title: '状态',
width: 100,
render: (_, row) => {
if (!row.enabled) return <Tag>已停用</Tag>;
if (row.needs_attention) return <Tag color="warning">需关注</Tag>;
return <Tag color="success">正常</Tag>;
},
},
...(canManage ? [{
title: '操作',
width: 110,
render: (_: unknown, row: DouyuAccountProtectionTarget) => (
<Button
type="link"
size="small"
icon={<EditOutlined />}
onClick={() => openProtectionEdit(row)}
>
设置目标
</Button>
),
}] : []),
];
const columns: TableProps<DouyuSupplyTarget>['columns'] = [
{
title: '分类',
@@ -368,10 +499,35 @@ export default function AccountPlatformStatusPanel() {
</Space>
) : null}
<Space orientation="vertical" size={2} style={{ width: '100%', marginTop: 20 }}>
<Text strong>履约保护目标</Text>
<Text type="secondary">
保持预售与外部协作单绑定账号的 CK 有效,失效后优先进入自动重登队列
</Text>
</Space>
<Table
rowKey="group_key"
size="small"
style={{ marginTop: 8 }}
columns={protectionColumns}
dataSource={status?.protection_targets || []}
pagination={false}
scroll={{ x: 950 }}
locale={{
emptyText: (
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无履约保护目标" />
),
}}
/>
<Space orientation="vertical" size={2} style={{ width: '100%', marginTop: 20 }}>
<Text strong>账号供给目标</Text>
<Text type="secondary">按高价值、普通账号和商品维度维护可兑换与可分配库存</Text>
</Space>
<Table
rowKey="target_id"
size="small"
style={{ marginTop: 16 }}
style={{ marginTop: 8 }}
columns={columns}
dataSource={targets}
pagination={false}
@@ -464,6 +620,63 @@ export default function AccountPlatformStatusPanel() {
</Form.Item>
</Form>
</Modal>
<Modal
title={editingProtection ? `设置${editingProtection.label}目标` : '设置履约保护目标'}
open={protectionModalOpen}
confirmLoading={protectionSaving}
onOk={() => void saveProtectionTarget()}
onCancel={() => setProtectionModalOpen(false)}
okText="保存"
cancelText="取消"
destroyOnHidden
>
<Form form={protectionForm} layout="vertical">
<Alert
type="info"
showIcon
message={editingProtection?.description}
style={{ marginBottom: 16 }}
/>
<Row gutter={12}>
<Col span={12}>
<Form.Item
name="target_count"
label="目标有效 CK"
tooltip="用于缺口预警;组内当前全部绑定账号都会纳入保护巡检"
>
<InputNumber min={0} precision={0} style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item
name="probe_interval_minutes"
label="保护巡检间隔"
tooltip="启用后,组内账号至少按此间隔检测一次 CK"
>
<InputNumber
min={15}
max={1440}
precision={0}
addonAfter="分钟"
style={{ width: '100%' }}
/>
</Form.Item>
</Col>
</Row>
<Form.Item name="enabled" label="启用保护" valuePropName="checked">
<Switch />
</Form.Item>
<Form.Item
name="auto_relogin"
label="失效后自动重登"
valuePropName="checked"
tooltip="仍受自动化页面中的 CK 自动重登总开关控制"
>
<Switch />
</Form.Item>
</Form>
</Modal>
</>
);
}