feat(douyu): 接入重登工作队列与平台状态观测
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
"""斗鱼账号平台运行状态与覆盖率快照。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import case, func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountSyncState,
|
||||
AccountWorkItem,
|
||||
)
|
||||
from .account_capability_service import AVAILABILITY_AVAILABLE, AVAILABILITY_LIMITED
|
||||
from .account_supply_service import evaluate_supply_targets
|
||||
|
||||
|
||||
def account_platform_status(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
moment = now or datetime.now(UTC)
|
||||
in_24h = moment + timedelta(hours=24)
|
||||
in_72h = moment + timedelta(hours=72)
|
||||
|
||||
account_counts = dict(
|
||||
db.query(Account.ck_check_status, func.count(Account.id))
|
||||
.group_by(Account.ck_check_status)
|
||||
.all()
|
||||
)
|
||||
total_accounts = int(db.query(func.count(Account.id)).scalar() or 0)
|
||||
cookie_accounts = int(
|
||||
db.query(func.count(Account.id))
|
||||
.filter(Account.cookie.isnot(None), Account.cookie != "")
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
expiring_24h = int(
|
||||
db.query(func.count(Account.id))
|
||||
.filter(
|
||||
Account.ck_check_status == "valid",
|
||||
Account.ck_expected_expires_at.isnot(None),
|
||||
Account.ck_expected_expires_at > moment,
|
||||
Account.ck_expected_expires_at <= in_24h,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
expiring_72h = int(
|
||||
db.query(func.count(Account.id))
|
||||
.filter(
|
||||
Account.ck_check_status == "valid",
|
||||
Account.ck_expected_expires_at.isnot(None),
|
||||
Account.ck_expected_expires_at > moment,
|
||||
Account.ck_expected_expires_at <= in_72h,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
predicted_expired = int(
|
||||
db.query(func.count(Account.id))
|
||||
.filter(
|
||||
Account.ck_check_status == "valid",
|
||||
Account.ck_expected_expires_at.isnot(None),
|
||||
Account.ck_expected_expires_at <= moment,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
queue_rows = (
|
||||
db.query(
|
||||
AccountWorkItem.work_type,
|
||||
AccountWorkItem.status,
|
||||
func.count(AccountWorkItem.id),
|
||||
func.min(AccountWorkItem.due_at),
|
||||
)
|
||||
.group_by(AccountWorkItem.work_type, AccountWorkItem.status)
|
||||
.all()
|
||||
)
|
||||
work_queue: dict[str, dict[str, Any]] = {}
|
||||
for work_type, status, count, oldest_due in queue_rows:
|
||||
bucket = work_queue.setdefault(str(work_type), {})
|
||||
bucket[str(status)] = int(count or 0)
|
||||
if status == "pending":
|
||||
bucket["oldest_pending_due_at"] = oldest_due
|
||||
|
||||
sync_rows = (
|
||||
db.query(
|
||||
AccountSyncState.domain,
|
||||
AccountSyncState.status,
|
||||
func.count(AccountSyncState.id),
|
||||
)
|
||||
.group_by(AccountSyncState.domain, AccountSyncState.status)
|
||||
.all()
|
||||
)
|
||||
sync_domains: dict[str, dict[str, Any]] = {}
|
||||
for domain, status, count in sync_rows:
|
||||
bucket = sync_domains.setdefault(str(domain), {})
|
||||
bucket[str(status)] = int(count or 0)
|
||||
fresh_rows = (
|
||||
db.query(AccountSyncState.domain, func.count(AccountSyncState.id))
|
||||
.filter(
|
||||
AccountSyncState.status == "success",
|
||||
AccountSyncState.valid_until.isnot(None),
|
||||
AccountSyncState.valid_until > moment,
|
||||
)
|
||||
.group_by(AccountSyncState.domain)
|
||||
.all()
|
||||
)
|
||||
for domain, count in fresh_rows:
|
||||
sync_domains.setdefault(str(domain), {})["fresh"] = int(count or 0)
|
||||
|
||||
commodity_rows = (
|
||||
db.query(
|
||||
AccountCommodityState.availability,
|
||||
func.count(AccountCommodityState.id),
|
||||
func.sum(
|
||||
case(
|
||||
(
|
||||
AccountCommodityState.valid_until > moment,
|
||||
1,
|
||||
),
|
||||
else_=0,
|
||||
)
|
||||
),
|
||||
)
|
||||
.filter(AccountCommodityState.platform == "douyu")
|
||||
.group_by(AccountCommodityState.availability)
|
||||
.all()
|
||||
)
|
||||
commodities = {
|
||||
str(availability): {
|
||||
"total": int(total or 0),
|
||||
"fresh": int(fresh or 0),
|
||||
}
|
||||
for availability, total, fresh in commodity_rows
|
||||
}
|
||||
fresh_available = int(commodities.get(AVAILABILITY_AVAILABLE, {}).get("fresh", 0))
|
||||
fresh_limited = int(commodities.get(AVAILABILITY_LIMITED, {}).get("fresh", 0))
|
||||
|
||||
inventories = evaluate_supply_targets(db, now=moment)
|
||||
return {
|
||||
"checked_at": moment,
|
||||
"accounts": {
|
||||
"total": total_accounts,
|
||||
"with_cookie": cookie_accounts,
|
||||
"ck_valid": int(account_counts.get("valid", 0)),
|
||||
"ck_invalid": int(account_counts.get("invalid", 0)),
|
||||
"ck_error": int(account_counts.get("error", 0)),
|
||||
"ck_unchecked": int(account_counts.get("", 0) or 0),
|
||||
"ck_expiring_24h": expiring_24h,
|
||||
"ck_expiring_72h": expiring_72h,
|
||||
"ck_predicted_expired": predicted_expired,
|
||||
},
|
||||
"work_queue": work_queue,
|
||||
"sync_domains": sync_domains,
|
||||
"commodities": {
|
||||
"by_availability": commodities,
|
||||
"fresh_available": fresh_available,
|
||||
"fresh_limited": fresh_limited,
|
||||
},
|
||||
"supply_targets": [
|
||||
{
|
||||
"target_id": item.target_id,
|
||||
"scope": item.scope,
|
||||
"commodity_id": item.commodity_id,
|
||||
"value_class": item.value_class,
|
||||
"covered": item.covered,
|
||||
"available": item.available,
|
||||
"allocatable": item.allocatable,
|
||||
"covered_shortage": item.covered_shortage,
|
||||
"available_shortage": item.available_shortage,
|
||||
"allocatable_shortage": item.allocatable_shortage,
|
||||
"needs_replenishment": item.needs_replenishment,
|
||||
}
|
||||
for item in inventories
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["account_platform_status"]
|
||||
Reference in New Issue
Block a user