Files
live-hub-py/web/backend/services/account_protection_service.py
T

380 lines
12 KiB
Python

"""斗鱼履约账号 CK 保护目标。
保护目标不是新的账号价值分类,也不固定挑选若干账号。成员始终由当前履约
关系动态计算:
- ``presale_bound``:仍在履约中的预售任务所分配的账号;
- ``collab_bound``:仍在进行中的斗鱼外部协作单所绑定的账号。
配置保存在 ``ck_health.config["protection_groups"]``。这样不需要扩容账号表,
也不会污染高价值/普通的库存供给口径。目标数量用于预警缺口;只要组已启用,
当前全部绑定账号都会获得更短的 CK 巡检间隔。
"""
from __future__ import annotations
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import and_, case, func, or_
from sqlalchemy.orm import Session
from ..models import (
Account,
AutomationJob,
DouyuExternalOrder,
PlatformFulfillmentTask,
)
from .account_query_filters import ACTIVE_COLLAB_STATUSES
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
COLLAB_ACTIVE_STATUSES = ACTIVE_COLLAB_STATUSES
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)
rows: list[dict[str, Any]] = []
for key, group in snapshot.groups.items():
account_ids = snapshot.group_account_ids.get(key, frozenset())
probe_cutoff = moment - timedelta(minutes=int(group["probe_interval_minutes"]))
if account_ids:
# 只做数据库条件聚合,不加载 Account ORM。完整账号行包含加密密码、
# 邮箱/Cookie 和大型 JSON 快照,2174 行实测可占 2 秒以上。
totals = (
db.query(
func.count(Account.id),
func.coalesce(
func.sum(
case((Account.ck_check_status == "valid", 1), else_=0)
),
0,
),
func.coalesce(
func.sum(
case((Account.ck_check_status == "invalid", 1), else_=0)
),
0,
),
func.coalesce(
func.sum(
case((Account.ck_check_status == "error", 1), else_=0)
),
0,
),
func.coalesce(
func.sum(
case(
(
func.coalesce(Account.ck_check_status, "").notin_(
("valid", "invalid", "error")
),
1,
),
else_=0,
)
),
0,
),
func.coalesce(
func.sum(
case(
(
or_(
Account.cookie.is_(None),
func.length(Account.cookie) == 0,
),
1,
),
else_=0,
)
),
0,
),
func.coalesce(
func.sum(
case(
(
or_(
Account.ck_checked_at.is_(None),
and_(
Account.ck_checked_at.isnot(None),
Account.ck_checked_at < probe_cutoff,
),
),
1,
),
else_=0,
)
),
0,
),
)
.filter(Account.id.in_(account_ids))
.one()
)
(
bound_count,
ck_valid,
ck_invalid,
ck_error,
ck_unchecked,
without_cookie,
probe_due_count,
) = (int(value or 0) for value in totals)
else:
bound_count = ck_valid = ck_invalid = ck_error = 0
ck_unchecked = without_cookie = probe_due_count = 0
status_counts = {
"ck_valid": ck_valid,
"ck_invalid": ck_invalid,
"ck_error": ck_error,
"ck_unchecked": ck_unchecked,
"without_cookie": without_cookie,
}
target = int(group["target_count"])
shortage = max(target - status_counts["ck_valid"], 0)
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": bound_count,
**status_counts,
"target_shortage": shortage,
"probe_due_count": probe_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",
]