|
| 1 | +import './GraphDebugPanel.scss'; |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { Icon, Label, List, Table } from 'semantic-ui-react'; |
| 5 | + |
| 6 | +import { TreeDocument } from '../../types'; |
| 7 | + |
| 8 | +interface NodeStats { |
| 9 | + id: string; |
| 10 | + displayName: string; |
| 11 | + doctype: string; |
| 12 | + inDegree: number; |
| 13 | + outDegree: number; |
| 14 | + isRoot: boolean; |
| 15 | + linkTypes: Record<string, number>; |
| 16 | +} |
| 17 | + |
| 18 | +interface GraphDebugPanelProps { |
| 19 | + dataStore: Record<string, TreeDocument>; |
| 20 | +} |
| 21 | + |
| 22 | +const computeStats = (dataStore: Record<string, TreeDocument>): NodeStats[] => { |
| 23 | + const inDegreeMap: Record<string, number> = {}; |
| 24 | + const linkTypeMap: Record<string, Record<string, number>> = {}; |
| 25 | + |
| 26 | + Object.values(dataStore).forEach((doc) => { |
| 27 | + if (!inDegreeMap[doc.id]) inDegreeMap[doc.id] = 0; |
| 28 | + if (!linkTypeMap[doc.id]) linkTypeMap[doc.id] = {}; |
| 29 | + |
| 30 | + if (doc.links) { |
| 31 | + doc.links.forEach((link) => { |
| 32 | + if (!link.document) return; |
| 33 | + const targetId = link.document.id; |
| 34 | + if (!inDegreeMap[targetId]) inDegreeMap[targetId] = 0; |
| 35 | + if (link.ltype === 'Contains') { |
| 36 | + inDegreeMap[targetId] = (inDegreeMap[targetId] || 0) + 1; |
| 37 | + } |
| 38 | + linkTypeMap[doc.id][link.ltype] = (linkTypeMap[doc.id][link.ltype] || 0) + 1; |
| 39 | + }); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + return Object.values(dataStore) |
| 44 | + .filter((doc) => doc.doctype === 'CRE') |
| 45 | + .map((doc) => ({ |
| 46 | + id: doc.id, |
| 47 | + displayName: doc.displayName, |
| 48 | + doctype: doc.doctype, |
| 49 | + inDegree: inDegreeMap[doc.id] || 0, |
| 50 | + outDegree: doc.links ? doc.links.length : 0, |
| 51 | + isRoot: (inDegreeMap[doc.id] || 0) === 0, |
| 52 | + linkTypes: linkTypeMap[doc.id] || {}, |
| 53 | + })) |
| 54 | + .sort((a, b) => (b.isRoot ? 1 : 0) - (a.isRoot ? 1 : 0)); |
| 55 | +}; |
| 56 | + |
| 57 | +export const GraphDebugPanel = ({ dataStore }: GraphDebugPanelProps) => { |
| 58 | + const stats = computeStats(dataStore); |
| 59 | + const rootCount = stats.filter((s) => s.isRoot).length; |
| 60 | + const totalNodes = stats.length; |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className="graph-debug-panel"> |
| 64 | + <div className="graph-debug-panel__header"> |
| 65 | + <Icon name="bug" /> |
| 66 | + <strong>Graph Debug Info</strong> |
| 67 | + <span className="graph-debug-panel__summary"> |
| 68 | + {totalNodes} CRE nodes — {rootCount} roots |
| 69 | + </span> |
| 70 | + </div> |
| 71 | + |
| 72 | + <div className="graph-debug-panel__legend"> |
| 73 | + <Label size="tiny" color="green"> |
| 74 | + Root |
| 75 | + </Label> |
| 76 | + <span> = no incoming Contains links</span> |
| 77 | + </div> |
| 78 | + |
| 79 | + <div className="graph-debug-panel__table-wrap"> |
| 80 | + <Table compact size="small" celled> |
| 81 | + <Table.Header> |
| 82 | + <Table.Row> |
| 83 | + <Table.HeaderCell>Node</Table.HeaderCell> |
| 84 | + <Table.HeaderCell>Root?</Table.HeaderCell> |
| 85 | + <Table.HeaderCell>In</Table.HeaderCell> |
| 86 | + <Table.HeaderCell>Out</Table.HeaderCell> |
| 87 | + <Table.HeaderCell>Link Types</Table.HeaderCell> |
| 88 | + </Table.Row> |
| 89 | + </Table.Header> |
| 90 | + <Table.Body> |
| 91 | + {stats.map((s) => ( |
| 92 | + <Table.Row key={s.id} positive={s.isRoot}> |
| 93 | + <Table.Cell> |
| 94 | + <span className="graph-debug-panel__node-name" title={s.displayName}> |
| 95 | + {s.id} |
| 96 | + </span> |
| 97 | + </Table.Cell> |
| 98 | + <Table.Cell textAlign="center"> |
| 99 | + {s.isRoot && ( |
| 100 | + <Label size="tiny" color="green"> |
| 101 | + Root |
| 102 | + </Label> |
| 103 | + )} |
| 104 | + </Table.Cell> |
| 105 | + <Table.Cell textAlign="center">{s.inDegree}</Table.Cell> |
| 106 | + <Table.Cell textAlign="center">{s.outDegree}</Table.Cell> |
| 107 | + <Table.Cell> |
| 108 | + <List horizontal size="mini"> |
| 109 | + {Object.entries(s.linkTypes).map(([ltype, count]) => ( |
| 110 | + <List.Item key={ltype}> |
| 111 | + <Label size="mini"> |
| 112 | + {ltype} |
| 113 | + <Label.Detail>{count}</Label.Detail> |
| 114 | + </Label> |
| 115 | + </List.Item> |
| 116 | + ))} |
| 117 | + </List> |
| 118 | + </Table.Cell> |
| 119 | + </Table.Row> |
| 120 | + ))} |
| 121 | + </Table.Body> |
| 122 | + </Table> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +}; |
0 commit comments