Skip to content

Commit a1bf8d3

Browse files
committed
fix: 增强知识图谱加载逻辑和错误处理
- 在 `KnowledgeGraphSection.vue` 中,更新 `loadGraph` 和 `scheduleGraphLoad` 函数,增加条件检查和日志输出,以确保图谱加载的稳定性。 - 在 `KnowledgeGraphViewer.vue` 中,优化数据库加载逻辑,确保在没有选中数据库时提供适当的警告信息,并添加日志以跟踪加载过程。 - 重置图谱统计信息以适应新的数据库选择逻辑,确保用户体验的一致性。
1 parent bbcfacf commit a1bf8d3

File tree

2 files changed

+96
-21
lines changed

2 files changed

+96
-21
lines changed

web/src/components/KnowledgeGraphSection.vue

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,24 @@ const isGraphSupported = computed(() => {
133133
134134
let pendingLoadTimer = null;
135135
136-
const loadGraph = () => {
136+
const loadGraph = async () => {
137+
console.log('loadGraph 调用:', {
138+
hasRef: !!graphViewerRef.value,
139+
hasLoadFullGraph: graphViewerRef.value && typeof graphViewerRef.value.loadFullGraph === 'function'
140+
});
141+
142+
// 等待一小段时间确保子组件已经完全初始化
143+
await nextTick();
144+
137145
if (graphViewerRef.value && typeof graphViewerRef.value.loadFullGraph === 'function') {
146+
console.log('调用 loadFullGraph');
138147
graphViewerRef.value.loadFullGraph();
148+
} else {
149+
console.warn('无法调用 loadFullGraph:', {
150+
hasRef: !!graphViewerRef.value,
151+
refValue: graphViewerRef.value,
152+
hasMethod: graphViewerRef.value && typeof graphViewerRef.value.loadFullGraph
153+
});
139154
}
140155
};
141156
@@ -197,16 +212,45 @@ const applySettings = () => {
197212
// };
198213
199214
const scheduleGraphLoad = (delay = 200) => {
200-
if (!props.active || !isGraphSupported.value) {
215+
console.log('scheduleGraphLoad 调用:', {
216+
active: props.active,
217+
supported: isGraphSupported.value,
218+
databaseId: databaseId.value,
219+
hasGraphViewer: !!graphViewerRef.value
220+
});
221+
222+
// 确保组件激活且数据库支持图谱功能
223+
if (!props.active) {
224+
console.log('组件未激活,跳过图谱加载');
225+
return;
226+
}
227+
228+
if (!isGraphSupported.value) {
229+
console.log('数据库不支持图谱功能,跳过加载');
230+
return;
231+
}
232+
233+
if (!databaseId.value) {
234+
console.log('没有选中数据库,跳过图谱加载');
201235
return;
202236
}
237+
203238
if (pendingLoadTimer) {
204239
clearTimeout(pendingLoadTimer);
205240
}
206-
pendingLoadTimer = setTimeout(() => {
207-
nextTick(() => {
208-
loadGraph();
209-
});
241+
pendingLoadTimer = setTimeout(async () => {
242+
await nextTick();
243+
// 再次检查条件,防止在延迟期间状态发生变化
244+
if (props.active && isGraphSupported.value && databaseId.value) {
245+
console.log('执行图谱加载');
246+
await loadGraph();
247+
} else {
248+
console.log('延迟检查时条件不满足:', {
249+
active: props.active,
250+
supported: isGraphSupported.value,
251+
databaseId: databaseId.value
252+
});
253+
}
210254
}, delay);
211255
};
212256
@@ -235,13 +279,31 @@ watch(
235279
);
236280
237281
watch(databaseId, () => {
238-
store.graphStats = defaultGraphStats();
239-
scheduleGraphLoad(300);
282+
// 重置统计信息
283+
store.graphStats = {
284+
total_nodes: 0,
285+
total_edges: 0,
286+
displayed_nodes: 0,
287+
displayed_edges: 0,
288+
is_truncated: false
289+
};
290+
clearGraph();
291+
292+
// 只有在新数据库支持图谱时才加载
293+
if (isGraphSupported.value) {
294+
scheduleGraphLoad(300);
295+
}
240296
});
241297
242298
watch(isGraphSupported, (supported) => {
243299
if (!supported) {
244-
store.graphStats = defaultGraphStats();
300+
store.graphStats = {
301+
total_nodes: 0,
302+
total_edges: 0,
303+
displayed_nodes: 0,
304+
displayed_edges: 0,
305+
is_truncated: false
306+
};
245307
clearGraph();
246308
return;
247309
}

web/src/components/KnowledgeGraphViewer.vue

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,24 +357,18 @@ const getEdgeDisplayName = (edge) => {
357357
358358
// 加载可用数据库
359359
const loadAvailableDatabases = async () => {
360-
// 如果隐藏数据库选择器且有初始数据库ID,直接使用
361-
if (props.hideDbSelector && props.initialDatabaseId) {
362-
selectedDatabase.value = props.initialDatabaseId
363-
await loadGraphLabels(selectedDatabase.value)
364-
return
365-
}
366-
367360
loadingDatabases.value = true
368361
try {
369362
const response = await lightragApi.getDatabases()
370363
if (response.success) {
371364
availableDatabases.value = response.data.databases || []
372365
373-
// 如果有初始数据库 ID,优先选择它
374-
if (props.initialDatabaseId && availableDatabases.value.some(db => db.db_id === props.initialDatabaseId)) {
366+
// 如果有初始数据库 ID,直接使用它(上层已经检查过类型)
367+
if (props.initialDatabaseId) {
375368
selectedDatabase.value = props.initialDatabaseId
376369
await onDatabaseChange(selectedDatabase.value)
377370
} else if (availableDatabases.value.length > 0 && !selectedDatabase.value) {
371+
// 没有初始数据库ID,选择第一个可用的数据库
378372
selectedDatabase.value = availableDatabases.value[0].db_id
379373
await onDatabaseChange(selectedDatabase.value)
380374
}
@@ -418,7 +412,7 @@ const onDatabaseChange = async (dbId) => {
418412
// 加载新数据库的标签
419413
await loadGraphLabels(dbId)
420414
421-
message.info(`已切换到数据库: ${availableDatabases.value.find(db => db.db_id === dbId)?.name || dbId}`)
415+
// message.info(`已切换到数据库: ${availableDatabases.value.find(db => db.db_id === dbId)?.name || dbId}`)
422416
}
423417
424418
// Sigma.js配置
@@ -673,7 +667,7 @@ const registerEvents = () => {
673667
// 加载图数据
674668
const loadGraphData = async () => {
675669
if (!selectedDatabase.value) {
676-
message.warning('请先选择数据库')
670+
console.warn('尝试加载图数据但没有选中数据库')
677671
return
678672
}
679673
@@ -751,6 +745,25 @@ const loadGraphData = async () => {
751745
752746
// 加载完整图数据
753747
const loadFullGraph = async () => {
748+
console.log('loadFullGraph 调用:', {
749+
selectedDatabase: selectedDatabase.value,
750+
availableDatabases: availableDatabases.value.length,
751+
initialDatabaseId: props.initialDatabaseId
752+
});
753+
754+
// 如果还没有选中数据库,等待数据库加载完成
755+
if (!selectedDatabase.value && props.initialDatabaseId) {
756+
console.log('等待数据库加载完成...');
757+
// 等待一小段时间让异步操作完成
758+
await new Promise(resolve => setTimeout(resolve, 100));
759+
}
760+
761+
// 再次检查是否有选中的数据库
762+
if (!selectedDatabase.value) {
763+
console.warn('loadFullGraph: 没有选中的数据库,无法加载图谱')
764+
return
765+
}
766+
754767
selectedLabel.value = '*'
755768
await loadGraphData()
756769
emit('refresh-graph')
@@ -852,7 +865,7 @@ const applyLayout = async (graph) => {
852865
// 展开节点
853866
const expandNode = async (nodeId) => {
854867
if (!selectedDatabase.value) {
855-
message.warning('请先选择数据库')
868+
console.warn('尝试展开节点但没有选中数据库')
856869
return
857870
}
858871

0 commit comments

Comments
 (0)