543 lines
17 KiB
React
543 lines
17 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 TableEmpty from '../../common/table/TableEmpty';
|
|
import React, { useMemo, useState } from 'react';
|
|
import AntTable from '../../ui/CenterActionTable';
|
|
import { Button as AntButton } from 'antd';
|
|
import { Modal, Typography } from '@/components/ui';
|
|
import {
|
|
Coins,
|
|
Cpu,
|
|
Gauge,
|
|
Layers3,
|
|
ReceiptText,
|
|
RadioTower,
|
|
Sparkles,
|
|
} from 'lucide-react';
|
|
import CardTable from '../../common/ui/CardTable';
|
|
|
|
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', plain }) => {
|
|
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={
|
|
plain
|
|
? 'usage-billing-tile'
|
|
: '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, plain }) => {
|
|
if (value === undefined || value === null || value === '' || value === '-')
|
|
return null;
|
|
return (
|
|
<span
|
|
className={
|
|
plain
|
|
? 'usage-billing-pill'
|
|
: '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,
|
|
usageRecordMode = false,
|
|
t,
|
|
}) => {
|
|
const plain = usageRecordMode;
|
|
const other = getLogOther(record.other) || {};
|
|
const inputTokens = Number(record.prompt_tokens || 0);
|
|
const outputTokens = Number(record.completion_tokens || 0);
|
|
const textTokens = inputTokens + outputTokens;
|
|
// 缓存创建按上游实际返回分三桶:明确的 5m / 1h,以及上游只给了总量没给分档的「未分档」。
|
|
// 三桶按各自倍率结算,所以三桶都得露出来——少露一桶,用户拿界面上的 token × 倍率就加不出账单金额。
|
|
const hasSplitCacheCreation =
|
|
other.cache_creation_tokens_5m > 0 || other.cache_creation_tokens_1h > 0;
|
|
// 未分档桶只在与分档并存时单独成行;没有分档时,下面的「缓存创建 / 缓存创建倍率」
|
|
// 本身说的就是这一桶,再列一遍是重复
|
|
const unlabeledCacheCreation = Number(
|
|
other.cache_creation_tokens_unlabeled || 0,
|
|
);
|
|
const showUnlabeledCacheCreation =
|
|
hasSplitCacheCreation && unlabeledCacheCreation > 0;
|
|
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),
|
|
},
|
|
!hasSplitCacheCreation &&
|
|
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),
|
|
},
|
|
showUnlabeledCacheCreation && {
|
|
label: t('未分档缓存创建'),
|
|
value: formatCount(unlabeledCacheCreation),
|
|
},
|
|
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 &&
|
|
other.cache_tokens > 0 && {
|
|
// cache_ratio 是缓存命中(读)的倍率,与缓存创建(写)无关。
|
|
// 过去这里的闸门把写入 token 也算进来,于是只写不读的请求也会挂一个
|
|
// 没有对应 token 的「缓存倍率 0.1x」,看着像漏算了一笔钱。
|
|
label: t('缓存倍率'),
|
|
value: formatRatio(other.cache_ratio),
|
|
},
|
|
!hasSplitCacheCreation &&
|
|
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),
|
|
},
|
|
showUnlabeledCacheCreation &&
|
|
other.cache_creation_ratio_unlabeled != null && {
|
|
label: t('未分档缓存倍率'),
|
|
value: formatRatio(other.cache_creation_ratio_unlabeled),
|
|
},
|
|
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: t('{{n}} 次', {
|
|
n: formatCount(other.web_search_call_count || 0),
|
|
count: other.web_search_call_count || 0,
|
|
}),
|
|
},
|
|
other.file_search && {
|
|
label: t('文件搜索'),
|
|
value: t('{{n}} 次', {
|
|
n: formatCount(other.file_search_call_count || 0),
|
|
count: other.file_search_call_count || 0,
|
|
}),
|
|
},
|
|
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}
|
|
plain={plain}
|
|
/>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className='usage-billing-detail'>
|
|
<div className='mb-3 flex flex-wrap items-center justify-between gap-2'>
|
|
<div>
|
|
<div className='usage-billing-heading flex items-center text-sm font-semibold text-gray-900 dark:text-white'>
|
|
<Sparkles size={15} className='text-brand' />
|
|
<span>{t('计费摘要')}</span>
|
|
</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} plain={plain} />
|
|
))}
|
|
</div>
|
|
|
|
<div
|
|
className={
|
|
plain
|
|
? 'usage-billing-factors'
|
|
: '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='usage-billing-heading mb-2 flex items-center text-xs font-medium uppercase tracking-[0.18em] text-gray-400'>
|
|
<Layers3 size={13} />
|
|
<span>{t('成本因子')}</span>
|
|
</div>
|
|
<div className='flex flex-wrap gap-2'>
|
|
{details.map((item) => (
|
|
<DetailPill key={item.label} {...item} plain={plain} />
|
|
))}
|
|
{mappedModel && (
|
|
<DetailPill
|
|
label={t('实际模型')}
|
|
value={other.upstream_model_name}
|
|
plain={plain}
|
|
/>
|
|
)}
|
|
{billingMode && (
|
|
<DetailPill label={t('计费源')} value={billingMode} plain={plain} />
|
|
)}
|
|
</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,
|
|
usageRecordMode = false,
|
|
} = logsData;
|
|
const [detailRecord, setDetailRecord] = useState(null);
|
|
const TableComponent = usageRecordMode ? AntTable : CardTable;
|
|
|
|
// Get all columns
|
|
const allColumns = useMemo(() => {
|
|
return getLogsColumns({
|
|
t,
|
|
COLUMN_KEYS,
|
|
copyText,
|
|
showUserInfoFunc,
|
|
isAdminUser,
|
|
isRootUser,
|
|
usageRecordMode,
|
|
});
|
|
}, [
|
|
t,
|
|
COLUMN_KEYS,
|
|
copyText,
|
|
showUserInfoFunc,
|
|
isAdminUser,
|
|
isRootUser,
|
|
usageRecordMode,
|
|
]);
|
|
|
|
const visibleColumnsList = useMemo(
|
|
() => allColumns.filter((column) => visibleColumns[column.key]),
|
|
[visibleColumns, allColumns],
|
|
);
|
|
|
|
const tableColumns = useMemo(() => {
|
|
const baseColumns = compactMode
|
|
? visibleColumnsList.map(({ fixed, ...rest }) => rest)
|
|
: visibleColumnsList;
|
|
if (!hasExpandableRows()) return baseColumns;
|
|
return [
|
|
...baseColumns,
|
|
{
|
|
title: usageRecordMode ? t('操作') : t('计费详情'),
|
|
key: 'billingDetailAction',
|
|
fixed: compactMode || usageRecordMode ? undefined : 'right',
|
|
width: 120,
|
|
render: (_, record) => {
|
|
const canView =
|
|
expandData[record.key] && expandData[record.key].length > 0;
|
|
if (usageRecordMode) {
|
|
return (
|
|
<AntButton
|
|
type='link'
|
|
size='small'
|
|
className='usage-record-view'
|
|
disabled={!canView}
|
|
onClick={(event) => {
|
|
event.stopPropagation();
|
|
if (canView) setDetailRecord(record);
|
|
}}
|
|
>
|
|
{t('详情')}
|
|
</AntButton>
|
|
);
|
|
}
|
|
return (
|
|
<AntButton
|
|
size='small'
|
|
type='link'
|
|
disabled={!canView}
|
|
onClick={(e) => {
|
|
e?.stopPropagation?.();
|
|
if (canView) setDetailRecord(record);
|
|
}}
|
|
>
|
|
{t('详情')}
|
|
</AntButton>
|
|
);
|
|
},
|
|
},
|
|
];
|
|
}, [
|
|
compactMode,
|
|
visibleColumnsList,
|
|
expandData,
|
|
hasExpandableRows,
|
|
t,
|
|
usageRecordMode,
|
|
]);
|
|
|
|
return (
|
|
<>
|
|
<TableComponent
|
|
columns={tableColumns}
|
|
dataSource={logs}
|
|
rowKey='key'
|
|
loading={loading}
|
|
scroll={compactMode ? undefined : { x: 'max-content' }}
|
|
className={
|
|
usageRecordMode
|
|
? 'usage-logs-table usage-record-table'
|
|
: 'rounded-xl overflow-hidden usage-logs-table'
|
|
}
|
|
size='middle'
|
|
{...(usageRecordMode
|
|
? {
|
|
tableLayout: 'fixed',
|
|
locale: {
|
|
emptyText: <TableEmpty />,
|
|
},
|
|
}
|
|
: {
|
|
empty: <TableEmpty />,
|
|
})}
|
|
pagination={
|
|
usageRecordMode
|
|
? false
|
|
: {
|
|
currentPage: activePage,
|
|
pageSize: pageSize,
|
|
total: logCount,
|
|
pageSizeOptions: [10, 20, 50, 100],
|
|
showSizeChanger: true,
|
|
onPageSizeChange: (size) => {
|
|
handlePageSizeChange(size);
|
|
},
|
|
onPageChange: handlePageChange,
|
|
}
|
|
}
|
|
{...(!usageRecordMode && { hidePagination: true })}
|
|
/>
|
|
<Modal
|
|
visible={!!detailRecord}
|
|
onCancel={() => setDetailRecord(null)}
|
|
footer={null}
|
|
centered
|
|
width={860}
|
|
title={
|
|
usageRecordMode ? (
|
|
t('计费详情')
|
|
) : (
|
|
<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}
|
|
usageRecordMode={usageRecordMode}
|
|
t={t}
|
|
/>
|
|
)}
|
|
</Modal>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default LogsTable;
|