2025-12-13 12:35:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Terminal Progress Dialog
|
|
|
|
|
|
* Cyberpunk Terminal Aesthetic
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-11-28 22:55:32 +08:00
|
|
|
|
import { useEffect, useRef, useState, useCallback } from "react";
|
2025-10-24 20:23:22 +08:00
|
|
|
|
import { Dialog, DialogOverlay, DialogPortal } from "@/components/ui/dialog";
|
|
|
|
|
|
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
2025-11-28 22:55:32 +08:00
|
|
|
|
import { Terminal, X as XIcon, Activity, Cpu, HardDrive, AlertTriangle, CheckCircle2 } from "lucide-react";
|
2025-10-25 22:14:56 +08:00
|
|
|
|
import { cn, calculateTaskProgress } from "@/shared/utils/utils";
|
2025-10-24 20:23:22 +08:00
|
|
|
|
import * as VisuallyHidden from "@radix-ui/react-visually-hidden";
|
2025-10-25 22:14:56 +08:00
|
|
|
|
import { taskControl } from "@/shared/services/taskControl";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
2025-12-13 12:35:03 +08:00
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
2025-10-25 22:14:56 +08:00
|
|
|
|
import { toast } from "sonner";
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
|
|
|
|
|
interface TerminalProgressDialogProps {
|
|
|
|
|
|
open: boolean;
|
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
|
|
|
|
|
taskId: string | null;
|
|
|
|
|
|
taskType: "repository" | "zip";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface LogEntry {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
id: string;
|
2025-10-24 20:23:22 +08:00
|
|
|
|
timestamp: string;
|
|
|
|
|
|
message: string;
|
|
|
|
|
|
type: "info" | "success" | "error" | "warning";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function TerminalProgressDialog({
|
|
|
|
|
|
open,
|
|
|
|
|
|
onOpenChange,
|
|
|
|
|
|
taskId,
|
|
|
|
|
|
taskType
|
|
|
|
|
|
}: TerminalProgressDialogProps) {
|
|
|
|
|
|
const [logs, setLogs] = useState<LogEntry[]>([]);
|
|
|
|
|
|
const [isCompleted, setIsCompleted] = useState(false);
|
|
|
|
|
|
const [isFailed, setIsFailed] = useState(false);
|
2025-10-25 22:14:56 +08:00
|
|
|
|
const [isCancelled, setIsCancelled] = useState(false);
|
2025-10-24 20:23:22 +08:00
|
|
|
|
const [currentTime, setCurrentTime] = useState(new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit", second: "2-digit" }));
|
|
|
|
|
|
const logsEndRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
const pollIntervalRef = useRef<number | null>(null);
|
|
|
|
|
|
const hasInitializedLogsRef = useRef(false);
|
|
|
|
|
|
|
2025-11-28 22:55:32 +08:00
|
|
|
|
// Refs for state accessed in intervals/effects to avoid dependency cycles
|
|
|
|
|
|
const logsRef = useRef<LogEntry[]>([]);
|
|
|
|
|
|
const isCompletedRef = useRef(false);
|
|
|
|
|
|
const isFailedRef = useRef(false);
|
|
|
|
|
|
const isCancelledRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
|
|
// Sync refs with state
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
logsRef.current = logs;
|
|
|
|
|
|
}, [logs]);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
isCompletedRef.current = isCompleted;
|
|
|
|
|
|
}, [isCompleted]);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
isFailedRef.current = isFailed;
|
|
|
|
|
|
}, [isFailed]);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
isCancelledRef.current = isCancelled;
|
|
|
|
|
|
}, [isCancelled]);
|
|
|
|
|
|
|
2025-10-24 20:23:22 +08:00
|
|
|
|
// 添加日志条目
|
2025-11-28 22:55:32 +08:00
|
|
|
|
const addLog = useCallback((message: string, type: LogEntry["type"] = "info") => {
|
2025-10-24 20:23:22 +08:00
|
|
|
|
const timestamp = new Date().toLocaleTimeString("zh-CN", {
|
|
|
|
|
|
hour: "2-digit",
|
|
|
|
|
|
minute: "2-digit",
|
|
|
|
|
|
second: "2-digit"
|
|
|
|
|
|
});
|
2025-11-28 22:55:32 +08:00
|
|
|
|
const newLog = { id: Math.random().toString(36).substr(2, 9), timestamp, message, type };
|
|
|
|
|
|
setLogs(prev => [...prev, newLog]);
|
|
|
|
|
|
}, []);
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
2025-10-25 22:14:56 +08:00
|
|
|
|
// 取消任务处理
|
|
|
|
|
|
const handleCancel = async () => {
|
|
|
|
|
|
if (!taskId) return;
|
2025-10-29 19:25:38 +08:00
|
|
|
|
|
2025-10-25 22:14:56 +08:00
|
|
|
|
if (!confirm('确定要取消此任务吗?已分析的结果将被保留。')) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-10-29 19:25:38 +08:00
|
|
|
|
|
2025-10-25 22:14:56 +08:00
|
|
|
|
// 1. 标记任务为取消状态
|
|
|
|
|
|
taskControl.cancelTask(taskId);
|
|
|
|
|
|
setIsCancelled(true);
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("[ERR] 用户取消任务,正在停止...", "error");
|
2025-10-29 19:25:38 +08:00
|
|
|
|
|
2025-10-25 22:14:56 +08:00
|
|
|
|
// 2. 立即更新数据库状态
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { api } = await import("@/shared/config/database");
|
2025-11-28 22:55:32 +08:00
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: API type mismatch workaround
|
2025-10-25 22:14:56 +08:00
|
|
|
|
await api.updateAuditTask(taskId, { status: 'cancelled' } as any);
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("[WARN] 任务状态已更新为已取消", "warning");
|
2025-10-25 22:14:56 +08:00
|
|
|
|
toast.success("任务已取消");
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('更新取消状态失败:', error);
|
|
|
|
|
|
toast.warning("任务已标记取消,后台正在停止...");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-24 20:23:22 +08:00
|
|
|
|
// 自动滚动到底部
|
2025-11-28 22:55:32 +08:00
|
|
|
|
// biome-ignore lint/correctness/useExhaustiveDependencies: We want to scroll when logs change
|
2025-10-24 20:23:22 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
logsEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
|
|
|
|
}, [logs]);
|
|
|
|
|
|
|
|
|
|
|
|
// 实时更新光标处的时间
|
|
|
|
|
|
useEffect(() => {
|
2025-10-25 22:14:56 +08:00
|
|
|
|
if (!open || isCompleted || isFailed || isCancelled) {
|
2025-10-24 20:23:22 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const timeInterval = setInterval(() => {
|
|
|
|
|
|
setCurrentTime(new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit", second: "2-digit" }));
|
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
clearInterval(timeInterval);
|
|
|
|
|
|
};
|
2025-11-28 22:55:32 +08:00
|
|
|
|
}, [open, isCompleted, isFailed, isCancelled]);
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
|
|
|
|
|
// 轮询任务状态
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!open || !taskId) {
|
|
|
|
|
|
// 清理状态
|
|
|
|
|
|
setLogs([]);
|
2025-11-28 22:55:32 +08:00
|
|
|
|
logsRef.current = [];
|
2025-10-24 20:23:22 +08:00
|
|
|
|
setIsCompleted(false);
|
|
|
|
|
|
setIsFailed(false);
|
2025-11-28 22:55:32 +08:00
|
|
|
|
setIsCancelled(false);
|
2025-10-24 20:23:22 +08:00
|
|
|
|
hasInitializedLogsRef.current = false;
|
|
|
|
|
|
if (pollIntervalRef.current) {
|
|
|
|
|
|
clearInterval(pollIntervalRef.current);
|
|
|
|
|
|
pollIntervalRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 只初始化日志一次(防止React严格模式重复)
|
|
|
|
|
|
if (!hasInitializedLogsRef.current) {
|
|
|
|
|
|
hasInitializedLogsRef.current = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化日志
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("[INFO] 审计任务已启动", "info");
|
|
|
|
|
|
addLog(`TASK_ID: ${taskId}`, "info");
|
|
|
|
|
|
addLog(`TYPE: ${taskType === "repository" ? "REPO_AUDIT" : "ZIP_AUDIT"}`, "info");
|
|
|
|
|
|
addLog("[WAIT] 正在初始化审计环境...", "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let lastScannedFiles = 0;
|
|
|
|
|
|
let lastIssuesCount = 0;
|
|
|
|
|
|
let lastTotalLines = 0;
|
|
|
|
|
|
let lastStatus = "";
|
2025-11-28 22:55:32 +08:00
|
|
|
|
let _pollCount = 0;
|
2025-10-24 20:23:22 +08:00
|
|
|
|
let hasDataChange = false;
|
|
|
|
|
|
let isFirstPoll = true;
|
|
|
|
|
|
|
|
|
|
|
|
// 开始轮询
|
|
|
|
|
|
const pollTask = async () => {
|
|
|
|
|
|
// 如果任务已完成或失败,停止轮询
|
2025-11-28 22:55:32 +08:00
|
|
|
|
if (isCompletedRef.current || isFailedRef.current) {
|
2025-10-24 20:23:22 +08:00
|
|
|
|
if (pollIntervalRef.current) {
|
|
|
|
|
|
clearInterval(pollIntervalRef.current);
|
|
|
|
|
|
pollIntervalRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
_pollCount++;
|
2025-10-24 20:23:22 +08:00
|
|
|
|
hasDataChange = false;
|
|
|
|
|
|
|
|
|
|
|
|
const requestStartTime = Date.now();
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 api.getAuditTaskById 获取任务状态
|
|
|
|
|
|
const { api } = await import("@/shared/config/database");
|
|
|
|
|
|
const task = await api.getAuditTaskById(taskId);
|
|
|
|
|
|
|
|
|
|
|
|
const requestDuration = Date.now() - requestStartTime;
|
|
|
|
|
|
|
|
|
|
|
|
if (!task) {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[ERR] 任务不存在 (${requestDuration}ms)`, "error");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
throw new Error("任务不存在");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否有数据变化
|
|
|
|
|
|
const statusChanged = task.status !== lastStatus;
|
|
|
|
|
|
const filesChanged = task.scanned_files !== lastScannedFiles;
|
|
|
|
|
|
const issuesChanged = task.issues_count !== lastIssuesCount;
|
|
|
|
|
|
const linesChanged = task.total_lines !== lastTotalLines;
|
|
|
|
|
|
|
|
|
|
|
|
hasDataChange = statusChanged || filesChanged || issuesChanged || linesChanged;
|
|
|
|
|
|
|
|
|
|
|
|
// 标记首次轮询已完成
|
|
|
|
|
|
if (isFirstPoll) {
|
|
|
|
|
|
isFirstPoll = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-27 16:27:01 +08:00
|
|
|
|
// 只在有变化时显示请求/响应信息(跳过 pending 状态)
|
|
|
|
|
|
if (hasDataChange && task.status !== "pending") {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[NET] 正在获取任务状态...`, "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
addLog(
|
2025-11-28 22:55:32 +08:00
|
|
|
|
`[OK] 状态: ${task.status} | 文件: ${task.scanned_files}/${task.total_files} | 问题: ${task.issues_count} (${requestDuration}ms)`,
|
2025-10-24 20:23:22 +08:00
|
|
|
|
"success"
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新上次状态
|
|
|
|
|
|
if (statusChanged) {
|
|
|
|
|
|
lastStatus = task.status;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查任务状态
|
|
|
|
|
|
if (task.status === "pending") {
|
2025-10-27 16:27:01 +08:00
|
|
|
|
// 静默跳过 pending 状态,不显示任何日志
|
2025-10-24 20:23:22 +08:00
|
|
|
|
} else if (task.status === "running") {
|
|
|
|
|
|
// 首次进入运行状态
|
2025-11-28 22:55:32 +08:00
|
|
|
|
if (statusChanged && logsRef.current.filter(l => l.message.includes("开始扫描")).length === 0) {
|
|
|
|
|
|
addLog("[SCAN] 开始扫描代码文件...", "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
if (task.project) {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[PROJ] 项目: ${task.project.name}`, "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
if (task.branch_name) {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[BRCH] 分支: ${task.branch_name}`, "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 显示进度更新(仅在有变化时)
|
|
|
|
|
|
if (filesChanged && task.scanned_files > lastScannedFiles) {
|
2025-10-25 22:14:56 +08:00
|
|
|
|
const progress = calculateTaskProgress(task.scanned_files, task.total_files);
|
2025-10-24 20:23:22 +08:00
|
|
|
|
const filesProcessed = task.scanned_files - lastScannedFiles;
|
|
|
|
|
|
addLog(
|
2025-11-28 22:55:32 +08:00
|
|
|
|
`[PROG] 扫描进度: ${task.scanned_files || 0}/${task.total_files || 0} 文件 (${progress}%) [+${filesProcessed}]`,
|
2025-10-24 20:23:22 +08:00
|
|
|
|
"info"
|
|
|
|
|
|
);
|
|
|
|
|
|
lastScannedFiles = task.scanned_files;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 显示问题发现(仅在有变化时)
|
|
|
|
|
|
if (issuesChanged && task.issues_count > lastIssuesCount) {
|
|
|
|
|
|
const newIssues = task.issues_count - lastIssuesCount;
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[WARN] 发现 ${newIssues} 个新问题 (总计: ${task.issues_count})`, "warning");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
lastIssuesCount = task.issues_count;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 显示代码行数(仅在有变化时)
|
|
|
|
|
|
if (linesChanged && task.total_lines > lastTotalLines) {
|
|
|
|
|
|
const newLines = task.total_lines - lastTotalLines;
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[STAT] 已分析 ${task.total_lines.toLocaleString()} 行代码 [+${newLines.toLocaleString()}]`, "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
lastTotalLines = task.total_lines;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (task.status === "completed") {
|
|
|
|
|
|
// 任务完成
|
2025-11-28 22:55:32 +08:00
|
|
|
|
if (!isCompletedRef.current) {
|
2025-10-24 20:23:22 +08:00
|
|
|
|
addLog("", "info"); // 空行分隔
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("[DONE] 代码扫描完成", "success");
|
|
|
|
|
|
addLog("----------------------------------", "info");
|
|
|
|
|
|
addLog(`[STAT] 总计扫描: ${task.total_files} 个文件`, "success");
|
|
|
|
|
|
addLog(`[STAT] 总计分析: ${task.total_lines.toLocaleString()} 行代码`, "success");
|
|
|
|
|
|
addLog(`[RSLT] 发现问题: ${task.issues_count} 个`, task.issues_count > 0 ? "warning" : "success");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
|
|
|
|
|
// 解析问题类型分布
|
|
|
|
|
|
if (task.issues_count > 0) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { api: apiImport } = await import("@/shared/config/database");
|
|
|
|
|
|
const issues = await apiImport.getAuditIssues(taskId);
|
|
|
|
|
|
|
|
|
|
|
|
const severityCounts = {
|
|
|
|
|
|
critical: issues.filter(i => i.severity === 'critical').length,
|
|
|
|
|
|
high: issues.filter(i => i.severity === 'high').length,
|
|
|
|
|
|
medium: issues.filter(i => i.severity === 'medium').length,
|
|
|
|
|
|
low: issues.filter(i => i.severity === 'low').length
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (severityCounts.critical > 0) {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(` [CRIT] 严重: ${severityCounts.critical} 个`, "error");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (severityCounts.high > 0) {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(` [HIGH] 高: ${severityCounts.high} 个`, "warning");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (severityCounts.medium > 0) {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(` [MED] 中等: ${severityCounts.medium} 个`, "warning");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (severityCounts.low > 0) {
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(` [LOW] 低: ${severityCounts.low} 个`, "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
2025-11-28 22:55:32 +08:00
|
|
|
|
} catch (_e) {
|
2025-10-24 20:23:22 +08:00
|
|
|
|
// 静默处理错误
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[SCOR] 质量评分: ${task.quality_score.toFixed(1)}/100`, "success");
|
|
|
|
|
|
addLog("----------------------------------", "info");
|
|
|
|
|
|
addLog("[FIN] 审计任务已完成!", "success");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
|
|
|
|
|
if (task.completed_at) {
|
|
|
|
|
|
const startTime = new Date(task.created_at).getTime();
|
|
|
|
|
|
const endTime = new Date(task.completed_at).getTime();
|
|
|
|
|
|
const duration = Math.round((endTime - startTime) / 1000);
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog(`[TIME] 总耗时: ${duration} 秒`, "info");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setIsCompleted(true);
|
|
|
|
|
|
if (pollIntervalRef.current) {
|
|
|
|
|
|
clearInterval(pollIntervalRef.current);
|
|
|
|
|
|
pollIntervalRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-25 22:14:56 +08:00
|
|
|
|
} else if (task.status === "cancelled") {
|
|
|
|
|
|
// 任务被取消
|
2025-11-28 22:55:32 +08:00
|
|
|
|
if (!isCancelledRef.current) {
|
2025-10-25 22:14:56 +08:00
|
|
|
|
addLog("", "info"); // 空行分隔
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("[STOP] 任务已被用户取消", "warning");
|
|
|
|
|
|
addLog("----------------------------------", "warning");
|
|
|
|
|
|
addLog(`[STAT] 完成统计:`, "info");
|
2025-10-25 22:14:56 +08:00
|
|
|
|
addLog(` • 已分析文件: ${task.scanned_files}/${task.total_files}`, "info");
|
|
|
|
|
|
addLog(` • 发现问题: ${task.issues_count} 个`, "info");
|
|
|
|
|
|
addLog(` • 代码行数: ${task.total_lines.toLocaleString()} 行`, "info");
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("----------------------------------", "warning");
|
|
|
|
|
|
addLog("[SAVE] 已分析的结果已保存到数据库", "success");
|
2025-10-25 22:14:56 +08:00
|
|
|
|
|
|
|
|
|
|
setIsCancelled(true);
|
|
|
|
|
|
if (pollIntervalRef.current) {
|
|
|
|
|
|
clearInterval(pollIntervalRef.current);
|
|
|
|
|
|
pollIntervalRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-24 20:23:22 +08:00
|
|
|
|
} else if (task.status === "failed") {
|
|
|
|
|
|
// 任务失败
|
2025-11-28 22:55:32 +08:00
|
|
|
|
if (!isFailedRef.current) {
|
2025-10-24 20:23:22 +08:00
|
|
|
|
addLog("", "info"); // 空行分隔
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("[FAIL] 审计任务执行失败", "error");
|
|
|
|
|
|
addLog("----------------------------------", "error");
|
2025-10-29 19:25:38 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试从日志系统获取具体错误信息
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { logger } = await import("@/shared/utils/logger");
|
|
|
|
|
|
const recentLogs = logger.getLogs({
|
|
|
|
|
|
startTime: Date.now() - 60000, // 最近1分钟
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 查找与当前任务相关的错误
|
|
|
|
|
|
const taskErrors = recentLogs
|
|
|
|
|
|
.filter(log =>
|
|
|
|
|
|
log.level === 'ERROR' &&
|
|
|
|
|
|
(log.message.includes(taskId) ||
|
|
|
|
|
|
log.message.includes('审计') ||
|
|
|
|
|
|
log.message.includes('API'))
|
|
|
|
|
|
)
|
|
|
|
|
|
.slice(-3); // 最近3条错误
|
|
|
|
|
|
|
|
|
|
|
|
if (taskErrors.length > 0) {
|
|
|
|
|
|
addLog("具体错误信息:", "error");
|
|
|
|
|
|
taskErrors.forEach(log => {
|
|
|
|
|
|
addLog(` • ${log.message}`, "error");
|
|
|
|
|
|
if (log.data?.error) {
|
|
|
|
|
|
const errorMsg = typeof log.data.error === 'string'
|
|
|
|
|
|
? log.data.error
|
|
|
|
|
|
: log.data.error.message || JSON.stringify(log.data.error);
|
|
|
|
|
|
addLog(` ${errorMsg}`, "error");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 如果没有找到具体错误,显示常见原因
|
|
|
|
|
|
addLog("可能的原因:", "error");
|
|
|
|
|
|
addLog(" • 网络连接问题", "error");
|
|
|
|
|
|
addLog(" • 仓库访问权限不足(私有仓库需配置 Token)", "error");
|
|
|
|
|
|
addLog(" • GitHub/GitLab API 限流", "error");
|
|
|
|
|
|
addLog(" • LLM API 配置错误或额度不足", "error");
|
|
|
|
|
|
}
|
2025-11-28 22:55:32 +08:00
|
|
|
|
} catch (_e) {
|
2025-10-29 19:25:38 +08:00
|
|
|
|
// 如果获取日志失败,显示常见原因
|
|
|
|
|
|
addLog("可能的原因:", "error");
|
|
|
|
|
|
addLog(" • 网络连接问题", "error");
|
|
|
|
|
|
addLog(" • 仓库访问权限不足(私有仓库需配置 Token)", "error");
|
|
|
|
|
|
addLog(" • GitHub/GitLab API 限流", "error");
|
|
|
|
|
|
addLog(" • LLM API 配置错误或额度不足", "error");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-28 22:55:32 +08:00
|
|
|
|
addLog("----------------------------------", "error");
|
|
|
|
|
|
addLog("[HINT] 建议: 检查系统配置和网络连接后重试", "warning");
|
|
|
|
|
|
addLog("[LOGS] 查看完整日志: 导航栏 -> 系统日志", "warning");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
|
|
|
|
|
setIsFailed(true);
|
|
|
|
|
|
if (pollIntervalRef.current) {
|
|
|
|
|
|
clearInterval(pollIntervalRef.current);
|
|
|
|
|
|
pollIntervalRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-28 22:55:32 +08:00
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
|
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
|
|
|
|
|
addLog(`[ERR] ${errorMessage}`, "error");
|
2025-10-24 20:23:22 +08:00
|
|
|
|
// 不中断轮询,继续尝试
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 立即执行一次
|
|
|
|
|
|
pollTask();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置定时轮询(每2秒)
|
|
|
|
|
|
pollIntervalRef.current = window.setInterval(pollTask, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
// 清理函数
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
if (pollIntervalRef.current) {
|
|
|
|
|
|
clearInterval(pollIntervalRef.current);
|
|
|
|
|
|
pollIntervalRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-11-28 22:55:32 +08:00
|
|
|
|
}, [open, taskId, taskType, addLog]);
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
// 获取日志颜色
|
2025-10-24 20:23:22 +08:00
|
|
|
|
const getLogColor = (type: LogEntry["type"]) => {
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
case "success":
|
2025-12-13 12:35:03 +08:00
|
|
|
|
return "text-emerald-400";
|
2025-10-24 20:23:22 +08:00
|
|
|
|
case "error":
|
2025-12-13 12:35:03 +08:00
|
|
|
|
return "text-rose-400";
|
2025-10-24 20:23:22 +08:00
|
|
|
|
case "warning":
|
2025-12-13 12:35:03 +08:00
|
|
|
|
return "text-amber-400";
|
2025-10-24 20:23:22 +08:00
|
|
|
|
default:
|
2025-12-13 12:35:03 +08:00
|
|
|
|
return "text-gray-400";
|
2025-10-24 20:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
|
<DialogPortal>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
<DialogOverlay className="bg-black/80 backdrop-blur-sm" />
|
2025-10-24 20:23:22 +08:00
|
|
|
|
<DialogPrimitive.Content
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"fixed left-[50%] top-[50%] z-50 translate-x-[-50%] translate-y-[-50%]",
|
2025-11-28 22:55:32 +08:00
|
|
|
|
"w-[95vw] max-w-[1000px] h-[85vh] max-h-[700px]",
|
2025-12-13 12:35:03 +08:00
|
|
|
|
"cyber-card p-0 gap-0 overflow-hidden",
|
2025-10-24 20:23:22 +08:00
|
|
|
|
"data-[state=open]:animate-in data-[state=closed]:animate-out",
|
|
|
|
|
|
"data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
|
|
|
|
|
|
"data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
|
2025-12-13 12:35:03 +08:00
|
|
|
|
"duration-300 font-mono"
|
2025-10-24 20:23:22 +08:00
|
|
|
|
)}
|
|
|
|
|
|
onPointerDownOutside={(e) => e.preventDefault()}
|
|
|
|
|
|
onInteractOutside={(e) => e.preventDefault()}
|
|
|
|
|
|
>
|
|
|
|
|
|
<VisuallyHidden.Root>
|
|
|
|
|
|
<DialogPrimitive.Title>审计进度监控</DialogPrimitive.Title>
|
|
|
|
|
|
<DialogPrimitive.Description>
|
|
|
|
|
|
实时显示代码审计任务的执行进度和详细信息
|
|
|
|
|
|
</DialogPrimitive.Description>
|
|
|
|
|
|
</VisuallyHidden.Root>
|
|
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
{/* Header */}
|
|
|
|
|
|
<div className="cyber-card-header justify-between">
|
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
|
<Terminal className="w-5 h-5 text-primary" />
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<span className="text-lg font-bold uppercase tracking-wider text-white">AUDIT_TERMINAL</span>
|
|
|
|
|
|
<span className="text-xs text-gray-500 ml-2">v2.0</span>
|
2025-11-27 21:33:51 +08:00
|
|
|
|
</div>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
|
|
{/* 状态指示灯 */}
|
|
|
|
|
|
<div className="flex items-center gap-2 px-3 py-1 bg-gray-900 rounded border border-gray-800">
|
|
|
|
|
|
<div className={`w-2 h-2 rounded-full ${!isCompleted && !isFailed && !isCancelled ? 'bg-emerald-400 shadow-[0_0_6px_rgba(52,211,153,0.6)] animate-pulse' : 'bg-gray-600'}`} />
|
|
|
|
|
|
<div className={`w-2 h-2 rounded-full ${isFailed ? 'bg-rose-400 shadow-[0_0_6px_rgba(251,113,133,0.6)]' : 'bg-gray-600'}`} />
|
|
|
|
|
|
<div className={`w-2 h-2 rounded-full ${isCompleted ? 'bg-sky-400 shadow-[0_0_6px_rgba(56,189,248,0.6)]' : 'bg-gray-600'}`} />
|
2025-11-27 21:33:51 +08:00
|
|
|
|
</div>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
|
2025-10-24 20:23:22 +08:00
|
|
|
|
<button
|
2025-11-28 22:55:32 +08:00
|
|
|
|
type="button"
|
2025-12-13 12:35:03 +08:00
|
|
|
|
className="w-8 h-8 flex items-center justify-center hover:bg-rose-500/20 rounded transition-colors"
|
2025-10-24 20:23:22 +08:00
|
|
|
|
onClick={() => onOpenChange(false)}
|
2025-11-27 21:33:51 +08:00
|
|
|
|
>
|
2025-12-13 12:35:03 +08:00
|
|
|
|
<XIcon className="w-5 h-5 text-gray-400 hover:text-rose-400" />
|
2025-11-27 21:33:51 +08:00
|
|
|
|
</button>
|
2025-10-24 20:23:22 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
{/* Main Content */}
|
|
|
|
|
|
<div className="flex h-[calc(100%-56px)]">
|
|
|
|
|
|
{/* Left Sidebar - Task Info */}
|
|
|
|
|
|
<div className="w-48 p-4 border-r border-gray-800 bg-gray-900/30 flex flex-col gap-4">
|
|
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
|
<div className="text-[10px] font-bold text-gray-600 uppercase tracking-wider">Task ID</div>
|
|
|
|
|
|
<div className="text-xs font-mono text-primary truncate bg-gray-900 p-2 rounded border border-gray-800">
|
|
|
|
|
|
{taskId?.slice(0, 8)}...
|
|
|
|
|
|
</div>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
</div>
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
|
<div className="text-[10px] font-bold text-gray-600 uppercase tracking-wider">Type</div>
|
|
|
|
|
|
<div className="flex items-center gap-2 bg-gray-900 p-2 rounded border border-gray-800">
|
|
|
|
|
|
{taskType === 'repository' ? <Cpu className="w-3 h-3 text-sky-400" /> : <HardDrive className="w-3 h-3 text-amber-400" />}
|
|
|
|
|
|
<span className="text-xs font-bold text-gray-300 uppercase">{taskType}</span>
|
2025-10-24 20:23:22 +08:00
|
|
|
|
</div>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
</div>
|
2025-10-24 20:23:22 +08:00
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
<div className="flex-1" />
|
|
|
|
|
|
|
|
|
|
|
|
{/* Status Badge */}
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
|
<div className="text-[10px] font-bold text-gray-600 uppercase tracking-wider">Status</div>
|
|
|
|
|
|
{isCancelled ? (
|
|
|
|
|
|
<Badge className="w-full justify-center cyber-badge-warning">CANCELLED</Badge>
|
|
|
|
|
|
) : isCompleted ? (
|
|
|
|
|
|
<Badge className="w-full justify-center cyber-badge-success">COMPLETED</Badge>
|
|
|
|
|
|
) : isFailed ? (
|
|
|
|
|
|
<Badge className="w-full justify-center cyber-badge-danger">FAILED</Badge>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Badge className="w-full justify-center cyber-badge-info animate-pulse">RUNNING</Badge>
|
|
|
|
|
|
)}
|
2025-11-28 22:55:32 +08:00
|
|
|
|
</div>
|
2025-11-27 21:33:51 +08:00
|
|
|
|
</div>
|
2025-10-29 19:25:38 +08:00
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
{/* Terminal Screen */}
|
|
|
|
|
|
<div className="flex-1 flex flex-col">
|
|
|
|
|
|
{/* Terminal Output */}
|
|
|
|
|
|
<div className="flex-1 bg-[#0a0a0f] p-4 overflow-y-auto font-mono text-sm custom-scrollbar relative">
|
|
|
|
|
|
{/* Grid background */}
|
|
|
|
|
|
<div className="absolute inset-0 cyber-grid-subtle pointer-events-none opacity-30" />
|
|
|
|
|
|
|
|
|
|
|
|
<div className="relative z-10 space-y-0.5 pb-10">
|
|
|
|
|
|
{logs.map((log) => (
|
|
|
|
|
|
<div key={log.id} className="flex items-start gap-3 hover:bg-white/5 px-2 py-0.5 transition-colors group rounded">
|
|
|
|
|
|
<span className="text-gray-600 text-xs flex-shrink-0 w-20 font-mono">
|
|
|
|
|
|
{log.timestamp}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span className={`${getLogColor(log.type)} flex-1 font-mono text-sm`}>
|
|
|
|
|
|
{log.message}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
|
|
{!isCompleted && !isFailed && !isCancelled && (
|
|
|
|
|
|
<div className="flex items-center gap-3 mt-4 px-2">
|
|
|
|
|
|
<span className="text-gray-600 text-xs w-20 font-mono">{currentTime}</span>
|
|
|
|
|
|
<span className="text-primary animate-pulse font-bold">_</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<div ref={logsEndRef} />
|
2025-11-28 22:55:32 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
{/* Bottom Controls */}
|
|
|
|
|
|
<div className="h-14 px-4 border-t border-gray-800 bg-gray-900/50 flex items-center justify-between">
|
|
|
|
|
|
<div className="flex items-center gap-2 text-xs text-gray-500 font-mono">
|
|
|
|
|
|
<Activity className="w-3 h-3" />
|
|
|
|
|
|
<span>
|
|
|
|
|
|
{isCompleted ? "任务已完成" : isFailed ? "任务失败" : isCancelled ? "任务已取消" : "正在执行..."}
|
|
|
|
|
|
</span>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-12-13 12:35:03 +08:00
|
|
|
|
<div className="flex items-center gap-3">
|
2025-11-28 22:55:32 +08:00
|
|
|
|
{!isCompleted && !isFailed && !isCancelled && (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={handleCancel}
|
2025-12-13 12:35:03 +08:00
|
|
|
|
className="h-8 cyber-btn-outline text-amber-400 border-amber-500/30 hover:bg-amber-500/10 hover:border-amber-500/50"
|
2025-11-28 22:55:32 +08:00
|
|
|
|
>
|
|
|
|
|
|
<AlertTriangle className="w-3 h-3 mr-1" />
|
|
|
|
|
|
取消任务
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{isFailed && (
|
2025-12-13 12:35:03 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
variant="outline"
|
2025-11-28 22:55:32 +08:00
|
|
|
|
onClick={() => window.open('/logs', '_blank')}
|
2025-12-13 12:35:03 +08:00
|
|
|
|
className="h-8 cyber-btn-outline"
|
2025-11-28 22:55:32 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Activity className="w-3 h-3 mr-1" />
|
|
|
|
|
|
查看日志
|
2025-12-13 12:35:03 +08:00
|
|
|
|
</Button>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{(isCompleted || isFailed || isCancelled) && (
|
2025-12-13 12:35:03 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
size="sm"
|
2025-11-28 22:55:32 +08:00
|
|
|
|
onClick={() => onOpenChange(false)}
|
2025-12-13 12:35:03 +08:00
|
|
|
|
className="h-8 cyber-btn-primary"
|
2025-11-28 22:55:32 +08:00
|
|
|
|
>
|
|
|
|
|
|
<CheckCircle2 className="w-3 h-3 mr-1" />
|
|
|
|
|
|
确认
|
2025-12-13 12:35:03 +08:00
|
|
|
|
</Button>
|
2025-11-28 22:55:32 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-10-24 20:23:22 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</DialogPrimitive.Content>
|
|
|
|
|
|
</DialogPortal>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|