-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotgen.go
203 lines (175 loc) · 4.95 KB
/
dotgen.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dotgen
import (
"fmt"
"io"
"log"
"reflect"
"strings"
"github.com/lovoo/goka"
)
// Topic represents a kafka topic (stream or table)
type Topic struct {
ID string
Name string
Codec string
IsTable bool
}
func (t *Topic) renderNode() string {
color := "lavender"
if t.IsTable {
color = "pink"
}
return fmt.Sprintf(`"%s" [shape="Mrecord" label="%s" style="filled" fillcolor="%s"];`, t.ID, t.Name, color)
}
// Processor represents a goka processor
type Processor struct {
ID string
Name string
Output []*Topic
Input []*Topic
LoopCodec string
Join []*Topic
Lookup []*Topic
Persist *Topic
}
func (p *Processor) renderNode() string {
return fmt.Sprintf(`"%s" [shape="box" label="%s" style="filled" fillcolor="lightcyan"];`, p.ID, p.Name)
}
func (p *Processor) renderEdges() []string {
var edges []string
for _, output := range p.Output {
edges = append(edges, fmt.Sprintf(`"%s" -> "%s" [fontsize=10];`, p.ID, output.ID))
}
for _, input := range p.Input {
edges = append(edges, fmt.Sprintf(`"%s" -> "%s" [fontsize=10];`, input.ID, p.ID))
}
for _, join := range p.Join {
edges = append(edges, fmt.Sprintf(`"%s" -> "%s" [label="join" fontsize=10];`, join.ID, p.ID))
}
for _, lookup := range p.Lookup {
edges = append(edges, fmt.Sprintf(`"%s" -> "%s" [label="lookup" fontsize=10];`, lookup.ID, p.ID))
}
if p.LoopCodec != "" {
edges = append(edges, fmt.Sprintf(`"%s" -> "%s" [label="loop" fontsize=10 constraint=false];`, p.ID, p.ID))
}
if p.Persist != nil {
edges = append(edges, fmt.Sprintf(`"%s" -> "%s" [label="persist" fontsize=10 constraint=false];`, p.ID, p.Persist.ID))
}
return edges
}
// Tree stores all processors and topics to be rendered together in one graph
type Tree struct {
Processors map[string]*Processor
Topics map[string]*Topic
out io.Writer
}
// NewTree creates a new tree for an output writer
func NewTree(out io.Writer) *Tree {
return &Tree{
out: out,
Processors: make(map[string]*Processor),
Topics: make(map[string]*Topic),
}
}
func (t *Tree) renderLine(line string) {
t.out.Write([]byte(line))
t.out.Write([]byte("\n"))
}
func (t *Tree) renderLines(lines []string) {
for _, line := range lines {
t.renderLine(line)
}
}
// Render renders the different parts of the dot graph
func (t *Tree) Render() {
t.renderLine("digraph main{")
t.renderLine("\tedge[arrowhead=vee]")
t.renderLine("\tgraph [rankdir=LR,compound=true,ranksep=1.0];")
var procIds []string
for id := range t.Processors {
procIds = append(procIds, id)
}
t.renderLine(fmt.Sprintf(`{rank=same; "%s";}`, strings.Join(procIds, `" "`)))
var (
streamTopics []string
tableTopics []string
)
for _, topic := range t.Topics {
if topic.IsTable {
tableTopics = append(tableTopics, topic.ID)
} else {
streamTopics = append(streamTopics, topic.ID)
}
}
if len(streamTopics) > 0 {
t.renderLine(fmt.Sprintf(`{rank=source; "%s";}`, strings.Join(streamTopics, `" "`)))
}
if len(tableTopics) > 0 {
t.renderLine(fmt.Sprintf(`{rank=sink; "%s";}`, strings.Join(tableTopics, `" "`)))
}
for _, proc := range t.Processors {
t.renderLine(proc.renderNode())
}
for _, topic := range t.Topics {
t.renderLine(topic.renderNode())
}
for _, proc := range t.Processors {
t.renderLines(proc.renderEdges())
}
t.renderLine("}")
}
func (t *Tree) TrackGroupGraph(gg *goka.GroupGraph) {
name := string(gg.Group())
id := makeID(name)
if _, exists := t.Processors[id]; exists {
log.Fatalf(`duplicate processor "%s"`, gg.Group())
}
proc := &Processor{
ID: id,
Name: makeName(name),
Output: t.getOrCreateTopicsFromEdges(gg.OutputStreams()),
Input: t.getOrCreateTopicsFromEdges(gg.InputStreams()),
Join: t.getOrCreateTopicsFromEdges(gg.JointTables()),
Lookup: t.getOrCreateTopicsFromEdges(gg.LookupTables()),
}
if gg.LoopStream() != nil {
proc.LoopCodec = codecName(gg.LoopStream().Codec())
}
if gg.GroupTable() != nil {
proc.Persist = t.getOrCreateTopic(gg.GroupTable().Topic(), codecName(gg.GroupTable().Codec()))
}
t.Processors[id] = proc
}
func (t *Tree) getOrCreateTopicsFromEdges(edges goka.Edges) []*Topic {
var topics []*Topic
for _, edge := range edges {
topics = append(topics, t.getOrCreateTopic(edge.Topic(), codecName(edge.Codec())))
}
return topics
}
func (t *Tree) getOrCreateTopic(name string, codec string) *Topic {
id := makeID(name)
if topic, exists := t.Topics[id]; exists {
if topic.Name != name || topic.Codec != codec {
log.Printf("Warning: name/codec conflict for topic %s (codec=%s/%s, name=%s/%s)", name, topic.Codec, codec, topic.Name, name)
}
return topic
}
topic := &Topic{
ID: id,
Name: makeName(name),
Codec: codec,
IsTable: strings.HasSuffix(name, "-table"),
}
t.Topics[id] = topic
return topic
}
func makeID(str string) string {
return strings.Replace(str, "-", "_", -1)
}
func makeName(str string) string {
return str
}
func codecName(codec goka.Codec) string {
return reflect.TypeOf(codec).Elem().Name()
}