Files
monkey-ai-web/src/components/table/task-logs/TaskLogsActions.jsx
T
qiubaishen b0860467d5 feat(org): expose enterprise drawing and task logs
(cherry picked from commit b80afd273c)
2026-11-06 20:28:42 +08:00

240 lines
8.6 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 React from 'react';
import { Button } from '@/components/ui';
import {
ListTodo,
Clock,
CheckCircle2,
Download,
XCircle,
Loader2,
} from 'lucide-react';
import { exportToCsv, formatTimestampForCsv } from '../../../helpers/exportCsv';
// CSV 导出:仅当前页已加载的数据,避免一次性拉取全量产生巨大请求
export const exportTaskLogsPageCsv = (logs, isAdminUser, t) => {
if (!logs || logs.length === 0) return;
const columns = [
{
key: 'submit_time',
title: t('提交时间'),
format: (r) => formatTimestampForCsv(r.submit_time),
},
{
key: 'finish_time',
title: t('结束时间'),
format: (r) => formatTimestampForCsv(r.finish_time),
},
{
key: 'duration',
title: t('耗时(秒)'),
format: (r) =>
r.submit_time && r.finish_time ? r.finish_time - r.submit_time : '',
},
...(isAdminUser
? [
{ key: 'channel_id', title: t('渠道ID') },
{ key: 'channel_name', title: t('渠道名称') },
]
: []),
{ key: 'action', title: t('类型') },
{
key: 'model',
title: t('模型'),
format: (r) =>
r.properties?.origin_model_name ||
r.properties?.upstream_model_name ||
'',
},
{ key: 'task_id', title: t('任务ID') },
{ key: 'status', title: t('状态') },
{ key: 'progress', title: t('进度') },
{ key: 'quota', title: t('费用'), format: (r) => r.quota || '' },
{ key: 'fail_reason', title: t('详情') },
];
const ts = formatTimestampForCsv(Math.floor(Date.now() / 1000)).replace(
/[: ]/g,
'-',
);
exportToCsv(columns, logs, `task-logs-${ts}.csv`);
};
// 本页任务统计:仅统计当前页已加载数据
export const useTaskPageStats = (logs = []) =>
React.useMemo(() => {
const successCount = logs.filter((log) => log.status === 'SUCCESS').length;
const failureCount = logs.filter((log) => log.status === 'FAILURE').length;
const pendingCount = logs.filter(
(log) =>
log.status === 'IN_PROGRESS' ||
log.status === 'SUBMITTED' ||
log.status === 'QUEUED' ||
log.status === '',
).length;
return { successCount, failureCount, pendingCount, total: logs.length };
}, [logs]);
const TaskLogsActions = ({
t,
logs = [],
isAdminUser,
usageRecordMode = false,
}) => {
const exportButtonClass =
'hover:!bg-gray-100 dark:hover:!bg-neutral-800 !transition-all !duration-300';
// 计算任务统计
const stats = useTaskPageStats(logs);
const handleExportCsv = () => exportTaskLogsPageCsv(logs, isAdminUser, t);
const StatBadge = ({ icon: Icon, label, value, gradient, iconBg }) => (
<div className='relative group'>
<div
className={`
relative overflow-hidden
px-3 py-2 rounded-xl
bg-gradient-to-br ${gradient}
border border-white/50 dark:border-neutral-700/50
shadow-sm hover:shadow-md
transition-all duration-300
hover:scale-[1.02]
`}
>
{/* 装饰光晕 */}
<div className='absolute -top-2 -right-2 w-10 h-10 bg-white/20 rounded-full blur-xl opacity-0 group-hover:opacity-100 transition-opacity duration-300' />
<div className='relative flex items-center gap-2'>
<div
className={`flex items-center justify-center w-7 h-7 rounded-lg ${iconBg} shadow-sm`}
>
<Icon className='w-3.5 h-3.5 text-white' strokeWidth={2} />
</div>
<div className='flex flex-col'>
<span className='text-[10px] font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wide'>
{label}
</span>
<span className='text-sm font-bold text-gray-800 dark:text-gray-100'>
{value}
</span>
</div>
</div>
</div>
</div>
);
// 个人端使用记录:原型为三张本页统计卡;导出本页移至“任务记录”表格卡标题栏
if (usageRecordMode) {
return (
<div className='usage-record-task-stats'>
<div className='usage-record-task-stat is-success'>
<span>{t('本页成功')}</span>
<strong>{stats.successCount}</strong>
</div>
<div className='usage-record-task-stat is-failure'>
<span>{t('本页失败')}</span>
<strong>{stats.failureCount}</strong>
</div>
<div className='usage-record-task-stat is-pending'>
<span>{t('本页进行中')}</span>
<strong>{stats.pendingCount}</strong>
</div>
</div>
);
}
return (
<div className='flex flex-col lg:flex-row justify-between items-start lg:items-center gap-4 w-full'>
{/* 左侧:标题和统计 */}
<div className='flex flex-wrap items-center gap-3'>
{/* 页面标识 */}
<div className='flex items-center gap-2 px-3 py-2 rounded-xl bg-gradient-to-r from-cyan-50 to-blue-50 dark:from-cyan-950/40 dark:to-blue-950/40 border border-cyan-200/50 dark:border-cyan-800/30'>
<div className='flex items-center justify-center w-8 h-8 rounded-lg bg-gradient-to-br from-cyan-500 to-blue-600 shadow-md'>
<ListTodo className='w-4 h-4 text-white' strokeWidth={2} />
</div>
<div className='flex flex-col'>
<span className='text-sm font-semibold text-gray-800 dark:text-gray-100'>
{t('任务记录')}
</span>
<span className='text-[10px] text-gray-500 dark:text-gray-400'>
{t('异步任务执行状态')}
</span>
</div>
</div>
{/* 快速统计 */}
{logs.length > 0 && (
<div className='hidden md:flex items-center gap-2'>
{/* 这些统计只包含当前页数据,因此 label 前缀"本页",避免用户误以为是全量统计 */}
<StatBadge
icon={CheckCircle2}
label={t('本页成功')}
value={stats.successCount}
gradient='from-emerald-50 to-green-50 dark:from-emerald-950/30 dark:to-green-950/30'
iconBg='bg-gradient-to-br from-emerald-500 to-green-600'
/>
<StatBadge
icon={XCircle}
label={t('本页失败')}
value={stats.failureCount}
gradient='from-rose-50 to-red-50 dark:from-rose-950/30 dark:to-red-950/30'
iconBg='bg-gradient-to-br from-rose-500 to-red-600'
/>
<StatBadge
icon={Loader2}
label={t('本页进行中')}
value={stats.pendingCount}
gradient='from-amber-50 to-orange-50 dark:from-amber-950/30 dark:to-orange-950/30'
iconBg='bg-gradient-to-br from-amber-500 to-orange-600'
/>
</div>
)}
</div>
{/* 右侧:操作按钮 */}
<div className='flex items-center gap-2'>
{/* 实时状态指示:只有真实存在进行中任务时才常亮,避免"实时更新"始终挂着误导用户 */}
{stats.pendingCount > 0 && (
<div className='hidden sm:flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-gradient-to-r from-brand-from/5 to-brand-to/5 dark:from-brand-from/30 dark:to-brand-to/30 border border-brand/50 dark:border-brand-deep/30'>
<Clock className='w-3.5 h-3.5 text-brand-deep dark:text-brand-from animate-pulse' />
<span className='text-xs font-medium text-brand-deep dark:text-brand-from'>
{t('实时更新中')}
</span>
</div>
)}
{/* 导出当前页为 CSV,空列表时禁用避免生成只有表头的无用文件 */}
<Button
size='small'
icon={<Download className='w-3.5 h-3.5' />}
onClick={handleExportCsv}
disabled={!logs || logs.length === 0}
className={exportButtonClass}
>
{t('导出本页')}
</Button>
</div>
</div>
);
};
export default TaskLogsActions;