feat(douyu): 增加账号平台七天趋势统计
This commit is contained in:
@@ -445,6 +445,10 @@
|
||||
"20261121_0111_account_platform_foundation.py": {
|
||||
"revision": "20261121_0111",
|
||||
"sha256": "0cf919b766769c84d357271acbf32bd968b2b6756b04cc059b26fd9fb5d41c10"
|
||||
},
|
||||
"20261121_0112_account_sync_trend_index.py": {
|
||||
"revision": "20261121_0112",
|
||||
"sha256": "9c1ebee5fba13efc413a4dcca258fa66dfbca5b41c1bd9b89ab794c394af3fee"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""add started-at index for account sync trend aggregation
|
||||
|
||||
The platform status panel aggregates the last seven days across all sync domains.
|
||||
The existing indexes start with domain/status, so they cannot serve that range
|
||||
efficiently when no domain filter is present.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261121_0112"
|
||||
down_revision: str | None = "20261121_0111"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
INDEX_NAME = "ix_account_sync_runs_started_domain_status"
|
||||
|
||||
|
||||
def _index_exists() -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'account_sync_runs'
|
||||
AND index_name = :index_name
|
||||
"""
|
||||
),
|
||||
{"index_name": INDEX_NAME},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if not _index_exists():
|
||||
op.create_index(
|
||||
INDEX_NAME,
|
||||
"account_sync_runs",
|
||||
["started_at", "domain", "status"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _index_exists():
|
||||
op.drop_index(INDEX_NAME, table_name="account_sync_runs")
|
||||
@@ -2449,6 +2449,12 @@ class AccountSyncRun(Base):
|
||||
"status",
|
||||
"started_at",
|
||||
),
|
||||
Index(
|
||||
"ix_account_sync_runs_started_domain_status",
|
||||
"started_at",
|
||||
"domain",
|
||||
"status",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from datetime import UTC, date, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import case, func
|
||||
@@ -11,12 +11,186 @@ from sqlalchemy.orm import Session
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountSyncRun,
|
||||
AccountSyncState,
|
||||
AccountWorkItem,
|
||||
LoginAttempt,
|
||||
)
|
||||
from .account_capability_service import AVAILABILITY_AVAILABLE, AVAILABILITY_LIMITED
|
||||
from .account_supply_service import evaluate_supply_targets
|
||||
|
||||
TREND_DAYS = 7
|
||||
|
||||
|
||||
def _day_key(value: Any) -> str:
|
||||
if isinstance(value, datetime):
|
||||
return value.date().isoformat()
|
||||
if isinstance(value, date):
|
||||
return value.isoformat()
|
||||
return str(value or "")[:10]
|
||||
|
||||
|
||||
def _trend_days(moment: datetime) -> list[str]:
|
||||
today = moment.date()
|
||||
return [
|
||||
(today - timedelta(days=offset)).isoformat()
|
||||
for offset in range(TREND_DAYS - 1, -1, -1)
|
||||
]
|
||||
|
||||
|
||||
def _rate(success: int, finished: int) -> float:
|
||||
if finished <= 0:
|
||||
return 0.0
|
||||
return round(success * 100 / finished, 1)
|
||||
|
||||
|
||||
def _platform_trends(db: Session, moment: datetime) -> dict[str, Any]:
|
||||
days = _trend_days(moment)
|
||||
start_at = datetime.combine(
|
||||
date.fromisoformat(days[0]),
|
||||
datetime.min.time(),
|
||||
tzinfo=UTC,
|
||||
)
|
||||
query_days = {
|
||||
day: {
|
||||
"date": day,
|
||||
"total": 0,
|
||||
"success": 0,
|
||||
"failed": 0,
|
||||
"running": 0,
|
||||
"success_rate": 0.0,
|
||||
"duration_ms": 0,
|
||||
"domains": {},
|
||||
}
|
||||
for day in days
|
||||
}
|
||||
sync_day = func.date(AccountSyncRun.started_at)
|
||||
sync_rows = (
|
||||
db.query(
|
||||
sync_day,
|
||||
AccountSyncRun.domain,
|
||||
AccountSyncRun.status,
|
||||
func.count(AccountSyncRun.id),
|
||||
func.coalesce(func.sum(AccountSyncRun.duration_ms), 0),
|
||||
)
|
||||
.filter(AccountSyncRun.started_at >= start_at)
|
||||
.group_by(sync_day, AccountSyncRun.domain, AccountSyncRun.status)
|
||||
.all()
|
||||
)
|
||||
for raw_day, domain, status, count, duration_ms in sync_rows:
|
||||
day = _day_key(raw_day)
|
||||
bucket = query_days.get(day)
|
||||
if bucket is None:
|
||||
continue
|
||||
count_value = int(count or 0)
|
||||
domain_bucket = bucket["domains"].setdefault(
|
||||
str(domain),
|
||||
{"total": 0, "success": 0, "failed": 0, "running": 0},
|
||||
)
|
||||
bucket["total"] += count_value
|
||||
bucket["duration_ms"] += int(duration_ms or 0)
|
||||
domain_bucket["total"] += count_value
|
||||
if status in {"success", "failed", "running"}:
|
||||
bucket[str(status)] += count_value
|
||||
domain_bucket[str(status)] += count_value
|
||||
for bucket in query_days.values():
|
||||
finished = bucket["success"] + bucket["failed"]
|
||||
bucket["success_rate"] = _rate(bucket["success"], finished)
|
||||
for domain_bucket in bucket["domains"].values():
|
||||
domain_finished = domain_bucket["success"] + domain_bucket["failed"]
|
||||
domain_bucket["success_rate"] = _rate(
|
||||
domain_bucket["success"],
|
||||
domain_finished,
|
||||
)
|
||||
|
||||
login_days = {
|
||||
day: {
|
||||
"date": day,
|
||||
"total": 0,
|
||||
"success": 0,
|
||||
"failed": 0,
|
||||
"pending": 0,
|
||||
"success_rate": 0.0,
|
||||
"categories": {},
|
||||
}
|
||||
for day in days
|
||||
}
|
||||
login_day = LoginAttempt.quota_day
|
||||
login_rows = (
|
||||
db.query(
|
||||
login_day,
|
||||
LoginAttempt.category,
|
||||
LoginAttempt.status,
|
||||
func.count(LoginAttempt.id),
|
||||
)
|
||||
.filter(LoginAttempt.quota_day >= date.fromisoformat(days[0]))
|
||||
.group_by(login_day, LoginAttempt.category, LoginAttempt.status)
|
||||
.all()
|
||||
)
|
||||
for raw_day, category, status, count in login_rows:
|
||||
day = _day_key(raw_day)
|
||||
bucket = login_days.get(day)
|
||||
if bucket is None:
|
||||
continue
|
||||
count_value = int(count or 0)
|
||||
category_bucket = bucket["categories"].setdefault(
|
||||
str(category),
|
||||
{"total": 0, "success": 0, "failed": 0, "pending": 0},
|
||||
)
|
||||
bucket["total"] += count_value
|
||||
category_bucket["total"] += count_value
|
||||
if status in {"success", "failed", "pending"}:
|
||||
bucket[str(status)] += count_value
|
||||
category_bucket[str(status)] += count_value
|
||||
for bucket in login_days.values():
|
||||
finished = bucket["success"] + bucket["failed"]
|
||||
bucket["success_rate"] = _rate(bucket["success"], finished)
|
||||
for category_bucket in bucket["categories"].values():
|
||||
category_finished = category_bucket["success"] + category_bucket["failed"]
|
||||
category_bucket["success_rate"] = _rate(
|
||||
category_bucket["success"],
|
||||
category_finished,
|
||||
)
|
||||
|
||||
lifecycle_days = {
|
||||
day: {
|
||||
"date": day,
|
||||
"ck_invalid": 0,
|
||||
"relogin_ok": 0,
|
||||
"relogin_failed": 0,
|
||||
}
|
||||
for day in days
|
||||
}
|
||||
lifecycle_events = ("ck_invalid", "relogin_ok", "relogin_failed")
|
||||
event_day = func.date(AccountEvent.occurred_at)
|
||||
event_rows = (
|
||||
db.query(
|
||||
event_day,
|
||||
AccountEvent.event_type,
|
||||
func.count(AccountEvent.id),
|
||||
)
|
||||
.filter(
|
||||
AccountEvent.occurred_at >= start_at,
|
||||
AccountEvent.event_type.in_(lifecycle_events),
|
||||
)
|
||||
.group_by(event_day, AccountEvent.event_type)
|
||||
.all()
|
||||
)
|
||||
for raw_day, event_type, count in event_rows:
|
||||
bucket = lifecycle_days.get(_day_key(raw_day))
|
||||
if bucket is not None and event_type in bucket:
|
||||
bucket[str(event_type)] = int(count or 0)
|
||||
|
||||
return {
|
||||
"days": TREND_DAYS,
|
||||
"from": days[0],
|
||||
"to": days[-1],
|
||||
"query": list(query_days.values()),
|
||||
"login": list(login_days.values()),
|
||||
"lifecycle": list(lifecycle_days.values()),
|
||||
}
|
||||
|
||||
|
||||
def account_platform_status(
|
||||
db: Session,
|
||||
@@ -251,6 +425,7 @@ def account_platform_status(
|
||||
},
|
||||
"supply_targets": supply_targets,
|
||||
"alerts": alerts,
|
||||
"trends": _platform_trends(db, moment),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user