Files
PolyHermes/frontend/src/pages/Statistics.tsx
T

161 lines
5.5 KiB
TypeScript
Raw Normal View History

2025-11-21 04:32:08 +08:00
import { useEffect, useState } from 'react'
import { Card, Row, Col, Statistic, message, DatePicker, Space, Button, Typography } from 'antd'
import { ArrowUpOutlined, ArrowDownOutlined, ReloadOutlined } from '@ant-design/icons'
import { useTranslation } from 'react-i18next'
import type { Dayjs } from 'dayjs'
2025-11-21 04:32:08 +08:00
import { apiService } from '../services/api'
import type { Statistics as StatisticsType } from '../types'
import { formatUSDC, formatNumber } from '../utils'
import { useMediaQuery } from 'react-responsive'
const { RangePicker } = DatePicker
const { Title } = Typography
2025-11-21 04:32:08 +08:00
const Statistics: React.FC = () => {
const { t } = useTranslation()
const isMobile = useMediaQuery({ maxWidth: 768 })
2025-11-21 04:32:08 +08:00
const [stats, setStats] = useState<StatisticsType | null>(null)
const [loading, setLoading] = useState(false)
const [dateRange, setDateRange] = useState<[Dayjs | null, Dayjs | null]>([null, null])
2025-11-21 04:32:08 +08:00
useEffect(() => {
fetchStatistics()
}, [])
2025-11-21 04:32:08 +08:00
const fetchStatistics = async () => {
setLoading(true)
try {
const startTime = dateRange[0] ? dateRange[0].valueOf() : undefined
const endTime = dateRange[1] ? dateRange[1].valueOf() : undefined
const response = await apiService.statistics.global({ startTime, endTime })
2025-11-21 04:32:08 +08:00
if (response.data.code === 0 && response.data.data) {
setStats(response.data.data)
} else {
message.error(response.data.msg || t('statistics.fetchFailed') || '获取统计信息失败')
2025-11-21 04:32:08 +08:00
}
} catch (error: any) {
message.error(error.message || t('statistics.fetchFailed') || '获取统计信息失败')
2025-11-21 04:32:08 +08:00
} finally {
setLoading(false)
}
}
const handleDateRangeChange = (dates: [Dayjs | null, Dayjs | null] | null) => {
setDateRange(dates || [null, null])
}
const handleReset = () => {
setDateRange([null, null])
// 重置后自动刷新
setTimeout(() => {
fetchStatistics()
}, 100)
}
2025-11-21 04:32:08 +08:00
return (
<div>
<div style={{ marginBottom: '16px', display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: '12px' }}>
<Title level={2} style={{ margin: 0 }}>{t('statistics.title') || '统计信息'}</Title>
<Space size="middle" wrap>
<RangePicker
value={dateRange}
onChange={handleDateRangeChange}
format="YYYY-MM-DD"
placeholder={[t('statistics.startDate') || '开始日期', t('statistics.endDate') || '结束日期']}
size={isMobile ? 'middle' : 'large'}
allowClear
/>
<Button
type="primary"
icon={<ReloadOutlined />}
onClick={fetchStatistics}
loading={loading}
size={isMobile ? 'middle' : 'large'}
>
{t('statistics.refresh') || '刷新'}
</Button>
{(dateRange[0] || dateRange[1]) && (
<Button
onClick={handleReset}
size={isMobile ? 'middle' : 'large'}
>
{t('statistics.reset') || '重置'}
</Button>
)}
</Space>
2025-11-21 04:32:08 +08:00
</div>
2025-11-21 04:32:08 +08:00
<Row gutter={[16, 16]}>
<Col xs={24} sm={12} md={8}>
<Card>
<Statistic
title={t('statistics.totalOrders') || '总订单数'}
value={formatNumber(stats?.totalOrders || 0)}
2025-11-21 04:32:08 +08:00
loading={loading}
/>
</Card>
</Col>
<Col xs={24} sm={12} md={8}>
<Card>
<Statistic
title={t('statistics.totalPnl') || '总盈亏'}
value={formatUSDC(stats?.totalPnl || '0')}
prefix={<>{stats?.totalPnl && parseFloat(stats.totalPnl) >= 0 ? <ArrowUpOutlined /> : <ArrowDownOutlined />} $</>}
2025-11-21 04:32:08 +08:00
valueStyle={{ color: stats?.totalPnl && parseFloat(stats.totalPnl || '0') >= 0 ? '#3f8600' : '#cf1322' }}
loading={loading}
/>
</Card>
</Col>
<Col xs={24} sm={12} md={8}>
<Card>
<Statistic
title={t('statistics.winRate') || '胜率'}
2025-11-21 04:32:08 +08:00
value={stats?.winRate || '0'}
precision={2}
suffix="%"
loading={loading}
/>
</Card>
</Col>
<Col xs={24} sm={12} md={8}>
<Card>
<Statistic
title={t('statistics.avgPnl') || '平均盈亏'}
value={formatUSDC(stats?.avgPnl || '0')}
prefix={<>{stats?.avgPnl && parseFloat(stats.avgPnl || '0') >= 0 ? <ArrowUpOutlined /> : <ArrowDownOutlined />} $</>}
valueStyle={{ color: stats?.avgPnl && parseFloat(stats.avgPnl || '0') >= 0 ? '#3f8600' : '#cf1322' }}
2025-11-21 04:32:08 +08:00
loading={loading}
/>
</Card>
</Col>
<Col xs={24} sm={12} md={8}>
<Card>
<Statistic
title={t('statistics.maxProfit') || '最大盈利'}
value={formatUSDC(stats?.maxProfit || '0')}
prefix={<><ArrowUpOutlined /> $</>}
2025-11-21 04:32:08 +08:00
valueStyle={{ color: '#3f8600' }}
loading={loading}
/>
</Card>
</Col>
<Col xs={24} sm={12} md={8}>
<Card>
<Statistic
title={t('statistics.maxLoss') || '最大亏损'}
value={formatUSDC(stats?.maxLoss || '0')}
prefix={<><ArrowDownOutlined /> $</>}
2025-11-21 04:32:08 +08:00
valueStyle={{ color: '#cf1322' }}
loading={loading}
/>
</Card>
</Col>
</Row>
</div>
)
}
export default Statistics