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 (
{label}
);
};
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 (
{`${year}-${month}-${day}`}
|
{`${hours}:${minutes}:${seconds}`}
);
};
function renderDuration(submit_time, finishTime, t) {
if (!submit_time || !finishTime) return '-';
const durationSec = finishTime - submit_time;
const colorKey = durationSec > 60 ? 'red' : 'green';
return (
);
}
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 (
);
};
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 (
);
};
// ---- 使用记录(个人端)呈现:原型为 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 ? (
{node.props.label}
) : (
node
);
const renderRecordTimestamp = (timestampInSeconds) => {
const date = new Date(timestampInSeconds * 1000);
const pad = (value) => ('0' + value).slice(-2);
return (
{`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`}
{`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`}
);
};
// 仅替换展示,不改变列条件、复制/弹窗回调与数据来源
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 (
{text}
);
},
};
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 (
{modelName}
);
},
};
case COLUMN_KEYS.TASK_ID:
return {
...column,
render: (text, record, index) => (
{column.render(text, record, index)}
),
};
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 ? (
'-'
) : (
{renderQuota(quota, 6)}
),
};
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 {text ? renderTimestamp(text) : '-'}
;
},
},
{
key: COLUMN_KEYS.FINISH_TIME,
title: t('结束时间'),
dataIndex: 'finish_time',
render: (text, record, index) => {
return {text ? renderTimestamp(text) : '-'}
;
},
},
{
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 (
copyText(text)} className='cursor-pointer'>
);
},
},
{
key: COLUMN_KEYS.TYPE,
title: t('类型'),
dataIndex: 'action',
render: (text, record, index) => {
return {renderType(text, t)}
;
},
},
{
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 (
-
);
}
return (
copyText(modelName)}
className='cursor-pointer'
>
);
},
},
{
key: COLUMN_KEYS.TASK_ID,
title: t('任务ID'),
dataIndex: 'task_id',
render: (text, record, index) => {
return (
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}
);
},
},
{
key: COLUMN_KEYS.TASK_STATUS,
title: t('任务状态'),
dataIndex: 'status',
render: (text, record, index) => {
return {renderStatus(text, t)}
;
},
},
{
key: COLUMN_KEYS.PROGRESS,
title: t('进度'),
dataIndex: 'progress',
render: (text, record, index) => {
if (isNaN(text?.replace('%', ''))) {
return {text || '-'};
}
const percent = text ? parseInt(text.replace('%', '')) : 0;
const isFailure = record.status === 'FAILURE';
return (
);
},
},
{
key: COLUMN_KEYS.QUOTA,
title: t('费用'),
dataIndex: 'quota',
render: (quota, record, index) => {
if (quota === undefined || quota === null || quota === 0) {
return (
-
);
}
return (
);
},
},
...(COLUMN_KEYS.FUND_DEPT
? [
{
key: COLUMN_KEYS.FUND_DEPT,
title: t('归属部门'),
dataIndex: 'fund_dept_name',
render: (v) => (
{v === undefined || v === null ? '—' : v || t('未归属部门')}
),
},
{
key: COLUMN_KEYS.FUND_ACCOUNT,
title: t('扣费账户'),
dataIndex: 'fund_account_type',
render: (v, record) =>
v === undefined || v === null ? (
'—'
) : (
{fundAccountDisplay(t, v, record.fund_dept_name)}
),
},
]
: []),
{
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 (
{
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'
}
>
{t('点击预览视频')}
);
}
if (!text) {
return (
{t('无')}
);
}
return (
{
openContentModal(text);
}}
className='cursor-pointer hover:text-blue-500 dark:hover:text-blue-400 transition-colors text-xs'
>
{text}
);
},
},
];
return usageRecordMode
? applyTaskRecordPresentation(columns, { COLUMN_KEYS })
: columns;
};