Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
@@ -0,0 +1,468 @@
/*
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, { useMemo, useState } from 'react';
import { Button, Empty, Modal, Typography } from '@douyinfe/semi-ui';
import {
Coins,
Cpu,
Eye,
Gauge,
Layers3,
ReceiptText,
RadioTower,
Sparkles,
} from 'lucide-react';
import CardTable from '../../common/ui/CardTable';
import {
IllustrationNoResult,
IllustrationNoResultDark,
} from '../../common/illustrations';
import { getLogOther, renderQuota } from '../../../helpers';
import { getLogsColumns } from './UsageLogsColumnDefs';
const { Text } = Typography;
const formatCount = (value) => {
const num = Number(value || 0);
if (!Number.isFinite(num)) return '-';
return num.toLocaleString();
};
const formatRatio = (value) => {
const num = Number(value);
if (!Number.isFinite(num)) return '-';
return `${Number.isInteger(num) ? num : num.toFixed(3).replace(/0+$/, '').replace(/\.$/, '')}x`;
};
const MetricCard = ({ icon: Icon, label, value, tone = 'brand' }) => {
const toneClass = {
brand:
'bg-brand/10 text-brand-deep border-brand/20 dark:bg-brand/15 dark:text-brand-from',
emerald:
'bg-emerald-50 text-emerald-700 border-emerald-200/70 dark:bg-emerald-900/[0.18] dark:text-emerald-300 dark:border-emerald-700/30',
cyan: 'bg-cyan-50 text-cyan-700 border-cyan-200/70 dark:bg-cyan-900/[0.18] dark:text-cyan-300 dark:border-cyan-700/30',
amber:
'bg-amber-50 text-amber-700 border-amber-200/70 dark:bg-amber-900/[0.18] dark:text-amber-300 dark:border-amber-700/30',
}[tone];
return (
<div className='rounded-2xl border border-white/55 bg-white/[0.45] p-3 backdrop-blur-xl dark:border-white/10 dark:bg-white/[0.045]'>
<div className='mb-2 flex items-center gap-2'>
<span
className={`inline-flex h-7 w-7 items-center justify-center rounded-xl border ${toneClass}`}
>
<Icon size={14} />
</span>
<Text type='tertiary' size='small'>
{label}
</Text>
</div>
<div className='truncate text-base font-semibold text-gray-900 dark:text-white'>
{value}
</div>
</div>
);
};
const DetailPill = ({ label, value }) => {
if (value === undefined || value === null || value === '' || value === '-')
return null;
return (
<span className='inline-flex items-center gap-1 rounded-full border border-white/55 bg-white/[0.45] px-2.5 py-1 text-xs text-gray-600 backdrop-blur dark:border-white/10 dark:bg-white/[0.055] dark:text-gray-300'>
<span className='text-gray-400 dark:text-gray-500'>{label}</span>
<strong className='font-semibold text-gray-800 dark:text-gray-100'>
{value}
</strong>
</span>
);
};
const UsageBillingDetail = ({ record, expandItems, isAdminUser, t }) => {
const other = getLogOther(record.other) || {};
const inputTokens = Number(record.prompt_tokens || 0);
const outputTokens = Number(record.completion_tokens || 0);
const cacheTokens =
Number(other.cache_tokens || 0) +
Number(other.cache_creation_tokens || 0) +
Number(other.cache_creation_tokens_5m || 0) +
Number(other.cache_creation_tokens_1h || 0);
const textTokens = inputTokens + outputTokens;
const effectiveGroupRatio =
Number.isFinite(Number(other.user_group_ratio)) &&
Number(other.user_group_ratio) !== -1
? other.user_group_ratio
: other.group_ratio;
const details = [
{ label: t('输入'), value: formatCount(inputTokens) },
outputTokens > 0 && { label: t('输出'), value: formatCount(outputTokens) },
other.cache_tokens > 0 && {
label: t('缓存命中'),
value: formatCount(other.cache_tokens),
},
other.cache_creation_tokens > 0 && {
label: t('缓存创建'),
value: formatCount(other.cache_creation_tokens),
},
other.cache_creation_tokens_5m > 0 && {
label: t('5m 缓存创建'),
value: formatCount(other.cache_creation_tokens_5m),
},
other.cache_creation_tokens_1h > 0 && {
label: t('1h 缓存创建'),
value: formatCount(other.cache_creation_tokens_1h),
},
other.model_ratio != null && {
label: t('模型倍率'),
value: formatRatio(other.model_ratio),
},
other.completion_ratio != null && {
label: t('输出倍率'),
value: formatRatio(other.completion_ratio),
},
other.cache_ratio != null &&
cacheTokens > 0 && {
label: t('缓存倍率'),
value: formatRatio(other.cache_ratio),
},
other.cache_creation_ratio != null &&
other.cache_creation_tokens > 0 && {
label: t('缓存创建倍率'),
value: formatRatio(other.cache_creation_ratio),
},
other.cache_creation_ratio_5m != null &&
other.cache_creation_tokens_5m > 0 && {
label: t('5m 缓存倍率'),
value: formatRatio(other.cache_creation_ratio_5m),
},
other.cache_creation_ratio_1h != null &&
other.cache_creation_tokens_1h > 0 && {
label: t('1h 缓存倍率'),
value: formatRatio(other.cache_creation_ratio_1h),
},
effectiveGroupRatio != null && {
label: t('分组倍率'),
value: formatRatio(effectiveGroupRatio),
},
other.audio_input > 0 && {
label: t('音频输入'),
value: formatCount(other.audio_input),
},
other.audio_output > 0 && {
label: t('音频输出'),
value: formatCount(other.audio_output),
},
other.audio_input_token_count > 0 && {
label: t('音频计费 Tokens'),
value: formatCount(other.audio_input_token_count),
},
other.audio_ratio != null &&
(other.audio_input > 0 || other.audio_input_token_count > 0) && {
label: t('音频倍率'),
value: formatRatio(other.audio_ratio),
},
other.audio_completion_ratio != null &&
other.audio_output > 0 && {
label: t('音频输出倍率'),
value: formatRatio(other.audio_completion_ratio),
},
other.image &&
other.image_output > 0 && {
label: t('图片 Tokens'),
value: formatCount(other.image_output),
},
other.web_search && {
label: t('联网搜索'),
value: `${formatCount(other.web_search_call_count || 0)} ${t('次')}`,
},
other.file_search && {
label: t('文件搜索'),
value: `${formatCount(other.file_search_call_count || 0)} ${t('次')}`,
},
other.image_generation_call && {
label: t('图片生成'),
value: t('已计费'),
},
].filter(Boolean);
const mappedModel =
other?.is_model_mapped &&
other?.upstream_model_name &&
other.upstream_model_name !== record.model_name;
const billingMode =
isAdminUser && other?.admin_info
? other?.admin_info?.local_count_tokens
? t('本地计费')
: t('上游返回')
: '';
const metrics = [
{
icon: Coins,
label: t('本次花费'),
value: renderQuota(record.quota, 6),
tone: 'brand',
},
{
icon: Gauge,
label: t('文本 Tokens'),
value: formatCount(textTokens),
tone: 'emerald',
},
{
icon: Cpu,
label: mappedModel ? t('计费模型') : t('模型'),
value: record.model_name || '-',
tone: 'cyan',
},
isAdminUser && {
icon: RadioTower,
label: t('渠道'),
value: record.channel
? `${record.channel}${record.channel_name ? ` · ${record.channel_name}` : ''}`
: '-',
tone: 'amber',
},
].filter(Boolean);
if (record.type !== 2) {
return (
<div className='usage-billing-detail'>
<div className='grid gap-2 sm:grid-cols-2 lg:grid-cols-3'>
{expandItems.map((item) => (
<MetricCard
key={item.key}
icon={Sparkles}
label={item.key}
value={item.value}
/>
))}
</div>
</div>
);
}
return (
<div className='usage-billing-detail'>
<div className='mb-3 flex flex-wrap items-center justify-between gap-2'>
<div>
<div className='flex items-center gap-2 text-sm font-semibold text-gray-900 dark:text-white'>
<Sparkles size={15} className='text-brand' />
{t('计费摘要')}
</div>
<Text type='tertiary' size='small'>
{t('保留关键成本因子,省略冗长计算公式')}
</Text>
</div>
<div className='rounded-full border border-brand/20 bg-brand/10 px-3 py-1 text-sm font-semibold text-brand-deep dark:bg-brand/15 dark:text-brand-from'>
{renderQuota(record.quota, 6)}
</div>
</div>
<div className='grid gap-3 sm:grid-cols-2 xl:grid-cols-4'>
{metrics.map((item) => (
<MetricCard key={item.label} {...item} />
))}
</div>
<div className='mt-3 rounded-2xl border border-white/55 bg-white/[0.35] p-3 backdrop-blur-xl dark:border-white/10 dark:bg-white/[0.04]'>
<div className='mb-2 flex items-center gap-2 text-xs font-medium uppercase tracking-[0.18em] text-gray-400'>
<Layers3 size={13} />
{t('成本因子')}
</div>
<div className='flex flex-wrap gap-2'>
{details.map((item) => (
<DetailPill key={item.label} {...item} />
))}
{mappedModel && (
<DetailPill
label={t('实际模型')}
value={other.upstream_model_name}
/>
)}
{billingMode && (
<DetailPill label={t('计费源')} value={billingMode} />
)}
</div>
</div>
</div>
);
};
const LogsTable = (logsData) => {
const {
logs,
expandData,
loading,
activePage,
pageSize,
logCount,
compactMode,
visibleColumns,
handlePageChange,
handlePageSizeChange,
copyText,
showUserInfoFunc,
hasExpandableRows,
isAdminUser,
isRootUser,
t,
COLUMN_KEYS,
} = logsData;
const [detailRecord, setDetailRecord] = useState(null);
// Get all columns
const allColumns = useMemo(() => {
return getLogsColumns({
t,
COLUMN_KEYS,
copyText,
showUserInfoFunc,
isAdminUser,
isRootUser,
});
}, [t, COLUMN_KEYS, copyText, showUserInfoFunc, isAdminUser, isRootUser]);
// Filter columns based on visibility settings
const getVisibleColumns = () => {
return allColumns.filter((column) => visibleColumns[column.key]);
};
const visibleColumnsList = useMemo(() => {
return getVisibleColumns();
}, [visibleColumns, allColumns]);
const tableColumns = useMemo(() => {
const baseColumns = compactMode
? visibleColumnsList.map(({ fixed, ...rest }) => rest)
: visibleColumnsList;
if (!hasExpandableRows()) return baseColumns;
return [
...baseColumns,
{
title: t('计费详情'),
key: 'billingDetailAction',
fixed: compactMode ? undefined : 'right',
width: 120,
render: (_, record) => {
const canView =
expandData[record.key] && expandData[record.key].length > 0;
return (
<Button
size='small'
theme='light'
type='primary'
disabled={!canView}
icon={<Eye size={14} />}
onClick={(e) => {
e?.stopPropagation?.();
if (canView) setDetailRecord(record);
}}
className='usage-record-action-button usage-record-action-button-soft !rounded-full'
>
{t('查看')}
</Button>
);
},
},
];
}, [
compactMode,
visibleColumnsList,
expandData,
hasExpandableRows,
t,
]);
return (
<>
<CardTable
columns={tableColumns}
dataSource={logs}
rowKey='key'
loading={loading}
scroll={compactMode ? undefined : { x: 'max-content' }}
className='rounded-xl overflow-hidden usage-logs-table'
size='middle'
empty={
<div className='py-12'>
<Empty
image={
<IllustrationNoResult style={{ width: 180, height: 180 }} />
}
darkModeImage={
<IllustrationNoResultDark style={{ width: 180, height: 180 }} />
}
description={
<div className='flex flex-col items-center gap-2'>
<span className='text-gray-500 dark:text-gray-400 text-sm'>
{t('搜索无结果')}
</span>
<span className='text-gray-400 dark:text-gray-500 text-xs'>
{t('尝试调整筛选条件')}
</span>
</div>
}
/>
</div>
}
pagination={{
currentPage: activePage,
pageSize: pageSize,
total: logCount,
pageSizeOptions: [10, 20, 50, 100],
showSizeChanger: true,
onPageSizeChange: (size) => {
handlePageSizeChange(size);
},
onPageChange: handlePageChange,
}}
hidePagination={true}
/>
<Modal
visible={!!detailRecord}
onCancel={() => setDetailRecord(null)}
footer={null}
centered
width={860}
title={
<div className='flex items-center gap-2'>
<span className='inline-flex h-9 w-9 items-center justify-center rounded-2xl bg-brand/10 text-brand-deep dark:bg-brand/15 dark:text-brand-from'>
<ReceiptText size={18} />
</span>
<span>{t('计费详情')}</span>
</div>
}
bodyStyle={{ padding: 20 }}
>
{detailRecord && (
<UsageBillingDetail
record={detailRecord}
expandItems={expandData[detailRecord.key] || []}
isAdminUser={isAdminUser}
t={t}
/>
)}
</Modal>
</>
);
};
export default LogsTable;