-
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 068ce2a
Showing
7 changed files
with
254 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,63 @@ | ||
package visualize | ||
|
||
import ( | ||
"net/http" | ||
"net/netip" | ||
"sync" | ||
|
||
"github.com/emicklei/dot" | ||
) | ||
|
||
type EventKind string | ||
|
||
const ( | ||
EventKindSuccess EventKind = "success" | ||
EventKindPending EventKind = "pending" | ||
EventKindFail EventKind = "fail" | ||
) | ||
|
||
type EventStore interface { | ||
Record(id string, peer netip.Addr, status int, mirror bool) | ||
Dot() string | ||
} | ||
|
||
type MemoryStore struct { | ||
mx sync.RWMutex | ||
graph *dot.Graph | ||
} | ||
|
||
func NewMemoryStore() *MemoryStore { | ||
g := dot.NewGraph(dot.Directed) | ||
g.Attr("root", "host") | ||
g.Attr("layout", "circo") | ||
g.Node("host").Attr("shape", "box") | ||
return &MemoryStore{ | ||
graph: g, | ||
} | ||
} | ||
|
||
func (m *MemoryStore) Record(id string, peer netip.Addr, status int, mirror bool) { | ||
m.mx.Lock() | ||
defer m.mx.Unlock() | ||
|
||
host := m.graph.Node("host") | ||
color := "green" | ||
if status != http.StatusOK { | ||
color = "red" | ||
} | ||
n := m.graph.Node(peer.String()).Attr("shape", "circle") | ||
from := host | ||
to := n | ||
if !mirror { | ||
from = n | ||
to = host | ||
} | ||
m.graph.Edge(from, to, id).Attr("color", color) | ||
} | ||
|
||
func (m *MemoryStore) Dot() string { | ||
m.mx.RLock() | ||
defer m.mx.RUnlock() | ||
|
||
return m.graph.String() | ||
} |
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,96 @@ | ||
package visualize | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/spegel-org/spegel/internal/mux" | ||
) | ||
|
||
type Server struct { | ||
eventStore EventStore | ||
} | ||
|
||
func NewServer(eventStore EventStore) *Server { | ||
return &Server{ | ||
eventStore: eventStore, | ||
} | ||
} | ||
|
||
func (s *Server) Server(addr string) *http.Server { | ||
srv := &http.Server{ | ||
Addr: addr, | ||
Handler: mux.NewServeMux(s.handle), | ||
} | ||
return srv | ||
} | ||
|
||
func (s *Server) handle(rw mux.ResponseWriter, req *http.Request) { | ||
switch req.URL.Path { | ||
case "/": | ||
s.indexHandler(rw, req) | ||
case "/graph": | ||
s.getGraph(rw, req) | ||
default: | ||
rw.WriteHeader(http.StatusNotFound) | ||
} | ||
} | ||
|
||
func (s *Server) indexHandler(rw mux.ResponseWriter, _ *http.Request) { | ||
index := ` | ||
<html> | ||
<head> | ||
<title>Spegel</title> | ||
<style> | ||
.content { | ||
width: 1110px; | ||
margin: auto; | ||
} | ||
#graph { | ||
width: 1110px; | ||
} | ||
</style> | ||
<script src="https://unpkg.com/[email protected]"></script> | ||
<script src="https://unpkg.com/@viz-js/[email protected]/lib/viz-standalone.js"></script> | ||
</head> | ||
<body> | ||
<div class="content"> | ||
<h1>Spegel</h1> | ||
<div id="graph-container" hx-get="/graph" hx-trigger="load, every 5s" hx-swap="none" | ||
hx-on::after-request="updateGraph(event.detail.xhr.responseText)"> | ||
</div> | ||
<script> | ||
function updateGraph(dot) { | ||
Viz.instance().then(function (viz) { | ||
let container = document.getElementById("graph-container"); | ||
let newNode = viz.renderSVGElement(dot); | ||
newNode.id = "graph" | ||
let oldNode = document.getElementById("graph"); | ||
if (oldNode == null) { | ||
container.appendChild(newNode) | ||
return | ||
} | ||
container.replaceChild(newNode, oldNode) | ||
}); | ||
} | ||
</script> | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
` | ||
rw.Write([]byte(index)) | ||
} | ||
|
||
// NOTE: image could be discoverd by peeking at the manifest content? | ||
// NOTE: In the future we could show all nodes in the cluster? | ||
// NOTE: Should be able to both see incoming and outgoing requests | ||
// NOTE: Hash result to avoid generating graph over again | ||
func (s *Server) getGraph(rw mux.ResponseWriter, _ *http.Request) { | ||
rw.Write([]byte(s.eventStore.Dot())) | ||
} |
Oops, something went wrong.