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),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -783,6 +783,55 @@ export interface DouyuPlatformAlert {
|
||||
count?: number;
|
||||
}
|
||||
|
||||
export interface DouyuQueryTrendDay {
|
||||
date: string;
|
||||
total: number;
|
||||
success: number;
|
||||
failed: number;
|
||||
running: number;
|
||||
success_rate: number;
|
||||
duration_ms: number;
|
||||
domains: Record<string, {
|
||||
total: number;
|
||||
success: number;
|
||||
failed: number;
|
||||
running: number;
|
||||
success_rate: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface DouyuLoginTrendDay {
|
||||
date: string;
|
||||
total: number;
|
||||
success: number;
|
||||
failed: number;
|
||||
pending: number;
|
||||
success_rate: number;
|
||||
categories: Record<string, {
|
||||
total: number;
|
||||
success: number;
|
||||
failed: number;
|
||||
pending: number;
|
||||
success_rate: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface DouyuLifecycleTrendDay {
|
||||
date: string;
|
||||
ck_invalid: number;
|
||||
relogin_ok: number;
|
||||
relogin_failed: number;
|
||||
}
|
||||
|
||||
export interface DouyuPlatformTrends {
|
||||
days: number;
|
||||
from: string;
|
||||
to: string;
|
||||
query: DouyuQueryTrendDay[];
|
||||
login: DouyuLoginTrendDay[];
|
||||
lifecycle: DouyuLifecycleTrendDay[];
|
||||
}
|
||||
|
||||
export interface DouyuAccountPlatformStatus {
|
||||
checked_at: string;
|
||||
accounts: {
|
||||
@@ -805,6 +854,7 @@ export interface DouyuAccountPlatformStatus {
|
||||
};
|
||||
supply_targets: DouyuSupplyTargetStatus[];
|
||||
alerts: DouyuPlatformAlert[];
|
||||
trends: DouyuPlatformTrends;
|
||||
}
|
||||
|
||||
export interface DouyuConfig {
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
Input,
|
||||
InputNumber,
|
||||
Modal,
|
||||
Progress,
|
||||
Row,
|
||||
Select,
|
||||
Space,
|
||||
@@ -53,6 +54,16 @@ interface TargetFormValues {
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
interface TrendRow {
|
||||
date: string;
|
||||
queryTotal: number;
|
||||
queryRate: number;
|
||||
loginTotal: number;
|
||||
loginRate: number;
|
||||
ckInvalid: number;
|
||||
reloginOk: number;
|
||||
}
|
||||
|
||||
export default function AccountPlatformStatusPanel() {
|
||||
const { can } = usePermissions();
|
||||
const canManage = can('douyu:config');
|
||||
@@ -100,6 +111,50 @@ export default function AccountPlatformStatusPanel() {
|
||||
),
|
||||
[status?.work_queue],
|
||||
);
|
||||
const trendRows = useMemo(() => {
|
||||
if (!status) return [];
|
||||
const rows = new Map<string, TrendRow>();
|
||||
for (const item of status.trends.query) {
|
||||
rows.set(item.date, {
|
||||
date: item.date,
|
||||
queryTotal: item.total,
|
||||
queryRate: item.success_rate,
|
||||
loginTotal: 0,
|
||||
loginRate: 0,
|
||||
ckInvalid: 0,
|
||||
reloginOk: 0,
|
||||
});
|
||||
}
|
||||
for (const item of status.trends.login) {
|
||||
const row = rows.get(item.date) || {
|
||||
date: item.date,
|
||||
queryTotal: 0,
|
||||
queryRate: 0,
|
||||
loginTotal: 0,
|
||||
loginRate: 0,
|
||||
ckInvalid: 0,
|
||||
reloginOk: 0,
|
||||
};
|
||||
row.loginTotal = item.total;
|
||||
row.loginRate = item.success_rate;
|
||||
rows.set(item.date, row);
|
||||
}
|
||||
for (const item of status.trends.lifecycle) {
|
||||
const row = rows.get(item.date) || {
|
||||
date: item.date,
|
||||
queryTotal: 0,
|
||||
queryRate: 0,
|
||||
loginTotal: 0,
|
||||
loginRate: 0,
|
||||
ckInvalid: 0,
|
||||
reloginOk: 0,
|
||||
};
|
||||
row.ckInvalid = item.ck_invalid;
|
||||
row.reloginOk = item.relogin_ok;
|
||||
rows.set(item.date, row);
|
||||
}
|
||||
return [...rows.values()].sort((left, right) => left.date.localeCompare(right.date));
|
||||
}, [status]);
|
||||
|
||||
const openCreate = () => {
|
||||
setEditing(null);
|
||||
@@ -232,6 +287,46 @@ export default function AccountPlatformStatusPanel() {
|
||||
}] : []),
|
||||
];
|
||||
|
||||
const trendColumns: TableProps<TrendRow>['columns'] = [
|
||||
{
|
||||
title: '日期',
|
||||
dataIndex: 'date',
|
||||
width: 100,
|
||||
render: (value: string) => value.slice(5),
|
||||
},
|
||||
{
|
||||
title: '查询成功率',
|
||||
width: 140,
|
||||
render: (_, row) => (
|
||||
<Progress
|
||||
percent={row.queryRate}
|
||||
size="small"
|
||||
status={row.queryRate < 70 ? 'exception' : 'normal'}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{ title: '查询量', dataIndex: 'queryTotal', width: 80 },
|
||||
{
|
||||
title: '登录成功率',
|
||||
width: 140,
|
||||
render: (_, row) => (
|
||||
<Progress
|
||||
percent={row.loginRate}
|
||||
size="small"
|
||||
status={row.loginRate < 50 ? 'exception' : 'normal'}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{ title: '登录量', dataIndex: 'loginTotal', width: 80 },
|
||||
{
|
||||
title: 'CK 失效',
|
||||
dataIndex: 'ckInvalid',
|
||||
width: 90,
|
||||
render: (value: number) => value > 0 ? <Text type="danger">{value}</Text> : 0,
|
||||
},
|
||||
{ title: '重登成功', dataIndex: 'reloginOk', width: 90 },
|
||||
];
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card
|
||||
@@ -287,6 +382,18 @@ export default function AccountPlatformStatusPanel() {
|
||||
),
|
||||
}}
|
||||
/>
|
||||
|
||||
<Space orientation="vertical" size={8} style={{ width: '100%', marginTop: 20 }}>
|
||||
<Text strong>{`近 ${status?.trends.days || 7} 天趋势`}</Text>
|
||||
<Table
|
||||
rowKey="date"
|
||||
size="small"
|
||||
columns={trendColumns}
|
||||
dataSource={trendRows}
|
||||
pagination={false}
|
||||
scroll={{ x: 750 }}
|
||||
/>
|
||||
</Space>
|
||||
</Card>
|
||||
|
||||
<Modal
|
||||
|
||||
Reference in New Issue
Block a user