1 module ProvenanceHelper
4 def initialize(pdata, opts)
12 def self.collection_uuid(uuid)
13 m = CollectionsHelper.match(uuid)
26 p = { :host => @opts[:request].host,
27 :port => @opts[:request].port,
28 :protocol => @opts[:request].protocol }
30 Rails.application.routes.url_helpers.url_for (p)
33 def determine_fillcolor(n)
34 fillcolor = %w(aaaaaa aaffaa aaaaff aaaaaa ffaaaa)[n || 0] || 'aaaaaa'
35 "style=filled,fillcolor=\"##{fillcolor}\""
38 def describe_node(uuid)
40 bgcolor = determine_fillcolor @opts[:pips].andand[uuid]
42 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
44 href = url_for ({:controller => rsc.to_s.tableize,
48 #"\"#{uuid}\" [label=\"#{rsc}\\n#{uuid}\",href=\"#{href}\"];\n"
50 if Collection.is_empty_blob_locator? uuid.to_s
52 return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
55 if @pdata[uuid][:name]
56 return "\"#{uuid}\" [label=\"#{@pdata[uuid][:name]}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
59 if @pdata[uuid].respond_to? :files
60 files = @pdata[uuid].files
61 elsif @pdata[uuid][:files]
62 files = @pdata[uuid][:files]
68 while i < 3 and i < files.length
69 label += "\\n" unless label == ""
74 label += "\\n⋮"
76 extra_s = @node_extra[uuid].andand.map { |k,v|
79 return "\"#{uuid}\" [label=\"#{label}\",href=\"#{href}\",shape=oval,#{bgcolor},#{extra_s}];\n"
84 return "\"#{uuid}\" [label=\"#{rsc}\",href=\"#{href}\",#{bgcolor}];\n"
86 "\"#{uuid}\" [#{bgcolor}];\n"
90 d = Digest::MD5.hexdigest(job[:script_parameters].to_json)
91 if @opts[:combine_jobs] == :script_only
92 uuid = "#{job[:script]}_#{d}"
93 elsif @opts[:combine_jobs] == :script_and_version
94 uuid = "#{job[:script]}_#{job[:script_version]}_#{d}"
96 uuid = "#{job[:uuid]}"
99 @jobs[uuid] = [] unless @jobs[uuid]
100 @jobs[uuid] << job unless @jobs[uuid].include? job
105 def edge(tail, head, extra)
106 if @opts[:direction] == :bottom_up
107 gr = "\"#{tail}\" -> \"#{head}\""
109 gr = "\"#{head}\" -> \"#{tail}\""
114 gr += "#{k}=\"#{v}\","
122 def script_param_edges(job, prefix, sp)
128 k = prefix + "::" + k.to_s
130 gr += script_param_edges(job, k.to_s, v)
137 if GenerateGraph::collection_uuid(v)
138 gr += script_param_edges(job, "#{prefix}[#{i}]", v)
139 elsif @opts[:all_script_parameters]
141 nl = (if (count+t.length) > 60 then "\\n" else " " end)
142 count = 0 if (count+t.length) > 60
143 node += "',#{nl}'" unless node == ""
144 node = "['" if node == ""
152 gr += "\"#{node}\" [label=\"#{node}\"];\n"
153 gr += edge(job_uuid(job), node, {:label => prefix})
156 return '' if sp.empty?
157 m = GenerateGraph::collection_uuid(sp)
158 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
159 gr += edge(job_uuid(job), m, {:label => prefix})
160 gr += generate_provenance_edges(m)
161 elsif @opts[:all_script_parameters]
162 gr += "\"#{sp}\" [label=\"#{sp}\"];\n"
163 gr += edge(job_uuid(job), sp, {:label => prefix})
169 def generate_provenance_edges(uuid)
171 m = GenerateGraph::collection_uuid(uuid)
174 uuid = uuid.intern if uuid
176 if (not uuid) or uuid.empty? or @visited[uuid]
180 if not @pdata[uuid] then
181 return describe_node(uuid)
183 @visited[uuid] = true
187 # uuid is a collection
188 if not Collection.is_empty_blob_locator? uuid.to_s
189 @pdata.each do |k, job|
190 if job[:output] == uuid.to_s
191 extra = { label: 'output' }
192 if job[:output_is_persistent]
193 extra[:label] += ' (persistent)'
194 @node_extra[uuid] ||= {}
195 @node_extra[uuid][:penwidth] = 4
197 gr += edge(uuid, job_uuid(job), extra)
198 gr += generate_provenance_edges(job[:uuid])
200 if job[:log] == uuid.to_s
201 gr += edge(uuid, job_uuid(job), {:label => "log"})
202 gr += generate_provenance_edges(job[:uuid])
206 gr += describe_node(uuid)
208 # uuid is something else
209 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
214 gr += script_param_edges(job, "", job[:script_parameters])
216 if @opts[:script_version_nodes]
217 gr += describe_node(job[:script_version])
218 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
224 gr += describe_node(uuid)
228 @pdata.each do |k, link|
229 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
230 href = url_for ({:controller => Link.to_s.tableize,
232 :id => link[:uuid] })
234 gr += describe_node(link[:tail_uuid])
235 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
236 gr += generate_provenance_edges(link[:tail_uuid])
246 href = url_for ({:controller => Job.to_s.tableize,
249 gr += "\"#{k}\" [href=\"#{href}?"
253 gr += "uuid%5b%5d=#{u[:uuid]}&"
254 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
259 if @opts[:combine_jobs] == :script_only
260 gr += "#{v[0][:script]}"
261 elsif @opts[:combine_jobs] == :script_and_version
262 gr += "#{v[0][:script]}" # Just show the name but the nodes will be distinct
264 gr += "#{v[0][:script]}\\n#{v[0][:finished_at]}"
266 gr += "\",#{determine_fillcolor n}];\n"
273 def self.create_provenance_graph(pdata, svgId, opts={})
274 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
277 p2[k[:uuid].intern] = k if k[:uuid]
282 unless pdata.is_a? Hash
283 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
286 gr = """strict digraph {
287 node [fontsize=10,shape=box];
291 if opts[:direction] == :bottom_up
292 gr += "edge [dir=back];"
295 g = GenerateGraph.new(pdata, opts)
298 gr += g.generate_provenance_edges(k)
301 gr += g.describe_jobs
308 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
316 svg = svg.sub(/<\?xml.*?\?>/m, "")
317 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
318 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
321 def self.find_collections(sp)
326 c.concat(find_collections(v))
330 c.concat(find_collections(v))
334 m = GenerateGraph::collection_uuid(sp)