perf(account-check): 优化账号检测首屏加载
This commit is contained in:
@@ -3,11 +3,13 @@ import type {
|
||||
DouyuAccountProtectionTarget,
|
||||
DouyuAccountProtectionTargetUpdate,
|
||||
DouyuAccountPlatformStatus,
|
||||
DouyuPlatformTrendsResponse,
|
||||
DouyuConfig,
|
||||
DouyuExchangeResultImage,
|
||||
DouyuGoodsItem,
|
||||
DouyuRechargeChannel,
|
||||
DouyuSupplyTarget,
|
||||
DouyuSupplyTargetConfig,
|
||||
DouyuSupplyTargetUpdate,
|
||||
DouyuSupplyTargetUpsert,
|
||||
DouyuTaskAccountItem,
|
||||
@@ -22,8 +24,12 @@ import type {
|
||||
|
||||
export const douyuApi = {
|
||||
taskTypes: () => api.get<Record<string, string>, Record<string, string>>('/douyu/task-types'),
|
||||
getAccountPlatformStatus: () =>
|
||||
api.get<DouyuAccountPlatformStatus, DouyuAccountPlatformStatus>('/douyu/account-platform-status'),
|
||||
getAccountPlatformStatus: (includeTrends = true) =>
|
||||
api.get<DouyuAccountPlatformStatus, DouyuAccountPlatformStatus>('/douyu/account-platform-status', {
|
||||
params: { include_trends: includeTrends },
|
||||
}),
|
||||
getAccountPlatformTrends: () =>
|
||||
api.get<DouyuPlatformTrendsResponse, DouyuPlatformTrendsResponse>('/douyu/account-platform-trends'),
|
||||
updateAccountProtectionTarget: (
|
||||
groupKey: DouyuAccountProtectionTarget['group_key'],
|
||||
data: DouyuAccountProtectionTargetUpdate,
|
||||
@@ -34,6 +40,8 @@ export const douyuApi = {
|
||||
),
|
||||
listSupplyTargets: () =>
|
||||
api.get<DouyuSupplyTarget[], DouyuSupplyTarget[]>('/douyu/supply-targets'),
|
||||
listSupplyTargetConfigs: () =>
|
||||
api.get<DouyuSupplyTargetConfig[], DouyuSupplyTargetConfig[]>('/douyu/supply-targets/config'),
|
||||
createSupplyTarget: (data: DouyuSupplyTargetUpsert) =>
|
||||
api.post<DouyuSupplyTarget, DouyuSupplyTarget>('/douyu/supply-targets', data),
|
||||
updateSupplyTarget: (targetId: number, data: DouyuSupplyTargetUpdate) =>
|
||||
|
||||
@@ -745,6 +745,22 @@ export interface DouyuSupplyTargetStatus {
|
||||
needs_replenishment: boolean;
|
||||
}
|
||||
|
||||
export interface DouyuSupplyTargetConfig {
|
||||
id: number;
|
||||
target_id: number;
|
||||
platform: string;
|
||||
scope: string;
|
||||
commodity_id: string;
|
||||
value_class: 'high_value' | 'ordinary';
|
||||
target_covered: number;
|
||||
target_available: number;
|
||||
target_allocatable: number;
|
||||
safety_buffer: number;
|
||||
enabled: boolean;
|
||||
updated_by: number | null;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
export interface DouyuSupplyTarget extends DouyuSupplyTargetStatus {
|
||||
platform: string;
|
||||
enabled: boolean;
|
||||
@@ -859,6 +875,11 @@ export interface DouyuPlatformTrends {
|
||||
lifecycle: DouyuLifecycleTrendDay[];
|
||||
}
|
||||
|
||||
export interface DouyuPlatformTrendsResponse {
|
||||
trends: DouyuPlatformTrends;
|
||||
alerts: DouyuPlatformAlert[];
|
||||
}
|
||||
|
||||
export interface DouyuAccountPlatformStatus {
|
||||
checked_at: string;
|
||||
accounts: {
|
||||
@@ -882,7 +903,7 @@ export interface DouyuAccountPlatformStatus {
|
||||
protection_targets: DouyuAccountProtectionTarget[];
|
||||
supply_targets: DouyuSupplyTargetStatus[];
|
||||
alerts: DouyuPlatformAlert[];
|
||||
trends: DouyuPlatformTrends;
|
||||
trends: DouyuPlatformTrends | null;
|
||||
}
|
||||
|
||||
export interface DouyuConfig {
|
||||
|
||||
@@ -30,7 +30,10 @@ import {
|
||||
douyuApi,
|
||||
type DouyuAccountProtectionTarget,
|
||||
type DouyuAccountPlatformStatus,
|
||||
type DouyuPlatformAlert,
|
||||
type DouyuPlatformTrends,
|
||||
type DouyuSupplyTarget,
|
||||
type DouyuSupplyTargetConfig,
|
||||
} from '../../api/modules';
|
||||
import { usePermissions } from '../../hooks/usePermissions';
|
||||
import { message } from '../../utils/antdMessage';
|
||||
@@ -83,8 +86,11 @@ export default function AccountPlatformStatusPanel() {
|
||||
const [form] = Form.useForm<TargetFormValues>();
|
||||
const [protectionForm] = Form.useForm<ProtectionFormValues>();
|
||||
const [status, setStatus] = useState<DouyuAccountPlatformStatus | null>(null);
|
||||
const [targets, setTargets] = useState<DouyuSupplyTarget[]>([]);
|
||||
const [targetConfigs, setTargetConfigs] = useState<DouyuSupplyTargetConfig[]>([]);
|
||||
const [trends, setTrends] = useState<DouyuPlatformTrends | null>(null);
|
||||
const [trendAlerts, setTrendAlerts] = useState<DouyuPlatformAlert[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [trendsLoading, setTrendsLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [modalOpen, setModalOpen] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
@@ -96,26 +102,43 @@ export default function AccountPlatformStatusPanel() {
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setTrendsLoading(true);
|
||||
setError('');
|
||||
try {
|
||||
const platformStatus = await douyuApi.getAccountPlatformStatus();
|
||||
if (canManage) {
|
||||
setTargets(await douyuApi.listSupplyTargets());
|
||||
} else {
|
||||
setTargets(platformStatus.supply_targets.map((row) => ({
|
||||
...row,
|
||||
platform: 'douyu',
|
||||
enabled: true,
|
||||
updated_by: null,
|
||||
updated_at: null,
|
||||
})));
|
||||
const statusPromise = douyuApi
|
||||
.getAccountPlatformStatus(false)
|
||||
.then((platformStatus) => {
|
||||
setStatus(platformStatus);
|
||||
})
|
||||
.catch((loadError: unknown) => {
|
||||
setError(getErrorMessage(loadError));
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
const targetsPromise = canManage
|
||||
? douyuApi.listSupplyTargetConfigs()
|
||||
.then(setTargetConfigs)
|
||||
.catch((loadError: unknown) => {
|
||||
setError((current) => current || getErrorMessage(loadError));
|
||||
})
|
||||
: Promise.resolve();
|
||||
// 主状态返回后再加载趋势,避免两条重查询同时争抢数据库和 CPU。
|
||||
const trendsPromise = statusPromise.then(async () => {
|
||||
try {
|
||||
const response = await douyuApi.getAccountPlatformTrends();
|
||||
setTrends(response.trends);
|
||||
setTrendAlerts(response.alerts);
|
||||
} catch (loadError: unknown) {
|
||||
setTrendAlerts([{
|
||||
code: 'account_trends_load_failed',
|
||||
severity: 'info',
|
||||
message: `趋势加载失败:${getErrorMessage(loadError)}`,
|
||||
}]);
|
||||
} finally {
|
||||
setTrendsLoading(false);
|
||||
}
|
||||
setStatus(platformStatus);
|
||||
} catch (loadError: unknown) {
|
||||
setError(getErrorMessage(loadError));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
});
|
||||
await Promise.allSettled([statusPromise, targetsPromise, trendsPromise]);
|
||||
}, [canManage]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -129,10 +152,48 @@ export default function AccountPlatformStatusPanel() {
|
||||
),
|
||||
[status?.work_queue],
|
||||
);
|
||||
const targets = useMemo<DouyuSupplyTarget[]>(() => {
|
||||
const inventoryByTarget = new Map(
|
||||
(status?.supply_targets || []).map((row) => [row.target_id, row]),
|
||||
);
|
||||
const statusConfigs: DouyuSupplyTargetConfig[] = (status?.supply_targets || []).map(
|
||||
(row) => ({
|
||||
id: row.target_id,
|
||||
target_id: row.target_id,
|
||||
platform: 'douyu',
|
||||
scope: row.scope,
|
||||
commodity_id: row.commodity_id,
|
||||
value_class: row.value_class,
|
||||
target_covered: row.target_covered,
|
||||
target_available: row.target_available,
|
||||
target_allocatable: row.target_allocatable,
|
||||
safety_buffer: row.safety_buffer,
|
||||
enabled: true,
|
||||
updated_by: null,
|
||||
updated_at: null,
|
||||
}),
|
||||
);
|
||||
const sourceConfigs = canManage && targetConfigs.length > 0
|
||||
? targetConfigs
|
||||
: statusConfigs;
|
||||
return sourceConfigs.map((config) => {
|
||||
const inventory = inventoryByTarget.get(config.target_id);
|
||||
return {
|
||||
...config,
|
||||
covered: inventory?.covered || 0,
|
||||
available: inventory?.available || 0,
|
||||
allocatable: inventory?.allocatable || 0,
|
||||
covered_shortage: inventory?.covered_shortage || 0,
|
||||
available_shortage: inventory?.available_shortage || 0,
|
||||
allocatable_shortage: inventory?.allocatable_shortage || 0,
|
||||
needs_replenishment: inventory?.needs_replenishment || false,
|
||||
};
|
||||
});
|
||||
}, [canManage, status?.supply_targets, targetConfigs]);
|
||||
const trendRows = useMemo(() => {
|
||||
if (!status) return [];
|
||||
if (!trends) return [];
|
||||
const rows = new Map<string, TrendRow>();
|
||||
for (const item of status.trends.query) {
|
||||
for (const item of trends.query) {
|
||||
rows.set(item.date, {
|
||||
date: item.date,
|
||||
queryTotal: item.total,
|
||||
@@ -143,7 +204,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
reloginOk: 0,
|
||||
});
|
||||
}
|
||||
for (const item of status.trends.login) {
|
||||
for (const item of trends.login) {
|
||||
const row = rows.get(item.date) || {
|
||||
date: item.date,
|
||||
queryTotal: 0,
|
||||
@@ -157,7 +218,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
row.loginRate = item.success_rate;
|
||||
rows.set(item.date, row);
|
||||
}
|
||||
for (const item of status.trends.lifecycle) {
|
||||
for (const item of trends.lifecycle) {
|
||||
const row = rows.get(item.date) || {
|
||||
date: item.date,
|
||||
queryTotal: 0,
|
||||
@@ -172,7 +233,11 @@ export default function AccountPlatformStatusPanel() {
|
||||
rows.set(item.date, row);
|
||||
}
|
||||
return [...rows.values()].sort((left, right) => left.date.localeCompare(right.date));
|
||||
}, [status]);
|
||||
}, [trends]);
|
||||
const visibleAlerts = useMemo(
|
||||
() => [...(status?.alerts || []), ...trendAlerts],
|
||||
[status?.alerts, trendAlerts],
|
||||
);
|
||||
|
||||
const openCreate = () => {
|
||||
setEditing(null);
|
||||
@@ -480,15 +545,15 @@ export default function AccountPlatformStatusPanel() {
|
||||
>
|
||||
{error ? <Alert type="error" showIcon message={error} style={{ marginBottom: 16 }} /> : null}
|
||||
<Row gutter={[16, 16]}>
|
||||
<Col xs={12} md={6}><Statistic title="CK 有效" value={status?.accounts.ck_valid || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="24h 预计到期" value={status?.accounts.ck_expiring_24h || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="新鲜可兑换商品" value={status?.commodities.fresh_available || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="待处理账号工作" value={pendingWork} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="CK 有效" value={status ? status.accounts.ck_valid : '--'} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="24h 预计到期" value={status ? status.accounts.ck_expiring_24h : '--'} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="新鲜可兑换商品" value={status ? status.commodities.fresh_available : '--'} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="待处理账号工作" value={status ? pendingWork : '--'} /></Col>
|
||||
</Row>
|
||||
|
||||
{status?.alerts.length ? (
|
||||
{visibleAlerts.length ? (
|
||||
<Space orientation="vertical" size={8} style={{ width: '100%', marginTop: 16 }}>
|
||||
{status.alerts.map((alert) => (
|
||||
{visibleAlerts.map((alert) => (
|
||||
<Alert
|
||||
key={`${alert.code}-${alert.scope || ''}-${alert.value_class || ''}-${alert.message}`}
|
||||
type={alert.severity === 'critical' ? 'error' : alert.severity === 'warning' ? 'warning' : 'info'}
|
||||
@@ -511,6 +576,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
style={{ marginTop: 8 }}
|
||||
columns={protectionColumns}
|
||||
dataSource={status?.protection_targets || []}
|
||||
loading={loading}
|
||||
pagination={false}
|
||||
scroll={{ x: 950 }}
|
||||
locale={{
|
||||
@@ -530,6 +596,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
style={{ marginTop: 8 }}
|
||||
columns={columns}
|
||||
dataSource={targets}
|
||||
loading={loading}
|
||||
pagination={false}
|
||||
scroll={{ x: 750 }}
|
||||
locale={{
|
||||
@@ -540,12 +607,13 @@ export default function AccountPlatformStatusPanel() {
|
||||
/>
|
||||
|
||||
<Space orientation="vertical" size={8} style={{ width: '100%', marginTop: 20 }}>
|
||||
<Text strong>{`近 ${status?.trends.days || 7} 天趋势`}</Text>
|
||||
<Text strong>{`近 ${trends?.days || 7} 天趋势`}</Text>
|
||||
<Table
|
||||
rowKey="date"
|
||||
size="small"
|
||||
columns={trendColumns}
|
||||
dataSource={trendRows}
|
||||
loading={trendsLoading}
|
||||
pagination={false}
|
||||
scroll={{ x: 750 }}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user