Files
monkey-ai-web/src/components/table/task-logs/TaskLogsColumnDefs.jsx
T

649 lines
18 KiB
React

import React from 'react';
import { Tag as AntTag } from 'antd';
import { Progress, Tooltip, Typography } from '@/components/ui';
import { fundAccountDisplay } from '../../../pages/Organization/fund/fundFormat';
import {
Music,
FileText,
HelpCircle,
CheckCircle,
Pause,
Clock,
Play,
XCircle,
Loader,
List,
Hash,
Video,
Sparkles,
Timer,
ImagePlus,
Coins,
Cpu,
} from 'lucide-react';
import {
TASK_ACTION_FIRST_TAIL_GENERATE,
TASK_ACTION_GENERATE,
TASK_ACTION_REFERENCE_GENERATE,
TASK_ACTION_TEXT_GENERATE,
IMAGE_ACTION_GENERATE,
IMAGE_ACTION_EDIT,
} from '../../../constants/common.constant';
import { renderQuota } from '../../../helpers';
// 仅保留与下方 COLOR_CONFIG 一一对应的色值,避免 amber/lime/violet/teal/indigo/light-blue
// 经历多级三元 fallback 后被挤压到少数颜色上,渠道颜色分布严重偏斜
const colors = [
'blue',
'cyan',
'green',
'grey',
'orange',
'pink',
'purple',
'red',
'yellow',
];
// 颜色配置映射
const COLOR_CONFIG = {
green: {
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',
gradient: 'from-emerald-500 to-green-600',
},
yellow: {
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',
gradient: 'from-amber-500 to-yellow-600',
},
blue: {
bg: 'bg-blue-50 dark:bg-blue-950/30',
text: 'text-blue-600 dark:text-blue-400',
border: 'border-blue-200/50 dark:border-blue-800/50',
gradient: 'from-blue-500 to-brand-to',
},
red: {
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',
gradient: 'from-rose-500 to-red-600',
},
orange: {
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',
gradient: 'from-orange-500 to-amber-600',
},
grey: {
bg: 'bg-gray-50 dark:bg-gray-900/30',
text: 'text-gray-600 dark:text-gray-400',
border: 'border-gray-200/50 dark:border-gray-700/50',
gradient: 'from-gray-400 to-gray-600',
},
pink: {
bg: 'bg-pink-50 dark:bg-pink-950/30',
text: 'text-pink-600 dark:text-pink-400',
border: 'border-pink-200/50 dark:border-pink-800/50',
gradient: 'from-pink-500 to-rose-600',
},
purple: {
bg: 'bg-brand/5 dark:bg-brand-deep/30',
text: 'text-brand-deep dark:text-brand-from',
border: 'border-brand/50 dark:border-brand-deep/50',
gradient: 'from-brand-from to-brand-to',
},
cyan: {
bg: 'bg-cyan-50 dark:bg-cyan-950/30',
text: 'text-cyan-600 dark:text-cyan-400',
border: 'border-cyan-200/50 dark:border-cyan-800/50',
gradient: 'from-cyan-500 to-teal-600',
},
};
// 自定义标签组件
const CustomTag = ({
icon: Icon,
label,
colorKey = 'grey',
animate = false,
}) => {
const config = COLOR_CONFIG[colorKey] || COLOR_CONFIG.grey;
return (
<span
className={`
inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium
${config.bg} ${config.text} border ${config.border}
transition-all duration-200 hover:scale-105
`}
>
<span
className={`flex items-center justify-center w-4 h-4 rounded-full bg-gradient-to-br ${config.gradient}`}
>
<Icon
className={`w-2.5 h-2.5 text-white ${animate ? 'animate-spin' : ''}`}
strokeWidth={2.5}
/>
</span>
{label}
</span>
);
};
const getTaskChannelColorKey = (value) => {
const idNum = parseInt(value);
const colorIndex = Number.isFinite(idNum)
? ((idNum % colors.length) + colors.length) % colors.length
: 0;
return colors[colorIndex];
};
// Render functions
const renderTimestamp = (timestampInSeconds) => {
const date = new Date(timestampInSeconds * 1000);
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
const hours = ('0' + date.getHours()).slice(-2);
const minutes = ('0' + date.getMinutes()).slice(-2);
const seconds = ('0' + date.getSeconds()).slice(-2);
return (
<span className='text-xs text-gray-600 dark:text-gray-300 font-mono'>
{`${year}-${month}-${day}`}
<span className='text-gray-400 dark:text-gray-500 mx-1'>|</span>
{`${hours}:${minutes}:${seconds}`}
</span>
);
};
function renderDuration(submit_time, finishTime, t) {
if (!submit_time || !finishTime) return '-';
const durationSec = finishTime - submit_time;
const colorKey = durationSec > 60 ? 'red' : 'green';
return (
<CustomTag
icon={Timer}
label={t('{{n}} 秒', { n: durationSec })}
colorKey={colorKey}
/>
);
}
const renderType = (type, t) => {
const typeConfig = {
MUSIC: { icon: Music, label: t('生成音乐'), color: 'grey' },
LYRICS: { icon: FileText, label: t('生成歌词'), color: 'pink' },
[TASK_ACTION_GENERATE]: {
icon: Sparkles,
label: t('图生视频'),
color: 'blue',
},
[TASK_ACTION_TEXT_GENERATE]: {
icon: Sparkles,
label: t('文生视频'),
color: 'blue',
},
[TASK_ACTION_FIRST_TAIL_GENERATE]: {
icon: Sparkles,
label: t('首尾生视频'),
color: 'blue',
},
[TASK_ACTION_REFERENCE_GENERATE]: {
icon: Sparkles,
label: t('参照生视频'),
color: 'blue',
},
[IMAGE_ACTION_GENERATE]: {
icon: Sparkles,
label: t('文生图'),
color: 'cyan',
},
[IMAGE_ACTION_EDIT]: {
icon: ImagePlus,
label: t('图像编辑'),
color: 'purple',
},
};
const config = typeConfig[type] || {
icon: HelpCircle,
label: t('未知'),
color: 'grey',
};
return (
<CustomTag
icon={config.icon}
label={config.label}
colorKey={config.color}
/>
);
};
const renderStatus = (type, t) => {
const statusConfig = {
SUCCESS: { icon: CheckCircle, label: t('成功'), color: 'green' },
NOT_START: { icon: Pause, label: t('未启动'), color: 'grey' },
SUBMITTED: { icon: Clock, label: t('队列中'), color: 'yellow' },
IN_PROGRESS: {
icon: Play,
label: t('执行中'),
color: 'blue',
animate: false,
},
FAILURE: { icon: XCircle, label: t('失败'), color: 'red' },
QUEUED: { icon: List, label: t('排队中'), color: 'orange' },
UNKNOWN: { icon: HelpCircle, label: t('未知'), color: 'grey' },
'': { icon: Loader, label: t('正在提交'), color: 'grey', animate: true },
};
const config = statusConfig[type] || statusConfig['UNKNOWN'];
return (
<CustomTag
icon={config.icon}
label={config.label}
colorKey={config.color}
animate={config.animate}
/>
);
};
// ---- 使用记录(个人端)呈现:原型为 Ant Design 预设无边框标签、两行时间与纯文本 ----
const RECORD_STATUS_COLORS = {
SUCCESS: 'success',
FAILURE: 'error',
IN_PROGRESS: 'processing',
SUBMITTED: 'warning',
QUEUED: 'warning',
};
const recordTagFrom = (node, color) =>
node?.props?.label !== undefined ? (
<AntTag variant='filled' color={color}>
{node.props.label}
</AntTag>
) : (
node
);
const renderRecordTimestamp = (timestampInSeconds) => {
const date = new Date(timestampInSeconds * 1000);
const pad = (value) => ('0' + value).slice(-2);
return (
<div className='usage-record-time'>
<span>{`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`}</span>
<strong>{`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`}</strong>
</div>
);
};
// 仅替换展示,不改变列条件、复制/弹窗回调与数据来源
const applyTaskRecordPresentation = (columns, { COLUMN_KEYS }) =>
columns.map((column) => {
const unwrap = (text, record, index) => {
const node = column.render(text, record, index);
return node?.type === 'div' ? node.props.children : node;
};
switch (column.key) {
case COLUMN_KEYS.SUBMIT_TIME:
case COLUMN_KEYS.FINISH_TIME:
return {
...column,
render: (text) => (text ? renderRecordTimestamp(text) : '-'),
};
case COLUMN_KEYS.DURATION:
return {
...column,
render: (finish, record) =>
finish && record.submit_time
? `${finish - record.submit_time} 秒`
: '-',
};
case COLUMN_KEYS.CHANNEL:
return {
...column,
render: (text, record, index) => {
const node = column.render(text, record, index);
if (!node) return null;
return (
<Tooltip content={record.channel_name || text}>
<span
className='usage-record-copy-text'
onClick={node.props.children.props.onClick}
>
<AntTag variant='filled'>{text}</AntTag>
</span>
</Tooltip>
);
},
};
case COLUMN_KEYS.TYPE:
return {
...column,
render: (text, record, index) =>
recordTagFrom(unwrap(text, record, index), 'blue'),
};
case COLUMN_KEYS.MODEL:
return {
...column,
render: (properties, record, index) => {
const modelName =
properties?.origin_model_name ||
properties?.upstream_model_name ||
'';
if (!modelName) return '-';
const node = column.render(properties, record, index);
return (
<Tooltip content={modelName}>
<span
className='usage-record-model usage-record-copy-text'
onClick={node.props.children.props.onClick}
>
{modelName}
</span>
</Tooltip>
);
},
};
case COLUMN_KEYS.TASK_ID:
return {
...column,
render: (text, record, index) => (
<span className='usage-record-mono'>
{column.render(text, record, index)}
</span>
),
};
case COLUMN_KEYS.TASK_STATUS:
return {
...column,
render: (text, record, index) =>
recordTagFrom(
unwrap(text, record, index),
RECORD_STATUS_COLORS[text],
),
};
case COLUMN_KEYS.QUOTA:
return {
...column,
render: (quota) =>
quota === undefined || quota === null || quota === 0 ? (
'-'
) : (
<span className='usage-record-cost'>{renderQuota(quota, 6)}</span>
),
};
default:
return column;
}
});
export const getTaskLogsColumns = ({
t,
COLUMN_KEYS,
copyText,
openContentModal,
isAdminUser,
isRootUser = false,
openVideoModal,
usageRecordMode = false,
}) => {
const columns = [
{
key: COLUMN_KEYS.SUBMIT_TIME,
title: t('提交时间'),
dataIndex: 'submit_time',
render: (text, record, index) => {
return <div>{text ? renderTimestamp(text) : '-'}</div>;
},
},
{
key: COLUMN_KEYS.FINISH_TIME,
title: t('结束时间'),
dataIndex: 'finish_time',
render: (text, record, index) => {
return <div>{text ? renderTimestamp(text) : '-'}</div>;
},
},
{
key: COLUMN_KEYS.DURATION,
title: t('花费时间'),
dataIndex: 'finish_time',
render: (finish, record) => {
return (
<>{finish ? renderDuration(record.submit_time, finish, t) : '-'}</>
);
},
},
{
key: COLUMN_KEYS.CHANNEL,
title: t('渠道'),
dataIndex: 'channel_id',
render: (text, record, index) => {
if (!isAdminUser) return null;
const colorKey = getTaskChannelColorKey(text);
// 若后端返回 channel_name 则在 Tooltip 中显示名称,便于管理员快速识别;否则回落为 ID
const tooltipContent = record.channel_name || text;
return (
<Tooltip content={tooltipContent}>
<span onClick={() => copyText(text)} className='cursor-pointer'>
<CustomTag icon={Hash} label={text} colorKey={colorKey} />
</span>
</Tooltip>
);
},
},
{
key: COLUMN_KEYS.TYPE,
title: t('类型'),
dataIndex: 'action',
render: (text, record, index) => {
return <div>{renderType(text, t)}</div>;
},
},
{
key: COLUMN_KEYS.MODEL,
title: t('模型'),
dataIndex: 'properties',
render: (properties, record, index) => {
const modelName =
properties?.origin_model_name ||
properties?.upstream_model_name ||
'';
if (!modelName) {
return (
<span className='text-xs text-gray-400 dark:text-gray-500'>-</span>
);
}
return (
<Tooltip content={modelName}>
<span
onClick={() => copyText(modelName)}
className='cursor-pointer'
>
<CustomTag icon={Cpu} label={modelName} colorKey='purple' />
</span>
</Tooltip>
);
},
},
{
key: COLUMN_KEYS.TASK_ID,
title: t('任务ID'),
dataIndex: 'task_id',
render: (text, record, index) => {
return (
<Typography.Text
ellipsis={{ showTooltip: true }}
onClick={
isRootUser
? () => openContentModal(JSON.stringify(record, null, 2))
: undefined
}
className={
isRootUser
? 'cursor-pointer hover:text-blue-500 dark:hover:text-blue-400 transition-colors font-mono text-xs'
: 'font-mono text-xs'
}
>
{text}
</Typography.Text>
);
},
},
{
key: COLUMN_KEYS.TASK_STATUS,
title: t('任务状态'),
dataIndex: 'status',
render: (text, record, index) => {
return <div>{renderStatus(text, t)}</div>;
},
},
{
key: COLUMN_KEYS.PROGRESS,
title: t('进度'),
dataIndex: 'progress',
render: (text, record, index) => {
if (isNaN(text?.replace('%', ''))) {
return <span className='text-xs text-gray-500'>{text || '-'}</span>;
}
const percent = text ? parseInt(text.replace('%', '')) : 0;
const isFailure = record.status === 'FAILURE';
return (
<div className='min-w-[140px]'>
<Progress
stroke={
isFailure
? 'var(--ui-color-warning)'
: usageRecordMode && percent >= 100
? 'var(--ur-progress-done)'
: 'var(--ui-color-primary)'
}
percent={percent}
showInfo={true}
aria-label='task progress'
size='small'
className='task-progress'
/>
</div>
);
},
},
{
key: COLUMN_KEYS.QUOTA,
title: t('费用'),
dataIndex: 'quota',
render: (quota, record, index) => {
if (quota === undefined || quota === null || quota === 0) {
return (
<span className='text-xs text-gray-400 dark:text-gray-500'>-</span>
);
}
return (
<CustomTag
icon={Coins}
label={renderQuota(quota, 6)}
colorKey='orange'
/>
);
},
},
...(COLUMN_KEYS.FUND_DEPT
? [
{
key: COLUMN_KEYS.FUND_DEPT,
title: t('归属部门'),
dataIndex: 'fund_dept_name',
render: (v) => (
<span className='block max-w-[140px] truncate' title={v || ''}>
{v === undefined || v === null ? '—' : v || t('未归属部门')}
</span>
),
},
{
key: COLUMN_KEYS.FUND_ACCOUNT,
title: t('扣费账户'),
dataIndex: 'fund_account_type',
render: (v, record) =>
v === undefined || v === null ? (
'—'
) : (
<span className='block max-w-[180px] truncate'>
{fundAccountDisplay(t, v, record.fund_dept_name)}
</span>
),
},
]
: []),
{
key: COLUMN_KEYS.FAIL_REASON,
title: t('详情'),
dataIndex: 'fail_reason',
fixed: 'right',
render: (text, record, index) => {
const isVideoTask =
record.action === TASK_ACTION_GENERATE ||
record.action === TASK_ACTION_TEXT_GENERATE ||
record.action === TASK_ACTION_FIRST_TAIL_GENERATE ||
record.action === TASK_ACTION_REFERENCE_GENERATE;
const isSuccess = record.status === 'SUCCESS';
const isUrl = typeof text === 'string' && /^https?:\/\//.test(text);
if (isSuccess && isVideoTask && isUrl) {
const videoUrl = `/v1/videos/${record.task_id}/content`;
// text 是上游原始视频 URL,传给弹窗用于"复制链接";代理路径仅用于播放
return (
<a
href='#'
onClick={(e) => {
e.preventDefault();
openVideoModal(videoUrl, text);
}}
className={
usageRecordMode
? 'usage-record-link'
: 'inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium bg-gradient-to-r from-blue-500 to-brand-to text-white hover:from-blue-600 hover:to-brand-to transition-all duration-300 shadow-sm hover:shadow-md'
}
>
<Video className='w-3.5 h-3.5' />
{t('点击预览视频')}
</a>
);
}
if (!text) {
return (
<span className='text-xs text-gray-400 dark:text-gray-500'>
{t('无')}
</span>
);
}
return (
<Typography.Text
ellipsis={{ showTooltip: true }}
style={{ width: 100 }}
onClick={() => {
openContentModal(text);
}}
className='cursor-pointer hover:text-blue-500 dark:hover:text-blue-400 transition-colors text-xs'
>
{text}
</Typography.Text>
);
},
},
];
return usageRecordMode
? applyTaskRecordPresentation(columns, { COLUMN_KEYS })
: columns;
};