-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.go
70 lines (57 loc) · 1.63 KB
/
graph.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
package main
import (
"strings"
"github.com/emicklei/dot"
)
func RenderWorkspacesDetailed(workspaces map[string]*Workspace) *dot.Graph {
g := dot.NewGraph()
// draw workspaces
for name, workspace := range workspaces {
// draw workspace
workspace.graphElement = g.Subgraph(name, dot.ClusterOption{})
// draw outputs
if len(workspace.Outputs) > 0 {
outputs := workspace.graphElement.Subgraph("outputs", dot.ClusterOption{})
for i, output := range workspace.Outputs {
workspace.Outputs[i].graphElement = outputs.Node(output.Name)
}
}
// draw inputs
if len(workspace.Inputs) > 0 {
inputs := workspace.graphElement.Subgraph("inputs", dot.ClusterOption{})
for i, input := range workspace.Inputs {
workspace.Inputs[i].graphElement = inputs.Node(input.Name)
}
}
}
// draw relations/dependencies
for _, workspace := range workspaces {
for i, input := range workspace.Inputs {
if input.ReferesTo != nil {
g.Edge(input.graphElement, input.ReferesTo.graphElement).Attr("label", strings.Join(input.InFile, ", "))
} else {
workspace.Inputs[i].graphElement.Attr("color", "red")
}
}
}
return g
}
func RenderWorkspaces(workspaces map[string]*Workspace) *dot.Graph {
g := dot.NewGraph(dot.Directed)
nodes := map[string]dot.Node{}
// draw workspaces
for name := range workspaces {
nodes[name] = g.Node(name)
}
// draw relations/dependencies
for name, workspace := range workspaces {
for _, dep := range workspace.Dependencies {
for otherName, other := range workspaces {
if dep.equals(other.RemoteState) {
g.Edge(nodes[name], nodes[otherName])
}
}
}
}
return g
}