-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visualization tool to debug image sources
- Loading branch information
1 parent
3d770a2
commit 3fe6c39
Showing
7 changed files
with
388 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package visualize | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/netip" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type EventStore interface { | ||
Record(id string, peer netip.Addr, status int, mirror bool) | ||
Dot(filter []string) string | ||
} | ||
|
||
type edge struct { | ||
node string | ||
status int | ||
rootIsSource bool | ||
} | ||
|
||
type MemoryStore struct { | ||
mx sync.RWMutex | ||
nodes map[string]string | ||
edges map[string]edge | ||
} | ||
|
||
func NewMemoryStore() *MemoryStore { | ||
return &MemoryStore{ | ||
nodes: map[string]string{}, | ||
edges: map[string]edge{}, | ||
} | ||
} | ||
|
||
func (m *MemoryStore) Record(id string, peer netip.Addr, status int, mirror bool) { | ||
m.mx.Lock() | ||
defer m.mx.Unlock() | ||
|
||
m.nodes[peer.String()] = peer.String() | ||
m.edges[id] = edge{ | ||
node: peer.String(), | ||
status: status, | ||
rootIsSource: mirror, | ||
} | ||
} | ||
|
||
func (m *MemoryStore) Dot(filter []string) string { | ||
m.mx.RLock() | ||
defer m.mx.RUnlock() | ||
|
||
if len(filter) == 0 { | ||
return dagToDot(m.nodes, m.edges) | ||
} | ||
filteredNodes := map[string]string{} | ||
filteredEdges := map[string]edge{} | ||
for _, v := range filter { | ||
edge, ok := m.edges[v] | ||
if !ok { | ||
continue | ||
} | ||
filteredNodes[edge.node] = edge.node | ||
filteredEdges[v] = edge | ||
} | ||
return dagToDot(filteredNodes, filteredEdges) | ||
} | ||
|
||
func dagToDot(nodes map[string]string, edges map[string]edge) string { | ||
dotNodes := []string{} | ||
for _, v := range nodes { | ||
node := fmt.Sprintf(`%q[label="%s"];`, v, v) | ||
dotNodes = append(dotNodes, node) | ||
} | ||
dotEdges := []string{} | ||
for _, v := range edges { | ||
color := "" | ||
switch v.status { | ||
case 0: | ||
color = "yellow" | ||
case http.StatusOK: | ||
color = "green" | ||
default: | ||
color = "red" | ||
} | ||
|
||
src := "n0" | ||
dest := v.node | ||
if !v.rootIsSource { | ||
src = v.node | ||
dest = "n0" | ||
} | ||
|
||
edge := fmt.Sprintf("%q -> %q[color=%s]", src, dest, color) | ||
dotEdges = append(dotEdges, edge) | ||
} | ||
|
||
return fmt.Sprintf(` | ||
digraph { | ||
layout="circo"; | ||
root="n0"; | ||
n0[label="host"] | ||
%s | ||
%s | ||
} | ||
`, strings.Join(dotNodes, "\n\t"), strings.Join(dotEdges, "\n\t")) | ||
} |
Oops, something went wrong.