-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmq-schema-visualizer.rb
247 lines (206 loc) · 6.55 KB
/
rmq-schema-visualizer.rb
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require 'json'
require 'getoptlong'
require 'delegate'
require "graphviz"
# Encloses RabbitMQ Queue information
RmqQueue = Struct.new(:name, :vhost, :durable, :auto_delete, :arguments, :sort_order, keyword_init: true) do
include Comparable
attr_accessor :node
def initialize(*args, **kwargs)
super
self.sort_order ||= 0
end
def self.from_json(d)
new(**d)
end
def <=>(other)
[self.vhost, self.sort_order, self.class.name, self.name] <=> [other.vhost, other.sort_order, other.name, other.class.name]
end
def full_name
"#{vhost.delete_suffix('/')}/#{name}".delete_prefix('/')
end
def durable?
self.durable == true
end
def auto_delete?
self.auto_delete == true
end
def dlx?
self.arguments['x-dead-letter-exchange'] && !self.arguments['x-dead-letter-exchange'].empty?
end
end
# Encloses RabbitMQ Exchange information
RmqExchange = Struct.new(:name, :vhost, :type, :durable, :auto_delete, :internal, :arguments, :sort_order, keyword_init: true) do
include Comparable
attr_accessor :node
def initialize(*args, **kwargs)
super
self.sort_order ||= 0
end
def self.from_json(d)
new(**d)
end
def <=>(other)
[self.vhost, self.sort_order, self.class.name, self.name] <=> [other.vhost, other.sort_order, other.name, other.class.name]
end
def full_name
"#{vhost.delete_suffix('/')}/#{name}".delete_prefix('/')
end
def durable?
self.durable == true
end
def auto_delete?
self.auto_delete == true
end
def internal?
self.internal== true
end
end
# Encloses RabbitMQ Binding information
RmqBinding = Struct.new(:source, :vhost, :destination, :routing_key, :dsx, :arguments, keyword_init: true) do
def dsx?
self.dsx == true
end
end
# Decorates queue or exchange for visualization representation
class NodeDecorator < SimpleDelegator
def to_s
case __getobj__
when RmqQueue
name_components = ["<B>Q: #{full_name}</B>"]
if arguments
args = arguments.slice(*%w[x-max-priority x-queue-type x-expires x-message-ttl x-max-length])
name_components.concat(args.map{ "#{_1}: #{_2}" })
end
"< " + name_components.join("<BR/>") + ">"
when RmqExchange
"< <B>E: #{full_name}</B><BR/>#{type.upcase}>"
end
end
def add_to_graph(g)
case __getobj__
when RmqQueue
self.node = g.add_nodes(to_s, shape: 'box', style: "solid,filled", fillcolor: '#DAE8FC')
when RmqExchange
line_style = internal? ? 'dashed' : 'solid'
self.node = g.add_nodes(to_s, shape: 'box', style: "#{line_style},rounded,filled", fillcolor: '#F8CECC')
end
end
end
# Decorates binding for visualization representation
class EdgeDecorator < SimpleDelegator
def label
routing_key || ''
end
def add_to_graph(g)
if dsx?
g.add_edges(source.node, destination.node, label: label, color: 'red')
else
g.add_edges(source.node, destination.node, label: label)
end
end
end
#####
def read_arguments
options = GetoptLong.new(
['--format', '-f', GetoptLong::REQUIRED_ARGUMENT],
['--output', '-o', GetoptLong::REQUIRED_ARGUMENT],
['--order-first', GetoptLong::REQUIRED_ARGUMENT],
['--order-last', GetoptLong::REQUIRED_ARGUMENT]
)
@output_file = nil
@output_format = 'dot'
@order_first = {}
@order_last = {}
options.each do |option, argument|
case option
when '--format'
@output_format = argument.downcase
when '--output'
@output_file = argument
when '--order-first'
# give then ascending sort_order from -n to -1 (default items have 0 order)
@order_first = argument.split(',').reverse.each.with_index.map{ |n, i| [n, -i-1] }.to_h
when '--order-last'
# give them ascending sort_order fron 1 to n (default items have 0 order)
@order_last = argument.split(',').each.with_index.map{ |n, i| [n, i+1] }.to_h
end
end
unless ARGV[0]
puts <<~HELP
Usage:
--format f, -f f:
Output format like dot, pdf, png, svg, ps, etc. Default is DOT.
--output o, -o o:
Output file name. STDOUT, if not given.
--order-first
--order-last
Comma-separated list of queues/exchanges names that should be sorted first/last.
Depending on the schema complexity, the ordering effect may be limited.
{JSON_FILENAME}:
Schema export file. REQUIRED.
HELP
exit(1)
end
end
def create_graph(data)
qs = data['queues'].map{ RmqQueue.from_json(_1) }
qs.each do |q|
q.sort_order = @order_first[q.name] || @order_last[q.name] || 0
end
xs = data['exchanges'].map{ RmqExchange.from_json(_1) }
xs.each do |x|
x.sort_order = @order_first[x.name] || @order_last[x.name] || 0
end
binds = data['bindings'].map do |b|
src = xs.find { |x| x.vhost == b['vhost'] && x.name == b['source'] }
dst_container = if b['destination_type'] == 'queue'
qs
elsif b['destination_type'] == 'exchange'
xs
end
dst = dst_container&.find { |x| x.vhost == b['vhost'] && x.name == b['destination'] }
unless src
puts "#{b['source']} exchange not found"
next
end
unless dst
puts "#{b['destination']} #{b['destination_type']} not found"
next
end
RmqBinding.new source: src,
vhost: b['vhost'],
destination: dst,
routing_key: b['routing_key'],
dsx: false,
arguments: b['arguments']
end
# Add DLX bindings
qs.filter(&:dlx?).each do |q|
dst = xs.find { |x| x.vhost == q.vhost && x.name == q.arguments['x-dead-letter-exchange'] }
unless dst
puts "#{b.destination} exchange not found"
next
end
binds.push(RmqBinding.new(source: q,
vhost: q.vhost,
destination: dst,
routing_key: q.arguments['x-dead-letter-routing-key'] || '',
arguments: {},
dsx: true))
end
nodes = (xs + qs).sort
vhosts = (qs.map(&:vhost) + xs.map(&:vhost) + binds.map(&:vhost)).to_set
GraphViz::new(:G, type: :digraph) do |graph|
vhosts.each do |vhost|
g = graph.subgraph("cluster_#{vhost}", label: vhost)
nodes.filter{ _1.vhost == vhost }.each { |node| NodeDecorator.new(node).add_to_graph(g) }
binds.filter{ _1.vhost == vhost }.each { |bind| EdgeDecorator.new(bind).add_to_graph(g) }
end
end
end
##### main
read_arguments
data = JSON.load_file(ARGV[0])
g = create_graph(data)
puts g.save(@output_format => @output_file)