-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
dot_helper.rb
259 lines (222 loc) · 6.74 KB
/
dot_helper.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
248
249
250
251
252
253
254
255
256
257
258
259
module DotHelper
def render_agents_diagram(agents, layout: nil)
if svg = dot_to_svg(agents_dot(agents, rich: true, layout: layout))
decorate_svg(svg, agents).html_safe
else
# Google chart request url
faraday = Faraday.new { |builder|
builder.request :url_encoded
builder.adapter Faraday.default_adapter
}
response = faraday.post('https://chart.googleapis.com/chart', { cht: 'gv', chl: agents_dot(agents) })
case response.status
when 200
# Display Base64-Encoded images
tag('img', src: 'data:image/jpg;base64,' + Base64.encode64(response.body))
when 400
"The diagram can't be displayed because it has too many nodes. Max allowed is 80."
when 413
"The diagram can't be displayed because it is too large."
else
"Unknow error. Response code is #{response.status}."
end
end
end
private def dot_to_svg(dot)
command = ENV['USE_GRAPHVIZ_DOT'] or return nil
IO.popen(%W[#{command} -Tsvg -q1 -o/dev/stdout /dev/stdin], 'w+') do |rw|
rw.print dot
rw.close_write
rw.read
rescue StandardError
end
end
class DotDrawer
def initialize(vars = {})
@dot = ''
vars.each do |key, value|
define_singleton_method(key) { value }
end
end
def to_s
@dot
end
def self.draw(*args, &block)
drawer = new(*args)
drawer.instance_exec(&block)
drawer.to_s
end
def raw(string)
@dot << string
end
ENDL = ';'.freeze
def endl
@dot << ENDL
end
def escape(string)
# Backslash escaping seems to work for the backslash itself,
# though it's not documented in the DOT language docs.
string.gsub(/[\\"\n]/,
"\\" => "\\\\",
"\"" => "\\\"",
"\n" => "\\n")
end
def id(value)
case string = value.to_s
when /\A(?!\d)\w+\z/, /\A(?:\.\d+|\d+(?:\.\d*)?)\z/
raw string
else
raw '"'
raw escape(string)
raw '"'
end
end
def ids(values)
values.each_with_index { |id, i|
raw ' ' if i > 0
id id
}
end
def attr_list(attrs = nil)
return if attrs.nil?
attrs = attrs.select { |_key, value| value.present? }
return if attrs.empty?
raw '['
attrs.each_with_index { |(key, value), i|
raw ',' if i > 0
id key
raw '='
id value
}
raw ']'
end
def node(id, attrs = nil)
id id
attr_list attrs
endl
end
def edge(from, to, attrs = nil, op = '->')
id from
raw op
id to
attr_list attrs
endl
end
def statement(ids, attrs = nil)
ids Array(ids)
attr_list attrs
endl
end
def block(*ids, &block)
ids ids
raw '{'
block.call
raw '}'
end
end
private
def draw(vars = {}, &block)
DotDrawer.draw(vars, &block)
end
def agents_dot(agents, rich: false, layout: nil)
draw(agents: agents,
agent_id: ->(agent) { 'a%d' % agent.id },
agent_label: ->(agent) {
agent.name.gsub(/(.{20}\S*)\s+/) {
# Fold after every 20+ characters
$1 + "\n"
}
},
agent_url: ->(agent) { agent_path(agent.id) },
rich: rich) {
@disabled = '#999999'
def agent_node(agent)
node(agent_id[agent],
label: agent_label[agent],
tooltip: (agent.short_type.titleize if rich),
URL: (agent_url[agent] if rich),
style: ('rounded,dashed' if agent.unavailable?),
color: (@disabled if agent.unavailable?),
fontcolor: (@disabled if agent.unavailable?))
end
def agent_edge(agent, receiver)
edge(agent_id[agent],
agent_id[receiver],
style: ('dashed' unless receiver.propagate_immediately?),
label: (" #{agent.control_action.pluralize} " if agent.can_control_other_agents?),
arrowhead: ('empty' if agent.can_control_other_agents?),
color: (@disabled if agent.unavailable? || receiver.unavailable?))
end
block('digraph', 'Agent Event Flow') {
layout ||= ENV['DIAGRAM_DEFAULT_LAYOUT'].presence
if rich && /\A[a-z]+\z/ === layout
statement 'graph', layout: layout, overlap: 'false'
end
statement 'node',
shape: 'box',
style: 'rounded',
target: '_blank',
fontsize: 10,
fontname: ('Helvetica' if rich)
statement 'edge',
fontsize: 10,
fontname: ('Helvetica' if rich)
agents.each.with_index { |agent, _index|
agent_node(agent)
[
*agent.receivers,
*(agent.control_targets if agent.can_control_other_agents?)
].each { |receiver|
agent_edge(agent, receiver) if agents.include?(receiver)
}
}
}
}
end
def decorate_svg(xml, agents)
svg = Nokogiri::XML(xml).at('svg')
Nokogiri::HTML::Document.new.tap { |doc|
doc << root = Nokogiri::XML::Node.new('div', doc) { |div|
div['class'] = 'agent-diagram'
}
svg['class'] = 'diagram'
root << svg
root << overlay_container = Nokogiri::XML::Node.new('div', doc) { |div|
div['class'] = 'overlay-container'
}
overlay_container << overlay = Nokogiri::XML::Node.new('div', doc) { |div|
div['class'] = 'overlay'
}
svg.xpath('//xmlns:g[@class="node"]', svg.namespaces).each { |node|
agent_id = (node.xpath('./xmlns:title/text()', svg.namespaces).to_s[/\d+/] or next).to_i
agent = agents.find { |a| a.id == agent_id }
count = agent.events_count
next unless count && count > 0
overlay << Nokogiri::XML::Node.new('a', doc) { |badge|
badge['id'] = id = 'b%d' % agent_id
badge['class'] = 'badge'
badge['href'] = agent_events_path(agent)
badge['target'] = '_blank'
badge['title'] = "#{count} events created"
badge.content = count.to_s
node['data-badge-id'] = id
badge << Nokogiri::XML::Node.new('span', doc) { |label|
# a dummy label only to obtain the background color
label['class'] = [
'label',
if agent.unavailable?
'label-warning'
elsif agent.working?
'label-success'
else
'label-danger'
end
].join(' ')
label['style'] = 'display: none'
}
}
}
# See also: app/assets/diagram.js.coffee
}.at('div.agent-diagram').to_s
end
end