/* 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 . 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 (
{label}
{value}
); }; const DetailPill = ({ label, value, plain }) => { if (value === undefined || value === null || value === '' || value === '-') return null; return ( {label} {value} ); }; 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 (
{expandItems.map((item) => ( ))}
); } return (
{t('计费摘要')}
{t('保留关键成本因子,省略冗长计算公式')}
{renderQuota(record.quota, 6)}
{metrics.map((item) => ( ))}
{t('成本因子')}
{details.map((item) => ( ))} {mappedModel && ( )} {billingMode && ( )}
); }; 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 ( { event.stopPropagation(); if (canView) setDetailRecord(record); }} > {t('详情')} ); } return ( { e?.stopPropagation?.(); if (canView) setDetailRecord(record); }} > {t('详情')} ); }, }, ]; }, [ compactMode, visibleColumnsList, expandData, hasExpandableRows, t, usageRecordMode, ]); return ( <> , }, } : { empty: , })} 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 })} /> setDetailRecord(null)} footer={null} centered width={860} title={ usageRecordMode ? ( t('计费详情') ) : (
{t('计费详情')}
) } bodyStyle={{ padding: 20 }} > {detailRecord && ( )}
); }; export default LogsTable;