优化(账号分配): 增加换绑与CK分类及测试操作
This commit is contained in:
@@ -2,7 +2,7 @@ import csv
|
||||
import io
|
||||
import threading
|
||||
import time
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
@@ -61,6 +61,7 @@ from ..services.account_events_service import (
|
||||
emit_bulk,
|
||||
)
|
||||
from ..services.account_lifecycle import (
|
||||
DEFAULT_CK_LIFETIME,
|
||||
STATUS_ACTIVE,
|
||||
STATUS_ARCHIVED,
|
||||
STATUS_DISABLED,
|
||||
@@ -91,9 +92,84 @@ from ..services.login_failure import classify_failure, failure_kind_label
|
||||
router = APIRouter(prefix="/api/accounts", tags=["账号管理"])
|
||||
|
||||
_ASSIGNMENT_SKIN_CACHE_SECONDS = 60.0
|
||||
_CK_EXPIRING_HOURS = 24
|
||||
_assignment_skin_cache_lock = threading.Lock()
|
||||
_assignment_skin_cache: tuple[float, int, list[dict[str, Any]]] | None = None
|
||||
|
||||
CK_EXPIRY_LABELS = {
|
||||
"valid": "有效",
|
||||
"expiring": "24 小时内到期",
|
||||
"expired": "已过期",
|
||||
"invalid": "已失效",
|
||||
"error": "检测异常",
|
||||
"untested": "未检测",
|
||||
"missing": "无 CK",
|
||||
"unknown": "未预测到期",
|
||||
}
|
||||
BIND_CHANGE_LABELS = {
|
||||
"available": "可换绑",
|
||||
"cooldown": "换绑冷却",
|
||||
"unbound": "未绑定",
|
||||
"pending": "待确认",
|
||||
"unknown": "未知",
|
||||
}
|
||||
|
||||
|
||||
def _as_utc(value: datetime | None) -> datetime | None:
|
||||
if value is None:
|
||||
return None
|
||||
return value.replace(tzinfo=UTC) if value.tzinfo is None else value.astimezone(UTC)
|
||||
|
||||
|
||||
def _account_ck_expiry(
|
||||
account: Account,
|
||||
*,
|
||||
now: datetime,
|
||||
) -> tuple[str, datetime | None, int | None]:
|
||||
"""账号列表的 CK 到期分类;未明确检测过的 CK 不和有效 CK 混在一起。"""
|
||||
if not account.cookie:
|
||||
return "missing", None, None
|
||||
status = str(account.ck_check_status or "")
|
||||
if status in {"invalid", "error"}:
|
||||
return status, _as_utc(account.ck_expected_expires_at), None
|
||||
if status != "valid":
|
||||
return "untested", _as_utc(account.ck_expected_expires_at), None
|
||||
expires_at = _as_utc(account.ck_expected_expires_at)
|
||||
if expires_at is None:
|
||||
# 有有效检测但从未形成到期预测时,只给“有效/未预测”,不能猜成过期。
|
||||
fallback_at = _as_utc(
|
||||
account.cookie_updated_at or account.ck_checked_at
|
||||
)
|
||||
if fallback_at is None:
|
||||
return "unknown", None, None
|
||||
expires_at = fallback_at + DEFAULT_CK_LIFETIME
|
||||
remaining_seconds = (expires_at - now).total_seconds()
|
||||
remaining_hours = max(0, int(remaining_seconds // 3600))
|
||||
if remaining_seconds <= 0:
|
||||
return "expired", expires_at, 0
|
||||
if remaining_seconds <= _CK_EXPIRING_HOURS * 3600:
|
||||
return "expiring", expires_at, remaining_hours
|
||||
return "valid", expires_at, remaining_hours
|
||||
|
||||
|
||||
def _account_bind_change(
|
||||
account: Account,
|
||||
*,
|
||||
now: datetime,
|
||||
) -> tuple[bool, str, datetime | None]:
|
||||
"""能否换绑:只看精英角色,并沿用账号上已合并本地 7 天锁的可用时间。"""
|
||||
role_status = str(account.role_bind_status or "")
|
||||
if role_status == "unbound":
|
||||
return False, "unbound", None
|
||||
if role_status == "pending":
|
||||
return False, "pending", _as_utc(account.role_change_available_at)
|
||||
if role_status != "bound":
|
||||
return False, "unknown", _as_utc(account.role_change_available_at)
|
||||
available_at = _as_utc(account.role_change_available_at)
|
||||
if available_at is not None and available_at > now:
|
||||
return False, "cooldown", available_at
|
||||
return True, "available", available_at
|
||||
|
||||
|
||||
def _visible_accounts_query(db: Session, current: User):
|
||||
"""返回当前用户可见的斗鱼账号查询。"""
|
||||
@@ -250,6 +326,8 @@ def _filter_accounts_query(
|
||||
account_names: str = "",
|
||||
status: str = "",
|
||||
ck_status: str = "",
|
||||
ck_expiry_status: str = "",
|
||||
bind_change_status: str = "",
|
||||
failure_kind: str = "",
|
||||
bind_status: str = "",
|
||||
high_value_filter: str = "",
|
||||
@@ -301,6 +379,71 @@ def _filter_accounts_query(
|
||||
ck_value = _query_str(ck_status)
|
||||
if ck_value:
|
||||
query = query.filter(Account.ck_check_status == ck_value)
|
||||
ck_expiry_value = _query_str(ck_expiry_status)
|
||||
if ck_expiry_value:
|
||||
now = datetime.now(UTC)
|
||||
expiring_cutoff = now + timedelta(hours=_CK_EXPIRING_HOURS)
|
||||
if ck_expiry_value == "missing":
|
||||
query = query.filter(or_(Account.cookie.is_(None), Account.cookie == ""))
|
||||
elif ck_expiry_value in {"invalid", "error"}:
|
||||
query = query.filter(Account.ck_check_status == ck_expiry_value)
|
||||
elif ck_expiry_value == "untested":
|
||||
query = query.filter(
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
Account.ck_check_status.notin_(["valid", "invalid", "error"]),
|
||||
)
|
||||
elif ck_expiry_value in {
|
||||
"not_expired",
|
||||
"valid",
|
||||
"expiring",
|
||||
"expired",
|
||||
"unknown",
|
||||
}:
|
||||
query = query.filter(Account.ck_check_status == "valid")
|
||||
if ck_expiry_value == "not_expired":
|
||||
query = query.filter(
|
||||
or_(
|
||||
Account.ck_expected_expires_at.is_(None),
|
||||
Account.ck_expected_expires_at > now,
|
||||
)
|
||||
)
|
||||
elif ck_expiry_value == "unknown":
|
||||
query = query.filter(Account.ck_expected_expires_at.is_(None))
|
||||
elif ck_expiry_value == "expired":
|
||||
query = query.filter(
|
||||
Account.ck_expected_expires_at.isnot(None),
|
||||
Account.ck_expected_expires_at <= now,
|
||||
)
|
||||
elif ck_expiry_value == "expiring":
|
||||
query = query.filter(
|
||||
Account.ck_expected_expires_at > now,
|
||||
Account.ck_expected_expires_at <= expiring_cutoff,
|
||||
)
|
||||
else:
|
||||
query = query.filter(
|
||||
Account.ck_expected_expires_at > expiring_cutoff
|
||||
)
|
||||
bind_change_value = _query_str(bind_change_status)
|
||||
if bind_change_value:
|
||||
now = datetime.now(UTC)
|
||||
if bind_change_value == "available":
|
||||
query = query.filter(
|
||||
Account.role_bind_status == "bound",
|
||||
or_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
Account.role_change_available_at <= now,
|
||||
),
|
||||
)
|
||||
elif bind_change_value == "cooldown":
|
||||
query = query.filter(
|
||||
Account.role_bind_status == "bound",
|
||||
Account.role_change_available_at > now,
|
||||
)
|
||||
elif bind_change_value == "unbound":
|
||||
query = query.filter(Account.role_bind_status == "unbound")
|
||||
elif bind_change_value == "pending":
|
||||
query = query.filter(Account.role_bind_status == "pending")
|
||||
fk_value = _query_str(failure_kind)
|
||||
if fk_value:
|
||||
query = query.filter(Account.relogin_failure_kind == fk_value)
|
||||
@@ -439,6 +582,8 @@ def _selection_query(db: Session, current: User, req: AccountBulkSelection):
|
||||
account_names=req.account_names,
|
||||
status=req.status,
|
||||
ck_status=req.ck_status,
|
||||
ck_expiry_status=req.ck_expiry_status,
|
||||
bind_change_status=req.bind_change_status,
|
||||
failure_kind=req.failure_kind,
|
||||
bind_status=req.bind_status,
|
||||
high_value_filter=req.high_value_filter,
|
||||
@@ -612,6 +757,17 @@ def list_accounts(
|
||||
ck_status: str = Query(
|
||||
"", description="按 CK 健康结论筛选:valid / invalid / error"
|
||||
),
|
||||
ck_expiry_status: str = Query(
|
||||
"",
|
||||
description=(
|
||||
"按 CK 到期分类筛选:valid/expiring/expired/invalid/error/"
|
||||
"untested/missing/unknown/not_expired"
|
||||
),
|
||||
),
|
||||
bind_change_status: str = Query(
|
||||
"",
|
||||
description="按换绑分类筛选:available/cooldown/unbound/pending",
|
||||
),
|
||||
failure_kind: str = Query(
|
||||
"",
|
||||
description="按最近登录失败分型筛选:must_bind_mobile/captcha/credential/email_code/email_quota/network",
|
||||
@@ -664,6 +820,8 @@ def list_accounts(
|
||||
account_names=account_names,
|
||||
status=status,
|
||||
ck_status=ck_status,
|
||||
ck_expiry_status=ck_expiry_status,
|
||||
bind_change_status=bind_change_status,
|
||||
failure_kind=failure_kind,
|
||||
bind_status=bind_status,
|
||||
high_value_filter=high_value_filter,
|
||||
@@ -828,6 +986,7 @@ def list_accounts(
|
||||
if ids
|
||||
else {}
|
||||
)
|
||||
output_now = datetime.now(UTC)
|
||||
result = []
|
||||
for acc in accounts:
|
||||
eff_kind = acc.relogin_failure_kind or ""
|
||||
@@ -835,6 +994,13 @@ def list_accounts(
|
||||
refined = classify_failure(acc.relogin_last_error)
|
||||
if not eff_kind or (eff_kind == "network" and refined != "network"):
|
||||
eff_kind = refined
|
||||
ck_expiry_status, ck_expires_at, ck_remaining_hours = _account_ck_expiry(
|
||||
acc,
|
||||
now=output_now,
|
||||
)
|
||||
can_change_bind, bind_change_status, bind_change_available_at = (
|
||||
_account_bind_change(acc, now=output_now)
|
||||
)
|
||||
|
||||
item = AccountOut(
|
||||
id=acc.id,
|
||||
@@ -855,6 +1021,13 @@ def list_accounts(
|
||||
has_cookie=bool(acc.cookie),
|
||||
ck_check_status=acc.ck_check_status or "",
|
||||
ck_checked_at=acc.ck_checked_at,
|
||||
ck_expires_at=ck_expires_at,
|
||||
ck_expiry_status=ck_expiry_status,
|
||||
ck_expiry_label=CK_EXPIRY_LABELS.get(
|
||||
ck_expiry_status,
|
||||
ck_expiry_status,
|
||||
),
|
||||
ck_remaining_hours=ck_remaining_hours,
|
||||
points=acc.points,
|
||||
gold_balance=acc.gold_balance,
|
||||
elite_opened=acc.elite_opened,
|
||||
@@ -863,6 +1036,13 @@ def list_accounts(
|
||||
last_exchange_at=exchange_stats.get(acc.id, (0, None, ""))[1],
|
||||
last_exchange_name=exchange_stats.get(acc.id, (0, None, ""))[2],
|
||||
**bind_payload(acc),
|
||||
can_change_bind=can_change_bind,
|
||||
bind_change_status=bind_change_status,
|
||||
bind_change_label=BIND_CHANGE_LABELS.get(
|
||||
bind_change_status,
|
||||
bind_change_status,
|
||||
),
|
||||
bind_change_available_at=bind_change_available_at,
|
||||
# 只返回官方 storehome 的紧凑三态,不把整份快照带进列表。
|
||||
high_value=high_value_payload(acc),
|
||||
overview_queried_at=acc.overview_queried_at,
|
||||
@@ -875,9 +1055,6 @@ def list_accounts(
|
||||
redundancy_class=(
|
||||
profile_map[acc.id].redundancy_class if acc.id in profile_map else ""
|
||||
),
|
||||
ck_remaining_hours=(
|
||||
profile_map[acc.id].ck_remaining_hours if acc.id in profile_map else None
|
||||
),
|
||||
active_lease_purpose=(
|
||||
str(lease_map[acc.id].purpose or "") if acc.id in lease_map else ""
|
||||
),
|
||||
@@ -915,6 +1092,8 @@ def list_account_ids(
|
||||
),
|
||||
status: str = Query(""),
|
||||
ck_status: str = Query(""),
|
||||
ck_expiry_status: str = Query(""),
|
||||
bind_change_status: str = Query(""),
|
||||
failure_kind: str = Query(""),
|
||||
bind_status: str = Query(""),
|
||||
high_value_filter: str = Query(""),
|
||||
@@ -949,6 +1128,8 @@ def list_account_ids(
|
||||
account_names=account_names,
|
||||
status=status,
|
||||
ck_status=ck_status,
|
||||
ck_expiry_status=ck_expiry_status,
|
||||
bind_change_status=bind_change_status,
|
||||
failure_kind=failure_kind,
|
||||
bind_status=bind_status,
|
||||
high_value_filter=high_value_filter,
|
||||
|
||||
+20
-2
@@ -217,6 +217,10 @@ class AccountBulkSelection(BaseModel):
|
||||
has_cookie: bool = False
|
||||
status: str = ""
|
||||
ck_status: str = ""
|
||||
#: CK 到期分类:valid/expiring/expired/invalid/error/untested/missing/unknown
|
||||
ck_expiry_status: str = ""
|
||||
#: 换绑分类:available/cooldown/unbound/pending/unknown
|
||||
bind_change_status: str = ""
|
||||
failure_kind: str = ""
|
||||
bind_status: str = ""
|
||||
high_value_filter: str = ""
|
||||
@@ -322,12 +326,20 @@ class AccountOut(BaseModel):
|
||||
has_cookie: bool = False
|
||||
ck_check_status: str = ""
|
||||
ck_checked_at: datetime | None = None
|
||||
ck_expires_at: datetime | None = None
|
||||
ck_expiry_status: str = ""
|
||||
ck_expiry_label: str = ""
|
||||
ck_remaining_hours: int | None = None
|
||||
points: int | None = None
|
||||
gold_balance: int | None = None
|
||||
elite_opened: bool | None = None
|
||||
esports_opened: bool | None = None
|
||||
role_bind_status: str = "unknown"
|
||||
role_bind_label: str = ""
|
||||
can_change_bind: bool = False
|
||||
bind_change_status: str = ""
|
||||
bind_change_label: str = ""
|
||||
bind_change_available_at: datetime | None = None
|
||||
esports_bind_status: str = "unknown"
|
||||
esports_bind_label: str = ""
|
||||
xpd_bind_status: str = "unknown"
|
||||
@@ -343,7 +355,6 @@ class AccountOut(BaseModel):
|
||||
supply_role: str = ""
|
||||
quality_score: int | None = None
|
||||
redundancy_class: str = ""
|
||||
ck_remaining_hours: int | None = None
|
||||
active_lease_purpose: str = ""
|
||||
active_lease_expires_at: datetime | None = None
|
||||
in_active_collab: bool = False
|
||||
@@ -373,12 +384,20 @@ class AccountOut(BaseModel):
|
||||
"has_cookie": self.has_cookie,
|
||||
"ck_check_status": self.ck_check_status,
|
||||
"ck_checked_at": _isoformat(self.ck_checked_at),
|
||||
"ck_expires_at": _isoformat(self.ck_expires_at),
|
||||
"ck_expiry_status": self.ck_expiry_status,
|
||||
"ck_expiry_label": self.ck_expiry_label,
|
||||
"ck_remaining_hours": self.ck_remaining_hours,
|
||||
"points": self.points,
|
||||
"gold_balance": self.gold_balance,
|
||||
"elite_opened": self.elite_opened,
|
||||
"esports_opened": self.esports_opened,
|
||||
"role_bind_status": self.role_bind_status,
|
||||
"role_bind_label": self.role_bind_label,
|
||||
"can_change_bind": self.can_change_bind,
|
||||
"bind_change_status": self.bind_change_status,
|
||||
"bind_change_label": self.bind_change_label,
|
||||
"bind_change_available_at": _isoformat(self.bind_change_available_at),
|
||||
"esports_bind_status": self.esports_bind_status,
|
||||
"esports_bind_label": self.esports_bind_label,
|
||||
"xpd_bind_status": self.xpd_bind_status,
|
||||
@@ -394,7 +413,6 @@ class AccountOut(BaseModel):
|
||||
"supply_role": self.supply_role,
|
||||
"quality_score": self.quality_score,
|
||||
"redundancy_class": self.redundancy_class,
|
||||
"ck_remaining_hours": self.ck_remaining_hours,
|
||||
"active_lease_purpose": self.active_lease_purpose,
|
||||
"active_lease_expires_at": _isoformat(self.active_lease_expires_at),
|
||||
"in_active_collab": self.in_active_collab,
|
||||
|
||||
Reference in New Issue
Block a user