Skip to content

Commit

Permalink
Render graphviz graph in page (prysmaticlabs#4748)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon authored and cryptomental committed Feb 24, 2020
1 parent f5fd471 commit f3db182
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion beacon-chain/blockchain/info.go
Expand Up @@ -45,6 +45,31 @@ func (s *Service) HeadsHandler(w http.ResponseWriter, _ *http.Request) {

}

const template = `<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/viz.js/2.1.2/viz.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/viz.js/2.1.2/full.render.js"></script>
<body>
<script type="application/javascript">
var graph = ` + "`%s`;" + `
var viz = new Viz();
viz.renderSVGElement(graph) // reading the graph.
.then(function(element) {
document.body.appendChild(element); // appends to document.
})
.catch(error => {
// Create a new Viz instance (@see Caveats page for more info)
viz = new Viz();
// Possibly display the error
console.error(error);
});
</script>
</head>
</body>
</html>`

// TreeHandler is a handler to serve /tree page in metrics.
func (s *Service) TreeHandler(w http.ResponseWriter, _ *http.Request) {
nodes := s.forkChoiceStore.Nodes()
Expand Down Expand Up @@ -75,7 +100,8 @@ func (s *Service) TreeHandler(w http.ResponseWriter, _ *http.Request) {
}

w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(graph.String())); err != nil {
w.Header().Set("Content-Type", "text/html")
if _, err := fmt.Fprintf(w, template, graph.String()); err != nil {
log.WithError(err).Error("Failed to render p2p info page")
}
}
Expand Down

0 comments on commit f3db182

Please sign in to comment.