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 if @opts[:combine_jobs] == :script_only
91 uuid = "#{job[:script]}"
92 elsif @opts[:combine_jobs] == :script_and_version
93 uuid = "#{job[:script]}_#{job[:script_version]}"
95 uuid = "#{job[:uuid]}"
98 @jobs[uuid] = [] unless @jobs[uuid]
99 @jobs[uuid] << job unless @jobs[uuid].include? job
104 def edge(tail, head, extra)
105 if @opts[:direction] == :bottom_up
106 gr = "\"#{tail}\" -> \"#{head}\""
108 gr = "\"#{head}\" -> \"#{tail}\""
113 gr += "#{k}=\"#{v}\","
121 def script_param_edges(job, prefix, sp)
127 k = prefix + "::" + k.to_s
129 gr += script_param_edges(job, k.to_s, v)
135 if GenerateGraph::collection_uuid(v)
136 gr += script_param_edges(job, "#{prefix}[#{i}]", v)
137 elsif @opts[:all_script_parameters]
138 node += "', '" unless node == ""
139 node = "['" if node == ""
146 gr += "\"#{node}\" [label=\"#{node}\"];\n"
147 gr += edge(job_uuid(job), node, {:label => prefix})
150 return '' if sp.empty?
151 m = GenerateGraph::collection_uuid(sp)
152 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
153 gr += edge(job_uuid(job), m, {:label => prefix})
154 gr += generate_provenance_edges(m)
155 elsif @opts[:all_script_parameters]
156 gr += "\"#{sp}\" [label=\"#{sp}\"];\n"
157 gr += edge(job_uuid(job), sp, {:label => prefix})
163 def generate_provenance_edges(uuid)
165 m = GenerateGraph::collection_uuid(uuid)
168 uuid = uuid.intern if uuid
170 if (not uuid) or uuid.empty? or @visited[uuid]
174 if not @pdata[uuid] then
175 return describe_node(uuid)
177 @visited[uuid] = true
181 # uuid is a collection
182 if not Collection.is_empty_blob_locator? uuid.to_s
183 @pdata.each do |k, job|
184 if job[:output] == uuid.to_s
185 extra = { label: 'output' }
186 if job[:output_is_persistent]
187 extra[:label] += ' (persistent)'
188 @node_extra[uuid] ||= {}
189 @node_extra[uuid][:penwidth] = 4
191 gr += edge(uuid, job_uuid(job), extra)
192 gr += generate_provenance_edges(job[:uuid])
194 if job[:log] == uuid.to_s
195 gr += edge(uuid, job_uuid(job), {:label => "log"})
196 gr += generate_provenance_edges(job[:uuid])
200 gr += describe_node(uuid)
202 # uuid is something else
203 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
208 gr += script_param_edges(job, "", job[:script_parameters])
210 if @opts[:script_version_nodes]
211 gr += describe_node(job[:script_version])
212 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
218 gr += describe_node(uuid)
222 @pdata.each do |k, link|
223 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
224 href = url_for ({:controller => Link.to_s.tableize,
226 :id => link[:uuid] })
228 gr += describe_node(link[:tail_uuid])
229 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
230 gr += generate_provenance_edges(link[:tail_uuid])
240 href = url_for ({:controller => Job.to_s.tableize,
243 gr += "\"#{k}\" [href=\"#{href}?"
247 gr += "uuid%5b%5d=#{u[:uuid]}&"
248 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
253 if @opts[:combine_jobs] == :script_only
254 gr += "#{v[0][:script]}"
255 elsif @opts[:combine_jobs] == :script_and_version
256 gr += "#{v[0][:script]}" # Just show the name but the nodes will be distinct
258 gr += "#{v[0][:script]}\\n#{v[0][:finished_at]}"
260 gr += "\",#{determine_fillcolor n}];\n"
267 def self.create_provenance_graph(pdata, svgId, opts={})
268 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
271 p2[k[:uuid].intern] = k if k[:uuid]
276 unless pdata.is_a? Hash
277 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
280 gr = """strict digraph {
281 node [fontsize=10,shape=box];
285 if opts[:direction] == :bottom_up
286 gr += "edge [dir=back];"
289 g = GenerateGraph.new(pdata, opts)
292 gr += g.generate_provenance_edges(k)
295 gr += g.describe_jobs
302 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
310 svg = svg.sub(/<\?xml.*?\?>/m, "")
311 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
312 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
315 def self.find_collections(sp)
320 c.concat(find_collections(v))
324 c.concat(find_collections(v))
328 m = GenerateGraph::collection_uuid(sp)