1 module ProvenanceHelper
4 def initialize(pdata, opts)
11 def self.collection_uuid(uuid)
12 m = CollectionsHelper.match(uuid)
19 # Collection.where(uuid: ['contains', m[1]]).each do |u|
20 # puts "fixup #{uuid} to #{u.uuid}"
30 p = { :host => @opts[:request].host,
31 :port => @opts[:request].port,
32 :protocol => @opts[:request].protocol }
34 Rails.application.routes.url_helpers.url_for (p)
37 def determine_fillcolor(n)
38 fillcolor = %w(aaaaaa aaffaa aaaaff aaaaaa ffaaaa)[n || 0] || 'aaaaaa'
39 "style=filled,fillcolor=\"##{fillcolor}\""
42 def describe_node(uuid)
44 bgcolor = determine_fillcolor @opts[:pips][uuid] if @opts[:pips]
46 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
48 href = url_for ({:controller => rsc.to_s.tableize,
52 #"\"#{uuid}\" [label=\"#{rsc}\\n#{uuid}\",href=\"#{href}\"];\n"
55 if uuid == :"d41d8cd98f00b204e9800998ecf8427e+0"
58 return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
60 puts "#{uuid.class} #{@pdata[uuid]}"
63 if @pdata[uuid][:name]
64 return "\"#{uuid}\" [label=\"#{@pdata[uuid][:name]}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
67 if @pdata[uuid].respond_to? :files
68 files = @pdata[uuid].files
69 elsif @pdata[uuid][:files]
70 files = @pdata[uuid][:files]
76 while i < 3 and i < files.length
77 label += "\\n" unless label == ""
82 label += "\\n⋮"
84 #puts "#{uuid} #{label} #{files}"
85 return "\"#{uuid}\" [label=\"#{label}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
90 return "\"#{uuid}\" [label=\"#{rsc}\",href=\"#{href}\",#{bgcolor}];\n"
92 "\"#{uuid}\" [#{bgcolor}];\n"
96 if @opts[:combine_jobs] == :script_only
97 uuid = "#{job[:script]}"
98 elsif @opts[:combine_jobs] == :script_and_version
99 uuid = "#{job[:script]}_#{job[:script_version]}"
101 uuid = "#{job[:uuid]}"
104 @jobs[uuid] = [] unless @jobs[uuid]
105 @jobs[uuid] << job unless @jobs[uuid].include? job
110 def edge(tail, head, extra)
111 if @opts[:direction] == :bottom_up
112 gr = "\"#{tail}\" -> \"#{head}\""
114 gr = "\"#{head}\" -> \"#{tail}\""
119 gr += "#{k}=\"#{v}\","
127 def script_param_edges(job, prefix, sp)
133 k = prefix + "::" + k.to_s
135 gr += script_param_edges(job, k.to_s, v)
141 if GenerateGraph::collection_uuid(v)
142 gr += script_param_edges(job, "#{prefix}[#{i}]", v)
143 elsif @opts[:all_script_parameters]
144 node += "', '" unless node == ""
145 node = "['" if node == ""
153 #id = "#{job[:uuid]}_#{prefix}"
154 gr += "\"#{node}\" [label=\"#{node}\"];\n"
155 gr += edge(job_uuid(job), node, {:label => prefix})
158 return '' if sp.empty?
159 m = GenerateGraph::collection_uuid(sp)
160 #puts "#{m} pdata is #{@pdata[m.intern]}"
161 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
162 gr += edge(job_uuid(job), m, {:label => prefix})
163 gr += generate_provenance_edges(m)
164 elsif @opts[:all_script_parameters]
165 #id = "#{job[:uuid]}_#{prefix}"
166 gr += "\"#{sp}\" [label=\"#{sp}\"];\n"
167 gr += edge(job_uuid(job), sp, {:label => prefix})
173 def generate_provenance_edges(uuid)
175 m = GenerateGraph::collection_uuid(uuid)
178 uuid = uuid.intern if uuid
180 if (not uuid) or uuid.empty? or @visited[uuid]
182 #puts "already @visited #{uuid}"
186 if not @pdata[uuid] then
187 return describe_node(uuid)
189 @visited[uuid] = true
192 #puts "visiting #{uuid}"
195 # uuid is a collection
196 gr += describe_node(uuid)
198 if m == :"d41d8cd98f00b204e9800998ecf8427e+0"
199 # empty collection, don't follow any further
203 @pdata.each do |k, job|
204 if job[:output] == uuid.to_s
205 gr += edge(uuid, job_uuid(job), {:label => "output"})
206 gr += generate_provenance_edges(job[:uuid])
208 if job[:log] == uuid.to_s
209 gr += edge(uuid, job_uuid(job), {:label => "log"})
210 gr += generate_provenance_edges(job[:uuid])
214 # uuid is something else
215 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
220 gr += script_param_edges(job, "", job[:script_parameters])
222 if @opts[:script_version_nodes]
223 gr += describe_node(job[:script_version])
224 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
230 gr += describe_node(uuid)
234 @pdata.each do |k, link|
235 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
236 href = url_for ({:controller => Link.to_s.tableize,
238 :id => link[:uuid] })
240 gr += describe_node(link[:tail_uuid])
241 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
242 gr += generate_provenance_edges(link[:tail_uuid])
246 #puts "finished #{uuid}"
254 href = url_for ({:controller => Job.to_s.tableize,
257 gr += "\"#{k}\" [href=\"#{href}?"
261 gr += "uuid%5b%5d=#{u[:uuid]}&"
262 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
267 if @opts[:combine_jobs] == :script_only
268 gr += "#{v[0][:script]}"
269 elsif @opts[:combine_jobs] == :script_and_version
270 gr += "#{v[0][:script]}" # Just show the name but the nodes will be distinct
272 gr += "#{v[0][:script]}\\n#{v[0][:finished_at]}"
274 gr += "\",#{determine_fillcolor n}];\n"
281 def self.create_provenance_graph(pdata, svgId, opts={})
282 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
285 p2[k[:uuid].intern] = k if k[:uuid]
290 unless pdata.is_a? Hash
291 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
294 gr = """strict digraph {
295 node [fontsize=10,shape=box];
299 if opts[:direction] == :bottom_up
300 gr += "edge [dir=back];"
303 #puts "@pdata is #{pdata}"
305 g = GenerateGraph.new(pdata, opts)
308 gr += g.generate_provenance_edges(k)
311 gr += g.describe_jobs
320 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
328 svg = svg.sub(/<\?xml.*?\?>/m, "")
329 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
330 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
333 def self.find_collections(sp)
338 c.concat(find_collections(v))
342 c.concat(find_collections(v))
346 m = GenerateGraph::collection_uuid(sp)