diff --git a/service.go b/service.go index afdd04b3..05964fa4 100644 --- a/service.go +++ b/service.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strings" "code.hackerspace.pl/q3k/topo/graph" "github.com/gobuffalo/packr" @@ -61,32 +62,31 @@ func (s *Service) Run() { http.HandleFunc("/debug/graphviz", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "graph G {\n") + fmt.Fprintf(w, " rankdir = LR\n") for _, machine := range s.gr.Machines { - fmt.Fprintf(w, " subgraph cluster%s {\n", machine.Name) - fmt.Fprintf(w, " label = %s\n", machine.Name) + portNames := []string{} for _, port := range machine.Ports { - a := machine.Name + "|" + port.Name - fmt.Fprintf(w, " %q [label = %q]\n", a, port.Name) + name := fmt.Sprintf("<%s> %s", port.Name, port.Name) + portNames = append(portNames, name) } - fmt.Fprintf(w, " }\n") + ports := strings.Join(portNames, "|") + fmt.Fprintf(w, " %s [shape=record label=\"{ %s | { %s }}\"]\n", machine.Name, machine.Name, ports) } for _, sw := range s.gr.Switches { - fmt.Fprintf(w, " subgraph cluster%s {\n", sw.Name) - fmt.Fprintf(w, " label = %s\n", sw.Name) + portNames := []string{} for _, port := range sw.Ports { - a := sw.Name + "|" + port.Name - fmt.Fprintf(w, " %q [label = %q]\n", a, port.Name) + name := fmt.Sprintf("<%s> %s", port.Name, port.Name) + portNames = append(portNames, name) } - fmt.Fprintf(w, " }\n") + ports := strings.Join(portNames, "|") + fmt.Fprintf(w, " %s [shape=record label=\"{{ %s } | %s}\"]\n", sw.Name, ports, sw.Name) } for _, machine := range s.gr.Machines { for _, port := range machine.Ports { if port.OtherEnd == nil { continue } - a := machine.Name + "|" + port.Name - b := port.OtherEnd.Switch.Name + "|" + port.OtherEnd.Name - fmt.Fprintf(w, " %q -- %q\n", a, b) + fmt.Fprintf(w, " %s:%q -- %s:%q\n", machine.Name, port.Name, port.OtherEnd.Switch.Name, port.OtherEnd.Name) } } fmt.Fprintf(w, "}\n")