Files
monkey-ai-web/src/components/table/models/ModelsCategoryStats.jsx
T
zhanjunhao cb9d836227 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

166 lines
4.6 KiB
React

import { useEffectEvent } from '../../../hooks/common/useEffectEvent';
import React, { useState, useEffect } from 'react';
import { Card, Row, Col, Typography, Tag, Spin, Empty } from '@/components/ui';
import { API, showError } from '../../../helpers';
import { MODEL_CATEGORY_NAMES } from '../../../constants';
const { Title, Text } = Typography;
const ModelsCategoryStats = ({ t }) => {
const [stats, setStats] = useState({});
const [loading, setLoading] = useState(false);
const loadStats = async () => {
setLoading(true);
try {
const res = await API.get('/api/models/category-stats');
const { success, message, data } = res.data;
if (success) {
setStats(data || {});
} else {
showError(message || t('获取统计数据失败'));
}
} catch (error) {
console.error('Load category stats failed:', error);
showError(t('获取统计数据失败'));
}
setLoading(false);
};
const onMount = useEffectEvent(() => {
loadStats();
});
useEffect(() => onMount(), [onMount]);
const getCategoryColor = (category) => {
const colorMap = {
text_generation: 'blue',
image_generation: 'purple',
video_generation: 'pink',
audio_tts: 'cyan',
audio_stt: 'teal',
audio_generation: 'green',
embedding: 'violet',
multimodal: 'indigo',
rerank: 'orange',
other: 'grey',
};
return colorMap[category] || 'grey';
};
const totalModels = Object.values(stats).reduce(
(sum, count) => sum + count,
0,
);
if (loading) {
return (
<Card style={{ marginBottom: '16px' }}>
<div style={{ textAlign: 'center', padding: '20px' }}>
<Spin />
</div>
</Card>
);
}
if (totalModels === 0) {
return (
<Card style={{ marginBottom: '16px' }}>
<Empty description={t('暂无模型数据')} />
</Card>
);
}
return (
<Card
title={
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
}}
>
<Title heading={5} style={{ margin: 0 }}>
{t('模型分类统计')}
</Title>
<Tag color='blue' size='large'>
{t('总计')}: {totalModels}
</Tag>
</div>
}
style={{ marginBottom: '16px' }}
>
<Row gutter={[16, 16]}>
{Object.entries(MODEL_CATEGORY_NAMES).map(([category, name]) => {
const count = stats[category] || 0;
const percentage =
totalModels > 0 ? ((count / totalModels) * 100).toFixed(1) : 0;
return (
<Col key={category} xs={24} sm={12} md={8} lg={6} xl={4}>
<div
style={{
padding: '12px',
borderRadius: '8px',
background: 'var(--ui-color-fill-0)',
border: '1px solid var(--ui-color-border)',
transition: 'all 0.3s',
cursor: 'pointer',
}}
onMouseEnter={(e) => {
e.currentTarget.style.transform = 'translateY(-2px)';
e.currentTarget.style.boxShadow =
'0 4px 12px rgba(0,0,0,0.1)';
}}
onMouseLeave={(e) => {
e.currentTarget.style.transform = 'translateY(0)';
e.currentTarget.style.boxShadow = 'none';
}}
>
<div
style={{
display: 'flex',
alignItems: 'center',
marginBottom: '8px',
}}
>
<Tag
color={getCategoryColor(category)}
size='small'
style={{ marginRight: '8px' }}
>
{name}
</Tag>
</div>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'baseline',
}}
>
<Text
strong
style={{
fontSize: '24px',
color: 'var(--ui-color-text-0)',
}}
>
{count}
</Text>
<Text type='tertiary' size='small'>
{percentage}%
</Text>
</div>
</div>
</Col>
);
})}
</Row>
</Card>
);
};
export default ModelsCategoryStats;