@atolis-hq/corum 0.1.26 → 0.1.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/web/server.js +74 -0
- package/package.json +1 -1
- package/web/app.jsx +221 -5
- package/web/graph-utils.js +74 -0
- package/web/graph-utils.test.js +63 -0
- package/web/graph.jsx +441 -0
- package/web/index.html +5 -0
- package/web/package.json +1 -0
- package/web/router.js +1 -1
- package/web/style.css +582 -0
package/dist/src/web/server.js
CHANGED
|
@@ -343,6 +343,80 @@ export function createApp(graph, reloadEvents = createReloadEvents(), source, mu
|
|
|
343
343
|
});
|
|
344
344
|
res.json(nodes);
|
|
345
345
|
});
|
|
346
|
+
const GRAPH_EXCLUDED_TEMPLATES = new Set([
|
|
347
|
+
'Schema', 'EnumDefinition', 'Field', 'EnumValue', 'Mapping',
|
|
348
|
+
]);
|
|
349
|
+
const GRAPH_SEMANTIC_EDGE_TYPES = new Set([
|
|
350
|
+
'triggers', 'produces', 'reads', 'calls', 'implements', 'maps-to', 'derived-from',
|
|
351
|
+
]);
|
|
352
|
+
app.get('/api/graph', async (req, res) => {
|
|
353
|
+
try {
|
|
354
|
+
let targetGraph = graph;
|
|
355
|
+
if (typeof req.query.ref === 'string' && multiCache) {
|
|
356
|
+
targetGraph = await getGraphForRef(req.query.ref, multiCache, graph);
|
|
357
|
+
}
|
|
358
|
+
const nodes = [];
|
|
359
|
+
for (const node of targetGraph.nodesById.values()) {
|
|
360
|
+
if (GRAPH_EXCLUDED_TEMPLATES.has(node.template))
|
|
361
|
+
continue;
|
|
362
|
+
nodes.push({
|
|
363
|
+
id: node.id,
|
|
364
|
+
template: node.template,
|
|
365
|
+
component: node.component,
|
|
366
|
+
state: node.state,
|
|
367
|
+
stability: node.stability,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
const nodeIds = new Set(nodes.map(n => n.id));
|
|
371
|
+
const edges = [];
|
|
372
|
+
for (const edgeList of targetGraph.edgesByFrom.values()) {
|
|
373
|
+
for (const edge of edgeList) {
|
|
374
|
+
if (!GRAPH_SEMANTIC_EDGE_TYPES.has(edge.type))
|
|
375
|
+
continue;
|
|
376
|
+
if (!nodeIds.has(edge.from) || !nodeIds.has(edge.to))
|
|
377
|
+
continue;
|
|
378
|
+
edges.push({ id: edge.id, from: edge.from, to: edge.to, type: edge.type });
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
res.json({ nodes, edges });
|
|
382
|
+
}
|
|
383
|
+
catch (err) {
|
|
384
|
+
res.status(500).json({ error: String(err) });
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
app.get('/api/stats', async (req, res) => {
|
|
388
|
+
let targetGraph = graph;
|
|
389
|
+
if (typeof req.query.ref === 'string' && multiCache) {
|
|
390
|
+
targetGraph = await getGraphForRef(req.query.ref, multiCache, graph);
|
|
391
|
+
}
|
|
392
|
+
const components = new Set();
|
|
393
|
+
for (const node of targetGraph.nodesById.values()) {
|
|
394
|
+
if (node.component)
|
|
395
|
+
components.add(node.component);
|
|
396
|
+
}
|
|
397
|
+
const nodesWithEdges = new Set();
|
|
398
|
+
const edgesByType = {
|
|
399
|
+
triggers: 0, produces: 0, reads: 0, calls: 0, implements: 0, 'maps-to': 0, 'derived-from': 0,
|
|
400
|
+
};
|
|
401
|
+
for (const edgeList of targetGraph.edgesByFrom.values()) {
|
|
402
|
+
for (const edge of edgeList) {
|
|
403
|
+
if (!GRAPH_SEMANTIC_EDGE_TYPES.has(edge.type))
|
|
404
|
+
continue;
|
|
405
|
+
edgesByType[edge.type]++;
|
|
406
|
+
nodesWithEdges.add(edge.from);
|
|
407
|
+
nodesWithEdges.add(edge.to);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
const orphanNodeCount = [...targetGraph.nodesById.keys()]
|
|
411
|
+
.filter(id => !nodesWithEdges.has(id)).length;
|
|
412
|
+
res.json({
|
|
413
|
+
nodeCount: targetGraph.nodesById.size,
|
|
414
|
+
componentCount: components.size,
|
|
415
|
+
orphanNodeCount,
|
|
416
|
+
edgesByType,
|
|
417
|
+
diagnosticCount: targetGraph.diagnostics.length,
|
|
418
|
+
});
|
|
419
|
+
});
|
|
346
420
|
app.get('/api/cluster', async (req, res) => {
|
|
347
421
|
const nodeId = typeof req.query.nodeId === 'string' ? req.query.nodeId : undefined;
|
|
348
422
|
if (!nodeId) {
|
package/package.json
CHANGED
package/web/app.jsx
CHANGED
|
@@ -92,6 +92,7 @@ function NavRail({ activeSection, onSection, debugMode, onDebugMode }) {
|
|
|
92
92
|
const items = [
|
|
93
93
|
{ id: 'dashboard', icon: 'grip', label: 'Dashboard' },
|
|
94
94
|
{ id: 'components', icon: 'circle-nodes', label: 'Models' },
|
|
95
|
+
{ id: 'graph', icon: 'diagram-project', label: 'Graph' },
|
|
95
96
|
];
|
|
96
97
|
|
|
97
98
|
return (
|
|
@@ -493,15 +494,196 @@ function BranchBar({ branches, branchResults, viewingRef, overlayRefs, overlayMo
|
|
|
493
494
|
);
|
|
494
495
|
}
|
|
495
496
|
|
|
496
|
-
function
|
|
497
|
-
return
|
|
497
|
+
function HeroCard({ value, label }) {
|
|
498
|
+
return (
|
|
499
|
+
<div className="hero-card">
|
|
500
|
+
<div className="hero-number">{value}</div>
|
|
501
|
+
<div className="hero-label">{label}</div>
|
|
502
|
+
</div>
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function DashboardPage({ nodes, templates, gitMode, viewingRef, branches, branchResults }) {
|
|
507
|
+
const [stats, setStats] = useState(null);
|
|
508
|
+
const [branchDiff, setBranchDiff] = useState(null);
|
|
509
|
+
|
|
510
|
+
const defaultBranchRef = branches.find(b => b.isDefault)?.ref ?? null;
|
|
511
|
+
const isOnNonDefaultBranch = gitMode && viewingRef && defaultBranchRef && viewingRef !== defaultBranchRef;
|
|
512
|
+
|
|
513
|
+
useEffect(() => {
|
|
514
|
+
const refParam = viewingRef ? `?ref=${encodeURIComponent(viewingRef)}` : '';
|
|
515
|
+
fetch(`/api/stats${refParam}`)
|
|
516
|
+
.then(res => res.ok ? res.json() : Promise.reject(res.status))
|
|
517
|
+
.then(setStats)
|
|
518
|
+
.catch(() => setStats(null));
|
|
519
|
+
}, [viewingRef]);
|
|
520
|
+
|
|
521
|
+
useEffect(() => {
|
|
522
|
+
if (!isOnNonDefaultBranch) { setBranchDiff(null); return; }
|
|
523
|
+
fetch(`/api/overlay/${encodeURIComponent(viewingRef)}`)
|
|
524
|
+
.then(res => res.ok ? res.json() : Promise.reject(res.status))
|
|
525
|
+
.then(data => {
|
|
526
|
+
const added = (data.nodes || []).filter(n => n.ghostState === 'local').length;
|
|
527
|
+
const removed = (data.nodes || []).filter(n => n.ghostState === 'default-only').length;
|
|
528
|
+
setBranchDiff({ added, removed, defaultBranch: defaultBranchRef });
|
|
529
|
+
})
|
|
530
|
+
.catch(() => setBranchDiff(null));
|
|
531
|
+
}, [isOnNonDefaultBranch, viewingRef, defaultBranchRef]);
|
|
532
|
+
|
|
533
|
+
const componentCount = new Set(nodes.map(n => n.component).filter(Boolean)).size;
|
|
534
|
+
const nodeCount = nodes.length;
|
|
535
|
+
const totalEdges = stats ? Object.values(stats.edgesByType).reduce((a, b) => a + b, 0) : '…';
|
|
536
|
+
|
|
537
|
+
const templateMap = new Map(templates.map(t => [t.name, t]));
|
|
538
|
+
|
|
539
|
+
const nodesByTemplate = [...nodes.reduce((acc, node) => {
|
|
540
|
+
acc.set(node.template, (acc.get(node.template) ?? 0) + 1);
|
|
541
|
+
return acc;
|
|
542
|
+
}, new Map()).entries()].sort((a, b) => b[1] - a[1]);
|
|
543
|
+
|
|
544
|
+
const nodesByState = [...nodes.reduce((acc, node) => {
|
|
545
|
+
acc.set(node.state, (acc.get(node.state) ?? 0) + 1);
|
|
546
|
+
return acc;
|
|
547
|
+
}, new Map()).entries()].sort((a, b) => b[1] - a[1]);
|
|
548
|
+
|
|
549
|
+
const nodesByStability = [...nodes.reduce((acc, node) => {
|
|
550
|
+
acc.set(node.stability, (acc.get(node.stability) ?? 0) + 1);
|
|
551
|
+
return acc;
|
|
552
|
+
}, new Map()).entries()].sort((a, b) => b[1] - a[1]);
|
|
553
|
+
|
|
554
|
+
const edgeRows = stats
|
|
555
|
+
? Object.entries(stats.edgesByType).filter(([, count]) => count > 0).sort((a, b) => b[1] - a[1])
|
|
556
|
+
: [];
|
|
557
|
+
|
|
558
|
+
return (
|
|
559
|
+
<div className="content">
|
|
560
|
+
<div className="dashboard-hero">
|
|
561
|
+
<HeroCard value={componentCount} label="Components" />
|
|
562
|
+
<HeroCard value={nodeCount} label="Nodes" />
|
|
563
|
+
<HeroCard value={totalEdges} label="Edges" />
|
|
564
|
+
<HeroCard value={stats ? stats.orphanNodeCount : '…'} label="Orphan Nodes" />
|
|
565
|
+
</div>
|
|
566
|
+
|
|
567
|
+
{branchDiff && (
|
|
568
|
+
<div className="branch-diff-callout">
|
|
569
|
+
<span className="branch-diff-branch">⎇ {viewingRef}</span>
|
|
570
|
+
<span className="branch-diff-label">vs {branchDiff.defaultBranch}</span>
|
|
571
|
+
{branchDiff.added > 0 && <span className="branch-diff-added">+{branchDiff.added} added</span>}
|
|
572
|
+
{branchDiff.removed > 0 && <span className="branch-diff-removed">−{branchDiff.removed} removed</span>}
|
|
573
|
+
{branchDiff.added === 0 && branchDiff.removed === 0 && (
|
|
574
|
+
<span className="branch-diff-label">No changes</span>
|
|
575
|
+
)}
|
|
576
|
+
</div>
|
|
577
|
+
)}
|
|
578
|
+
|
|
579
|
+
<div className="dashboard-grid">
|
|
580
|
+
<div className="dashboard-panel">
|
|
581
|
+
<div className="card-head">Nodes by Type</div>
|
|
582
|
+
<div className="card-body" style={{ padding: '12px 16px' }}>
|
|
583
|
+
{nodesByTemplate.map(([name, count]) => {
|
|
584
|
+
const tmpl = templateMap.get(name);
|
|
585
|
+
const colour = tmpl?.ui?.colour ?? 'var(--ink-4)';
|
|
586
|
+
return (
|
|
587
|
+
<div key={name} className="stat-row">
|
|
588
|
+
<span className="stat-row-label">
|
|
589
|
+
<span className="stat-colour-dot" style={{ background: colour }} />
|
|
590
|
+
{tmpl?.ui?.displayName ?? name}
|
|
591
|
+
</span>
|
|
592
|
+
<span className="stat-row-value">{count}</span>
|
|
593
|
+
</div>
|
|
594
|
+
);
|
|
595
|
+
})}
|
|
596
|
+
</div>
|
|
597
|
+
</div>
|
|
598
|
+
|
|
599
|
+
<div className="dashboard-panel">
|
|
600
|
+
<div className="card-head">State Distribution</div>
|
|
601
|
+
<div className="card-body" style={{ padding: '12px 16px' }}>
|
|
602
|
+
{nodesByState.map(([state, count]) => (
|
|
603
|
+
<div key={state} className="stat-row">
|
|
604
|
+
<span className="stat-row-label"><StateTag state={state} /></span>
|
|
605
|
+
<span className="stat-row-value">{count}</span>
|
|
606
|
+
</div>
|
|
607
|
+
))}
|
|
608
|
+
</div>
|
|
609
|
+
</div>
|
|
610
|
+
|
|
611
|
+
<div className="dashboard-panel">
|
|
612
|
+
<div className="card-head">Edge Types</div>
|
|
613
|
+
<div className="card-body" style={{ padding: '12px 16px' }}>
|
|
614
|
+
{stats === null && <div className="label-sm">Loading…</div>}
|
|
615
|
+
{stats !== null && edgeRows.length === 0 && <div className="label-sm">No semantic edges.</div>}
|
|
616
|
+
{edgeRows.map(([type, count]) => (
|
|
617
|
+
<div key={type} className="stat-row">
|
|
618
|
+
<span className="stat-row-label">
|
|
619
|
+
<span className="tag" style={EDGE_TYPE_STYLES[type] ?? {}}>{type}</span>
|
|
620
|
+
</span>
|
|
621
|
+
<span className="stat-row-value">{count}</span>
|
|
622
|
+
</div>
|
|
623
|
+
))}
|
|
624
|
+
</div>
|
|
625
|
+
</div>
|
|
626
|
+
|
|
627
|
+
<div className="dashboard-panel">
|
|
628
|
+
<div className="card-head">Stability</div>
|
|
629
|
+
<div className="card-body" style={{ padding: '12px 16px' }}>
|
|
630
|
+
{nodesByStability.map(([stability, count]) => (
|
|
631
|
+
<div key={stability} className="stat-row">
|
|
632
|
+
<span className="stat-row-label"><StabilityTag stability={stability} /></span>
|
|
633
|
+
<span className="stat-row-value">{count}</span>
|
|
634
|
+
</div>
|
|
635
|
+
))}
|
|
636
|
+
</div>
|
|
637
|
+
</div>
|
|
638
|
+
|
|
639
|
+
<div className="dashboard-panel">
|
|
640
|
+
<div className="card-head">Diagnostics</div>
|
|
641
|
+
<div className="card-body" style={{ padding: '12px 16px' }}>
|
|
642
|
+
{stats === null && <div className="label-sm">Loading…</div>}
|
|
643
|
+
{stats !== null && stats.diagnosticCount > 0 && (
|
|
644
|
+
<span style={{ color: 'var(--warn)' }}>
|
|
645
|
+
{stats.diagnosticCount} load warning{stats.diagnosticCount !== 1 ? 's' : ''}
|
|
646
|
+
</span>
|
|
647
|
+
)}
|
|
648
|
+
{stats !== null && stats.diagnosticCount === 0 && (
|
|
649
|
+
<span style={{ color: 'var(--ok)' }}>No issues</span>
|
|
650
|
+
)}
|
|
651
|
+
</div>
|
|
652
|
+
</div>
|
|
653
|
+
|
|
654
|
+
{gitMode && (
|
|
655
|
+
<div className="dashboard-panel">
|
|
656
|
+
<div className="card-head">Branch Health</div>
|
|
657
|
+
<div className="card-body" style={{ padding: '12px 16px' }}>
|
|
658
|
+
{branchResults.map(result => (
|
|
659
|
+
<div key={result.ref} className="stat-row">
|
|
660
|
+
<span className="stat-row-label">
|
|
661
|
+
<Icon
|
|
662
|
+
name={result.status === 'loaded' ? 'circle-check' : 'circle-xmark'}
|
|
663
|
+
size={12}
|
|
664
|
+
/>
|
|
665
|
+
<span>{result.ref}</span>
|
|
666
|
+
</span>
|
|
667
|
+
{result.status === 'failed' && (
|
|
668
|
+
<span className="label-sm" style={{ color: 'var(--warn)', maxWidth: 200, textAlign: 'right' }}>
|
|
669
|
+
{result.diagnostics?.[0]?.message ?? result.error ?? 'failed'}
|
|
670
|
+
</span>
|
|
671
|
+
)}
|
|
672
|
+
</div>
|
|
673
|
+
))}
|
|
674
|
+
</div>
|
|
675
|
+
</div>
|
|
676
|
+
)}
|
|
677
|
+
</div>
|
|
678
|
+
</div>
|
|
679
|
+
);
|
|
498
680
|
}
|
|
499
681
|
|
|
500
682
|
function ComponentsPage() {
|
|
501
683
|
return <div className="content"><h1>Components</h1></div>;
|
|
502
684
|
}
|
|
503
685
|
|
|
504
|
-
function EdgePanel({ inbound, outbound, allNodes, templates, onNavigate }) {
|
|
686
|
+
function EdgePanel({ inbound, outbound, allNodes, templates, onNavigate, viewingRef }) {
|
|
505
687
|
const { useState: useLocalState } = React;
|
|
506
688
|
const [open, setOpen] = useLocalState(() => {
|
|
507
689
|
try { return localStorage.getItem('corum:edgePanelOpen') === 'true'; } catch { return false; }
|
|
@@ -536,6 +718,14 @@ function EdgePanel({ inbound, outbound, allNodes, templates, onNavigate }) {
|
|
|
536
718
|
<span style={{ fontWeight: 500, fontSize: 13 }}>{name}</span>
|
|
537
719
|
{node && <StateTag state={node.state} />}
|
|
538
720
|
{node && <StabilityTag stability={node.stability} />}
|
|
721
|
+
<button
|
|
722
|
+
className="graph-nav-link"
|
|
723
|
+
title="View in graph"
|
|
724
|
+
onClick={e => { e.stopPropagation(); navigate(buildRoute({ pathname: '/graph', params: { focus: linkedNodeId }, branch: viewingRef })); }}
|
|
725
|
+
style={{ marginLeft: 'auto' }}
|
|
726
|
+
>
|
|
727
|
+
<Icon name="diagram-project" size={11} />
|
|
728
|
+
</button>
|
|
539
729
|
</div>
|
|
540
730
|
);
|
|
541
731
|
}
|
|
@@ -638,6 +828,14 @@ function NodePage({ nodeId, templates, onNavigate, refreshToken, viewingRef, ove
|
|
|
638
828
|
<TemplateBadge name={templateDisplayName(template)} colour={colour} />
|
|
639
829
|
<StateTag state={root.state} />
|
|
640
830
|
<StabilityTag stability={root.stability} />
|
|
831
|
+
<button
|
|
832
|
+
className="graph-nav-link"
|
|
833
|
+
onClick={() => navigate(buildRoute({ pathname: '/graph', params: { focus: root.id }, branch: viewingRef }))}
|
|
834
|
+
title="View in graph"
|
|
835
|
+
>
|
|
836
|
+
<Icon name="diagram-project" size={11} />
|
|
837
|
+
<span>Graph</span>
|
|
838
|
+
</button>
|
|
641
839
|
</div>
|
|
642
840
|
<div className="label-sm mono">{root.id}</div>
|
|
643
841
|
</div>
|
|
@@ -648,6 +846,7 @@ function NodePage({ nodeId, templates, onNavigate, refreshToken, viewingRef, ove
|
|
|
648
846
|
allNodes={[root, ...descendants, ...includedNodes]}
|
|
649
847
|
templates={templates}
|
|
650
848
|
onNavigate={onNavigate}
|
|
849
|
+
viewingRef={viewingRef}
|
|
651
850
|
/>
|
|
652
851
|
|
|
653
852
|
<div className="meta-strip">
|
|
@@ -844,7 +1043,7 @@ function App() {
|
|
|
844
1043
|
const activeNodeId = route.pathname === '/node' ? route.params.get('id') : null;
|
|
845
1044
|
const activeSection = activeNodeId ? 'components' : (route.pathname.slice(1) || 'dashboard');
|
|
846
1045
|
const navTree = useMemo(() => buildNavTree(nodes, templates), [nodes, templates]);
|
|
847
|
-
const showTree = activeSection === 'components' || activeNodeId;
|
|
1046
|
+
const showTree = (activeSection === 'components' || activeNodeId) && activeSection !== 'graph';
|
|
848
1047
|
const activeOverlayRefs = useMemo(() =>
|
|
849
1048
|
overlayMode === 'single' ? [] :
|
|
850
1049
|
overlayMode === 'consolidated' ? branches.filter(branch => branch.ref !== viewingRef).map(branch => branch.ref) :
|
|
@@ -882,7 +1081,16 @@ function App() {
|
|
|
882
1081
|
} else if (error) {
|
|
883
1082
|
page = <div className="content"><p style={{ color: 'var(--warn)' }}>Error loading graph: {error}</p></div>;
|
|
884
1083
|
} else if (route.pathname === '/dashboard' || route.pathname === '/') {
|
|
885
|
-
page =
|
|
1084
|
+
page = (
|
|
1085
|
+
<DashboardPage
|
|
1086
|
+
nodes={nodes}
|
|
1087
|
+
templates={templates}
|
|
1088
|
+
gitMode={gitMode}
|
|
1089
|
+
viewingRef={viewingRef}
|
|
1090
|
+
branches={branches}
|
|
1091
|
+
branchResults={branchResults}
|
|
1092
|
+
/>
|
|
1093
|
+
);
|
|
886
1094
|
} else if (route.pathname === '/components') {
|
|
887
1095
|
page = <ComponentsPage />;
|
|
888
1096
|
} else if (route.pathname === '/node') {
|
|
@@ -897,6 +1105,14 @@ function App() {
|
|
|
897
1105
|
compact={!debugMode}
|
|
898
1106
|
/>
|
|
899
1107
|
);
|
|
1108
|
+
} else if (route.pathname === '/graph') {
|
|
1109
|
+
page = (
|
|
1110
|
+
<window.CorumGraph.GraphView
|
|
1111
|
+
route={route}
|
|
1112
|
+
viewingRef={viewingRef}
|
|
1113
|
+
templates={templates}
|
|
1114
|
+
/>
|
|
1115
|
+
);
|
|
900
1116
|
} else {
|
|
901
1117
|
page = <div className="content"><p className="label-sm">Page not found.</p></div>;
|
|
902
1118
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/* Pure graph utility functions — browser globals + Node require() compatible. */
|
|
2
|
+
|
|
3
|
+
function getDisplayName(nodeId) {
|
|
4
|
+
return nodeId.split('.').pop();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function buildComponentMap(nodes, edges) {
|
|
8
|
+
const componentCounts = new Map();
|
|
9
|
+
for (const node of nodes) {
|
|
10
|
+
componentCounts.set(node.component, (componentCounts.get(node.component) ?? 0) + 1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const componentNodes = [...componentCounts.entries()].map(([component, count]) => ({
|
|
14
|
+
id: component,
|
|
15
|
+
component,
|
|
16
|
+
count,
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
const nodeById = new Map(nodes.map(n => [n.id, n]));
|
|
20
|
+
const compEdgeMap = new Map();
|
|
21
|
+
for (const edge of edges) {
|
|
22
|
+
const fromNode = nodeById.get(edge.from);
|
|
23
|
+
const toNode = nodeById.get(edge.to);
|
|
24
|
+
if (!fromNode || !toNode) continue;
|
|
25
|
+
if (fromNode.component === toNode.component) continue;
|
|
26
|
+
const key = `${fromNode.component}__${toNode.component}`;
|
|
27
|
+
if (!compEdgeMap.has(key)) compEdgeMap.set(key, { from: fromNode.component, to: toNode.component, types: new Set() });
|
|
28
|
+
compEdgeMap.get(key).types.add(edge.type);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const componentEdges = [...compEdgeMap.entries()].map(([key, entry]) => ({
|
|
32
|
+
id: key,
|
|
33
|
+
from: entry.from,
|
|
34
|
+
to: entry.to,
|
|
35
|
+
types: [...entry.types],
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
return { nodes: componentNodes, edges: componentEdges };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function buildFocusGraph(focalNodeId, nodes, edges, depth) {
|
|
42
|
+
const nodeById = new Map(nodes.map(n => [n.id, n]));
|
|
43
|
+
const visited = new Set([focalNodeId]);
|
|
44
|
+
const queue = [{ id: focalNodeId, d: 0 }];
|
|
45
|
+
|
|
46
|
+
while (queue.length > 0) {
|
|
47
|
+
const { id, d } = queue.shift();
|
|
48
|
+
if (d >= depth) continue;
|
|
49
|
+
for (const edge of edges) {
|
|
50
|
+
let neighborId = null;
|
|
51
|
+
if (edge.from === id) neighborId = edge.to;
|
|
52
|
+
else if (edge.to === id) neighborId = edge.from;
|
|
53
|
+
if (!neighborId || visited.has(neighborId) || !nodeById.has(neighborId)) continue;
|
|
54
|
+
visited.add(neighborId);
|
|
55
|
+
queue.push({ id: neighborId, d: d + 1 });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
nodes: [...visited].map(id => nodeById.get(id)).filter(Boolean),
|
|
61
|
+
edges: edges.filter(e => visited.has(e.from) && visited.has(e.to)),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function applyEdgeTypeFilter(edges, visibleTypes) {
|
|
66
|
+
return edges.filter(e => visibleTypes.has(e.type));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
70
|
+
module.exports = { buildComponentMap, buildFocusGraph, applyEdgeTypeFilter, getDisplayName };
|
|
71
|
+
}
|
|
72
|
+
if (typeof window !== 'undefined') {
|
|
73
|
+
window.CorumGraphUtils = { buildComponentMap, buildFocusGraph, applyEdgeTypeFilter, getDisplayName };
|
|
74
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// run with: node web/graph-utils.test.js
|
|
2
|
+
const { buildComponentMap, buildFocusGraph, applyEdgeTypeFilter, getDisplayName } = require('./graph-utils.js');
|
|
3
|
+
|
|
4
|
+
(async () => {
|
|
5
|
+
|
|
6
|
+
let passed = 0, failed = 0;
|
|
7
|
+
function assert(condition, name) {
|
|
8
|
+
if (condition) { console.log(` ✓ ${name}`); passed++; }
|
|
9
|
+
else { console.error(` ✗ ${name}`); failed++; }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const nodes = [
|
|
13
|
+
{ id: 'orders.DomainModel.order', template: 'DomainModel', component: 'orders', state: 'agreed', stability: 'stable' },
|
|
14
|
+
{ id: 'orders.RestAPI.ordersApi', template: 'RestAPI', component: 'orders', state: 'agreed', stability: 'stable' },
|
|
15
|
+
{ id: 'payments.DomainModel.payment', template: 'DomainModel', component: 'payments', state: 'proposed', stability: 'unstable' },
|
|
16
|
+
{ id: 'payments.RestAPI.paymentsApi', template: 'RestAPI', component: 'payments', state: 'proposed', stability: 'unstable' },
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const edges = [
|
|
20
|
+
{ id: 'e1', from: 'orders.RestAPI.ordersApi', to: 'orders.DomainModel.order', type: 'reads' },
|
|
21
|
+
{ id: 'e2', from: 'orders.RestAPI.ordersApi', to: 'payments.RestAPI.paymentsApi', type: 'calls' },
|
|
22
|
+
{ id: 'e3', from: 'payments.RestAPI.paymentsApi', to: 'payments.DomainModel.payment', type: 'reads' },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
// getDisplayName
|
|
26
|
+
assert(getDisplayName('orders.DomainModel.order') === 'order', 'getDisplayName returns last segment');
|
|
27
|
+
assert(getDisplayName('payments.RestAPI.paymentsApi') === 'paymentsApi', 'getDisplayName handles nested id');
|
|
28
|
+
|
|
29
|
+
// buildComponentMap
|
|
30
|
+
const cm = buildComponentMap(nodes, edges);
|
|
31
|
+
assert(cm.nodes.length === 2, 'buildComponentMap: one node per component');
|
|
32
|
+
assert(cm.nodes.every(n => typeof n.count === 'number' && n.count > 0), 'buildComponentMap: nodes have count');
|
|
33
|
+
const cmOrders = cm.nodes.find(n => n.id === 'orders');
|
|
34
|
+
assert(cmOrders?.count === 2, 'buildComponentMap: orders has 2 nodes');
|
|
35
|
+
assert(cm.edges.length === 1, 'buildComponentMap: one cross-component edge');
|
|
36
|
+
assert(cm.edges[0].from === 'orders' && cm.edges[0].to === 'payments', 'buildComponentMap: edge direction correct');
|
|
37
|
+
assert(cm.edges[0].types.includes('calls'), 'buildComponentMap: edge types collected');
|
|
38
|
+
const sameCmEdges = buildComponentMap(nodes, [{ id: 'x', from: 'orders.DomainModel.order', to: 'orders.RestAPI.ordersApi', type: 'reads' }]);
|
|
39
|
+
assert(sameCmEdges.edges.length === 0, 'buildComponentMap: intra-component edges excluded');
|
|
40
|
+
|
|
41
|
+
// buildFocusGraph - depth 1
|
|
42
|
+
const focus1 = buildFocusGraph('orders.RestAPI.ordersApi', nodes, edges, 1);
|
|
43
|
+
assert(focus1.nodes.some(n => n.id === 'orders.RestAPI.ordersApi'), 'buildFocusGraph depth=1: includes focal node');
|
|
44
|
+
assert(focus1.nodes.some(n => n.id === 'orders.DomainModel.order'), 'buildFocusGraph depth=1: includes 1-hop neighbour (via reads)');
|
|
45
|
+
assert(focus1.nodes.some(n => n.id === 'payments.RestAPI.paymentsApi'), 'buildFocusGraph depth=1: includes 1-hop neighbour (via calls)');
|
|
46
|
+
assert(!focus1.nodes.some(n => n.id === 'payments.DomainModel.payment'), 'buildFocusGraph depth=1: excludes 2-hop neighbour');
|
|
47
|
+
assert(focus1.edges.length === 2, 'buildFocusGraph depth=1: includes only edges within visible nodes');
|
|
48
|
+
|
|
49
|
+
// buildFocusGraph - depth Infinity
|
|
50
|
+
const focusAll = buildFocusGraph('orders.RestAPI.ordersApi', nodes, edges, Infinity);
|
|
51
|
+
assert(focusAll.nodes.length === 4, 'buildFocusGraph depth=Infinity: includes all reachable nodes');
|
|
52
|
+
assert(focusAll.edges.length === 3, 'buildFocusGraph depth=Infinity: includes all reachable edges');
|
|
53
|
+
|
|
54
|
+
// applyEdgeTypeFilter
|
|
55
|
+
const filtered = applyEdgeTypeFilter(edges, new Set(['reads']));
|
|
56
|
+
assert(filtered.length === 2, 'applyEdgeTypeFilter: keeps only matching type');
|
|
57
|
+
assert(filtered.every(e => e.type === 'reads'), 'applyEdgeTypeFilter: all results have correct type');
|
|
58
|
+
const none = applyEdgeTypeFilter(edges, new Set([]));
|
|
59
|
+
assert(none.length === 0, 'applyEdgeTypeFilter: empty set returns nothing');
|
|
60
|
+
|
|
61
|
+
console.log(`\n${passed} passed, ${failed} failed`);
|
|
62
|
+
if (failed > 0) process.exit(1);
|
|
63
|
+
})();
|