Optimize frontend to use batch API for equity history requests

- Add batch API methods (getEquityHistoryBatch, getTopTraders, getPublicTraderConfig) to api.ts
- Update ComparisonChart to use batch endpoint instead of individual calls
- Reduce network requests from 10 to 1 for performance comparison page
- Maintain backward compatibility with existing data structure

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
icy
2025-11-03 22:04:34 +08:00
parent 95ae6510e2
commit 32bcd82c08
3 changed files with 35 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 175 KiB

After

Width:  |  Height:  |  Size: 446 KiB

View File

@@ -31,11 +31,14 @@ export function ComparisonChart({ traders }: ComparisonChartProps) {
const { data: allTraderHistories, isLoading } = useSWR(
traders.length > 0 ? `all-equity-histories-${tradersKey}` : null,
async () => {
// 并发请求所有trader的历史数据
const promises = traders.map(trader =>
api.getEquityHistory(trader.trader_id)
);
return Promise.all(promises);
// 使用批量API一次性获取所有trader的历史数据
const traderIds = traders.map(trader => trader.trader_id);
const batchData = await api.getEquityHistoryBatch(traderIds);
// 转换为原格式,保持与原有代码兼容
return traders.map(trader => {
return batchData.histories[trader.trader_id] || [];
});
},
{
refreshInterval: 30000, // 30秒刷新对比图表数据更新频率较低

View File

@@ -247,6 +247,33 @@ export const api = {
return res.json();
},
// 批量获取多个交易员的历史数据(无需认证)
async getEquityHistoryBatch(traderIds: string[]): Promise<any> {
const res = await fetch(`${API_BASE}/equity-history-batch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ trader_ids: traderIds }),
});
if (!res.ok) throw new Error('获取批量历史数据失败');
return res.json();
},
// 获取前10名交易员数据无需认证
async getTopTraders(): Promise<any[]> {
const res = await fetch(`${API_BASE}/top-traders`);
if (!res.ok) throw new Error('获取前10名交易员失败');
return res.json();
},
// 获取公开交易员配置(无需认证)
async getPublicTraderConfig(traderId: string): Promise<any> {
const res = await fetch(`${API_BASE}/trader/${traderId}/config`);
if (!res.ok) throw new Error('获取公开交易员配置失败');
return res.json();
},
// 获取AI学习表现分析支持trader_id
async getPerformance(traderId?: string): Promise<any> {
const url = traderId