/*
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 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 }) => (
);
// 个人端使用记录:原型为三张本页统计卡;导出本页移至“任务记录”表格卡标题栏
if (usageRecordMode) {
return (
{t('本页成功')}
{stats.successCount}
{t('本页失败')}
{stats.failureCount}
{t('本页进行中')}
{stats.pendingCount}
);
}
return (
{/* 左侧:标题和统计 */}
{/* 页面标识 */}
{t('任务记录')}
{t('异步任务执行状态')}
{/* 快速统计 */}
{logs.length > 0 && (
{/* 这些统计只包含当前页数据,因此 label 前缀"本页",避免用户误以为是全量统计 */}
)}
{/* 右侧:操作按钮 */}
{/* 实时状态指示:只有真实存在进行中任务时才常亮,避免"实时更新"始终挂着误导用户 */}
{stats.pendingCount > 0 && (
{t('实时更新中')}
)}
{/* 导出当前页为 CSV,空列表时禁用避免生成只有表头的无用文件 */}
}
onClick={handleExportCsv}
disabled={!logs || logs.length === 0}
className={exportButtonClass}
>
{t('导出本页')}
);
};
export default TaskLogsActions;