feat(douyu): 增加账号平台七天趋势统计

This commit is contained in:
yml
2026-11-21 23:00:32 +08:00
parent 1598fa353f
commit 3e618c1a88
9 changed files with 484 additions and 4 deletions
+78
View File
@@ -7,10 +7,13 @@ from datetime import UTC, datetime, timedelta
from web.backend.models import (
Account,
AccountCommodityState,
AccountEvent,
AccountLease,
AccountSupplyTarget,
AccountSyncRun,
AccountSyncState,
AccountWorkItem,
LoginAttempt,
User,
)
from web.backend.routers.douyu import create_supply_target, list_supply_targets
@@ -266,3 +269,78 @@ def test_account_platform_status_aggregates_queue_sync_and_supply(db):
assert {
alert["code"] for alert in status["alerts"]
} >= {"supply_shortage", "ck_expiring_24h"}
def test_account_platform_status_builds_seven_day_trends(db):
account = _account("platform-trends", points=400)
db.add(account)
db.flush()
db.add_all(
[
AccountSyncRun(
account_id=account.id,
domain="storehome",
started_at=NOW,
finished_at=NOW,
status="success",
duration_ms=120,
),
AccountSyncRun(
account_id=account.id,
domain="role",
started_at=NOW,
finished_at=NOW,
status="failed",
duration_ms=300,
),
LoginAttempt(
account_id=account.id,
category="manual_login",
trigger_source="manual",
started_at=NOW,
finished_at=NOW,
status="success",
quota_day=NOW.date(),
),
LoginAttempt(
account_id=account.id,
category="relogin_ordinary",
trigger_source="system",
started_at=NOW,
finished_at=NOW,
status="failed",
failure_kind="captcha",
quota_day=NOW.date(),
),
AccountEvent(
account_id=account.id,
event_type="ck_invalid",
category="auth",
occurred_at=NOW,
title="CK 失效",
source="pytest",
actor_type="system",
source_ref=f"pytest-trend:{account.id}",
),
]
)
db.commit()
status = account_platform_status(db, now=NOW)
today = NOW.date().isoformat()
query_today = next(row for row in status["trends"]["query"] if row["date"] == today)
login_today = next(row for row in status["trends"]["login"] if row["date"] == today)
lifecycle_today = next(
row for row in status["trends"]["lifecycle"] if row["date"] == today
)
assert status["trends"]["days"] == 7
assert query_today["total"] == 2
assert query_today["success"] == 1
assert query_today["failed"] == 1
assert query_today["success_rate"] == 50.0
assert "storehome" in query_today["domains"]
assert login_today["success"] == 1
assert login_today["failed"] == 1
assert login_today["success_rate"] == 50.0
assert lifecycle_today["ck_invalid"] == 1
+1 -1
View File
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = TESTS_DIR.parent
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
HEAD_REVISION = "20261121_0111"
HEAD_REVISION = "20261121_0112"
# 迁移新增、但不声明在模型里的复合查询索引。
EXTRA_INDEXES = (