perf(douyu): 优化自动化中心运行总览加载
This commit is contained in:
@@ -35,6 +35,7 @@ import {
|
||||
type DouyuPlatformTrends,
|
||||
type DouyuSupplyTarget,
|
||||
type DouyuSupplyTargetConfig,
|
||||
type DouyuSupplyTargetStatus,
|
||||
} from '../../api/modules';
|
||||
import { usePermissions } from '../../hooks/usePermissions';
|
||||
import { message } from '../../utils/antdMessage';
|
||||
@@ -158,9 +159,12 @@ export default function AccountPlatformStatusPanel() {
|
||||
const [protectionForm] = Form.useForm<ProtectionFormValues>();
|
||||
const [status, setStatus] = useState<DouyuAccountPlatformStatus | null>(null);
|
||||
const [targetConfigs, setTargetConfigs] = useState<DouyuSupplyTargetConfig[]>([]);
|
||||
const [supplyTargets, setSupplyTargets] = useState<DouyuSupplyTargetStatus[]>([]);
|
||||
const [supplyAlerts, setSupplyAlerts] = useState<DouyuPlatformAlert[]>([]);
|
||||
const [trends, setTrends] = useState<DouyuPlatformTrends | null>(null);
|
||||
const [trendAlerts, setTrendAlerts] = useState<DouyuPlatformAlert[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [statusLoading, setStatusLoading] = useState(false);
|
||||
const [supplyLoading, setSupplyLoading] = useState(false);
|
||||
const [trendsLoading, setTrendsLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [modalOpen, setModalOpen] = useState(false);
|
||||
@@ -171,12 +175,15 @@ export default function AccountPlatformStatusPanel() {
|
||||
const [editingProtection, setEditingProtection] =
|
||||
useState<DouyuAccountProtectionTarget | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setTrendsLoading(true);
|
||||
const loadStatus = useCallback(async () => {
|
||||
setStatusLoading(true);
|
||||
setError('');
|
||||
const statusPromise = douyuApi
|
||||
.getAccountPlatformStatus(false)
|
||||
.getAccountPlatformStatus({
|
||||
includeTrends: false,
|
||||
includeSupply: false,
|
||||
includeDiagnostics: false,
|
||||
})
|
||||
.then((platformStatus) => {
|
||||
setStatus(platformStatus);
|
||||
})
|
||||
@@ -184,7 +191,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
setError(getErrorMessage(loadError));
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false);
|
||||
setStatusLoading(false);
|
||||
});
|
||||
const targetsPromise = canManage
|
||||
? douyuApi.listSupplyTargetConfigs()
|
||||
@@ -193,25 +200,50 @@ export default function AccountPlatformStatusPanel() {
|
||||
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);
|
||||
}
|
||||
});
|
||||
await Promise.allSettled([statusPromise, targetsPromise, trendsPromise]);
|
||||
await Promise.allSettled([statusPromise, targetsPromise]);
|
||||
}, [canManage]);
|
||||
|
||||
const loadSupply = useCallback(async () => {
|
||||
setSupplyLoading(true);
|
||||
try {
|
||||
const response = await douyuApi.getAccountPlatformSupply();
|
||||
setSupplyTargets(response.supply_targets);
|
||||
setSupplyAlerts(response.alerts);
|
||||
} catch (loadError: unknown) {
|
||||
setSupplyAlerts([{
|
||||
code: 'account_supply_load_failed',
|
||||
severity: 'warning',
|
||||
message: `供给库存加载失败:${getErrorMessage(loadError)}`,
|
||||
}]);
|
||||
} finally {
|
||||
setSupplyLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const loadTrends = useCallback(async () => {
|
||||
setTrendsLoading(true);
|
||||
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);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
await loadStatus();
|
||||
// 首屏状态已经可交互;供给和趋势较重,按顺序加载,避免同时争抢数据库。
|
||||
await loadSupply();
|
||||
await loadTrends();
|
||||
}, [loadStatus, loadSupply, loadTrends]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
@@ -225,9 +257,9 @@ export default function AccountPlatformStatusPanel() {
|
||||
);
|
||||
const targets = useMemo<DouyuSupplyTarget[]>(() => {
|
||||
const inventoryByTarget = new Map(
|
||||
(status?.supply_targets || []).map((row) => [row.target_id, row]),
|
||||
supplyTargets.map((row) => [row.target_id, row]),
|
||||
);
|
||||
const statusConfigs: DouyuSupplyTargetConfig[] = (status?.supply_targets || []).map(
|
||||
const statusConfigs: DouyuSupplyTargetConfig[] = supplyTargets.map(
|
||||
(row) => ({
|
||||
id: row.target_id,
|
||||
target_id: row.target_id,
|
||||
@@ -260,7 +292,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
needs_replenishment: inventory?.needs_replenishment || false,
|
||||
};
|
||||
});
|
||||
}, [canManage, status?.supply_targets, targetConfigs]);
|
||||
}, [canManage, supplyTargets, targetConfigs]);
|
||||
const trendRows = useMemo(() => {
|
||||
if (!trends) return [];
|
||||
const rows = new Map<string, TrendRow>();
|
||||
@@ -306,9 +338,10 @@ export default function AccountPlatformStatusPanel() {
|
||||
return [...rows.values()].sort((left, right) => left.date.localeCompare(right.date));
|
||||
}, [trends]);
|
||||
const visibleAlerts = useMemo(
|
||||
() => [...(status?.alerts || []), ...trendAlerts],
|
||||
[status?.alerts, trendAlerts],
|
||||
() => [...(status?.alerts || []), ...supplyAlerts, ...trendAlerts],
|
||||
[status?.alerts, supplyAlerts, trendAlerts],
|
||||
);
|
||||
const loading = statusLoading || supplyLoading || trendsLoading;
|
||||
|
||||
const openCreate = () => {
|
||||
setEditing(null);
|
||||
@@ -647,7 +680,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
style={{ marginTop: 8 }}
|
||||
columns={protectionColumns}
|
||||
dataSource={status?.protection_targets || []}
|
||||
loading={loading}
|
||||
loading={statusLoading}
|
||||
pagination={false}
|
||||
scroll={{ x: 950 }}
|
||||
locale={{
|
||||
@@ -667,7 +700,7 @@ export default function AccountPlatformStatusPanel() {
|
||||
style={{ marginTop: 8 }}
|
||||
columns={columns}
|
||||
dataSource={targets}
|
||||
loading={loading}
|
||||
loading={supplyLoading}
|
||||
pagination={false}
|
||||
scroll={{ x: 750 }}
|
||||
locale={{
|
||||
|
||||
Reference in New Issue
Block a user