1 module ProvenanceHelper
4 def initialize(pdata, opts)
12 def self.collection_uuid(uuid)
13 m = CollectionsHelper.match(uuid)
20 # Collection.where(uuid: ['contains', m[1]]).each do |u|
21 # puts "fixup #{uuid} to #{u.uuid}"
31 p = { :host => @opts[:request].host,
32 :port => @opts[:request].port,
33 :protocol => @opts[:request].protocol }
35 Rails.application.routes.url_helpers.url_for (p)
38 def determine_fillcolor(n)
39 fillcolor = %w(aaaaaa aaffaa aaaaff aaaaaa ffaaaa)[n || 0] || 'aaaaaa'
40 "style=filled,fillcolor=\"##{fillcolor}\""
43 def describe_node(uuid)
45 bgcolor = determine_fillcolor @opts[:pips].andand[uuid]
47 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
49 href = url_for ({:controller => rsc.to_s.tableize,
53 #"\"#{uuid}\" [label=\"#{rsc}\\n#{uuid}\",href=\"#{href}\"];\n"
56 if uuid == :"d41d8cd98f00b204e9800998ecf8427e+0"
59 return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
61 #puts "#{uuid.class} #{@pdata[uuid]}"
64 if @pdata[uuid][:name]
65 return "\"#{uuid}\" [label=\"#{@pdata[uuid][:name]}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
68 if @pdata[uuid].respond_to? :files
69 files = @pdata[uuid].files
70 elsif @pdata[uuid][:files]
71 files = @pdata[uuid][:files]
77 while i < 3 and i < files.length
78 label += "\\n" unless label == ""
83 label += "\\n⋮"
85 #puts "#{uuid} #{label} #{files}"
86 extra_s = @node_extra[uuid].andand.map { |k,v|
89 return "\"#{uuid}\" [label=\"#{label}\",href=\"#{href}\",shape=oval,#{bgcolor},#{extra_s}];\n"
94 return "\"#{uuid}\" [label=\"#{rsc}\",href=\"#{href}\",#{bgcolor}];\n"
96 "\"#{uuid}\" [#{bgcolor}];\n"
100 if @opts[:combine_jobs] == :script_only
101 uuid = "#{job[:script]}"
102 elsif @opts[:combine_jobs] == :script_and_version
103 uuid = "#{job[:script]}_#{job[:script_version]}"
105 uuid = "#{job[:uuid]}"
108 @jobs[uuid] = [] unless @jobs[uuid]
109 @jobs[uuid] << job unless @jobs[uuid].include? job
114 def edge(tail, head, extra)
115 if @opts[:direction] == :bottom_up
116 gr = "\"#{tail}\" -> \"#{head}\""
118 gr = "\"#{head}\" -> \"#{tail}\""
123 gr += "#{k}=\"#{v}\","
131 def script_param_edges(job, prefix, sp)
137 k = prefix + "::" + k.to_s
139 gr += script_param_edges(job, k.to_s, v)
145 if GenerateGraph::collection_uuid(v)
146 gr += script_param_edges(job, "#{prefix}[#{i}]", v)
147 elsif @opts[:all_script_parameters]
148 node += "', '" unless node == ""
149 node = "['" if node == ""
157 #id = "#{job[:uuid]}_#{prefix}"
158 gr += "\"#{node}\" [label=\"#{node}\"];\n"
159 gr += edge(job_uuid(job), node, {:label => prefix})
162 return '' if sp.empty?
163 m = GenerateGraph::collection_uuid(sp)
164 #puts "#{m} pdata is #{@pdata[m.intern]}"
165 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
166 gr += edge(job_uuid(job), m, {:label => prefix})
167 gr += generate_provenance_edges(m)
168 elsif @opts[:all_script_parameters]
169 #id = "#{job[:uuid]}_#{prefix}"
170 gr += "\"#{sp}\" [label=\"#{sp}\"];\n"
171 gr += edge(job_uuid(job), sp, {:label => prefix})
177 def generate_provenance_edges(uuid)
179 m = GenerateGraph::collection_uuid(uuid)
182 uuid = uuid.intern if uuid
184 if (not uuid) or uuid.empty? or @visited[uuid]
186 #puts "already @visited #{uuid}"
190 if not @pdata[uuid] then
191 return describe_node(uuid)
193 @visited[uuid] = true
196 #puts "visiting #{uuid.inspect}"
199 # uuid is a collection
200 if uuid != :"d41d8cd98f00b204e9800998ecf8427e+0"
201 # not the empty collection
203 @pdata.each do |k, job|
204 if job[:output] == uuid.to_s
205 extra = { label: 'output' }
206 if job[:output_is_persistent]
207 extra[:label] += ' (persistent)'
208 @node_extra[uuid] ||= {}
209 @node_extra[uuid][:penwidth] = 4
211 gr += edge(uuid, job_uuid(job), extra)
212 gr += generate_provenance_edges(job[:uuid])
214 if job[:log] == uuid.to_s
215 gr += edge(uuid, job_uuid(job), {:label => "log"})
216 gr += generate_provenance_edges(job[:uuid])
220 gr += describe_node(uuid)
222 # uuid is something else
223 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
228 gr += script_param_edges(job, "", job[:script_parameters])
230 if @opts[:script_version_nodes]
231 gr += describe_node(job[:script_version])
232 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
238 gr += describe_node(uuid)
242 @pdata.each do |k, link|
243 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
244 href = url_for ({:controller => Link.to_s.tableize,
246 :id => link[:uuid] })
248 gr += describe_node(link[:tail_uuid])
249 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
250 gr += generate_provenance_edges(link[:tail_uuid])
254 #puts "finished #{uuid}"
262 href = url_for ({:controller => Job.to_s.tableize,
265 gr += "\"#{k}\" [href=\"#{href}?"
269 gr += "uuid%5b%5d=#{u[:uuid]}&"
270 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
275 if @opts[:combine_jobs] == :script_only
276 gr += "#{v[0][:script]}"
277 elsif @opts[:combine_jobs] == :script_and_version
278 gr += "#{v[0][:script]}" # Just show the name but the nodes will be distinct
280 gr += "#{v[0][:script]}\\n#{v[0][:finished_at]}"
282 gr += "\",#{determine_fillcolor n}];\n"
289 def self.create_provenance_graph(pdata, svgId, opts={})
290 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
293 p2[k[:uuid].intern] = k if k[:uuid]
298 unless pdata.is_a? Hash
299 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
302 gr = """strict digraph {
303 node [fontsize=10,shape=box];
307 if opts[:direction] == :bottom_up
308 gr += "edge [dir=back];"
311 #puts "@pdata is #{pdata}"
313 g = GenerateGraph.new(pdata, opts)
316 gr += g.generate_provenance_edges(k)
319 gr += g.describe_jobs
328 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
336 svg = svg.sub(/<\?xml.*?\?>/m, "")
337 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
338 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
341 def self.find_collections(sp)
346 c.concat(find_collections(v))
350 c.concat(find_collections(v))
354 m = GenerateGraph::collection_uuid(sp)