Files
monkey-ai-web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx
T

893 lines
25 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 { Tag as AntTag } from 'antd';
import {
Avatar,
Space,
Tag,
Tooltip,
Popover,
Typography,
} from '@/components/ui';
import {
renderGroup,
renderQuota,
stringToColor,
getLogOther,
renderModelTag,
renderModelPriceSimple,
hoverOrClickTrigger,
} from '../../../helpers';
import { IconHelpCircle } from '@/components/ui/icons';
import { Route } from 'lucide-react';
const colors = [
'amber',
'blue',
'cyan',
'green',
'grey',
'indigo',
'light-blue',
'lime',
'orange',
'pink',
'purple',
'red',
'teal',
'violet',
'yellow',
];
// 类型配置映射
const TYPE_CONFIG = {
1: {
label: '充值',
gradient: 'from-cyan-500 to-teal-500',
bgLight: 'bg-cyan-50',
bgDark: 'dark:bg-cyan-950/30',
textLight: 'text-cyan-700',
textDark: 'dark:text-cyan-300',
},
2: {
label: '消费',
gradient: 'from-emerald-500 to-green-500',
bgLight: 'bg-emerald-50',
bgDark: 'dark:bg-emerald-950/30',
textLight: 'text-emerald-700',
textDark: 'dark:text-emerald-300',
},
3: {
label: '管理',
gradient: 'from-amber-500 to-orange-500',
bgLight: 'bg-amber-50',
bgDark: 'dark:bg-amber-950/30',
textLight: 'text-amber-700',
textDark: 'dark:text-amber-300',
},
4: {
label: '系统',
gradient: 'from-brand-from to-brand-to',
bgLight: 'bg-brand/5',
bgDark: 'dark:bg-brand-deep/30',
textLight: 'text-brand-deep',
textDark: 'dark:text-brand-from',
},
5: {
label: '错误',
gradient: 'from-rose-500 to-red-500',
bgLight: 'bg-rose-50',
bgDark: 'dark:bg-rose-950/30',
textLight: 'text-rose-700',
textDark: 'dark:text-rose-300',
},
default: {
label: '未知',
gradient: 'from-gray-400 to-gray-500',
bgLight: 'bg-gray-50',
bgDark: 'dark:bg-gray-900/30',
textLight: 'text-gray-600',
textDark: 'dark:text-gray-400',
},
};
// Render functions
function renderType(type, t) {
const config = TYPE_CONFIG[type] || TYPE_CONFIG.default;
return (
<span
className={`
inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-medium
${config.bgLight} ${config.bgDark} ${config.textLight} ${config.textDark}
border border-current/10
transition-all duration-200 hover:scale-105
`}
>
<span
className={`w-1.5 h-1.5 rounded-full bg-gradient-to-r ${config.gradient}`}
/>
{t(config.label)}
</span>
);
}
function renderIsStream(bool, t) {
if (bool) {
return (
<span className='inline-flex items-center gap-1 px-2 py-0.5 rounded-md text-xs font-medium bg-blue-50 dark:bg-blue-950/30 text-blue-600 dark:text-blue-400 border border-blue-200/50 dark:border-blue-800/50'>
<span className='w-1 h-1 rounded-full bg-blue-500 animate-pulse' />
{t('流')}
</span>
);
} else {
return (
<span className='inline-flex items-center px-2 py-0.5 rounded-md text-xs font-medium bg-gray-50 dark:bg-gray-900/30 text-gray-500 dark:text-gray-400 border border-gray-200/50 dark:border-gray-700/50'>
{t('非流')}
</span>
);
}
}
function renderUseTime(type, t) {
const time = parseInt(type);
let colorConfig;
if (time < 101) {
colorConfig = {
bg: 'bg-emerald-50 dark:bg-emerald-950/30',
text: 'text-emerald-600 dark:text-emerald-400',
border: 'border-emerald-200/50 dark:border-emerald-800/50',
dot: 'bg-emerald-500',
};
} else if (time < 300) {
colorConfig = {
bg: 'bg-amber-50 dark:bg-amber-950/30',
text: 'text-amber-600 dark:text-amber-400',
border: 'border-amber-200/50 dark:border-amber-800/50',
dot: 'bg-amber-500',
};
} else {
colorConfig = {
bg: 'bg-rose-50 dark:bg-rose-950/30',
text: 'text-rose-600 dark:text-rose-400',
border: 'border-rose-200/50 dark:border-rose-800/50',
dot: 'bg-rose-500',
};
}
return (
<span
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-md text-xs font-medium ${colorConfig.bg} ${colorConfig.text} border ${colorConfig.border}`}
>
{time}s
</span>
);
}
function renderFirstUseTime(type, t) {
let time = parseFloat(type) / 1000.0;
time = time.toFixed(1);
let colorConfig;
if (time < 3) {
colorConfig = {
bg: 'bg-teal-50 dark:bg-teal-950/30',
text: 'text-teal-600 dark:text-teal-400',
border: 'border-teal-200/50 dark:border-teal-800/50',
};
} else if (time < 10) {
colorConfig = {
bg: 'bg-orange-50 dark:bg-orange-950/30',
text: 'text-orange-600 dark:text-orange-400',
border: 'border-orange-200/50 dark:border-orange-800/50',
};
} else {
colorConfig = {
bg: 'bg-red-50 dark:bg-red-950/30',
text: 'text-red-600 dark:text-red-400',
border: 'border-red-200/50 dark:border-red-800/50',
};
}
return (
<span
className={`inline-flex items-center px-2 py-0.5 rounded-md text-xs font-medium ${colorConfig.bg} ${colorConfig.text} border ${colorConfig.border}`}
>
{time}s
</span>
);
}
function renderModelName(record, copyText, t, isRootUser) {
let other = getLogOther(record.other);
let modelMapped =
isRootUser &&
other?.is_model_mapped &&
other?.upstream_model_name &&
other?.upstream_model_name !== '';
if (!modelMapped) {
return renderModelTag(record.model_name, {
onClick: (event) => {
copyText(event, record.model_name).then((r) => {});
},
});
} else {
return (
<>
<Space vertical align={'start'}>
<Popover
// 触屏没有 hover,Semi 默认 trigger='hover' 时这个详情面板永远打不开
trigger={hoverOrClickTrigger()}
content={
<div style={{ padding: 10 }}>
<Space vertical align={'start'}>
<div className='flex items-center'>
<Typography.Text strong style={{ marginRight: 8 }}>
{t('请求并计费模型')}:
</Typography.Text>
{renderModelTag(record.model_name, {
onClick: (event) => {
copyText(event, record.model_name).then((r) => {});
},
})}
</div>
<div className='flex items-center'>
<Typography.Text strong style={{ marginRight: 8 }}>
{t('实际模型')}:
</Typography.Text>
{renderModelTag(other.upstream_model_name, {
onClick: (event) => {
copyText(event, other.upstream_model_name).then(
(r) => {},
);
},
})}
</div>
</Space>
</div>
}
>
{renderModelTag(record.model_name, {
onClick: (event) => {
copyText(event, record.model_name).then((r) => {});
},
suffixIcon: (
<Route
style={{ width: '0.9em', height: '0.9em', opacity: 0.75 }}
/>
),
})}
</Popover>
</Space>
</>
);
}
}
// ---- 使用记录(个人端)呈现:原型为 Ant Design 预设无边框标签与纯文本,不含渐变点、脉冲与悬停缩放 ----
const RECORD_TYPE_COLORS = {
1: 'cyan',
2: 'green',
3: 'orange',
4: 'purple',
5: 'red',
};
function renderRecordTime(text) {
const [date, time] = String(text || '').split(' ');
return (
<div className='usage-record-time'>
<span>{date}</span>
<strong>{time}</strong>
</div>
);
}
function renderRecordType(type, t) {
const config = TYPE_CONFIG[type] || TYPE_CONFIG.default;
return (
<AntTag bordered={false} color={RECORD_TYPE_COLORS[type]}>
{t(config.label)}
</AntTag>
);
}
function renderRecordUseTime(record, t) {
const useTime = `${parseInt(record.use_time)}s`;
const other = getLogOther(record.other);
const firstTime =
record.is_stream && other?.frt
? ` / ${(parseFloat(other.frt) / 1000.0).toFixed(1)}s`
: '';
return (
<span className='usage-record-use-time'>
<span>
{useTime}
{firstTime && <em>{firstTime}</em>}
</span>
<AntTag bordered={false} color={record.is_stream ? 'cyan' : undefined}>
{record.is_stream ? t('流') : t('非流')}
</AntTag>
</span>
);
}
const formatRecordNumber = (value) => {
const number = Number(value);
return Number.isFinite(number) ? number.toLocaleString('en-US') : value;
};
// Both reference images show the same 2097px table at opposite scroll positions.
const RECORD_COLUMN_WIDTHS = {
time: 152,
channel: 85,
username: 120,
token: 120,
group: 148,
type: 110,
model: 225,
use_time: 126,
prompt: 110,
completion: 110,
cost: 136,
ip: 110,
retry: 120,
details: 305,
};
// 仅替换展示,不改变列的显示条件、复制回调与数据来源
const applyUsageRecordPresentation = (
columns,
{ t, COLUMN_KEYS, copyText, showUserInfoFunc, isAdminUser },
) =>
columns.map((baseColumn) => {
const column = {
...baseColumn,
width: RECORD_COLUMN_WIDTHS[baseColumn.key],
fixed: undefined,
className: `usage-record-cell-${baseColumn.key}`,
};
switch (column.key) {
case COLUMN_KEYS.TIME:
return { ...column, render: (text) => renderRecordTime(text) };
case COLUMN_KEYS.USERNAME:
return {
...column,
render: (text, record) =>
isAdminUser ? (
<button
type='button'
className='usage-record-copy-text'
onClick={(event) => {
event.stopPropagation();
showUserInfoFunc(record.user_id);
}}
>
{record.display_name || text}
</button>
) : null,
};
case COLUMN_KEYS.TOKEN:
return {
...column,
title: t('密钥'),
render: (text, record) =>
record.type === 0 || record.type === 2 || record.type === 5 ? (
<button
type='button'
className='usage-record-copy-text'
onClick={(event) => copyText(event, text)}
>
{t(text)}
</button>
) : null,
};
case COLUMN_KEYS.GROUP:
return {
...column,
render: (text, record, index) => {
const node = column.render(text, record, index);
const group =
record.group || getLogOther(record.other)?.group || '';
return group && node?.props?.children ? (
<AntTag bordered={false}>{group}</AntTag>
) : null;
},
};
case COLUMN_KEYS.TYPE:
return { ...column, render: (text) => renderRecordType(text, t) };
case COLUMN_KEYS.MODEL:
return {
...column,
render: (text, record) =>
record.type === 0 || record.type === 2 || record.type === 5 ? (
<button
type='button'
className='usage-record-copy-text usage-record-model'
onClick={(event) => copyText(event, record.model_name)}
>
{record.model_name}
</button>
) : null,
};
case COLUMN_KEYS.USE_TIME:
return {
...column,
title: t('用时/首字').replace('/', ' / '),
render: (text, record) =>
record.type === 2 || record.type === 5
? renderRecordUseTime(record, t)
: null,
};
case COLUMN_KEYS.PROMPT:
case COLUMN_KEYS.COMPLETION:
return {
...column,
align: 'right',
render: (text, record, index) => {
const node = column.render(text, record, index);
const showZero =
column.key === COLUMN_KEYS.COMPLETION &&
Number(text) === 0 &&
[0, 2, 5].includes(record.type);
return node?.props?.children || showZero
? formatRecordNumber(text)
: null;
},
};
case COLUMN_KEYS.COST:
return {
...column,
align: 'right',
render: (text, record, index) => (
<span className='usage-record-cost'>
{column.render(text, record, index)}
</span>
),
};
case COLUMN_KEYS.IP:
return {
...column,
title: (
<Tooltip
content={t(
'只有当用户设置开启IP记录时,才会进行请求和错误类型日志的IP记录',
)}
>
<span>{t('IP')}</span>
</Tooltip>
),
render: (text, record, index) =>
(record.type === 2 || record.type === 5) && text
? column.render(text, record, index)
: '—',
};
case COLUMN_KEYS.RETRY:
return {
...column,
render: (text, record) => {
if (!isAdminUser || (record.type !== 2 && record.type !== 5))
return null;
const other = getLogOther(record.other);
const channels = other?.admin_info?.use_channel;
return Array.isArray(channels) && channels.length
? channels.join(' → ')
: record.channel;
},
};
case COLUMN_KEYS.DETAILS:
return {
...column,
render: (text, record, index) => {
const node = column.render(text, record, index);
return React.cloneElement(
node,
{
className: 'usage-record-details',
style: { maxWidth: 'none', whiteSpace: 'nowrap' },
},
typeof node.props.children === 'string'
? node.props.children.replaceAll(' * ', ' × ')
: node.props.children,
);
},
};
default:
return column;
}
});
export const getLogsColumns = ({
t,
COLUMN_KEYS,
copyText,
showUserInfoFunc,
isAdminUser,
isRootUser = false,
usageRecordMode = false,
}) => {
const columns = [
{
key: COLUMN_KEYS.TIME,
title: t('时间'),
dataIndex: 'timestamp2string',
},
{
key: COLUMN_KEYS.CHANNEL,
title: t('渠道'),
dataIndex: 'channel',
render: (text, record, index) => {
let isMultiKey = false;
let multiKeyIndex = -1;
let other = getLogOther(record.other);
if (other?.admin_info) {
let adminInfo = other.admin_info;
if (adminInfo?.is_multi_key) {
isMultiKey = true;
multiKeyIndex = adminInfo.multi_key_index;
}
}
return isAdminUser &&
(record.type === 0 || record.type === 2 || record.type === 5) ? (
<Space>
<Tooltip content={record.channel_name || t('未知渠道')}>
<span>
<Tag
color={colors[parseInt(text) % colors.length]}
shape='circle'
>
{text}
</Tag>
</span>
</Tooltip>
{isMultiKey && (
<Tag color='white' shape='circle'>
{multiKeyIndex}
</Tag>
)}
</Space>
) : null;
},
},
{
key: COLUMN_KEYS.USERNAME,
title: t('用户'),
dataIndex: 'username',
render: (text, record, index) => {
// 优先显示 display_name,如果没有则显示 username
const displayText = record.display_name || text;
return isAdminUser ? (
<div>
<Avatar
size='extra-small'
color={stringToColor(text)}
style={{ marginRight: 4 }}
onClick={(event) => {
event.stopPropagation();
showUserInfoFunc(record.user_id);
}}
>
{typeof displayText === 'string' && displayText.slice(0, 1)}
</Avatar>
{displayText}
</div>
) : (
<></>
);
},
},
{
key: COLUMN_KEYS.TOKEN,
title: t('密钥'),
dataIndex: 'token_name',
render: (text, record, index) => {
return record.type === 0 || record.type === 2 || record.type === 5 ? (
<div>
<Tag
color='grey'
shape='circle'
onClick={(event) => {
copyText(event, text);
}}
>
{' '}
{t(text)}{' '}
</Tag>
</div>
) : (
<></>
);
},
},
{
key: COLUMN_KEYS.GROUP,
title: t('分组'),
dataIndex: 'group',
render: (text, record, index) => {
if (record.type === 0 || record.type === 2 || record.type === 5) {
if (record.group) {
return <>{renderGroup(record.group)}</>;
} else {
let other = null;
try {
other = JSON.parse(record.other);
} catch (e) {
console.error(
`Failed to parse record.other: "${record.other}".`,
e,
);
}
if (other === null) {
return <></>;
}
if (other.group !== undefined) {
return <>{renderGroup(other.group)}</>;
} else {
return <></>;
}
}
} else {
return <></>;
}
},
},
{
key: COLUMN_KEYS.TYPE,
title: t('类型'),
dataIndex: 'type',
render: (text, record, index) => {
return <>{renderType(text, t)}</>;
},
},
{
key: COLUMN_KEYS.MODEL,
title: t('模型'),
dataIndex: 'model_name',
render: (text, record, index) => {
return record.type === 0 || record.type === 2 || record.type === 5 ? (
<>{renderModelName(record, copyText, t, isRootUser)}</>
) : (
<></>
);
},
},
{
key: COLUMN_KEYS.USE_TIME,
title: t('用时/首字'),
dataIndex: 'use_time',
render: (text, record, index) => {
if (!(record.type === 2 || record.type === 5)) {
return <></>;
}
if (record.is_stream) {
let other = getLogOther(record.other);
return (
<>
<Space>
{renderUseTime(text, t)}
{renderFirstUseTime(other?.frt, t)}
{renderIsStream(record.is_stream, t)}
</Space>
</>
);
} else {
return (
<>
<Space>
{renderUseTime(text, t)}
{renderIsStream(record.is_stream, t)}
</Space>
</>
);
}
},
},
{
key: COLUMN_KEYS.PROMPT,
title: t('输入'),
dataIndex: 'prompt_tokens',
render: (text, record, index) => {
return record.type === 0 || record.type === 2 || record.type === 5 ? (
<>{<span> {text} </span>}</>
) : (
<></>
);
},
},
{
key: COLUMN_KEYS.COMPLETION,
title: t('输出'),
dataIndex: 'completion_tokens',
render: (text, record, index) => {
return parseInt(text) > 0 &&
(record.type === 0 || record.type === 2 || record.type === 5) ? (
<>{<span> {text} </span>}</>
) : (
<></>
);
},
},
{
key: COLUMN_KEYS.COST,
title: t('花费'),
dataIndex: 'quota',
render: (text, record, index) => {
return record.type === 0 || record.type === 2 || record.type === 5 ? (
<>{renderQuota(text, 6)}</>
) : (
<></>
);
},
},
{
key: COLUMN_KEYS.IP,
title: (
<div className='flex items-center gap-1'>
{t('IP')}
<Tooltip
content={t(
'只有当用户设置开启IP记录时,才会进行请求和错误类型日志的IP记录',
)}
>
<IconHelpCircle className='text-gray-400 cursor-help' />
</Tooltip>
</div>
),
dataIndex: 'ip',
render: (text, record, index) => {
return (record.type === 2 || record.type === 5) && text ? (
<Tooltip content={text}>
<span>
<Tag
color='orange'
shape='circle'
onClick={(event) => {
copyText(event, text);
}}
>
{text}
</Tag>
</span>
</Tooltip>
) : (
<></>
);
},
},
{
key: COLUMN_KEYS.RETRY,
title: t('重试'),
dataIndex: 'retry',
render: (text, record, index) => {
if (!(record.type === 2 || record.type === 5)) {
return <></>;
}
let content = t('渠道') + `:${record.channel}`;
if (record.other !== '') {
let other = JSON.parse(record.other);
if (other === null) {
return <></>;
}
if (other.admin_info !== undefined) {
if (
other.admin_info.use_channel !== null &&
other.admin_info.use_channel !== undefined &&
other.admin_info.use_channel !== ''
) {
let useChannel = other.admin_info.use_channel;
let useChannelStr = useChannel.join('->');
content = t('渠道') + `:${useChannelStr}`;
}
}
}
return isAdminUser ? <div>{content}</div> : <></>;
},
},
{
key: COLUMN_KEYS.DETAILS,
title: t('详情'),
dataIndex: 'content',
fixed: 'right',
render: (text, record, index) => {
let other = getLogOther(record.other);
if (other == null || record.type !== 2) {
return (
<Typography.Paragraph
ellipsis={{
rows: 2,
showTooltip: {
type: 'popover',
opts: { style: { width: 240 } },
},
}}
style={{ maxWidth: 240 }}
>
{text}
</Typography.Paragraph>
);
}
let content = other?.claude
? renderModelPriceSimple(
other.model_ratio,
other.model_price,
other.group_ratio,
other?.user_group_ratio,
other.cache_tokens || 0,
other.cache_ratio || 1.0,
other.cache_creation_tokens || 0,
other.cache_creation_ratio || 1.0,
other.cache_creation_tokens_5m || 0,
other.cache_creation_ratio_5m ||
other.cache_creation_ratio ||
1.0,
other.cache_creation_tokens_1h || 0,
other.cache_creation_ratio_1h ||
other.cache_creation_ratio ||
1.0,
false,
1.0,
other?.is_system_prompt_overwritten,
'claude',
)
: renderModelPriceSimple(
other.model_ratio,
other.model_price,
other.group_ratio,
other?.user_group_ratio,
other.cache_tokens || 0,
other.cache_ratio || 1.0,
0,
1.0,
0,
1.0,
0,
1.0,
false,
1.0,
other?.is_system_prompt_overwritten,
'openai',
);
return (
<Typography.Paragraph
ellipsis={{
rows: 3,
}}
style={{ maxWidth: 240, whiteSpace: 'pre-line' }}
>
{content}
</Typography.Paragraph>
);
},
},
];
return usageRecordMode
? applyUsageRecordPresentation(columns, {
t,
COLUMN_KEYS,
copyText,
showUserInfoFunc,
isAdminUser,
})
: columns;
};