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 href = url_for ({:controller => Collection.to_s.tableize,
44 return "\"#{uuid}\" [label=\"#{encode_quotes(describe_opts[:label] || (@pdata[uuid] and @pdata[uuid][:name]) || uuid)}\",shape=box,href=\"#{href}\",#{bgcolor}];\n"
47 if describe_opts[:href]
48 href = ",href=\"#{url_for ({:controller => describe_opts[:href][:controller],
50 :id => describe_opts[:href][:id] })}\""
52 return "\"#{uuid}\" [label=\"#{encode_quotes(describe_opts[:label] || uuid)}\",#{bgcolor},shape=#{describe_opts[:shape] || 'box'}#{href}];\n"
57 d = Digest::MD5.hexdigest(job[:script_parameters].to_json)
58 if @opts[:combine_jobs] == :script_only
59 uuid = "#{job[:script]}_#{d}"
60 elsif @opts[:combine_jobs] == :script_and_version
61 uuid = "#{job[:script]}_#{job[:script_version]}_#{d}"
63 uuid = "#{job[:uuid]}"
66 @jobs[uuid] = [] unless @jobs[uuid]
67 @jobs[uuid] << job unless @jobs[uuid].include? job
72 def edge(tail, head, extra)
73 if @opts[:direction] == :bottom_up
74 gr = "\"#{encode_quotes head}\" -> \"#{encode_quotes tail}\""
76 gr = "\"#{encode_quotes tail}\" -> \"#{encode_quotes head}\""
82 gr += "#{k}=\"#{encode_quotes v}\","
90 def script_param_edges(uuid, sp)
94 if @opts[:all_script_parameters]
95 if v.is_a? Array or v.is_a? Hash
96 encv = JSON.pretty_generate(v).gsub("\n", "\\l") + "\\l"
100 gr += "\"#{encode_quotes encv}\" [shape=box];\n"
101 gr += edge(encv, uuid, {:label => k})
111 # Search for input mounts
112 input_obj = cr[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content] || cr[:mounts] || {}
114 ProvenanceHelper::find_collections input_obj, 'input' do |col_hash, col_uuid, key|
115 # Only include input PDHs
117 gr += describe_node(col_hash)
118 gr += edge(col_hash, uuid, {:label => key})
123 # Add CR outputs by PDH so they connect with the child CR's inputs.
125 output_pdh = Collection.find(cr[:output_uuid])[:portable_data_hash]
127 gr += describe_node(output_pdh)
128 gr += edge(uuid, output_pdh, {label: 'output'})
135 def job_edges job, edge_opts={}
139 ProvenanceHelper::find_collections job[:script_parameters] do |collection_hash, collection_uuid, key|
141 gr += describe_node(collection_uuid)
142 gr += edge(collection_uuid, uuid, {:label => key})
144 gr += describe_node(collection_hash)
145 gr += edge(collection_hash, uuid, {:label => key})
149 if job[:docker_image_locator] and !@opts[:no_docker]
150 gr += describe_node(job[:docker_image_locator], {label: (job[:runtime_constraints].andand[:docker_image] || job[:docker_image_locator])})
151 gr += edge(job[:docker_image_locator], uuid, {label: "docker_image"})
154 if @opts[:script_version_nodes]
155 gr += describe_node(job[:script_version], {:label => "git:#{job[:script_version]}"})
156 gr += edge(job[:script_version], uuid, {:label => "script_version"})
159 if job[:output] and !edge_opts[:no_output]
160 gr += describe_node(job[:output])
161 gr += edge(uuid, job[:output], {label: "output" })
164 if job[:log] and !edge_opts[:no_log]
165 gr += describe_node(job[:log])
166 gr += edge(uuid, job[:log], {label: "log"})
172 def generate_provenance_edges(uuid)
174 m = GenerateGraph::collection_uuid(uuid)
177 if uuid.nil? or uuid.empty? or @visited[uuid]
184 @visited[uuid] = true
187 if uuid.start_with? "component_"
188 # Pipeline component inputs
189 job = @pdata[@pdata[uuid][:job].andand[:uuid]]
192 gr += describe_node(job_uuid(job), {label: uuid[38..-1], pip: @opts[:pips].andand[job[:uuid]], shape: "oval",
193 href: {controller: 'jobs', id: job[:uuid]}})
194 gr += job_edges job, {no_output: true, no_log: true}
197 # Pipeline component output
198 outuuid = @pdata[uuid][:output_uuid]
200 outcollection = @pdata[outuuid]
202 gr += edge(job_uuid(job), outcollection[:portable_data_hash], {label: "output"})
203 gr += describe_node(outcollection[:portable_data_hash], {label: outcollection[:name]})
205 elsif job and job[:output]
206 gr += describe_node(job[:output])
207 gr += edge(job_uuid(job), job[:output], {label: "output" })
210 rsc = ArvadosBase::resource_class_for_uuid uuid
214 gr += job_edges job if job
215 elsif rsc == ContainerRequest
219 gr += describe_node(uuid, {href: {controller: 'container_requests',
221 label: @pdata[uuid][:name],
223 # Search for child CRs
224 if cr[:container_uuid]
225 child_crs = ContainerRequest.where(requesting_container_uuid: cr[:container_uuid])
226 child_crs.each do |child|
227 gr += generate_provenance_edges(child[:uuid])
228 gr += edge(child[:uuid], uuid, {label: 'child'})
235 @pdata.each do |k, link|
236 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
237 href = url_for ({:controller => Link.to_s.tableize,
239 :id => link[:uuid] })
241 gr += describe_node(link[:tail_uuid])
242 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
243 gr += generate_provenance_edges(link[:tail_uuid])
253 href = url_for ({:controller => Job.to_s.tableize,
256 gr += "\"#{k}\" [href=\"#{href}?"
260 gr += ";" unless gr.end_with? "?"
261 gr += "uuid%5b%5d=#{u[:uuid]}"
262 n |= @opts[:pips][u[:uuid]] if @opts[:pips] and @opts[:pips][u[:uuid]]
267 label = "#{v[0][:script]}"
269 if label == "run-command" and v[0][:script_parameters][:command].is_a? Array
270 label = v[0][:script_parameters][:command].join(' ')
273 if not @opts[:combine_jobs]
274 label += "\\n#{v[0][:finished_at]}"
277 gr += encode_quotes label
279 gr += "\",#{determine_fillcolor n}];\n"
284 def encode_quotes value
285 value.to_s.gsub("\"", "\\\"").gsub("\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]] = 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,fontname=\"Helvetica,Arial,sans-serif\"];
304 edge [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
307 if opts[:direction] == :bottom_up
308 gr += "edge [dir=back];"
312 pdata = pdata.stringify_keys
314 g = GenerateGraph.new(pdata, opts)
317 if !opts[:only_components] or k.start_with? "component_"
318 gr += g.generate_provenance_edges(k)
320 #gr += describe_node(k)
324 if !opts[:only_components]
325 gr += g.describe_jobs
329 Rails.logger.warn "#{e.inspect}"
330 Rails.logger.warn "#{e.backtrace.join("\n\t")}"
339 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
347 svg = svg.sub(/<\?xml.*?\?>/m, "")
348 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
349 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
353 # Position indicates whether it is a content hash or arvados uuid.
354 # One will hold a value, the other will always be nil.
355 def self.find_collections(sp, key=nil, &b)
358 sp.class.columns.each do |c|
359 find_collections(sp[c.name.to_sym], nil, &b)
363 find_collections(v, key || k, &b)
367 find_collections(v, key, &b)
370 if m = /[a-f0-9]{32}\+\d+/.match(sp)
372 elsif m = /[0-9a-z]{5}-4zz18-[0-9a-z]{15}/.match(sp)