bada-terminal / src /utils /graph-intelligence.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
9.39 kB
const db = require('../../knowledge-db');
/**
* Pure JS ๊ธฐ๋ฐ˜์˜ ๊ฒฝ๋Ÿ‰ Louvain ์ปค๋ฎค๋‹ˆํ‹ฐ ํƒ์ง€ ๋ฐ ๊ทธ๋ž˜ํ”„ ์ธํ…”๋ฆฌ์ „์Šค ๋ชจ๋“ˆ
*/
/**
* ๊ทธ๋ž˜ํ”„์˜ ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•˜๊ณ  ๋ฐ€์ง‘์„ฑ(Cohesion)์„ ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค.
* @param {object} graphData { nodes, edges } ๊ตฌ์กฐ์˜ ๊ทธ๋ž˜ํ”„ ๋ฐ์ดํ„ฐ
* @returns {object} { communityMap, communitiesDetail }
*/
function detectCommunities(graphData) {
const nodes = graphData.nodes || [];
const edges = graphData.edges || [];
if (nodes.length === 0) {
return { communityMap: {}, communitiesDetail: {} };
}
// 1. ์ดˆ๊ธฐํ™”: ๊ฐ ๋…ธ๋“œ๋ฅผ ๊ณ ์œ  ์ปค๋ฎค๋‹ˆํ‹ฐ ID๋กœ ์ง€์ •
const communityMap = {};
nodes.forEach((n, idx) => {
communityMap[n.id] = idx;
});
// ๋…ธ๋“œ ์ฐจ์ˆ˜(degree) ๋ฐ ๊ฐ€์ค‘์น˜ ํ•ฉ ๊ณ„์‚ฐ
const degrees = {};
nodes.forEach(n => {
degrees[n.id] = 0;
});
const adj = {};
nodes.forEach(n => {
adj[n.id] = {};
});
edges.forEach(e => {
if (degrees[e.source] !== undefined && degrees[e.target] !== undefined) {
degrees[e.source]++;
degrees[e.target]++;
// ๊ฐ€์ค‘์น˜(๊ธฐ๋ณธ๊ฐ’ 1.0)
const weight = parseFloat(e.properties?.weight) || 1.0;
adj[e.source][e.target] = (adj[e.source][e.target] || 0) + weight;
adj[e.target][e.source] = (adj[e.target][e.source] || 0) + weight;
}
});
// ์ „์ฒด ์ฐจ์ˆ˜ ํ•ฉ (2m)
let totalWeight2m = 0;
nodes.forEach(n => {
Object.keys(adj[n.id]).forEach(nbr => {
totalWeight2m += adj[n.id][nbr];
});
});
if (totalWeight2m === 0) {
totalWeight2m = nodes.length; // fallback
}
// 2. Modularity ์ตœ์ ํ™” ๋ฃจํ”„ (์ตœ๋Œ€ 15ํšŒ ๋ฐ˜๋ณต)
let improvement = true;
let iterations = 0;
while (improvement && iterations < 15) {
improvement = false;
iterations++;
// ๊ฐ ๋…ธ๋“œ ์ˆœํšŒํ•˜๋ฉฐ ์ด์›ƒ ์ปค๋ฎค๋‹ˆํ‹ฐ๋กœ ์ด์‹ํ•˜์—ฌ modularity delta ๊ณ„์‚ฐ
for (const node of nodes) {
const u = node.id;
const currentComm = communityMap[u];
const neighbors = Object.keys(adj[u]);
if (neighbors.length === 0) continue;
// ์ด์›ƒ ๋…ธ๋“œ๋“ค์ด ์†ํ•ด์žˆ๋Š” ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฆฌ์ŠคํŠธ ์ถ”์ถœ
const neighborComms = new Set();
neighbors.forEach(v => neighborComms.add(communityMap[v]));
let bestComm = currentComm;
let maxDeltaQ = 0;
// ๊ฐ ์ด์›ƒ ์ปค๋ฎค๋‹ˆํ‹ฐ๋กœ u๋ฅผ ์˜ฎ๊ฒผ์„ ๋•Œ์˜ Delta Q ์—ฐ์‚ฐ
neighborComms.forEach(c => {
if (c === currentComm) return;
// u์™€ ์ปค๋ฎค๋‹ˆํ‹ฐ c ๋‚ด์˜ ๋…ธ๋“œ๋“ค ๊ฐ„์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ (Ki,in)
let k_i_in = 0;
neighbors.forEach(v => {
if (communityMap[v] === c) {
k_i_in += adj[u][v] || 0;
}
});
// ์ปค๋ฎค๋‹ˆํ‹ฐ c์— ์†ํ•œ ์ „์ฒด ๋…ธ๋“œ๋“ค์˜ ์ฐจ์ˆ˜ ํ•ฉ (Sigma_tot)
let sigma_tot = 0;
nodes.forEach(n => {
if (communityMap[n.id] === c) {
Object.keys(adj[n.id]).forEach(nbr => {
sigma_tot += adj[n.id][nbr] || 0;
});
}
});
// u์˜ ์ž์ฒด ์ฐจ์ˆ˜ (Ki)
let k_i = 0;
Object.keys(adj[u]).forEach(nbr => {
k_i += adj[u][nbr] || 0;
});
// Delta Q ๊ฐ„์†Œ ๊ณ„์‚ฐ ๊ณต์‹
const deltaQ = (k_i_in / totalWeight2m) - (sigma_tot * k_i) / (totalWeight2m * totalWeight2m);
if (deltaQ > maxDeltaQ) {
maxDeltaQ = deltaQ;
bestComm = c;
}
});
if (bestComm !== currentComm) {
communityMap[u] = bestComm;
improvement = true;
}
}
}
// 3. ์ปค๋ฎค๋‹ˆํ‹ฐ ์ƒ์„ธ ์ง€ํ‘œ ๋ฐ Cohesion ๊ณ„์‚ฐ
const communitiesDetail = {};
nodes.forEach(n => {
const cId = communityMap[n.id];
if (!communitiesDetail[cId]) {
communitiesDetail[cId] = {
id: cId,
nodes: [],
internalEdgesCount: 0,
cohesion: 1.0
};
}
communitiesDetail[cId].nodes.push(n.id);
});
// ๊ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ ๋‚ด๋ถ€ ์—ฃ์ง€ ์ˆ˜ ๊ณ„์‚ฐ
edges.forEach(e => {
const cSrc = communityMap[e.source];
const cTgt = communityMap[e.target];
if (cSrc === cTgt && communitiesDetail[cSrc]) {
communitiesDetail[cSrc].internalEdgesCount++;
}
});
// Cohesion ์ ์ˆ˜ ๊ณต์‹ ์ ์šฉ
Object.keys(communitiesDetail).forEach(cId => {
const comm = communitiesDetail[cId];
const n = comm.nodes.length;
if (n >= 2) {
const maxPossibleEdges = (n * (n - 1)) / 2;
comm.cohesion = comm.internalEdgesCount / maxPossibleEdges;
} else {
comm.cohesion = 1.0;
}
});
return { communityMap, communitiesDetail };
}
/**
* ํ”„๋กœ์ ํŠธ ์ง€์‹์— ๊ธฐ๋ฐ˜ํ•œ Heuristics Graph Insights๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
* @param {string} projectId ํ”„๋กœ์ ํŠธ ํด๋”๋ช…
* @returns {object} { surprisingConnections, knowledgeGaps }
*/
function generateGraphInsights(projectId) {
const graphData = db.exportProjectData(projectId);
if (!graphData || !graphData.nodes || graphData.nodes.length === 0) {
return { surprisingConnections: [], knowledgeGaps: { isolatedNodes: [], sparseCommunities: [], bridgeNodes: [] } };
}
const { communityMap, communitiesDetail } = detectCommunities(graphData);
// 1. ์ฐจ์ˆ˜(degree) ๋ฐ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
const degrees = {};
const adj = {};
graphData.nodes.forEach(n => {
degrees[n.id] = 0;
adj[n.id] = new Set();
});
graphData.edges.forEach(e => {
if (degrees[e.source] !== undefined && degrees[e.target] !== undefined) {
degrees[e.source]++;
degrees[e.target]++;
adj[e.source].add(e.target);
adj[e.target].add(e.source);
}
});
// 2. Surprising Connections (์˜์™ธ์˜ ์—ฐ๊ฒฐ) ํƒ์ง€ ๊ทœ์น™ Heuristics
const surprisingConnections = [];
graphData.edges.forEach(e => {
// ๊ตฌ์กฐ ๋…ธ๋“œ(index, log ๋“ฑ)๋Š” ๋…ธ์ด์ฆˆ ์œ ๋ฐœํ•˜๋ฏ€๋กœ ์ œ์™ธ
if (e.source.includes('index') || e.target.includes('index')) return;
let score = 0;
const cSrc = communityMap[e.source];
const cTgt = communityMap[e.target];
// ๊ทœ์น™ 1: ์„œ๋กœ ๋‹ค๋ฅธ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ฐ„์˜ ์—ฐ๊ฒฐ
if (cSrc !== undefined && cTgt !== undefined && cSrc !== cTgt) {
score += 3.0;
}
const nodeSrc = graphData.nodes.find(n => n.id === e.source);
const nodeTgt = graphData.nodes.find(n => n.id === e.target);
if (nodeSrc && nodeTgt) {
// ๊ทœ์น™ 2: ํฌ๋กœ์Šค ํƒ€์ž… (์˜ˆ: ์†Œ์ŠคํŒŒ์ผ๊ณผ ์„ธ์…˜ ๋˜๋Š” ์†Œ์ŠคํŒŒ์ผ๊ณผ ์œ„ํ‚ค๊ฐ€ ๋จผ ๊ฒฐํ•ฉ์„ ๊ฐ€์งˆ ๋•Œ)
if (nodeSrc.type !== nodeTgt.type) {
if ((nodeSrc.type === 'file' && nodeTgt.type === 'session') || (nodeSrc.type === 'session' && nodeTgt.type === 'file')) {
score += 2.0;
} else {
score += 1.0;
}
}
// ๊ทœ์น™ 3: ๋ณ€๋‘๋ฆฌ ๋…ธ๋“œ(์ฐจ์ˆ˜ <= 2)์™€ ํ—ˆ๋ธŒ ๋…ธ๋“œ(์ฐจ์ˆ˜ >= 8) ๊ฐ„์˜ ์—ฐ๊ฒฐ
const degSrc = degrees[e.source] || 0;
const degTgt = degrees[e.target] || 0;
if ((degSrc <= 2 && degTgt >= 8) || (degTgt <= 2 && degSrc >= 8)) {
score += 2.0;
}
}
if (score >= 4.0) {
surprisingConnections.push({
edge: e,
sourceLabel: nodeSrc ? nodeSrc.label : e.source,
targetLabel: nodeTgt ? nodeTgt.label : e.target,
score
});
}
});
// ์ค‘๋ณต ์—ฃ์ง€ ์ œ๊ฑฐ ๋ฐ ์ •๋ ฌ
const uniqueSurprising = surprisingConnections
.sort((a, b) => b.score - a.score)
.slice(0, 10);
// 3. Knowledge Gaps (์ง€์‹ ๊ฒฐํ•จ) ํƒ์ง€ ๊ทœ์น™ Heuristics
// A. Isolated Nodes (๊ณ ๋ฆฝ ๋…ธ๋“œ: ์—ฐ๊ฒฐ ์ฐจ์ˆ˜ <= 1 ์ธ ์œ„ํ‚ค/๋…ธํŠธ ๋ฐ ์†Œ์Šค ํŒŒ์ผ)
const isolatedNodes = graphData.nodes
.filter(n => {
if (n.id.includes('index') || n.id.includes('claude')) return false;
const deg = degrees[n.id] || 0;
return deg <= 1 && (n.type === 'note' || n.type === 'wiki' || n.type === 'file');
})
.map(n => ({ id: n.id, label: n.label, type: n.type }));
// B. Sparse Communities (ํฌ์†Œ ์ปค๋ฎค๋‹ˆํ‹ฐ: ๋…ธ๋“œ >= 3๊ฐœ, Cohesion < 0.2)
const sparseCommunities = [];
Object.keys(communitiesDetail).forEach(cId => {
const comm = communitiesDetail[cId];
if (comm.nodes.length >= 3 && comm.cohesion < 0.20) {
// ํ•ด๋‹น ์ปค๋ฎค๋‹ˆํ‹ฐ์˜ ๋…ธ๋“œ ๋ผ๋ฒจ ์ˆ˜์ง‘
const commLabels = comm.nodes.map(nId => {
const found = graphData.nodes.find(n => n.id === nId);
return found ? found.label : nId;
});
sparseCommunities.push({
communityId: cId,
nodeCount: comm.nodes.length,
cohesion: comm.cohesion,
nodes: commLabels
});
}
});
// C. Bridge Nodes (๋ธŒ๋ฆฌ์ง€ ๋…ธ๋“œ: 3๊ฐœ ์ด์ƒ์˜ ๋‹ค๋ฅธ ์ปค๋ฎค๋‹ˆํ‹ฐ์˜ ๋…ธ๋“œ๋“ค๊ณผ ๊ฐ๊ฐ ์—ฃ์ง€๋ฅผ ๊ฐ€์ง€๋Š” ๋…ธ๋“œ)
const bridgeNodes = [];
graphData.nodes.forEach(n => {
const connectedComms = new Set();
const neighbors = adj[n.id] || new Set();
neighbors.forEach(nbrId => {
const cNbr = communityMap[nbrId];
if (cNbr !== undefined && cNbr !== communityMap[n.id]) {
connectedComms.add(cNbr);
}
});
if (connectedComms.size >= 3) {
bridgeNodes.push({
id: n.id,
label: n.label,
type: n.type,
connectedCommunitiesCount: connectedComms.size
});
}
});
return {
surprisingConnections: uniqueSurprising,
knowledgeGaps: {
isolatedNodes,
sparseCommunities,
bridgeNodes
}
};
}
module.exports = {
detectCommunities,
generateGraphInsights
};