Files
monkey-ai-web/src/components/analytics/DailyStatsTable.jsx
T
zhanjunhao c492e2b7c0 fix(*): 全量 UI 审查与 undefined 展示异常修复
分析模块:补齐看板、排行、趋势及报表数值兜底
企业与用户中心:修复统计、成员、技能及资金字段展示
充值与交易:修复充值、发票、订单、订阅及支付字段拼接
平台页面:修复渠道、模型、服务监控与状态空值展示
通用能力:新增安全展示工具,补齐九语言文案与回归测试
文档记录:更新修复范围、影响及回滚说明
2026-11-20 10:18:17 +08:00

218 lines
5.9 KiB
React

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React from 'react';
import { Card, Table, Button } from '@/components/ui';
import { IconDownload } from '@/components/ui/icons';
import { renderQuota } from '../../helpers';
import { formatNumber, toFiniteNumber } from '../../helpers/safeDisplay';
const DailyStatsTable = ({ data, title, icon, t, embedded = false }) => {
const columns = [
{
title: t('日期'),
dataIndex: 'date',
key: 'date',
width: 120,
fixed: 'left',
render: (text) => <span className='font-medium'>{text}</span>,
},
{
title: t('新增用户'),
dataIndex: 'new_users',
key: 'new_users',
width: 100,
align: 'right',
render: (val) => (
<span className='text-green-600'>{(val || 0).toLocaleString()}</span>
),
},
{
title: t('活跃用户'),
dataIndex: 'active_users',
key: 'active_users',
width: 100,
align: 'right',
render: (val) => (
<span className='text-blue-600'>{(val || 0).toLocaleString()}</span>
),
},
{
title: t('充值人数'),
dataIndex: 'topup_users',
key: 'topup_users',
width: 100,
align: 'right',
render: (val) => <span>{(val || 0).toLocaleString()}</span>,
},
{
title: t('充值金额'),
dataIndex: 'topup_amount',
key: 'topup_amount',
width: 120,
align: 'right',
render: (val) => (
<span className='text-yellow-600 font-medium'>
¥{formatNumber(val, 2, '0.00')}
</span>
),
},
{
title: t('消耗额度'),
dataIndex: 'consume_quota',
key: 'consume_quota',
width: 120,
align: 'right',
render: (val) => (
<span className='text-brand-deep font-medium'>
{renderQuota(val || 0)}
</span>
),
},
{
title: t('请求次数'),
dataIndex: 'requests',
key: 'requests',
width: 120,
align: 'right',
render: (val) => <span>{(val || 0).toLocaleString()}</span>,
},
{
title: t('Token消耗'),
dataIndex: 'tokens',
key: 'tokens',
width: 120,
align: 'right',
render: (val) => {
const tokens = toFiniteNumber(val) ?? 0;
if (tokens >= 1000000) {
return <span>{(tokens / 1000000).toFixed(2)}M</span>;
} else if (tokens >= 1000) {
return <span>{(tokens / 1000).toFixed(1)}K</span>;
}
return <span>{tokens.toLocaleString()}</span>;
},
},
{
title: t('人均消耗'),
dataIndex: 'avg_consume',
key: 'avg_consume',
width: 120,
align: 'right',
render: (val) => (
<span className='text-gray-600'>{renderQuota(val || 0)}</span>
),
},
];
// 导出CSV
const handleExport = () => {
if (!data || data.length === 0) return;
const headers = [
t('日期'),
t('新增用户'),
t('活跃用户'),
t('充值人数'),
t('充值金额'),
t('消耗额度'),
t('请求次数'),
t('Token消耗'),
t('人均消耗'),
];
const rows = data.map((row) => [
row.date,
row.new_users || 0,
row.active_users || 0,
row.topup_users || 0,
row.topup_amount || 0,
row.consume_quota || 0,
row.requests || 0,
row.tokens || 0,
row.avg_consume || 0,
]);
const csvContent =
'\uFEFF' +
[headers.join(','), ...rows.map((row) => row.join(','))].join('\n');
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `analytics_${new Date().toISOString().split('T')[0]}.csv`;
link.click();
};
return (
<Card
className={`shadow-sm hover:shadow-md transition-shadow ${embedded ? 'analytics-fold-content' : ''}`}
title={
embedded ? undefined : (
<div className='flex items-center justify-between w-full'>
<div className='flex items-center gap-2'>
<span className='text-brand'>{icon}</span>
<span className='font-semibold'>{title}</span>
</div>
<Button
icon={<IconDownload />}
size='small'
onClick={handleExport}
disabled={!data || data.length === 0}
>
{t('导出CSV')}
</Button>
</div>
)
}
styles={{ body: { padding: '0' } }}
>
{embedded && (
<div className='flex justify-end p-4'>
<Button
icon={<IconDownload />}
size='small'
onClick={handleExport}
disabled={!data || data.length === 0}
>
{t('导出CSV')}
</Button>
</div>
)}
<Table
columns={columns}
dataSource={data || []}
pagination={{
pageSize: 10,
showSizeChanger: true,
showTotal: (total) => t('共 {{total}} 条', { total, count: total }),
}}
scroll={{ x: 1100 }}
size='small'
rowKey='date'
empty={
<div className='py-8 text-center text-gray-400'>{t('暂无数据')}</div>
}
/>
</Card>
);
};
export default DailyStatsTable;