1 module ProvenanceHelper
4 def initialize(pdata, opts)
12 def self.collection_uuid(uuid)
13 Keep::Locator.parse(uuid).andand.strip_hints.andand.to_s
17 p = { :host => @opts[:request].host,
18 :port => @opts[:request].port,
19 :protocol => @opts[:request].protocol }
21 Rails.application.routes.url_helpers.url_for (p)
24 def determine_fillcolor(n)
25 fillcolor = %w(666666 669966 666699 666666 996666)[n || 0] || '666666'
26 "style=\"filled\",color=\"#ffffff\",fillcolor=\"##{fillcolor}\",fontcolor=\"#ffffff\""
29 def describe_node(uuid, describe_opts={})
30 bgcolor = determine_fillcolor (describe_opts[:pip] || @opts[:pips].andand[uuid])
32 rsc = ArvadosBase::resource_class_for_uuid uuid
34 if GenerateGraph::collection_uuid(uuid) || rsc == Collection
35 if Collection.is_empty_blob_locator? uuid.to_s
37 return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
40 if describe_opts[:col_uuid]
41 href = url_for ({:controller => Collection.to_s.tableize,
43 :id => describe_opts[:col_uuid].to_s })
45 href = url_for ({:controller => Collection.to_s.tableize,
50 return "\"#{uuid}\" [label=\"#{encode_quotes(describe_opts[:label] || (@pdata[uuid] and @pdata[uuid][:name]) || uuid)}\",shape=box,href=\"#{href}\",#{bgcolor}];\n"
53 if describe_opts[:href]
54 href = ",href=\"#{url_for ({:controller => describe_opts[:href][:controller],
56 :id => describe_opts[:href][:id] })}\""
58 return "\"#{uuid}\" [label=\"#{encode_quotes(describe_opts[:label] || uuid)}\",#{bgcolor},shape=#{describe_opts[:shape] || 'box'}#{href}];\n"
63 d = Digest::MD5.hexdigest(job[:script_parameters].to_json)
64 if @opts[:combine_jobs] == :script_only
65 uuid = "#{job[:script]}_#{d}"
66 elsif @opts[:combine_jobs] == :script_and_version
67 uuid = "#{job[:script]}_#{job[:script_version]}_#{d}"
69 uuid = "#{job[:uuid]}"
72 @jobs[uuid] = [] unless @jobs[uuid]
73 @jobs[uuid] << job unless @jobs[uuid].include? job
78 def edge(tail, head, extra)
79 if @opts[:direction] == :bottom_up
80 gr = "\"#{encode_quotes head}\" -> \"#{encode_quotes tail}\""
82 gr = "\"#{encode_quotes tail}\" -> \"#{encode_quotes head}\""
88 gr += "#{k}=\"#{encode_quotes v}\","
96 def script_param_edges(uuid, sp)
100 if @opts[:all_script_parameters]
101 if v.is_a? Array or v.is_a? Hash
102 encv = JSON.pretty_generate(v).gsub("\n", "\\l") + "\\l"
106 gr += "\"#{encode_quotes encv}\" [shape=box];\n"
107 gr += edge(encv, uuid, {:label => k})
113 def job_edges job, edge_opts={}
117 ProvenanceHelper::find_collections job[:script_parameters] do |collection_hash, collection_uuid, key|
119 gr += describe_node(collection_uuid)
120 gr += edge(collection_uuid, uuid, {:label => key})
122 gr += describe_node(collection_hash)
123 gr += edge(collection_hash, uuid, {:label => key})
127 if job[:docker_image_locator] and !@opts[:no_docker]
128 gr += describe_node(job[:docker_image_locator], {label: (job[:runtime_constraints].andand[:docker_image] || job[:docker_image_locator])})
129 gr += edge(job[:docker_image_locator], uuid, {label: "docker_image"})
132 if @opts[:script_version_nodes]
133 gr += describe_node(job[:script_version], {:label => "git:#{job[:script_version]}"})
134 gr += edge(job[:script_version], uuid, {:label => "script_version"})
137 if job[:output] and !edge_opts[:no_output]
138 gr += describe_node(job[:output])
139 gr += edge(uuid, job[:output], {label: "output" })
142 if job[:log] and !edge_opts[:no_log]
143 gr += describe_node(job[:log])
144 gr += edge(uuid, job[:log], {label: "log"})
150 def generate_provenance_edges(uuid)
152 m = GenerateGraph::collection_uuid(uuid)
155 if uuid.nil? or uuid.empty? or @visited[uuid]
162 @visited[uuid] = true
165 if uuid.start_with? "component_"
166 # Pipeline component inputs
167 job = @pdata[@pdata[uuid][:job].andand[:uuid]]
170 gr += describe_node(job_uuid(job), {label: uuid[38..-1], pip: @opts[:pips].andand[job[:uuid]], shape: "oval",
171 href: {controller: 'jobs', id: job[:uuid]}})
172 gr += job_edges job, {no_output: true, no_log: true}
175 # Pipeline component output
176 outuuid = @pdata[uuid][:output_uuid]
178 outcollection = @pdata[outuuid]
180 gr += edge(job_uuid(job), outcollection[:portable_data_hash], {label: "output"})
181 gr += describe_node(outcollection[:portable_data_hash], {label: outcollection[:name]})
183 elsif job and job[:output]
184 gr += describe_node(job[:output])
185 gr += edge(job_uuid(job), job[:output], {label: "output" })
188 rsc = ArvadosBase::resource_class_for_uuid uuid
192 gr += job_edges job if job
193 elsif rsc == ContainerRequest
196 gr += describe_node(cr[:uuid], {href: {controller: 'container_requests',
201 children = @opts[:cr_children_of].andand[cr[:uuid]]
203 children.each do |child|
204 gr += edge(child[:uuid], cr[:uuid], {label: 'child'})
207 # Output collection node
208 if cr[:output_uuid] and @opts[:output_collections][cr[:output_uuid]]
209 c = @opts[:output_collections][cr[:output_uuid]]
210 gr += describe_node(c[:portable_data_hash],
215 gr += edge(cr[:uuid],
216 c[:portable_data_hash],
219 # Input collection nodes
220 output_pdhs = @opts[:output_collections].values.collect{|c|
221 c[:portable_data_hash]}
222 ProvenanceHelper::cr_input_pdhs(cr).each do |pdh|
223 if not output_pdhs.include?(pdh)
224 # Search for collections on the same project first
225 cols = @opts[:input_collections][pdh].andand.select{|c|
226 c[:owner_uuid] == cr[:owner_uuid]}
227 if not cols or cols.empty?
228 # Search for any collection with this PDH
229 cols = @opts[:input_collections][pdh]
231 names = cols.collect{|x| x[:name]}.uniq
232 input_name = names.first
234 input_name += " + #{names.length - 1} more"
236 gr += describe_node(pdh, {label: input_name})
238 gr += edge(pdh, cr[:uuid], {label: 'input'})
244 @pdata.each do |k, link|
245 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
246 href = url_for ({:controller => Link.to_s.tableize,
248 :id => link[:uuid] })
250 gr += describe_node(link[:tail_uuid])
251 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
252 gr += generate_provenance_edges(link[:tail_uuid])
262 href = url_for ({:controller => Job.to_s.tableize,
265 gr += "\"#{k}\" [href=\"#{href}?"
269 gr += ";" unless gr.end_with? "?"
270 gr += "uuid%5b%5d=#{u[:uuid]}"
271 n |= @opts[:pips][u[:uuid]] if @opts[:pips] and @opts[:pips][u[:uuid]]
276 label = "#{v[0][:script]}"
278 if label == "run-command" and v[0][:script_parameters][:command].is_a? Array
279 label = v[0][:script_parameters][:command].join(' ')
282 if not @opts[:combine_jobs]
283 label += "\\n#{v[0][:finished_at]}"
286 gr += encode_quotes label
288 gr += "\",#{determine_fillcolor n}];\n"
293 def encode_quotes value
294 value.to_s.gsub("\"", "\\\"").gsub("\n", "\\n")
298 def self.create_provenance_graph(pdata, svgId, opts={})
299 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
302 p2[k[:uuid]] = k if k[:uuid]
307 unless pdata.is_a? Hash
308 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
311 gr = """strict digraph {
312 node [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
313 edge [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
316 if opts[:direction] == :bottom_up
317 gr += "edge [dir=back];"
321 pdata = pdata.stringify_keys
323 g = GenerateGraph.new(pdata, opts)
326 if !opts[:only_components] or k.start_with? "component_"
327 gr += g.generate_provenance_edges(k)
329 #gr += describe_node(k)
333 if !opts[:only_components]
334 gr += g.describe_jobs
338 Rails.logger.warn "#{e.inspect}"
339 Rails.logger.warn "#{e.backtrace.join("\n\t")}"
348 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
356 svg = svg.sub(/<\?xml.*?\?>/m, "")
357 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
358 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
362 # Position indicates whether it is a content hash or arvados uuid.
363 # One will hold a value, the other will always be nil.
364 def self.find_collections(sp, key=nil, &b)
367 sp.class.columns.each do |c|
368 find_collections(sp[c.name.to_sym], nil, &b)
372 find_collections(v, key || k, &b)
376 find_collections(v, key, &b)
379 if m = /[a-f0-9]{32}\+\d+/.match(sp)
381 elsif m = /[0-9a-z]{5}-4zz18-[0-9a-z]{15}/.match(sp)
387 def self.cr_input_pdhs cr
389 input_obj = cr[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content] || cr[:mounts]
391 find_collections input_obj do |col_hash, col_uuid, key|