MCPcopy Create free account
hub / github.com/diillson/chatcli / sanitizeGraphData

Function sanitizeGraphData

cli/plugins/builtin_graphview.go:492–526  ·  view source on GitHub ↗

sanitizeGraphData dedupes nodes by ID and drops edges whose endpoints are missing, so the rendered graph is always well-formed.

(data GraphData)

Source from the content-addressed store, hash-verified

490// sanitizeGraphData dedupes nodes by ID and drops edges whose endpoints are
491// missing, so the rendered graph is always well-formed.
492func sanitizeGraphData(data GraphData) GraphData {
493 seen := make(map[string]bool, len(data.Nodes))
494 nodes := make([]GraphNode, 0, len(data.Nodes))
495 for _, n := range data.Nodes {
496 n.ID = strings.TrimSpace(n.ID)
497 if n.ID == "" || seen[n.ID] {
498 continue
499 }
500 if strings.TrimSpace(n.Label) == "" {
501 n.Label = n.ID
502 }
503 seen[n.ID] = true
504 nodes = append(nodes, n)
505 }
506 edges := make([]GraphEdge, 0, len(data.Edges))
507 edgeSeen := make(map[string]bool, len(data.Edges))
508 for _, e := range data.Edges {
509 if !seen[e.Source] || !seen[e.Target] || e.Source == e.Target {
510 continue
511 }
512 a, b := e.Source, e.Target
513 if a > b {
514 a, b = b, a
515 }
516 key := a + "\x00" + b
517 if edgeSeen[key] {
518 continue
519 }
520 edgeSeen[key] = true
521 edges = append(edges, e)
522 }
523 data.Nodes = nodes
524 data.Edges = edges
525 return data
526}
527
528// renderGraphViewHTML injects the data into the template and writes the file.
529func renderGraphViewHTML(data GraphData, output string) (string, error) {

Callers 2

resolveGraphDataFunction · 0.85
TestSanitizeGraphDataFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestSanitizeGraphDataFunction · 0.68