feat: add neo4j and prompt and prompt-graph
This commit is contained in:
73
src/routes/prompt-graph/d3/get-graph.ts
Normal file
73
src/routes/prompt-graph/d3/get-graph.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { getSession } from '@/app.ts';
|
||||
export async function fetchData() {
|
||||
const session = getSession();
|
||||
try {
|
||||
const query = `MATCH (n)
|
||||
OPTIONAL MATCH (n)-[r]->(m)
|
||||
RETURN n, r, m`;
|
||||
const queryConnect = 'MATCH (n)-[r]->(m) RETURN n, r, m LIMIT 25';
|
||||
const result = await session.run(query);
|
||||
|
||||
const graphData = { nodes: [], links: [] };
|
||||
const nodeMap = new Map();
|
||||
// n和n的关系用 relatedPrompts 进行关联
|
||||
result.records.forEach((record) => {
|
||||
const node = record.get('n');
|
||||
const relation = record.get('r');
|
||||
const target = record.get('m');
|
||||
if (!nodeMap.has(node.identity)) {
|
||||
nodeMap.set(node.identity, {
|
||||
id: node.identity.toString(),
|
||||
label: node.labels[0],
|
||||
properties: node.properties,
|
||||
});
|
||||
graphData.nodes.push(nodeMap.get(node.identity));
|
||||
}
|
||||
|
||||
if (relation && !nodeMap.has(relation.identity)) {
|
||||
nodeMap.set(relation.identity, {
|
||||
id: relation.identity.toString(),
|
||||
label: relation.type,
|
||||
properties: relation.properties,
|
||||
});
|
||||
graphData.nodes.push(nodeMap.get(relation.identity));
|
||||
}
|
||||
|
||||
if (target && !nodeMap.has(target.identity)) {
|
||||
nodeMap.set(target.identity, {
|
||||
id: target.identity.toString(),
|
||||
label: target.labels[0],
|
||||
properties: target.properties,
|
||||
});
|
||||
graphData.nodes.push(nodeMap.get(target.identity));
|
||||
}
|
||||
|
||||
if (relation) {
|
||||
graphData.links.push({
|
||||
source: node.identity.toString(),
|
||||
target: relation.identity.toString(),
|
||||
type: relation.type,
|
||||
properties: relation.properties,
|
||||
});
|
||||
}
|
||||
|
||||
if (target) {
|
||||
graphData.links.push({
|
||||
source: node.identity.toString(),
|
||||
target: target.identity.toString(),
|
||||
type: 'RELATED_TO',
|
||||
properties: {},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return graphData;
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
}
|
||||
|
||||
// fetchData().then((graphData) => {
|
||||
// console.log(graphData); // 用于验证获取的数据
|
||||
// drawGraph(graphData); // 调用 D3 绘制函数
|
||||
// });
|
||||
Reference in New Issue
Block a user