feat(douyu): 落地账号平台同步与登录配额
This commit is contained in:
@@ -0,0 +1,341 @@
|
||||
"""登录尝试流水和数据库级登录预算。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, date, datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import (
|
||||
Account,
|
||||
LoginAttempt,
|
||||
LoginQuotaSetting,
|
||||
LoginQuotaUsage,
|
||||
LoginTask,
|
||||
)
|
||||
from .account_value import is_high_value_account
|
||||
from .douyu_service import LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
|
||||
CATEGORY_ALL = "all"
|
||||
CATEGORY_MANUAL = "manual_login"
|
||||
CATEGORY_ONBOARDING = "onboarding"
|
||||
CATEGORY_RELOGIN_HIGH = "relogin_high_value"
|
||||
CATEGORY_RELOGIN_ORDINARY = "relogin_ordinary"
|
||||
CATEGORY_ACCOUNT_CHECK = "account_check"
|
||||
|
||||
BUSINESS_TZ = ZoneInfo("Asia/Shanghai")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class QuotaDecision:
|
||||
allowed: bool
|
||||
reason: str = ""
|
||||
attempt: LoginAttempt | None = None
|
||||
|
||||
|
||||
def _bucket(now: datetime | None = None) -> tuple[date, int]:
|
||||
moment = now or datetime.now(UTC)
|
||||
aware = moment if moment.tzinfo else moment.replace(tzinfo=UTC)
|
||||
local = aware.astimezone(BUSINESS_TZ)
|
||||
return local.date(), local.hour
|
||||
|
||||
|
||||
def classify_login_category(
|
||||
account: Account,
|
||||
*,
|
||||
mode: str,
|
||||
trigger_source: str = "manual",
|
||||
now: datetime | None = None,
|
||||
) -> str:
|
||||
if mode == "check":
|
||||
return CATEGORY_ACCOUNT_CHECK
|
||||
if mode == "relogin":
|
||||
return (
|
||||
CATEGORY_RELOGIN_HIGH
|
||||
if is_high_value_account(account, now=now)
|
||||
else CATEGORY_RELOGIN_ORDINARY
|
||||
)
|
||||
if (
|
||||
trigger_source == "automation"
|
||||
or account.status == "imported"
|
||||
or not account.cookie
|
||||
or account.cookie_updated_at is None
|
||||
):
|
||||
return CATEGORY_ONBOARDING
|
||||
return CATEGORY_MANUAL
|
||||
|
||||
|
||||
def _setting(db: Session) -> LoginQuotaSetting:
|
||||
row = (
|
||||
db.query(LoginQuotaSetting)
|
||||
.filter(LoginQuotaSetting.id == 1)
|
||||
.with_for_update()
|
||||
.first()
|
||||
)
|
||||
if row is None:
|
||||
row = LoginQuotaSetting(id=1)
|
||||
db.add(row)
|
||||
db.flush()
|
||||
return row
|
||||
|
||||
|
||||
def login_concurrency_limit(db: Session) -> int:
|
||||
"""读取数据库配置的登录并发上限;未初始化时使用保守默认值。"""
|
||||
setting = db.query(LoginQuotaSetting).filter(LoginQuotaSetting.id == 1).first()
|
||||
if setting is None or not setting.enabled:
|
||||
return 3
|
||||
return max(1, min(int(setting.login_concurrency or 3), 10))
|
||||
|
||||
|
||||
def _usage_row(
|
||||
db: Session,
|
||||
*,
|
||||
quota_day: date,
|
||||
quota_hour: int,
|
||||
category: str,
|
||||
) -> LoginQuotaUsage:
|
||||
row = (
|
||||
db.query(LoginQuotaUsage)
|
||||
.filter(
|
||||
LoginQuotaUsage.quota_day == quota_day,
|
||||
LoginQuotaUsage.quota_hour == quota_hour,
|
||||
LoginQuotaUsage.category == category,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if row is None:
|
||||
row = LoginQuotaUsage(
|
||||
quota_day=quota_day,
|
||||
quota_hour=quota_hour,
|
||||
category=category,
|
||||
)
|
||||
db.add(row)
|
||||
db.flush()
|
||||
return row
|
||||
|
||||
|
||||
def _daily_total(db: Session, quota_day: date) -> int:
|
||||
rows = (
|
||||
db.query(LoginQuotaUsage.attempt_count)
|
||||
.filter(
|
||||
LoginQuotaUsage.quota_day == quota_day,
|
||||
LoginQuotaUsage.category == CATEGORY_ALL,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return sum(int(value or 0) for (value,) in rows)
|
||||
|
||||
|
||||
def _hourly_total(db: Session, quota_day: date, quota_hour: int) -> int:
|
||||
value = (
|
||||
db.query(LoginQuotaUsage.attempt_count)
|
||||
.filter(
|
||||
LoginQuotaUsage.quota_day == quota_day,
|
||||
LoginQuotaUsage.quota_hour == quota_hour,
|
||||
LoginQuotaUsage.category == CATEGORY_ALL,
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
return int(value or 0)
|
||||
|
||||
|
||||
def _category_daily_total(db: Session, quota_day: date, category: str) -> int:
|
||||
rows = (
|
||||
db.query(LoginQuotaUsage.attempt_count)
|
||||
.filter(
|
||||
LoginQuotaUsage.quota_day == quota_day,
|
||||
LoginQuotaUsage.category == category,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return sum(int(value or 0) for (value,) in rows)
|
||||
|
||||
|
||||
def _category_limit(setting: LoginQuotaSetting, category: str) -> int:
|
||||
return {
|
||||
CATEGORY_ONBOARDING: int(setting.onboarding_daily_limit),
|
||||
CATEGORY_RELOGIN_HIGH: int(setting.relogin_high_value_daily_limit),
|
||||
CATEGORY_RELOGIN_ORDINARY: int(setting.relogin_ordinary_daily_limit),
|
||||
CATEGORY_MANUAL: int(setting.manual_daily_limit),
|
||||
CATEGORY_ACCOUNT_CHECK: int(setting.manual_daily_limit),
|
||||
}.get(category, int(setting.daily_attempt_limit))
|
||||
|
||||
|
||||
def reserve_login_attempt(
|
||||
db: Session,
|
||||
*,
|
||||
account: Account,
|
||||
mode: str,
|
||||
trigger_source: str,
|
||||
batch_id: str,
|
||||
created_by: int | None,
|
||||
now: datetime | None = None,
|
||||
) -> QuotaDecision:
|
||||
moment = now or datetime.now(UTC)
|
||||
quota_day, quota_hour = _bucket(moment)
|
||||
setting = _setting(db)
|
||||
if not setting.enabled:
|
||||
category = classify_login_category(
|
||||
account,
|
||||
mode=mode,
|
||||
trigger_source=trigger_source,
|
||||
now=moment,
|
||||
)
|
||||
attempt = LoginAttempt(
|
||||
account_id=int(account.id),
|
||||
batch_id=batch_id[:64],
|
||||
category=category,
|
||||
trigger_source=trigger_source[:16],
|
||||
created_by=created_by,
|
||||
started_at=moment,
|
||||
status="pending",
|
||||
quota_day=quota_day,
|
||||
)
|
||||
db.add(attempt)
|
||||
db.flush()
|
||||
return QuotaDecision(True, attempt=attempt)
|
||||
|
||||
in_flight = (
|
||||
db.query(LoginTask.id)
|
||||
.filter(
|
||||
LoginTask.account_id == account.id,
|
||||
LoginTask.attempt_status.in_(LOGIN_ATTEMPT_IN_FLIGHT_STATUSES),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
in_flight_attempt = (
|
||||
db.query(LoginAttempt.id)
|
||||
.filter(
|
||||
LoginAttempt.account_id == account.id,
|
||||
LoginAttempt.status.in_(("pending", "running")),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if in_flight is not None or in_flight_attempt is not None:
|
||||
return QuotaDecision(False, reason="该账号已有登录任务在执行")
|
||||
|
||||
account_day_count = (
|
||||
db.query(LoginAttempt.id)
|
||||
.filter(
|
||||
LoginAttempt.account_id == account.id,
|
||||
LoginAttempt.quota_day == quota_day,
|
||||
)
|
||||
.count()
|
||||
)
|
||||
if account_day_count >= int(setting.per_account_daily_limit):
|
||||
return QuotaDecision(False, reason="该账号已达到今日登录尝试上限")
|
||||
|
||||
if _daily_total(db, quota_day) >= int(setting.daily_attempt_limit):
|
||||
return QuotaDecision(False, reason="全局每日登录尝试预算已用完")
|
||||
if _hourly_total(db, quota_day, quota_hour) >= int(
|
||||
setting.hourly_attempt_limit
|
||||
):
|
||||
return QuotaDecision(False, reason="全局每小时登录尝试预算已用完")
|
||||
|
||||
category = classify_login_category(
|
||||
account,
|
||||
mode=mode,
|
||||
trigger_source=trigger_source,
|
||||
now=moment,
|
||||
)
|
||||
if _category_daily_total(db, quota_day, category) >= _category_limit(
|
||||
setting, category
|
||||
):
|
||||
return QuotaDecision(False, reason=f"{category} 今日登录预算已用完")
|
||||
|
||||
attempt = LoginAttempt(
|
||||
account_id=int(account.id),
|
||||
batch_id=batch_id[:64],
|
||||
category=category,
|
||||
trigger_source=trigger_source[:16],
|
||||
created_by=created_by,
|
||||
started_at=moment,
|
||||
status="pending",
|
||||
quota_day=quota_day,
|
||||
)
|
||||
db.add(attempt)
|
||||
for usage_category in (CATEGORY_ALL, category):
|
||||
usage = _usage_row(
|
||||
db,
|
||||
quota_day=quota_day,
|
||||
quota_hour=quota_hour,
|
||||
category=usage_category,
|
||||
)
|
||||
usage.attempt_count = int(usage.attempt_count or 0) + 1
|
||||
db.flush()
|
||||
return QuotaDecision(True, attempt=attempt)
|
||||
|
||||
|
||||
def finish_login_attempt(
|
||||
db: Session,
|
||||
attempt: LoginAttempt,
|
||||
*,
|
||||
status: str,
|
||||
failure_kind: str = "",
|
||||
now: datetime | None = None,
|
||||
) -> None:
|
||||
if attempt.status in {"success", "failed", "error", "stopped"}:
|
||||
return
|
||||
moment = now or datetime.now(UTC)
|
||||
attempt.status = status[:16]
|
||||
attempt.failure_kind = failure_kind[:16]
|
||||
attempt.finished_at = moment
|
||||
attempt.duration_ms = max(
|
||||
0,
|
||||
int((moment - attempt.started_at).total_seconds() * 1000),
|
||||
)
|
||||
quota_day, quota_hour = _bucket(attempt.started_at)
|
||||
for usage_category in (CATEGORY_ALL, attempt.category):
|
||||
usage = _usage_row(
|
||||
db,
|
||||
quota_day=quota_day,
|
||||
quota_hour=quota_hour,
|
||||
category=usage_category,
|
||||
)
|
||||
if status == "success":
|
||||
usage.success_count = int(usage.success_count or 0) + 1
|
||||
elif status in {"failed", "error"}:
|
||||
usage.failure_count = int(usage.failure_count or 0) + 1
|
||||
db.flush()
|
||||
|
||||
|
||||
def cleanup_orphan_login_attempts(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> int:
|
||||
"""服务重启后把遗留的尝试流水收敛为 interrupted。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
attempts = (
|
||||
db.query(LoginAttempt)
|
||||
.filter(LoginAttempt.status.in_(("pending", "running")))
|
||||
.all()
|
||||
)
|
||||
for attempt in attempts:
|
||||
finish_login_attempt(
|
||||
db,
|
||||
attempt,
|
||||
status="failed",
|
||||
failure_kind="interrupted",
|
||||
now=moment,
|
||||
)
|
||||
if attempts:
|
||||
db.commit()
|
||||
return len(attempts)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"CATEGORY_ACCOUNT_CHECK",
|
||||
"CATEGORY_MANUAL",
|
||||
"CATEGORY_ONBOARDING",
|
||||
"CATEGORY_RELOGIN_HIGH",
|
||||
"CATEGORY_RELOGIN_ORDINARY",
|
||||
"QuotaDecision",
|
||||
"classify_login_category",
|
||||
"cleanup_orphan_login_attempts",
|
||||
"finish_login_attempt",
|
||||
"login_concurrency_limit",
|
||||
"reserve_login_attempt",
|
||||
]
|
||||
Reference in New Issue
Block a user