Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

在你是一位资深的前端程序员,你将修复下面这一段即将生产环境使用(因此,你需要审慎地生成你的代码)的、完整的,并非是示例的HTML程序源代码(注:使用适合的思维导图外部库,将htm…

  1. User

    在你是一位资深的前端程序员,你将修复下面这一段即将生产环境使用(因此,你需要审慎地生成你的代码)的、完整的,并非是示例的HTML程序源代码(注:使用适合的思维导图外部库,将html/css/js写在同一个文件,并只用写在同一个代码框中,代码不能太简单,要满足下面所有的需求),用于将特定格式的日志进行节点化的可视化展示和基于结构化、思维导图化的精简化处理,对标Xmind。该程序的主要功能是解析并展示以下格式的日志信息:

    部分示例日志: I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: KpiInfoCache::getCustomKpi,retsize=255,netype=-9,solutionVer=3100 I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: getKPIFormula use cache I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: KpiMgrComponentImpl::getKPIFormula Exited... I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: KpiMgrComponentImpl::getRawItemFormula Entered... I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: KpiInfoCache::getCustomKpi,retsize=255 I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: getRawItemFormula use cache neTypeId:295 versionId:1586450140 I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: KpiMgrComponentImpl::getRawItemFormula Exited... I001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: KpiMgrComponentImpl::getFormulaByVersion_i Exited... E001:10-26 10:33:23.194(2355912|2854059)Sum.Rpt: mapCounterParseInfo number:1854 日志解析规则 日志等级:日志开头字符表示日志等级,I表示信息日志,E表示错误日志。 时间戳:日志格式中的10-26 10:33:23.192部分表示日志发生的时间。 进程和线程号:(2355912|2400359)分别表示进程号和线程号。 日志内容:日志正文内容在其后的部分。 如果内容以Entered...或Exited...结尾,表示函数的进入或退出; 如果内容并不以上述内容结尾,则这只是一般的日志。

    可视化需求 输入方式:日志文件通过 文本区域输入。

    还需要修复下面的功能: 1、节点右键菜单添加“备注”功能,可以在指定节点编辑备注信息,并可以编辑节点的颜色(只能在7种预设的颜色中选择),并支持保存。备注完成后,在节点的小字区域(显示线程号、时间的部分)显示用户的备注,自动换行。 2、当节点的内容超出了显示范围时,应该把超出部分隐藏。 3、支持将日志跟踪、线程跟踪按图片导出,支持导入导出功能(备注也要支持导入导出)。 需要反思如何把这些功能做好,不能使用iframe。

    <!-- 自定义右键菜单 -->
    <div id="context-menu" class="custom-context-menu">
        <ul>
            <li id="toggle-importance">标记/去标记重要性</li>
            <li id="generate-sequence">生成时序图</li>
        </ul>
    </div>
    
    <!-- Tooltip -->
    <div id="tooltip" class="tooltip" style="opacity:0;"></div>
    
    <script>
        // 全局变量存储解析后的日志数据
        let parsedLogs = {};
        let threadParents = {};
        let markedNodes = new Set();
    
        let selectedNode = {
            type: null, // 'thread' 或 'log'
            id: null,
            data: null,
            tree: null
        };
    
        // 右键菜单元素
        const contextMenu = document.getElementById('context-menu');
        const toggleImportanceMenu = document.getElementById('toggle-importance');
        const generateSequenceMenu = document.getElementById('generate-sequence');
    
        // Tooltip元素
        const tooltip = d3.select("#tooltip");
    
        // 事件绑定:解析并可视化日志
        document.getElementById('parse-button').addEventListener('click', function() {
            const logText = document.getElementById('log-input').value;
            parseLogs(logText);
            renderThreadTree();
            clearLogTree();
        });
    
        /**
         * 解析日志文本
         * @param {string} text - 日志文本
         */
        function parseLogs(text) {
            const lines = text.split('\n').filter(line => line.trim() !== '');
            parsedLogs = {};
            threadParents = {};
    
            const logEntries = [];
    
            const regex = /^(I|E)\d{3}:(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\((\d+)\|(\d+)\)Sum\.Rpt: (.+)$/;
    
            for (const line of lines) {
                const match = line.match(regex);
                if (match) {
                    const [_, level, timestamp, processId, threadId, content] = match;
                    const log = { level, timestamp, processId, threadId, content, raw: line };
                    logEntries.push(log);
                    if (!parsedLogs[threadId]) {
                        parsedLogs[threadId] = [];
                    }
                    parsedLogs[threadId].push(log);
                }
            }
    
            // 处理线程关系
            inferThreadRelationships(logEntries);
        }
    
        /**
         * 推断线程之间的关系
         * 简化假设:一个线程的启动时间在另一个线程的启动时间之后,则视为子线程
         * @param {Array} logEntries - 所有日志条目
         */
        function inferThreadRelationships(logEntries) {
            // 获取每个线程的最早时间
            const threadStartTimes = {};
            for (const threadId in parsedLogs) {
                const firstLog = parsedLogs[threadId][0];
                // 假设日期为10-26,年份为2023
                const date = new Date(`2023-10-26T${convertTimestamp(firstLog.timestamp)}Z`);
                threadStartTimes[threadId] = date;
            }
    
            // 排序线程按开始时间
            const sortedThreads = Object.keys(threadStartTimes).sort((a, b) => threadStartTimes[a] - threadStartTimes[b]);
    
            if (sortedThreads.length === 0) return;
    
            // 使用虚拟根节点
            threadParents['root'] = null; // 虚拟根
    
            // 第一线程作为根线程的子线程
            for (let i = 0; i < sortedThreads.length; i++) {
                const childThread = sortedThreads[i];
                if (i === 0) {
                    threadParents[childThread] = 'root';
                } else {
                    // 根据启动时间找到最近的一个父线程
                    let parentThread = 'root';
                    for (let j = i - 1; j >= 0; j--) {
                        const potentialParent = sortedThreads[j];
                        if (threadStartTimes[potentialParent] < threadStartTimes[childThread]) {
                            parentThread = potentialParent;
                            break;
                        }
                    }
                    threadParents[childThread] = parentThread;
                }
            }
        }
    
        /**
         * 转换时间戳为ISO格式
         * @param {string} timestamp - 原始时间戳(MM-DD HH:MM:SS.mmm)
         * @returns {string} - 转换后的时间戳(HH:MM:SS.mmm)
         */
        function convertTimestamp(timestamp) {
            return timestamp.replace(' ', 'T');
        }
    
        /**
         * 渲染线程关系树
         */
        function renderThreadTree() {
            // 清除旧的SVG
            d3.select("#thread-tree").selectAll("*").remove();
    
            // 构建树形结构数据,包括虚拟根
            const treeData = buildThreadTree('root', threadParents);
    
            const margin = { top: 20, right: 200, bottom: 20, left: 200 },
                  width = document.getElementById('thread-tree').clientWidth - margin.left - margin.right,
                  height = document.getElementById('thread-tree').clientHeight - margin.top - margin.bottom;
    
            const svg = d3.select("#thread-tree")
                .attr("width", width + margin.left + margin.right)
                .attr("height", height + margin.top + margin.bottom)
              .append("g")
                .attr("transform", `translate(${margin.left},${margin.top})`);
    
            // 创建树布局,使用固定的节点大小
            const treemap = d3.tree()
                .nodeSize([100, 300]); // [垂直间距, 水平间距]
    
            // 创建层级结构
            const root = d3.hierarchy(treeData, d => d.children);
    
            // 生成树布局
            treemap(root);
    
            // Links
            const link = svg.selectAll(".link")
                .data(root.links())
              .enter().append("path")
                .attr("class", "link")
                .attr("d", d3.linkHorizontal()
                    .x(d => d.y)
                    .y(d => d.x))
                .attr("fill", "none")
                .attr("stroke", "#ccc")
                .attr("stroke-width", 2);
    
            // Nodes
            const node = svg.selectAll(".node")
                .data(root.descendants())
              .enter().append("g")
                .attr("class", "node")
                .attr("transform", d => `translate(${d.y},${d.x})`)
                .on("click", (event, d) => {
                    if (d.data.id !== 'root') { // 排除虚拟根
                        const threadId = d.data.id;
                        renderLogTree(threadId);
                    }
                })
                .on("contextmenu", (event, d) => {
                    event.preventDefault();
                    if (d.data.id !== 'root') { // 排除虚拟根
                        selectedNode = { type: 'thread', id: d.data.id, data: d.data, tree: 'thread' };
                        showContextMenu(event.pageX, event.pageY);
                    }
                });
    
            // Node rectangles
            node.append("rect")
                .attr("width", 150)
                .attr("height", 40)
                .attr("x", -75)
                .attr("y", -20)
                .attr("rx", 5)
                .attr("ry", 5)
                .attr("fill", d => d.data.id === 'root' ? "#ffffff00" : "#4FC3F7")
                .attr("stroke", d => d.data.id === 'root' ? "none" : "#0288D1")
                .attr("stroke-width", 2)
                .attr("opacity", d => markedNodes.has(d.data.id) ? 0.5 : 1.0);
    
            // Node labels
            node.append("text")
                .attr("dy", 5)
                .attr("text-anchor", "middle")
                .attr("fill", "#ffffff")
                .style("pointer-events", "none")
                .text(d => d.data.id === 'root' ? '' : `线程 ${d.data.id}`);
    
            // 为树添加缩放和平移
            const zoom = d3.zoom()
                .scaleExtent([0.5, 2])
                .on("zoom", (event) => {
                    svg.attr("transform", event.transform);
                });
    
            d3.select("#thread-tree").call(zoom);
        }
    
        /**
         * 构建线程树结构,包括虚拟根
         * @param {string} rootId - 虚拟根ID
         * @param {Object} parents - 线程父子关系
         * @returns {Object} - 树形结构
         */
        function buildThreadTree(rootId, parents) {
            const node = { id: rootId, children: [] };
            for (const child in parents) {
                if (parents[child] === rootId && child !== 'root') {
                    node.children.push(buildThreadTree(child, parents));
                }
            }
            return node;
        }
    
        /**
         * 清除日志树
         */
        function clearLogTree() {
            d3.select("#log-tree").selectAll("*").remove();
            const svg = d3.select("#log-tree");
            svg.append("text")
                .attr("x", "50%")
                .attr("y", "50%")
                .attr("text-anchor", "middle")
                .attr("fill", "#7f8c8d")
                .text("请选择左侧的线程以查看日志");
        }
    
        /**
         * 渲染指定线程的日志树
         * @param {string} threadId - 线程ID
         */
        function renderLogTree(threadId) {
            const logs = parsedLogs[threadId];
            if (!logs) {
                alert('没有找到该线程的日志。');
                return;
            }
    
            // 构建层级结构
            const treeData = buildLogTree(logs);
    
            // 清除旧的SVG
            d3.select("#log-tree").selectAll("*").remove();
    
            const margin = { top: 20, right: 200, bottom: 20, left: 200 },
                  width = document.getElementById('log-tree').clientWidth - margin.left - margin.right,
                  height = document.getElementById('log-tree').clientHeight - margin.top - margin.bottom;
    
            const svg = d3.select("#log-tree")
                .attr("width", width + margin.left + margin.right)
                .attr("height", height + margin.top + margin.bottom)
              .append("g")
                .attr("transform", `translate(${margin.left},${margin.top})`);
    
            // 创建树布局,使用固定的节点大小
            const treemap = d3.tree()
                .nodeSize([80, 400]); // [垂直间距, 水平间距]
    
            // 创建层级结构
            const root = d3.hierarchy(treeData, d => d.children);
    
            // 生成树布局
            treemap(root);
    
            // Links
            const link = svg.selectAll(".link")
                .data(root.links())
              .enter().append("path")
                .attr("class", "link")
                .attr("d", d3.linkHorizontal()
                    .x(d => d.y)
                    .y(d => d.x))
                .attr("fill", "none")
                .attr("stroke", "#ccc")
                .attr("stroke-width", 2);
    
            // Nodes
            const node = svg.selectAll(".node")
                .data(root.descendants())
              .enter().append("g")
                .attr("class", "node")
                .attr("transform", d => `translate(${d.y},${d.x})`)
                .on("contextmenu", (event, d) => {
                    event.preventDefault();
                    selectedNode = { type: 'log', id: d.data.id, data: d.data, tree: 'log' };
                    showContextMenu(event.pageX, event.pageY);
                })
                .on("mouseover", (event, d) => {
                    tooltip.transition().duration(200).style("opacity", .9);
                    tooltip.html(formatTooltip(d.data))
                        .style("left", (event.pageX + 10) + "px")
                        .style("top", (event.pageY - 28) + "px");
                })
                .on("mouseout", () => {
                    tooltip.transition().duration(500).style("opacity", 0);
                });
    
            // Node rectangles
            node.append("rect")
                .attr("width", 250)
                .attr("height", 60)
                .attr("x", -125)
                .attr("y", -30)
                .attr("rx", 5)
                .attr("ry", 5)
                .attr("fill", d => getColorByLevel(d.data.level))
                .attr("stroke", "#424242")
                .attr("stroke-width", 1)
                .attr("opacity", d => markedNodes.has(d.data.id) ? 0.5 : 1.0);
    
            // Node labels
            node.append("text")
                .attr("dy", -5)
                .attr("text-anchor", "middle")
                .attr("fill", "#ffffff")
                .style("font-weight", "bold")
                .text(d => d.data.content.split(',')[0]); // 显示主要内容
    
            node.append("text")
                .attr("dy", 15)
                .attr("text-anchor", "middle")
                .attr("fill", "#d1d1d1")
                .style("font-size", "10px")
                .text(d => `[${d.data.timestamp}] | 线程:${d.data.threadId}`);
    
            // 为树添加缩放和平移
            const zoom = d3.zoom()
                .scaleExtent([0.5, 2])
                .on("zoom", (event) => {
                    svg.attr("transform", event.transform);
                });
    
            d3.select("#log-tree").call(zoom);
        }
    
        /**
         * 构建日志树结构
         * @param {Array} logs - 日志数组
         * @returns {Object} - 树形结构
         */
        function buildLogTree(logs) {
            const root = { id: 0, content: "根", children: [], level: "I" };
            let currentNode = root;
            let stack = [root];
            let nodeId = 1;
    
            for (const log of logs) {
                const isEntry = /Entered\.\.\.$/.test(log.content);
                const isExit = /Exited\.\.\.$/.test(log.content);
    
                if (isEntry) {
                    const newNode = {
                        id: nodeId++,
                        content: log.content.replace(/ Entered\.\.\.$/, ''),
                        timestamp: log.timestamp,
                        threadId: log.threadId,
                        level: log.level,
                        children: []
                    };
                    currentNode.children.push(newNode);
                    stack.push(newNode);
                    currentNode = newNode;
                } else if (isExit) {
                    if (stack.length > 1) {
                        stack.pop();
                        currentNode = stack[stack.length - 1];
                    }
                } else {
                    const newNode = {
                        id: nodeId++,
                        content: log.content,
                        timestamp: log.timestamp,
                        threadId: log.threadId,
                        level: log.level,
                        children: []
                    };
                    currentNode.children.push(newNode);
                }
            }
    
            return root;
        }
    
        /**
         * 格式化Tooltip内容
         * @param {Object} data - 节点数据
         * @returns {string} - Tooltip的HTML内容
         */
        function formatTooltip(data) {
            return `<strong>时间:</strong> ${data.timestamp}<br/>
                    <strong>级别:</strong> ${data.level}<br/>
                    <strong>内容:</strong> ${data.content}`;
        }
    
        /**
         * 获取节点颜色根据日志级别
         * @param {string} level - 日志级别
         * @returns {string} - 颜色代码
         */
        function getColorByLevel(level) {
            if (level === 'E') return '#e57373';
            if (level === 'I') return '#64b5f6';
            return '#A1887F';
        }
    
        /**
         * 显示自定义右键菜单
         * @param {number} x - 鼠标点击的X坐标
         * @param {number} y - 鼠标点击的Y坐标
         */
        function showContextMenu(x, y) {
            contextMenu.style.left = `${x}px`;
            contextMenu.style.top = `${y}px`;
            contextMenu.style.display = 'block';
        }
    
        /**
         * 隐藏自定义右键菜单
         */
        function hideContextMenu() {
            contextMenu.style.display = 'none';
            selectedNode = { type: null, id: null, data: null, tree: null };
        }
    
        /**
         * 切换节点的重要性(透明度)
         */
        function toggleNodeImportance() {
            if (!selectedNode.id) return;
            if (markedNodes.has(selectedNode.id)) {
                markedNodes.delete(selectedNode.id);
            } else {
                markedNodes.add(selectedNode.id);
            }
    
            if (selectedNode.type === 'log') {
                // 更新日志树节点的透明度
                d3.selectAll("#log-tree .node").filter(d => d.data.id === selectedNode.id)
                    .select("rect")
                    .transition()
                    .duration(300)
                    .attr("opacity", markedNodes.has(selectedNode.id) ? 0.5 : 1.0);
            } else if (selectedNode.type === 'thread') {
                // 更新线程树节点的透明度
                d3.selectAll("#thread-tree .node").filter(d => d.data.id === selectedNode.id)
                    .select("rect")
                    .transition()
                    .duration(300)
                    .attr("opacity", markedNodes.has(selectedNode.id) ? 0.5 : 1.0);
            }
            hideContextMenu();
        }
    
        /**
         * 生成PlantUML时序图代码并复制到剪贴板
         */
        function generateSequenceDiagram() {
            if (!selectedNode.id) return;
            let plantUMLCode = "@startuml\n";
            if (selectedNode.tree === 'log') {
                const logNode = selectedNode.data;
                const callStack = getCallStack(logNode);
    
                callStack.forEach((node, index) => {
                    plantUMLCode += `participant ${sanitizeParticipantName(node.content)}\n`;
                    if (index > 0) {
                        plantUMLCode += `${sanitizeParticipantName(callStack[index - 1].content)} -> ${sanitizeParticipantName(node.content)} : 调用\n`;
                    }
                });
    
                plantUMLCode += "@enduml";
    
                // 复制到剪贴板
                copyToClipboard(plantUMLCode);
                alert('PlantUML时序图代码已复制到剪贴板。');
            } else if (selectedNode.tree === 'thread') {
                // 线程级别的时序图生成(可根据需求扩展)
                alert('线程级别的时序图生成尚未实现。');
            }
            hideContextMenu();
        }
    
        /**
         * 获取调用栈
         * @param {Object} node - 当前节点数据
         * @returns {Array} - 调用栈数组
         */
        function getCallStack(node) {
            const stack = [];
            let current = node;
            while (current.parent) {
                stack.push(current);
                current = current.parent.data;
            }
            return stack;
        }
    
        /**
         * 清理和格式化参与者名称
         * @param {string} name - 原始名称
         * @returns {string} - 清理后的名称
         */
        function sanitizeParticipantName(name) {
            return name.replace(/[^a-zA-Z0-9_\-]/g, '_');
        }
    
        /**
         * 复制文本到剪贴板
         * @param {string} text - 要复制的文本
         */
        function copyToClipboard(text) {
            const textarea = document.createElement('textarea');
            textarea.value = text;
            document.body.appendChild(textarea);
            textarea.select();
            try {
                document.execCommand('copy');
            } catch (err) {
                console.error('复制失败', err);
            }
            document.body.removeChild(textarea);
        }
    
        // 右键菜单选项事件
        toggleImportanceMenu.addEventListener('click', toggleNodeImportance);
        generateSequenceMenu.addEventListener('click', generateSequenceDiagram);
    
        // 点击其他地方隐藏右键菜单
        document.addEventListener('click', function(e) {
            if (e.button !== 2) { // 非右键点击
                hideContextMenu();
            }
        });
    </script>
    
  2. Assistant
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>日志可视化工具</title>
        <!-- D3.js CDN -->
        <script src="https://d3js.org/d3.v7.min.js"></script>
        <!-- FileSaver.js CDN for exporting files -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
        <!-- jscolor for color picker -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.4.5/jscolor.min.js"></script>
        <!-- Basic Styles -->
        <style>
            body, html {
                margin: 0;
                padding: 0;
                height: 100%;
                font-family: Arial, sans-serif;
                overflow: hidden;
            }
            header {
                padding: 10px;
                background-color: #2c3e50;
                color: white;
                text-align: center;
            }
            #controls {
                padding: 10px;
                background-color: #ecf0f1;
                overflow: auto;
            }
            #controls textarea {
                width: 100%;
                height: 150px;
                resize: vertical;
                padding: 10px;
                box-sizing: border-box;
                font-family: monospace;
                font-size: 14px;
            }
            #controls button {
                margin-top: 10px;
                padding: 10px 20px;
                font-size: 16px;
                background-color: #2980b9;
                color: white;
                border: none;
                cursor: pointer;
                border-radius: 4px;
            }
            #controls button:hover {
                background-color: #3498db;
            }
            #export-controls {
                margin-top: 10px;
            }
            #export-controls button {
                margin-right: 10px;
                padding: 8px 16px;
                font-size: 14px;
                background-color: #27ae60;
                color: white;
                border: none;
                cursor: pointer;
                border-radius: 4px;
            }
            #export-controls button:hover {
                background-color: #2ecc71;
            }
            #main {
                display: flex;
                height: calc(100% - 220px);
                overflow: hidden;
            }
            .container {
                flex: 1;
                position: relative;
                border: 1px solid #bdc3c7;
                overflow: hidden;
            }
            .container::after {
                content: attr(data-title);
                position: absolute;
                top: 5px;
                left: 10px;
                background: rgba(255,255,255,0.8);
                padding: 2px 5px;
                border-radius: 3px;
                font-weight: bold;
                z-index: 1;
            }
            .tree-container {
                width: 100%;
                height: 100%;
            }
            /* Custom Context Menu Styles */
            .custom-context-menu {
                position: absolute;
                z-index: 1000;
                width: 220px;
                background: white;
                border: 1px solid #bdc3c7;
                box-shadow: 2px 2px 5px rgba(0,0,0,0.3);
                display: none;
            }
            .custom-context-menu ul {
                list-style: none;
                margin: 0;
                padding: 5px 0;
            }
            .custom-context-menu ul li {
                padding: 8px 12px;
                cursor: pointer;
            }
            .custom-context-menu ul li:hover {
                background-color: #ecf0f1;
            }
            /* Tooltip Styles */
            .tooltip {
                position: absolute;
                text-align: left;
                width: auto;
                padding: 10px;
                font: 12px sans-serif;
                background: rgba(255, 255, 255, 0.9);
                border: 1px solid #bdc3c7;
                border-radius: 4px;
                pointer-events: none;
                box-shadow: 2px 2px 5px rgba(0,0,0,0.3);
            }
            /* Modal Styles */
            .modal {
                display: none; 
                position: fixed; 
                z-index: 2000; 
                left: 0;
                top: 0;
                width: 100%; 
                height: 100%; 
                overflow: auto; 
                background-color: rgba(0,0,0,0.4); 
            }
            .modal-content {
                background-color: #fefefe;
                margin: 10% auto; 
                padding: 20px;
                border: 1px solid #888;
                width: 300px; 
                border-radius: 5px;
            }
            .modal-content h2 {
                margin-top: 0;
            }
            .modal-content textarea {
                width: 100%;
                height: 80px;
                resize: vertical;
                padding: 5px;
                box-sizing: border-box;
                font-family: Arial, sans-serif;
                font-size: 14px;
            }
            .modal-content .color-options {
                margin-top: 10px;
            }
            .modal-content .color-options label {
                margin-right: 10px;
                cursor: pointer;
            }
            .modal-content .buttons {
                margin-top: 15px;
                text-align: right;
            }
            .modal-content .buttons button {
                padding: 6px 12px;
                margin-left: 10px;
                font-size: 14px;
                border: none;
                border-radius: 4px;
                cursor: pointer;
            }
            .modal-content .buttons .save-btn {
                background-color: #27ae60;
                color: white;
            }
            .modal-content .buttons .cancel-btn {
                background-color: #e74c3c;
                color: white;
            }
            /* Scrollbar Styles */
            ::-webkit-scrollbar {
                width: 8px;
                height: 8px;
            }
            ::-webkit-scrollbar-track {
                background: #ecf0f1;
            }
            ::-webkit-scrollbar-thumb {
                background: #bdc3c7;
                border-radius: 4px;
            }
            ::-webkit-scrollbar-thumb:hover {
                background: #95a5a6;
            }
        </style>
    </head>
    <body>
        <header>
            <h1>日志可视化工具</h1>
        </header>
        <div id="controls">
            <textarea id="log-input" placeholder="在此处粘贴日志内容..."></textarea>
            <button id="parse-button">解析并可视化</button>
            <div id="export-controls">
                <button id="export-thread">导出线程关系图</button>
                <button id="export-log">导出日志跟踪图</button>
                <button id="export-data">导出数据(JSON)</button>
                <button id="import-data">导入数据(JSON)</button>
                <input type="file" id="import-file" accept=".json" style="display:none;">
            </div>
        </div>
        <div id="main">
            <div class="container" data-title="线程关系">
                <svg id="thread-tree" class="tree-container"></svg>
            </div>
            <div class="container" data-title="日志跟踪">
                <svg id="log-tree" class="tree-container"></svg>
            </div>
        </div>
    
        <!-- Custom Context Menu -->
        <div id="context-menu" class="custom-context-menu">
            <ul>
                <li id="mark-importance">标记重要性</li>
                <li id="unmark-importance">去标记重要性</li>
                <li id="add-remark">添加备注</li>
                <li id="export-sequence">导出时序图</li>
            </ul>
        </div>
    
        <!-- Tooltip -->
        <div id="tooltip" class="tooltip" style="opacity:0;"></div>
    
        <!-- Modal for Adding Remarks -->
        <div id="remark-modal" class="modal">
            <div class="modal-content">
                <h2>添加备注</h2>
                <textarea id="remark-text" placeholder="输入备注内容..."></textarea>
                <div class="color-options">
                    <label>选择颜色:</label>
                    <input class="jscolor" id="remark-color" value="FFFFFF">
                </div>
                <div class="buttons">
                    <button class="save-btn" id="save-remark">保存</button>
                    <button class="cancel-btn" id="cancel-remark">取消</button>
                </div>
            </div>
        </div>
    
        <script>
            // Global Variables
            let parsedLogs = {};
            let threadParents = {};
            let nodeAnnotations = {}; // Stores annotations: { nodeId: { note: "", color: "" } }
            let markedNodes = new Set();
    
            let selectedNode = {
                type: null, // 'thread' 或 'log'
                id: null,
                data: null,
                tree: null
            };
    
            // DOM Elements
            const contextMenu = document.getElementById('context-menu');
            const markImportance = document.getElementById('mark-importance');
            const unmarkImportance = document.getElementById('unmark-importance');
            const addRemark = document.getElementById('add-remark');
            const exportSequence = document.getElementById('export-sequence');
            const tooltip = d3.select("#tooltip");
    
            const remarkModal = document.getElementById('remark-modal');
            const saveRemarkBtn = document.getElementById('save-remark');
            const cancelRemarkBtn = document.getElementById('cancel-remark');
            const remarkText = document.getElementById('remark-text');
            const remarkColor = document.getElementById('remark-color');
    
            const exportThreadBtn = document.getElementById('export-thread');
            const exportLogBtn = document.getElementById('export-log');
            const exportDataBtn = document.getElementById('export-data');
            const importDataBtn = document.getElementById('import-data');
            const importFileInput = document.getElementById('import-file');
    
            // Event Listeners
            document.getElementById('parse-button').addEventListener('click', function() {
                const logText = document.getElementById('log-input').value;
                parseLogs(logText);
                renderThreadTree();
                clearLogTree();
            });
    
            // Context Menu Event Listeners
            markImportance.addEventListener('click', () => toggleNodeImportance(true));
            unmarkImportance.addEventListener('click', () => toggleNodeImportance(false));
            addRemark.addEventListener('click', () => openRemarkModal());
            exportSequence.addEventListener('click', generateSequenceDiagram);
    
            // Modal Event Listeners
            saveRemarkBtn.addEventListener('click', saveRemark);
            cancelRemarkBtn.addEventListener('click', closeRemarkModal);
    
            // Export/Import Event Listeners
            exportThreadBtn.addEventListener('click', () => exportSVG('thread-tree', '线程关系图'));
            exportLogBtn.addEventListener('click', () => exportSVG('log-tree', '日志跟踪图'));
            exportDataBtn.addEventListener('click', exportData);
            importDataBtn.addEventListener('click', () => importFileInput.click());
            importFileInput.addEventListener('change', importData);
    
            // Hide context menu on outside click
            document.addEventListener('click', function(e) {
                if (e.button !== 2) { // Not right-click
                    hideContextMenu();
                }
            });
    
            // Function to Parse Logs
            function parseLogs(text) {
                const lines = text.split('\n').filter(line => line.trim() !== '');
                parsedLogs = {};
                threadParents = {};
                nodeAnnotations = {}; // Reset annotations on new parse
                markedNodes.clear();
    
                const logEntries = [];
    
                const regex = /^(I|E)\d{3}:(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\((\d+)\|(\d+)\)Sum\.Rpt: (.+)$/;
    
                for (const line of lines) {
                    const match = line.match(regex);
                    if (match) {
                        const [_, level, timestamp, processId, threadId, content] = match;
                        const log = { level, timestamp, processId, threadId, content, raw: line };
                        logEntries.push(log);
                        if (!parsedLogs[threadId]) {
                            parsedLogs[threadId] = [];
                        }
                        parsedLogs[threadId].push(log);
                    }
                }
    
                // Infer Thread Relationships
                inferThreadRelationships(logEntries);
            }
    
            // Function to Infer Thread Relationships
            function inferThreadRelationships(logEntries) {
                // Get the earliest timestamp for each thread
                const threadStartTimes = {};
                for (const threadId in parsedLogs) {
                    const firstLog = parsedLogs[threadId][0];
                    const date = new Date(`2023-10-26T${convertTimestamp(firstLog.timestamp)}Z`);
                    threadStartTimes[threadId] = date;
                }
    
                // Sort threads by start time
                const sortedThreads = Object.keys(threadStartTimes).sort((a, b) => threadStartTimes[a] - threadStartTimes[b]);
    
                if (sortedThreads.length === 0) return;
    
                // Virtual root
                threadParents['root'] = null;
    
                // Assign parents based on start times
                for (let i = 0; i < sortedThreads.length; i++) {
                    const childThread = sortedThreads[i];
                    if (i === 0) {
                        threadParents[childThread] = 'root';
                    } else {
                        let parentThread = 'root';
                        for (let j = i - 1; j >= 0; j--) {
                            const potentialParent = sortedThreads[j];
                            if (threadStartTimes[potentialParent] < threadStartTimes[childThread]) {
                                parentThread = potentialParent;
                                break;
                            }
                        }
                        threadParents[childThread] = parentThread;
                    }
                }
            }
    
            // Function to Convert Timestamp
            function convertTimestamp(timestamp) {
                return timestamp.replace(' ', 'T');
            }
    
            // Function to Build Thread Tree Structure
            function buildThreadTree(rootId, parents) {
                const node = { id: rootId, name: rootId === 'root' ? '虚拟根' : `线程 ${rootId}`, children: [] };
                for (const child in parents) {
                    if (parents[child] === rootId && child !== 'root') {
                        node.children.push(buildThreadTree(child, parents));
                    }
                }
                return node;
            }
    
            // Function to Render Thread Tree
            function renderThreadTree() {
                // Clear previous SVG
                d3.select("#thread-tree").selectAll("*").remove();
    
                const treeData = buildThreadTree('root', threadParents);
    
                const margin = { top: 20, right: 200, bottom: 20, left: 200 },
                      width = document.getElementById('thread-tree').clientWidth - margin.left - margin.right,
                      height = document.getElementById('thread-tree').clientHeight - margin.top - margin.bottom;
    
                const svg = d3.select("#thread-tree")
                    .attr("width", width + margin.left + margin.right)
                    .attr("height", height + margin.top + margin.bottom)
                  .append("g")
                    .attr("transform", `translate(${margin.left},${margin.top})`);
    
                const treemap = d3.tree()
                    .size([height, width]);
    
                const root = d3.hierarchy(treeData, d => d.children);
    
                treemap(root);
    
                // Links
                svg.selectAll(".link")
                    .data(root.links())
                  .enter().append("path")
                    .attr("class", "link")
                    .attr("d", d3.linkHorizontal()
                        .x(d => d.y)
                        .y(d => d.x))
                    .attr("fill", "none")
                    .attr("stroke", "#ccc")
                    .attr("stroke-width", 2);
    
                // Nodes
                const node = svg.selectAll(".node")
                    .data(root.descendants())
                  .enter().append("g")
                    .attr("class", "node")
                    .attr("transform", d => `translate(${d.y},${d.x})`)
                    .on("click", (event, d) => {
                        if (d.data.id !== 'root') { // Exclude virtual root
                            const threadId = d.data.id;
                            renderLogTree(threadId);
                        }
                    })
                    .on("contextmenu", (event, d) => {
                        event.preventDefault();
                        if (d.data.id !== 'root') {
                            selectedNode = { type: 'thread', id: d.data.id, data: d.data, tree: 'thread' };
                            showContextMenu(event.pageX, event.pageY);
                        }
                    });
    
                // Node Rectangles
                node.append("rect")
                    .attr("width", 120)
                    .attr("height", 40)
                    .attr("x", -60)
                    .attr("y", -20)
                    .attr("rx", 5)
                    .attr("ry", 5)
                    .attr("fill", d => d.data.id === 'root' ? "#ffffff00" : getNodeColor(d.data.id))
                    .attr("stroke", d => d.data.id === 'root' ? "none" : "#0288D1")
                    .attr("stroke-width", 2)
                    .attr("opacity", d => markedNodes.has(d.data.id) ? 0.5 : 1.0);
    
                // Node Labels
                node.append("text")
                    .attr("dy", 5)
                    .attr("text-anchor", "middle")
                    .attr("fill", "#ffffff")
                    .style("pointer-events", "none")
                    .text(d => d.data.id === 'root' ? '' : `线程 ${d.data.id}`);
    
                // Append Remarks if any
                node.each(function(d) {
                    if (nodeAnnotations[d.data.id] && nodeAnnotations[d.data.id].note) {
                        d3.select(this).append("text")
                            .attr("dy", 20)
                            .attr("text-anchor", "middle")
                            .attr("fill", getContrastColor(nodeAnnotations[d.data.id].color))
                            .style("font-size", "10px")
                            .text(nodeAnnotations[d.data.id].note)
                            .call(wrapText, 100);
                    }
                });
    
                // Zoom and Pan
                const zoom = d3.zoom()
                    .scaleExtent([0.5, 2])
                    .on("zoom", (event) => {
                        svg.attr("transform", event.transform);
                    });
    
                d3.select("#thread-tree").call(zoom);
            }
    
            // Function to Get Node Color based on Annotation
            function getNodeColor(nodeId) {
                if (nodeAnnotations[nodeId] && nodeAnnotations[nodeId].color) {
                    return `#${nodeAnnotations[nodeId].color}`;
                }
                return "#4FC3F7"; // Default color
            }
    
            // Function to Get Contrast Color for Text
            function getContrastColor(hex) {
                const r = parseInt(hex.substr(0,2),16);
                const g = parseInt(hex.substr(2,2),16);
                const b = parseInt(hex.substr(4,2),16);
                const yiq = ((r*299)+(g*587)+(b*114))/1000;
                return (yiq >= 128) ? "#000000" : "#FFFFFF";
            }
    
            // Function to Wrap Text within a Given Width
            function wrapText(text, width) {
                text.each(function() {
                    const text = d3.select(this);
                    const words = text.text().split(/\s+/).reverse();
                    let word;
                    let line = [];
                    let lineNumber = 0;
                    const lineHeight = 1.1; // ems
                    const y = text.attr("y");
                    const dy = parseFloat(text.attr("dy"));
                    let tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
                    while (word = words.pop()) {
                        line.push(word);
                        tspan.text(line.join(" "));
                        if (tspan.node().getComputedTextLength() > width) {
                            line.pop();
                            tspan.text(line.join(" "));
                            line = [word];
                            tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", lineHeight + "em").text(word);
                        }
                    }
                });
            }
    
            // Function to Clear Log Tree
            function clearLogTree() {
                d3.select("#log-tree").selectAll("*").remove();
                const svg = d3.select("#log-tree");
                svg.append("text")
                    .attr("x", "50%")
                    .attr("y", "50%")
                    .attr("text-anchor", "middle")
                    .attr("fill", "#7f8c8d")
                    .text("请选择左侧的线程以查看日志");
            }
    
            // Function to Render Log Tree
            function renderLogTree(threadId) {
                const logs = parsedLogs[threadId];
                if (!logs) {
                    alert('没有找到该线程的日志。');
                    return;
                }
    
                const treeData = buildLogTree(logs);
    
                // Clear previous SVG
                d3.select("#log-tree").selectAll("*").remove();
    
                const margin = { top: 20, right: 200, bottom: 20, left: 200 },
                      width = document.getElementById('log-tree').clientWidth - margin.left - margin.right,
                      height = document.getElementById('log-tree').clientHeight - margin.top - margin.bottom;
    
                const svg = d3.select("#log-tree")
                    .attr("width", width + margin.left + margin.right)
                    .attr("height", height + margin.top + margin.bottom)
                  .append("g")
                    .attr("transform", `translate(${margin.left},${margin.top})`);
    
                const treemap = d3.tree()
                    .size([height, width]);
    
                const root = d3.hierarchy(treeData, d => d.children);
    
                treemap(root);
    
                // Links
                svg.selectAll(".link")
                    .data(root.links())
                  .enter().append("path")
                    .attr("class", "link")
                    .attr("d", d3.linkHorizontal()
                        .x(d => d.y)
                        .y(d => d.x))
                    .attr("fill", "none")
                    .attr("stroke", "#ccc")
                    .attr("stroke-width", 2);
    
                // Nodes
                const node = svg.selectAll(".node")
                    .data(root.descendants())
                  .enter().append("g")
                    .attr("class", "node")
                    .attr("transform", d => `translate(${d.y},${d.x})`)
                    .on("contextmenu", (event, d) => {
                        event.preventDefault();
                        selectedNode = { type: 'log', id: d.data.id, data: d.data, tree: 'log' };
                        showContextMenu(event.pageX, event.pageY);
                    })
                    .on("mouseover", (event, d) => {
                        tooltip.transition().duration(200).style("opacity", .9);
                        tooltip.html(formatTooltip(d.data))
                            .style("left", (event.pageX + 10) + "px")
                            .style("top", (event.pageY - 28) + "px");
                    })
                    .on("mouseout", () => {
                        tooltip.transition().duration(500).style("opacity", 0);
                    });
    
                // Node Rectangles
                node.append("rect")
                    .attr("width", 300)
                    .attr("height", 60)
                    .attr("x", -150)
                    .attr("y", -30)
                    .attr("rx", 5)
                    .attr("ry", 5)
                    .attr("fill", d => getLogNodeColor(d.data.level, d.data.id))
                    .attr("stroke", "#424242")
                    .attr("stroke-width", 1)
                    .attr("opacity", d => markedNodes.has(d.data.id) ? 0.5 : 1.0);
    
                // Node Content
                node.append("text")
                    .attr("dy", -5)
                    .attr("text-anchor", "middle")
                    .attr("fill", "#ffffff")
                    .style("font-weight", "bold")
                    .text(d => truncateText(d.data.content, 50))
                    .call(trimText, 280); // Adjust as needed
    
                // Node Metadata
                node.append("text")
                    .attr("dy", 15)
                    .attr("text-anchor", "middle")
                    .attr("fill", "#d1d1d1")
                    .style("font-size", "10px")
                    .text(d => `[${d.data.timestamp}] | 线程:${d.data.threadId}`);
    
                // Append Remarks if any
                node.each(function(d) {
                    if (nodeAnnotations[d.data.id] && nodeAnnotations[d.data.id].note) {
                        d3.select(this).append("text")
                            .attr("dy", 25)
                            .attr("text-anchor", "middle")
                            .attr("fill", getContrastColor(nodeAnnotations[d.data.id].color))
                            .style("font-size", "10px")
                            .text(nodeAnnotations[d.data.id].note)
                            .call(wrapText, 280);
                    }
                });
    
                // Zoom and Pan
                const zoom = d3.zoom()
                    .scaleExtent([0.5, 2])
                    .on("zoom", (event) => {
                        svg.attr("transform", event.transform);
                    });
    
                d3.select("#log-tree").call(zoom);
            }
    
            // Function to Truncate Text
            function truncateText(text, maxLength) {
                return text.length > maxLength ? text.substring(0, maxLength - 3) + "..." : text;
            }
    
            // Function to Trim Text with Ellipsis
            function trimText(text, maxWidth) {
                text.each(function() {
                    const node = d3.select(this);
                    let textContent = node.text();
                    while (node.node().getComputedTextLength() > maxWidth && textContent.length > 0) {
                        textContent = textContent.slice(0, -1);
                        node.text(textContent + '...');
                    }
                });
            }
    
            // Function to Get Log Node Color based on Level and Annotation
            function getLogNodeColor(level, nodeId) {
                if (nodeAnnotations[nodeId] && nodeAnnotations[nodeId].color) {
                    return `#${nodeAnnotations[nodeId].color}`;
                }
                if (level === 'E') return '#e57373';
                if (level === 'I') return '#64b5f6';
                return '#A1887F';
            }
    
            // Function to Build Log Tree Structure
            function buildLogTree(logs) {
                const root = { id: 0, content: "根", children: [], level: "I" };
                let currentNode = root;
                let stack = [root];
                let nodeId = 1;
    
                for (const log of logs) {
                    const isEntry = /Entered\.\.\.$/.test(log.content);
                    const isExit = /Exited\.\.\.$/.test(log.content);
    
                    if (isEntry) {
                        const newNode = {
                            id: nodeId++,
                            content: log.content.replace(/ Entered\.\.\.$/, ''),
                            timestamp: log.timestamp,
                            threadId: log.threadId,
                            level: log.level,
                            children: []
                        };
                        currentNode.children.push(newNode);
                        stack.push(newNode);
                        currentNode = newNode;
                    } else if (isExit) {
                        if (stack.length > 1) {
                            stack.pop();
                            currentNode = stack[stack.length - 1];
                        }
                    } else {
                        const newNode = {
                            id: nodeId++,
                            content: log.content,
                            timestamp: log.timestamp,
                            threadId: log.threadId,
                            level: log.level,
                            children: []
                        };
                        currentNode.children.push(newNode);
                    }
                }
    
                return root;
            }
    
            // Function to Format Tooltip Content
            function formatTooltip(data) {
                const annotation = nodeAnnotations[data.id];
                let tooltipHTML = `<strong>时间:</strong> ${data.timestamp}<br/>
                                   <strong>级别:</strong> ${data.level}<br/>
                                   <strong>内容:</strong> ${data.content}`;
                if (annotation && annotation.note) {
                    tooltipHTML += `<br/><strong>备注:</strong> ${annotation.note}`;
                }
                return tooltipHTML;
            }
    
            // Function to Show Custom Context Menu
            function showContextMenu(x, y) {
                contextMenu.style.left = `${x}px`;
                contextMenu.style.top = `${y}px`;
                contextMenu.style.display = 'block';
                updateContextMenuOptions();
            }
    
            // Function to Hide Context Menu
            function hideContextMenu() {
                contextMenu.style.display = 'none';
                selectedNode = { type: null, id: null, data: null, tree: null };
            }
    
            // Function to Toggle Node Importance
            function toggleNodeImportance(mark) {
                if (!selectedNode.id) return;
                if (mark) {
                    markedNodes.add(selectedNode.id);
                } else {
                    markedNodes.delete(selectedNode.id);
                }
    
                updateNodeOpacity(selectedNode);
                hideContextMenu();
            }
    
            // Function to Update Node Opacity
            function updateNodeOpacity(node) {
                if (node.tree === 'log') {
                    d3.selectAll("#log-tree .node").filter(d => d.data.id === node.id)
                        .select("rect")
                        .transition()
                        .duration(300)
                        .attr("opacity", markedNodes.has(node.id) ? 0.5 : 1.0);
                } else if (node.tree === 'thread') {
                    d3.selectAll("#thread-tree .node").filter(d => d.data.id === node.id)
                        .select("rect")
                        .transition()
                        .duration(300)
                        .attr("opacity", markedNodes.has(node.id) ? 0.5 : 1.0);
                }
            }
    
            // Function to Update Context Menu Options
            function updateContextMenuOptions() {
                if (selectedNode.type === 'log') {
                    if (markedNodes.has(selectedNode.id)) {
                        markImportance.style.display = 'none';
                        unmarkImportance.style.display = 'block';
                    } else {
                        markImportance.style.display = 'block';
                        unmarkImportance.style.display = 'none';
                    }
                } else if (selectedNode.type === 'thread') {
                    if (markedNodes.has(selectedNode.id)) {
                        markImportance.style.display = 'none';
                        unmarkImportance.style.display = 'block';
                    } else {
                        markImportance.style.display = 'block';
                        unmarkImportance.style.display = 'none';
                    }
                }
            }
    
            // Function to Open Remark Modal
            function openRemarkModal() {
                if (!selectedNode.id) return;
                const annotation = nodeAnnotations[selectedNode.id];
                if (annotation) {
                    remarkText.value = annotation.note;
                    remarkColor.jscolor.fromString(annotation.color || 'FFFFFF');
                } else {
                    remarkText.value = '';
                    remarkColor.value = 'FFFFFF';
                }
                remarkModal.style.display = 'block';
            }
    
            // Function to Close Remark Modal
            function closeRemarkModal() {
                remarkModal.style.display = 'none';
                selectedNode = { type: null, id: null, data: null, tree: null };
            }
    
            // Function to Save Remark
            function saveRemark() {
                const note = remarkText.value.trim();
                const color = remarkColor.value.toUpperCase();
                if (selectedNode.id) {
                    nodeAnnotations[selectedNode.id] = {
                        note: note,
                        color: color
                    };
                    renderCurrentView();
                }
                closeRemarkModal();
            }
    
            // Function to Render Current View with Annotations
            function renderCurrentView() {
                if (selectedNode.tree === 'thread') {
                    renderThreadTree();
                } else if (selectedNode.tree === 'log') {
                    const threadId = selectedNode.data.threadId;
                    renderLogTree(threadId);
                }
            }
    
            // Function to Generate Sequence Diagram (PlantUML)
            function generateSequenceDiagram() {
                if (!selectedNode.id) return;
                let plantUMLCode = "@startuml\n";
                if (selectedNode.tree === 'log') {
                    const logNode = selectedNode.data;
                    const callStack = getCallStack(selectedNode.data);
    
                    const participants = new Set();
                    callStack.forEach(node => {
                        participants.add(sanitizeParticipantName(node.content));
                    });
    
                    participants.forEach(p => {
                        plantUMLCode += `participant ${p}\n`;
                    });
    
                    for (let i = 0; i < callStack.length - 1; i++) {
                        const caller = sanitizeParticipantName(callStack[i].content);
                        const callee = sanitizeParticipantName(callStack[i + 1].content);
                        plantUMLCode += `${caller} -> ${callee} : 调用\n`;
                    }
    
                    plantUMLCode += "@enduml";
    
                    // Copy to Clipboard
                    copyToClipboard(plantUMLCode);
                    alert('PlantUML时序图代码已复制到剪贴板。');
                } else if (selectedNode.tree === 'thread') {
                    // Thread-level sequence diagram generation can be implemented here
                    alert('线程级别的时序图生成尚未实现。');
                }
                hideContextMenu();
            }
    
            // Function to Get Call Stack
            function getCallStack(node) {
                const stack = [];
                let current = node;
                while (current.parent && current.parent.data) {
                    stack.unshift(current.parent.data);
                    current = current.parent.data;
                }
                stack.push(node);
                return stack;
            }
    
            // Function to Sanitize Participant Name
            function sanitizeParticipantName(name) {
                return name.replace(/[^a-zA-Z0-9_\-]/g, '_');
            }
    
            // Function to Copy Text to Clipboard
            function copyToClipboard(text) {
                const textarea = document.createElement('textarea');
                textarea.value = text;
                document.body.appendChild(textarea);
                textarea.select();
                try {
                    document.execCommand('copy');
                } catch (err) {
                    console.error('复制失败', err);
                }
                document.body.removeChild(textarea);
            }
    
            // Function to Export SVG as PNG
            function exportSVG(svgId, filename) {
                const svgElement = document.getElementById(svgId);
                const serializer = new XMLSerializer();
                const svgString = serializer.serializeToString(svgElement);
    
                const canvas = document.createElement('canvas');
                const bbox = svgElement.getBBox();
                canvas.width = bbox.width + 20;
                canvas.height = bbox.height + 20;
                const ctx = canvas.getContext('2d');
    
                const img = new Image();
                const svgBlob = new Blob([svgString], {type: 'image/svg+xml;charset=utf-8'});
                const url = URL.createObjectURL(svgBlob);
    
                img.onload = function() {
                    ctx.fillStyle = "#ffffff"; // Optional: set background color
                    ctx.fillRect(0, 0, canvas.width, canvas.height);
                    ctx.drawImage(img, 10 - bbox.x, 10 - bbox.y);
                    URL.revokeObjectURL(url);
                    canvas.toBlob(function(blob) {
                        saveAs(blob, `${filename}.png`);
                    });
                };
                img.src = url;
            }
    
            // Function to Export Data as JSON
            function exportData() {
                const data = {
                    parsedLogs,
                    threadParents,
                    nodeAnnotations
                };
                const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
                saveAs(blob, 'log_visualization_data.json');
            }
    
            // Function to Import Data from JSON
            function importData(event) {
                const file = event.target.files[0];
                if (!file) return;
                const reader = new FileReader();
                reader.onload = function(e) {
                    try {
                        const data = JSON.parse(e.target.result);
                        parsedLogs = data.parsedLogs || {};
                        threadParents = data.threadParents || {};
                        nodeAnnotations = data.nodeAnnotations || {};
                        markedNodes = new Set(Object.keys(nodeAnnotations).filter(id => nodeAnnotations[id].important));
                        renderThreadTree();
                        clearLogTree();
                        alert('数据导入成功。');
                    } catch (err) {
                        alert('导入失败,文件格式不正确。');
                        console.error(err);
                    }
                };
                reader.readAsText(file);
            }
    
            // Handle Modal Click Outside
            window.onclick = function(event) {
                if (event.target == remarkModal) {
                    closeRemarkModal();
                }
            };
        </script>
    </body>
    </html>
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.