Revert "feat(visualizer): 引入核心图按需加载和节点扩展功能"

This reverts commit 0c41cd2a13.
This commit is contained in:
minecraft1024a
2025-11-08 11:17:37 +08:00
parent 96dbb8fc55
commit 6521e681cd
2 changed files with 72 additions and 207 deletions

View File

@@ -62,10 +62,7 @@ def find_available_data_files() -> List[Path]:
def load_graph_data_from_file(file_path: Optional[Path] = None) -> Dict[str, Any]:
"""
从磁盘加载图数据,并构建索引以加速查询。
哼,别看我代码写得多,这叫专业!一次性把事情做对,就不用返工了。
"""
"""从磁盘加载图数据"""
global graph_data_cache, current_data_file
if file_path and file_path != current_data_file:
@@ -80,12 +77,12 @@ def load_graph_data_from_file(file_path: Optional[Path] = None) -> Dict[str, Any
if not graph_file:
available_files = find_available_data_files()
if not available_files:
return {"error": "未找到数据文件", "nodes": [], "edges": [], "stats": {}, "nodes_dict": {}, "adjacency_list": {}}
return {"error": "未找到数据文件", "nodes": [], "edges": [], "stats": {}}
graph_file = available_files[0]
current_data_file = graph_file
if not graph_file.exists():
return {"error": f"文件不存在: {graph_file}", "nodes": [], "edges": [], "stats": {}, "nodes_dict": {}, "adjacency_list": {}}
return {"error": f"文件不存在: {graph_file}", "nodes": [], "edges": [], "stats": {}}
with open(graph_file, "r", encoding="utf-8") as f:
data = orjson.loads(f.read())
@@ -100,7 +97,6 @@ def load_graph_data_from_file(file_path: Optional[Path] = None) -> Dict[str, Any
"label": node.get("content", ""),
"group": node.get("node_type", ""),
"title": f"{node.get('node_type', '')}: {node.get('content', '')}",
"degree": 0, # 初始化度为0
}
for node in nodes
if node.get("id")
@@ -108,39 +104,26 @@ def load_graph_data_from_file(file_path: Optional[Path] = None) -> Dict[str, Any
edges_list = []
seen_edge_ids = set()
adjacency_list = {node_id: [] for node_id in nodes_dict}
for edge in edges:
edge_id = edge.get("id")
source_id = edge.get("source", edge.get("source_id"))
target_id = edge.get("target", edge.get("target_id"))
if edge_id and edge_id not in seen_edge_ids and source_id in nodes_dict and target_id in nodes_dict:
formatted_edge = {
**edge,
"from": source_id,
"to": target_id,
"label": edge.get("relation", ""),
"arrows": "to",
}
edges_list.append(formatted_edge)
if edge_id and edge_id not in seen_edge_ids:
edges_list.append(
{
**edge,
"from": edge.get("source", edge.get("source_id")),
"to": edge.get("target", edge.get("target_id")),
"label": edge.get("relation", ""),
"arrows": "to",
}
)
seen_edge_ids.add(edge_id)
# 构建邻接表并计算度
adjacency_list[source_id].append(formatted_edge)
adjacency_list[target_id].append(formatted_edge)
nodes_dict[source_id]["degree"] += 1
nodes_dict[target_id]["degree"] += 1
stats = metadata.get("statistics", {})
total_memories = stats.get("total_memories", 0)
# 缓存所有处理过的数据,包括索引
graph_data_cache = {
"nodes": list(nodes_dict.values()),
"edges": edges_list,
"nodes_dict": nodes_dict, # 缓存节点字典,方便快速查找
"adjacency_list": adjacency_list, # 缓存邻接表,光速定位邻居
"memories": [],
"stats": {
"total_nodes": len(nodes_dict),
@@ -155,9 +138,11 @@ def load_graph_data_from_file(file_path: Optional[Path] = None) -> Dict[str, Any
except Exception as e:
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"加载图数据失败: {e}")
@router.get("/", response_class=HTMLResponse)
async def index(request: Request):
"""主页面"""
@@ -218,91 +203,29 @@ def _format_graph_data_from_manager(memory_manager) -> Dict[str, Any]:
"current_file": "memory_manager (实时数据)",
}
@router.get("/api/graph/core")
async def get_core_graph(limit: int = 100):
"""
获取核心图数据
这可比一下子把所有东西都丢给前端聪明多了,哼。
"""
@router.get("/api/graph/full")
async def get_full_graph():
"""获取完整记忆图数据"""
try:
full_data = load_graph_data_from_file()
if "error" in full_data:
return JSONResponse(content={"success": False, "error": full_data["error"]}, status_code=404)
from src.memory_graph.manager_singleton import get_memory_manager
# 智能选择核心节点: 优先选择度最高的节点
# 这是一个简单的策略,但比随机选择要好得多
all_nodes = full_data.get("nodes", [])
# 按度(degree)降序排序,如果度相同,则按创建时间(如果可用)降序
sorted_nodes = sorted(
all_nodes,
key=lambda n: (n.get("degree", 0), n.get("created_at", 0)),
reverse=True
)
core_nodes = sorted_nodes[:limit]
core_node_ids = {node["id"] for node in core_nodes}
memory_manager = get_memory_manager()
# 只包含核心节点之间的边,保持初始视图的整洁
core_edges = [
edge for edge in full_data.get("edges", [])
if edge.get("from") in core_node_ids and edge.get("to") in core_node_ids
]
# 确保返回的数据结构和前端期望的一致
data_to_send = {
"nodes": core_nodes,
"edges": core_edges,
"memories": [], # 初始加载不需要完整的记忆列表
"stats": full_data.get("stats", {}), # 统计数据还是完整的
"current_file": full_data.get("current_file", "")
}
data = {}
if memory_manager and memory_manager._initialized:
data = _format_graph_data_from_manager(memory_manager)
else:
# 如果内存管理器不可用,则从文件加载
data = load_graph_data_from_file()
return JSONResponse(content={"success": True, "data": data_to_send})
return JSONResponse(content={"success": True, "data": data})
except Exception as e:
import traceback
traceback.print_exc()
return JSONResponse(content={"success": False, "error": str(e)}, status_code=500)
@router.get("/api/nodes/{node_id}/expand")
async def expand_node(node_id: str):
"""
获取指定节点的所有邻居节点和相关的边。
看,这就是按需加载的魔法。我可真是个天才,哼!
"""
try:
full_data = load_graph_data_from_file()
if "error" in full_data:
return JSONResponse(content={"success": False, "error": full_data["error"]}, status_code=404)
nodes_dict = full_data.get("nodes_dict", {})
adjacency_list = full_data.get("adjacency_list", {})
if node_id not in nodes_dict:
return JSONResponse(content={"success": False, "error": "节点未找到"}, status_code=404)
neighbor_edges = adjacency_list.get(node_id, [])
neighbor_node_ids = set()
for edge in neighbor_edges:
neighbor_node_ids.add(edge["from"])
neighbor_node_ids.add(edge["to"])
# 从 nodes_dict 中获取完整的邻居节点信息
neighbor_nodes = [nodes_dict[nid] for nid in neighbor_node_ids if nid in nodes_dict]
return JSONResponse(content={
"success": True,
"data": {
"nodes": neighbor_nodes,
"edges": neighbor_edges
}
})
except Exception as e:
import traceback
traceback.print_exc()
return JSONResponse(content={"success": False, "error": str(e)}, status_code=500)
@router.get("/api/files")
async def list_files_api():

View File

@@ -532,17 +532,12 @@
<script>
let network = null;
let availableFiles = [];
// 现在的数据集是动态增长的,我们需要用 vis.DataSet 来管理
let nodesDataSet = new vis.DataSet([])
;
let edgesDataSet = new vis.DataSet([]);
let graphData = {
nodes: [], // 这将作为原始数据的备份
nodes: [],
edges: [],
memories: []
};
let originalData = null; // 用于过滤器
let originalData = null;
// 节点颜色配置
const nodeColors = {
@@ -623,32 +618,26 @@
dragView: true
}
};
// 初始化时使用我们可动态管理的 DataSet
const data = {
nodes: nodesDataSet,
edges: edgesDataSet
};
network = new vis.Network(container, data, options);
const data = {
nodes: new vis.DataSet([]),
edges: new vis.DataSet([])
};
network = new vis.Network(container, data, options);
// 添加事件监听
network.on('click', function(params) {
if (params.nodes.length > 0) {
const nodeId = params.nodes[0];
showNodeInfo(nodeId);
// 单击时只高亮,不再执行复杂的BFS
highlightConnectedNodes(nodeId);
} else {
resetNodeHighlight(); // 点击空白处恢复
// 点击空白处恢复所有节点
resetNodeHighlight();
}
});
// 这才是我们的秘密武器: 双击扩展! 哼哼~
network.on('doubleClick', async function(params) {
if (params.nodes.length > 0) {
const nodeId = params.nodes[0];
await expandNode(nodeId);
}
});
// 稳定化完成后停止物理引擎
network.on('stabilizationIterationsDone', function() {
console.log('初始稳定化完成,停止物理引擎');
@@ -668,20 +657,16 @@ network = new vis.Network(container, data, options);
async function loadGraph() {
try {
document.getElementById('loading').style.display = 'block';
// 请求新的核心节点接口,而不是那个又笨又重的full接口
const response = await fetch('/visualizer/api/graph/core');
const response = await fetch('/visualizer/api/graph/full');
const result = await response.json();
if (result.success) {
originalData = result.data; // 保存原始数据用于过滤
// 初始加载时,清空旧数据
nodesDataSet.clear();
edgesDataSet.clear();
updateGraph(result.data, true); // true表示是初始加载
originalData = result.data;
updateGraph(result.data);
updateStats(result.data.stats);
} else {
alert('加载核心节点失败: ' + result.error);
alert('加载失败: ' + result.error);
}
} catch (error) {
console.error('加载图形失败:', error);
@@ -690,62 +675,41 @@ network = new vis.Network(container, data, options);
document.getElementById('loading').style.display = 'none';
}
}
// 更新图形显示
function updateGraph(data, isInitialLoad = false) {
if (isInitialLoad) {
// 如果是初始加载,则完全替换数据
graphData = data;
} else {
// 如果是扩展,则合并数据
// 使用一个Set来避免重复添加节点
const existingNodeIds = new Set(graphData.nodes.map(n => n.id));
data.nodes.forEach(newNode => {
if (!existingNodeIds.has(newNode.id)) {
graphData.nodes.push(newNode);
existingNodeIds.add(newNode.id);
}
});
function updateGraph(data) {
graphData = data;
// 同样避免重复添加边
const existingEdgeIds = new Set(graphData.edges.map(e => e.id));
data.edges.forEach(newEdge => {
if (!existingEdgeIds.has(newEdge.id)) {
graphData.edges.push(newEdge);
existingEdgeIds.add(newEdge.id);
}
});
}
// 处理节点数据,添加或更新到DataSet
const nodesToAdd = data.nodes.map(node => ({
// 处理节点数据
const nodes = data.nodes.map(node => ({
id: node.id,
label: node.label,
title: node.title,
group: node.group,
color: nodeColors[node.group] || '#999',
// 瞧,现在节点越大,就说明它越重要,是不是很酷?
size: 15 + Math.min((node.degree || 0) * 2, 20),
metadata: node.metadata
}));
nodesDataSet.update(nodesToAdd);
// 处理边数据,添加到DataSet
const edgesToAdd = data.edges.map(edge => ({
// 处理边数据
const edges = data.edges.map(edge => ({
id: edge.id,
from: edge.from,
to: edge.to,
label: edge.label,
title: edge.title,
// 根据重要性调整边的宽度
width: (edge.importance || 0.5) * 2 + 1
width: edge.importance * 3 + 1
}));
edgesDataSet.update(edgesToAdd);
// 只有在添加新节点时才需要重新稳定布局
if (nodesToAdd.length > 0) {
network.stabilize();
}
// 更新网络
network.setData({
nodes: new vis.DataSet(nodes),
edges: new vis.DataSet(edges)
});
// 注意setData 会自动触发物理引擎重新布局
// stabilizationIterationsDone 事件监听器会自动停止物理引擎
}
// 更新统计信息
function updateStats(stats) {
document.getElementById('statNodes').textContent = stats.total_nodes;
@@ -1049,40 +1013,18 @@ network = new vis.Network(container, data, options);
});
}
}
// 适应窗口
function fitNetwork() {
if (network) {
network.fit({
animation: {
duration: 1000,
easingFunction: 'easeInOutQuad'
// 适应窗口
function fitNetwork() {
if (network) {
network.fit({
animation: {
duration: 1000,
easingFunction: 'easeInOutQuad'
}
});
}
});
}
}
// 新增: 扩展节点的函数
async function expandNode(nodeId) {
console.log(`正在扩展节点: ${nodeId}`);
document.getElementById('loading').style.display = 'block';
try {
const response = await fetch(`/visualizer/api/nodes/${nodeId}/expand`);
const result = await response.json();
if (result.success) {
console.log(`收到 ${result.data.nodes.length} 个新节点, ${result.data.edges.length} 条新边`);
updateGraph(result.data);
} else {
alert(`扩展节点失败: ${result.error}`);
}
} catch (error) {
console.error('扩展节点失败:', error);
alert('扩展节点失败: ' + error.message);
} finally {
document.getElementById('loading').style.display = 'none';
}
}
// 导出图形数据
function exportGraph() {