perf(account-check): 优化账号检测首屏加载

This commit is contained in:
yml
2026-11-22 17:05:10 +08:00
parent 7233bdfa50
commit 22e106bf9b
9 changed files with 421 additions and 150 deletions
@@ -14,9 +14,10 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import UTC, datetime
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 (
@@ -97,9 +98,7 @@ def normalize_group_config(
MIN_PROBE_INTERVAL_MINUTES,
MAX_PROBE_INTERVAL_MINUTES,
),
"auto_relogin": bool(
value.get("auto_relogin", definition["auto_relogin"])
),
"auto_relogin": bool(value.get("auto_relogin", definition["auto_relogin"])),
}
@@ -124,9 +123,7 @@ def _presale_account_ids(db: Session) -> set[int]:
.filter(
PlatformFulfillmentTask.sale_mode == "presale",
PlatformFulfillmentTask.account_id.isnot(None),
PlatformFulfillmentTask.execution_status.notin_(
("completed", "cancelled")
),
PlatformFulfillmentTask.execution_status.notin_(("completed", "cancelled")),
)
.distinct()
.all()
@@ -246,59 +243,109 @@ def evaluate_protection_targets(
) -> 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
]
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": 0,
"ck_invalid": 0,
"ck_error": 0,
"ck_unchecked": 0,
"without_cookie": 0,
"ck_valid": ck_valid,
"ck_invalid": ck_invalid,
"ck_error": ck_error,
"ck_unchecked": ck_unchecked,
"without_cookie": without_cookie,
}
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 (
@@ -317,10 +364,10 @@ def evaluate_protection_targets(
"target_count": target,
"probe_interval_minutes": int(group["probe_interval_minutes"]),
"auto_relogin": bool(group["auto_relogin"]),
"bound_count": len(group_accounts),
"bound_count": bound_count,
**status_counts,
"target_shortage": shortage,
"probe_due_count": due_count,
"probe_due_count": probe_due_count,
"needs_attention": needs_attention,
}
)