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 if GenerateGraph::collection_uuid(uuid)
33 if Collection.is_empty_blob_locator? uuid.to_s
35 return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
38 href = url_for ({:controller => Collection.to_s.tableize,
42 return "\"#{uuid}\" [label=\"#{encode_quotes(describe_opts[:label] || @pdata[uuid][:name] || uuid)}\",shape=box,href=\"#{href}\",#{bgcolor}];\n"
44 "\"#{uuid}\" [label=\"#{encode_quotes(describe_opts[:label] || uuid)}\",#{bgcolor},shape=#{describe_opts[:shape] || 'box'}];\n"
49 d = Digest::MD5.hexdigest(job[:script_parameters].to_json)
50 if @opts[:combine_jobs] == :script_only
51 uuid = "#{job[:script]}_#{d}"
52 elsif @opts[:combine_jobs] == :script_and_version
53 uuid = "#{job[:script]}_#{job[:script_version]}_#{d}"
55 uuid = "#{job[:uuid]}"
58 @jobs[uuid] = [] unless @jobs[uuid]
59 @jobs[uuid] << job unless @jobs[uuid].include? job
64 def edge(tail, head, extra)
65 if @opts[:direction] == :bottom_up
66 gr = "\"#{head}\" -> \"#{tail}\""
68 gr = "\"#{tail}\" -> \"#{head}\""
74 gr += "#{k}=\"#{v}\","
82 def script_param_edges(uuid, prefix, sp)
89 k = prefix + "::" + k.to_s
91 gr += script_param_edges(uuid, k.to_s, v)
98 if GenerateGraph::collection_uuid(v)
99 gr += script_param_edges(uuid, "#{prefix}[#{i}]", v)
100 elsif @opts[:all_script_parameters]
102 nl = (if (count+t.length) > 60 then "\\n" else " " end)
103 count = 0 if (count+t.length) > 60
104 node += "',#{nl}'" unless node == ""
105 node = "['" if node == ""
113 node_value = encode_quotes node
114 gr += "\"#{node_value}\" [label=\"#{node_value}\"];\n"
115 gr += edge(uuid, node_value, {:label => prefix})
118 return '' if sp.empty?
119 m = GenerateGraph::collection_uuid(sp)
120 if m and (@pdata[m] or (not @opts[:pdata_only]))
121 gr += edge(m, uuid, {:label => prefix})
122 elsif @opts[:all_script_parameters]
123 sp_value = encode_quotes sp
124 gr += "\"#{sp_value}\" [label=\"\\\"#{sp_value}\\\"\",shape=box];\n"
125 gr += edge(sp_value, uuid, {:label => prefix})
131 def job_edges job, edge_opts={}
135 gr += script_param_edges(uuid, "", job[:script_parameters])
136 if job[:docker_image_locator]
137 gr += describe_node(job[:docker_image_locator], {label: (job[:runtime_constraints].andand[:docker_image] || job[:docker_image_locator])})
138 gr += edge(job[:docker_image_locator], uuid, {:label => "docker_image"})
141 if @opts[:script_version_nodes]
142 #gr += describe_node(job[:script_version])
143 gr += edge(job[:script_version], uuid, {:label => "script_version"})
146 gr += edge(uuid, job[:output], {label: "output" }) if job[:output] and !edge_opts[:no_output]
147 #gr += edge(uuid, job[:log], {label: "log"}) if job[:log] and !edge_opts[:no_log]
152 def generate_provenance_edges(uuid)
154 m = GenerateGraph::collection_uuid(uuid)
157 if uuid.nil? or uuid.empty? or @visited[uuid]
164 @visited[uuid] = true
167 if uuid.start_with? "component_"
168 # Pipeline component inputs
169 job = @pdata[@pdata[uuid][:job].andand[:uuid]]
171 gr += describe_node(job_uuid(job), {label: uuid[38..-1], pip: @opts[:pips].andand[job[:uuid]], shape: "oval"})
172 gr += job_edges job, {no_output: true, no_log: true}
174 # Pipeline component output
175 outuuid = @pdata[uuid][:output_uuid]
176 outcollection = @pdata[outuuid]
177 gr += edge(job_uuid(job), outcollection[:portable_data_hash], {label: "output"}) if outuuid
178 gr += describe_node(outcollection[:portable_data_hash], {label: outcollection[:name]})
180 rsc = ArvadosBase::resource_class_for_uuid uuid
184 gr += job_edges job if job
188 gr += describe_node(uuid)
192 @pdata.each do |k, link|
193 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
194 href = url_for ({:controller => Link.to_s.tableize,
196 :id => link[:uuid] })
198 gr += describe_node(link[:tail_uuid])
199 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
200 gr += generate_provenance_edges(link[:tail_uuid])
210 href = url_for ({:controller => Job.to_s.tableize,
213 gr += "\"#{k}\" [href=\"#{href}?"
217 gr += ";" unless gr.end_with? "?"
218 gr += "uuid%5b%5d=#{u[:uuid]}"
219 n |= @opts[:pips][u[:uuid]] if @opts[:pips] and @opts[:pips][u[:uuid]]
224 if @opts[:combine_jobs] == :script_only
225 gr += "#{v[0][:script]}"
226 elsif @opts[:combine_jobs] == :script_and_version
227 gr += "#{v[0][:script]}" # Just show the name but the nodes will be distinct
229 gr += "#{v[0][:script]}\\n#{v[0][:finished_at]}"
231 gr += "\",#{determine_fillcolor n}];\n"
236 def encode_quotes value
237 value.andand.gsub("\"", "\\\"")
241 def self.create_provenance_graph(pdata, svgId, opts={})
242 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
245 p2[k[:uuid]] = k if k[:uuid]
250 unless pdata.is_a? Hash
251 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
254 gr = """strict digraph {
255 node [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
256 edge [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
259 if opts[:direction] == :bottom_up
260 gr += "edge [dir=back];"
263 g = GenerateGraph.new(pdata, opts)
266 if !opts[:only_components] or k.start_with? "component_"
267 gr += g.generate_provenance_edges(k)
269 #gr += describe_node(k)
273 if !opts[:only_components]
274 gr += g.describe_jobs
282 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
290 svg = svg.sub(/<\?xml.*?\?>/m, "")
291 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
292 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
295 def self.find_collections(sp, &b)
298 sp.class.columns.each do |c|
299 find_collections(sp[c.name.to_sym], &b)
303 find_collections(v, &b)
307 find_collections(v, &b)
310 if m = /[a-f0-9]{32}\+\d+/.match(sp)
312 elsif m = /[0-9a-z]{5}-4zz18-[0-9a-z]{15}/.match(sp)