-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
111 lines (105 loc) · 2.17 KB
/
draw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package glow
import (
"bytes"
"fmt"
"text/template"
)
const tmpl = `strict digraph {
labelloc="t"
label="{{ netProp "label" }}"
node [shape=ellipse]
{{ range .Nodes -}}
"{{ .Key }}"
[
label="{{ nodeProp "label" . }}",
style="{{ nodeProp "style" . }}",
fillcolor="{{ nodeProp "color" . }}"
];
{{ end -}}
{{ range .Links -}}
"{{ fromNode . }}" -> "{{ toNode . }}"
[
label="{{ linkProp "label" . }}",
color="{{ linkProp "color" . }}"
arrowhead="{{ linkProp "arrowhead" . }}"
];
{{ end }}
}`
// DOT describes the Network.
func DOT(n *Network) ([]byte, error) {
t := template.New("tmpl")
t.Funcs(template.FuncMap{
"fromNode": func(link *Link) string {
return link.x.Key()
},
"toNode": func(link *Link) string {
return link.y.Key()
},
"netProp": func(prop string) any {
switch prop {
case "label":
return fmt.Sprintf("Network Uptime: %s\n", n.Uptime())
default:
return ""
}
},
"nodeProp": func(prop string, node *Node) any {
switch prop {
case "label":
return fmt.Sprintf("%s\n(%s)", node.key, node.Uptime())
case "color":
switch {
case len(n.Egress(node.Key())) > 0 && node.distributor:
// node with egress and distributor mode set
return "lightyellow"
default:
return ""
}
case "style":
switch {
case len(n.Egress(node.Key())) > 0 && node.distributor:
// node with egress and distributor mode set
return "filled"
default:
return ""
}
default:
return ""
}
},
"linkProp": func(prop string, link *Link) any {
switch prop {
case "label":
return fmt.Sprintf("%d\n (%s)", link.Tally(), link.Uptime())
case "color":
switch {
case link.paused:
return "gray"
case link.removed:
return "red"
default:
return "lightblue"
}
case "arrowhead":
switch {
case link.paused || link.removed:
return "none"
default:
return "normal"
}
//case "penwidth":
default:
return ""
}
},
})
_, err := t.Parse(tmpl)
if err != nil {
return nil, err
}
var tpl bytes.Buffer
if err = t.Execute(&tpl, n); err != nil {
return nil, err
}
return tpl.Bytes(), nil
}