perf(douyu): 优化自动化中心运行总览加载
This commit is contained in:
@@ -261,21 +261,104 @@ def account_platform_trends(
|
||||
}
|
||||
|
||||
|
||||
def _supply_snapshot(
|
||||
db: Session,
|
||||
moment: datetime,
|
||||
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
|
||||
inventories = evaluate_supply_targets(db, now=moment)
|
||||
supply_targets: list[dict[str, Any]] = []
|
||||
alerts: list[dict[str, Any]] = []
|
||||
for item in inventories:
|
||||
shortage = max(
|
||||
item.covered_shortage,
|
||||
item.available_shortage,
|
||||
item.allocatable_shortage,
|
||||
)
|
||||
supply_targets.append(
|
||||
{
|
||||
"target_id": item.target_id,
|
||||
"scope": item.scope,
|
||||
"commodity_id": item.commodity_id,
|
||||
"value_class": item.value_class,
|
||||
"target_covered": item.target_covered,
|
||||
"target_available": item.target_available,
|
||||
"target_allocatable": item.target_allocatable,
|
||||
"safety_buffer": item.safety_buffer,
|
||||
"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,
|
||||
}
|
||||
)
|
||||
if not item.needs_replenishment:
|
||||
continue
|
||||
critical = (item.target_available > 0 and item.available <= 0) or (
|
||||
item.target_allocatable > 0 and item.allocatable <= 0
|
||||
)
|
||||
alerts.append(
|
||||
{
|
||||
"code": "supply_shortage",
|
||||
"severity": "critical" if critical else "warning",
|
||||
"scope": item.scope,
|
||||
"commodity_id": item.commodity_id,
|
||||
"value_class": item.value_class,
|
||||
"shortage": shortage,
|
||||
"message": (
|
||||
f"{item.scope}/{item.commodity_id or 'all'}/"
|
||||
f"{item.value_class} 库存短缺 {shortage}"
|
||||
),
|
||||
}
|
||||
)
|
||||
if not inventories:
|
||||
alerts.append(
|
||||
{
|
||||
"code": "supply_targets_missing",
|
||||
"severity": "warning",
|
||||
"message": "尚未配置启用的账号供给目标",
|
||||
}
|
||||
)
|
||||
return supply_targets, alerts
|
||||
|
||||
|
||||
def account_supply_snapshot(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""返回可延迟加载的账号供给库存,避免阻塞运行总览首屏。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
supply_targets, alerts = _supply_snapshot(db, moment)
|
||||
return {
|
||||
"checked_at": moment,
|
||||
"supply_targets": supply_targets,
|
||||
"alerts": alerts,
|
||||
}
|
||||
|
||||
|
||||
def account_platform_status(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
include_trends: bool = True,
|
||||
include_supply: bool = True,
|
||||
include_diagnostics: bool = True,
|
||||
) -> 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))
|
||||
account_counts: dict[str, int] = {
|
||||
str(status or ""): int(count or 0)
|
||||
for status, count in 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))
|
||||
@@ -334,129 +417,110 @@ def account_platform_status(
|
||||
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,
|
||||
if include_diagnostics:
|
||||
sync_rows = (
|
||||
db.query(
|
||||
AccountSyncState.domain,
|
||||
AccountSyncState.status,
|
||||
func.count(AccountSyncState.id),
|
||||
)
|
||||
.filter(AccountSyncState.platform == "douyu")
|
||||
.group_by(AccountSyncState.domain, AccountSyncState.status)
|
||||
.all()
|
||||
)
|
||||
.group_by(AccountSyncState.domain)
|
||||
.all()
|
||||
)
|
||||
for domain, count in fresh_rows:
|
||||
sync_domains.setdefault(str(domain), {})["fresh"] = int(count or 0)
|
||||
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.platform == "douyu",
|
||||
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,
|
||||
)
|
||||
),
|
||||
commodities: dict[str, dict[str, int]] = {}
|
||||
eligibility: dict[str, dict[str, int]] = {}
|
||||
fresh_available = 0
|
||||
fresh_limited = 0
|
||||
if include_diagnostics:
|
||||
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()
|
||||
)
|
||||
.filter(AccountCommodityState.platform == "douyu")
|
||||
.group_by(AccountCommodityState.availability)
|
||||
.all()
|
||||
)
|
||||
commodities = {
|
||||
str(availability): {
|
||||
"total": int(total or 0),
|
||||
"fresh": int(fresh or 0),
|
||||
commodities = {
|
||||
str(availability): {
|
||||
"total": int(total or 0),
|
||||
"fresh": int(fresh or 0),
|
||||
}
|
||||
for availability, total, fresh in commodity_rows
|
||||
}
|
||||
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))
|
||||
eligibility_rows = (
|
||||
db.query(
|
||||
AccountCommodityState.account_eligibility,
|
||||
func.count(AccountCommodityState.id),
|
||||
func.sum(
|
||||
case(
|
||||
(AccountCommodityState.valid_until > moment, 1),
|
||||
else_=0,
|
||||
)
|
||||
),
|
||||
fresh_available = int(
|
||||
commodities.get(AVAILABILITY_AVAILABLE, {}).get("fresh", 0)
|
||||
)
|
||||
.filter(AccountCommodityState.platform == "douyu")
|
||||
.group_by(AccountCommodityState.account_eligibility)
|
||||
.all()
|
||||
)
|
||||
eligibility = {
|
||||
str(state): {
|
||||
"total": int(total or 0),
|
||||
"fresh": int(fresh or 0),
|
||||
fresh_limited = int(commodities.get(AVAILABILITY_LIMITED, {}).get("fresh", 0))
|
||||
eligibility_rows = (
|
||||
db.query(
|
||||
AccountCommodityState.account_eligibility,
|
||||
func.count(AccountCommodityState.id),
|
||||
func.sum(
|
||||
case(
|
||||
(AccountCommodityState.valid_until > moment, 1),
|
||||
else_=0,
|
||||
)
|
||||
),
|
||||
)
|
||||
.filter(AccountCommodityState.platform == "douyu")
|
||||
.group_by(AccountCommodityState.account_eligibility)
|
||||
.all()
|
||||
)
|
||||
eligibility = {
|
||||
str(state): {
|
||||
"total": int(total or 0),
|
||||
"fresh": int(fresh or 0),
|
||||
}
|
||||
for state, total, fresh in eligibility_rows
|
||||
}
|
||||
for state, total, fresh in eligibility_rows
|
||||
}
|
||||
fresh_eligible = int(eligibility.get("eligible", {}).get("fresh", 0))
|
||||
else:
|
||||
fresh_eligible = int(
|
||||
db.query(func.count(AccountCommodityState.id))
|
||||
.filter(
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.account_eligibility == "eligible",
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > moment,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
inventories = evaluate_supply_targets(db, now=moment)
|
||||
supply_targets: list[dict[str, Any]] = []
|
||||
alerts: list[dict[str, Any]] = []
|
||||
for item in inventories:
|
||||
shortage = max(
|
||||
item.covered_shortage,
|
||||
item.available_shortage,
|
||||
item.allocatable_shortage,
|
||||
)
|
||||
supply_targets.append(
|
||||
{
|
||||
"target_id": item.target_id,
|
||||
"scope": item.scope,
|
||||
"commodity_id": item.commodity_id,
|
||||
"value_class": item.value_class,
|
||||
"target_covered": item.target_covered,
|
||||
"target_available": item.target_available,
|
||||
"target_allocatable": item.target_allocatable,
|
||||
"safety_buffer": item.safety_buffer,
|
||||
"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,
|
||||
}
|
||||
)
|
||||
if not item.needs_replenishment:
|
||||
continue
|
||||
critical = (item.target_available > 0 and item.available <= 0) or (
|
||||
item.target_allocatable > 0 and item.allocatable <= 0
|
||||
)
|
||||
alerts.append(
|
||||
{
|
||||
"code": "supply_shortage",
|
||||
"severity": "critical" if critical else "warning",
|
||||
"scope": item.scope,
|
||||
"commodity_id": item.commodity_id,
|
||||
"value_class": item.value_class,
|
||||
"shortage": shortage,
|
||||
"message": (
|
||||
f"{item.scope}/{item.commodity_id or 'all'}/"
|
||||
f"{item.value_class} 库存短缺 {shortage}"
|
||||
),
|
||||
}
|
||||
)
|
||||
if include_supply:
|
||||
supply_targets, supply_alerts = _supply_snapshot(db, moment)
|
||||
alerts.extend(supply_alerts)
|
||||
|
||||
protection_targets = evaluate_protection_targets(db, now=moment)
|
||||
for item in protection_targets:
|
||||
@@ -494,14 +558,6 @@ def account_platform_status(
|
||||
}
|
||||
)
|
||||
|
||||
if not inventories:
|
||||
alerts.append(
|
||||
{
|
||||
"code": "supply_targets_missing",
|
||||
"severity": "warning",
|
||||
"message": "尚未配置启用的账号供给目标",
|
||||
}
|
||||
)
|
||||
if expiring_24h:
|
||||
alerts.append(
|
||||
{
|
||||
@@ -552,7 +608,7 @@ def account_platform_status(
|
||||
"commodities": {
|
||||
"by_availability": commodities,
|
||||
"by_account_eligibility": eligibility,
|
||||
"fresh_eligible": int(eligibility.get("eligible", {}).get("fresh", 0)),
|
||||
"fresh_eligible": fresh_eligible,
|
||||
"fresh_available": fresh_available,
|
||||
"fresh_limited": fresh_limited,
|
||||
},
|
||||
@@ -563,4 +619,8 @@ def account_platform_status(
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["account_platform_status", "account_platform_trends"]
|
||||
__all__ = [
|
||||
"account_platform_status",
|
||||
"account_platform_trends",
|
||||
"account_supply_snapshot",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user