1 module ProvenanceHelper
4 def initialize(pdata, opts)
11 def self.collection_uuid(uuid)
12 m = /^([a-f0-9]{32}(\+[0-9]+)?)(\+.*)?$/.match(uuid.to_s)
17 # Collection.where(uuid: ['contains', m[1]]).each do |u|
18 # puts "fixup #{uuid} to #{u.uuid}"
27 def determine_fillcolor(n)
28 fillcolor = %w(aaaaaa aaffaa aaaaff aaaaaa ffaaaa)[n || 0] || 'aaaaaa'
29 "style=filled,fillcolor=\"##{fillcolor}\""
32 def describe_node(uuid)
33 bgcolor = determine_fillcolor @opts[:pips][uuid] if @opts[:pips]
35 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
37 href = "/#{rsc.to_s.underscore.pluralize rsc}/#{uuid}"
39 #"\"#{uuid}\" [label=\"#{rsc}\\n#{uuid}\",href=\"#{href}\"];\n"
42 if uuid == :"d41d8cd98f00b204e9800998ecf8427e+0"
45 return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
49 if @pdata[uuid][:name]
50 return "\"#{uuid}\" [label=\"#{@pdata[uuid][:name]}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
53 if @pdata[uuid].respond_to? :files
54 files = @pdata[uuid].files
55 elsif @pdata[uuid][:files]
56 files = @pdata[uuid][:files]
62 while i < 3 and i < files.length
63 label += "\\n" unless label == ""
68 label += "\\n⋮"
70 return "\"#{uuid}\" [label=\"#{label}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
74 return "\"#{uuid}\" [label=\"#{rsc}\",href=\"#{href}\",#{bgcolor}];\n"
77 "\"#{uuid}\" [#{bgcolor}];\n"
81 if @opts[:combine_jobs] == :script_only
82 uuid = "#{job[:script]}"
83 elsif @opts[:combine_jobs] == :script_and_version
84 uuid = "#{job[:script]}_#{job[:script_version]}"
86 uuid = "#{job[:uuid]}"
89 @jobs[uuid] = [] unless @jobs[uuid]
90 @jobs[uuid] << job unless @jobs[uuid].include? job
95 def edge(tail, head, extra)
96 if @opts[:direction] == :bottom_up
97 gr = "\"#{tail}\" -> \"#{head}\""
99 gr = "\"#{head}\" -> \"#{tail}\""
104 gr += "#{k}=\"#{v}\","
112 def script_param_edges(job, prefix, sp)
114 if sp and not sp.empty?
119 k = prefix + "::" + k.to_s
121 gr += script_param_edges(job, k.to_s, v)
127 if GenerateGraph::collection_uuid(v)
128 gr += script_param_edges(job, "#{prefix}[#{i}]", v)
129 elsif @opts[:all_script_parameters]
130 node += "', '" unless node == ""
131 node = "['" if node == ""
139 #id = "#{job[:uuid]}_#{prefix}"
140 gr += "\"#{node}\" [label=\"#{node}\"];\n"
141 gr += edge(job_uuid(job), node, {:label => prefix})
144 m = GenerateGraph::collection_uuid(sp)
145 #puts "#{m} pdata is #{@pdata[m.intern]}"
146 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
147 gr += edge(job_uuid(job), m, {:label => prefix})
148 gr += generate_provenance_edges(m)
149 elsif @opts[:all_script_parameters]
150 #id = "#{job[:uuid]}_#{prefix}"
151 gr += "\"#{sp}\" [label=\"#{sp}\"];\n"
152 gr += edge(job_uuid(job), sp, {:label => prefix})
159 def generate_provenance_edges(uuid)
161 m = GenerateGraph::collection_uuid(uuid)
164 uuid = uuid.intern if uuid
166 if (not uuid) or uuid.empty? or @visited[uuid]
168 #puts "already @visited #{uuid}"
172 if not @pdata[uuid] then
173 return describe_node(uuid)
175 @visited[uuid] = true
178 #puts "visiting #{uuid}"
181 # uuid is a collection
182 gr += describe_node(uuid)
184 if m == :"d41d8cd98f00b204e9800998ecf8427e+0"
185 # empty collection, don't follow any further
189 @pdata.each do |k, job|
190 if job[:output] == uuid.to_s
191 gr += edge(uuid, job_uuid(job), {:label => "output"})
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 # uuid is something else
201 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
206 gr += script_param_edges(job, "", job[:script_parameters])
208 if @opts[:script_version_nodes]
209 gr += describe_node(job[:script_version])
210 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
214 gr += describe_node(uuid)
218 @pdata.each do |k, link|
219 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
220 gr += describe_node(link[:tail_uuid])
221 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => "/links/#{link[:uuid]}"})
222 gr += generate_provenance_edges(link[:tail_uuid])
226 #puts "finished #{uuid}"
234 gr += "\"#{k}\" [href=\"/jobs?"
238 gr += "uuid%5b%5d=#{u[:uuid]}&"
239 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
244 if @opts[:combine_jobs] == :script_only
245 gr += uuid = "#{v[0][:script]}"
246 elsif @opts[:combine_jobs] == :script_and_version
247 gr += uuid = "#{v[0][:script]}"
249 gr += uuid = "#{v[0][:script]}\\n#{v[0][:finished_at]}"
251 gr += "\",#{determine_fillcolor n}];\n"
258 def self.create_provenance_graph(pdata, svgId, opts={})
259 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
262 p2[k[:uuid].intern] = k if k[:uuid]
267 unless pdata.is_a? Hash
268 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
271 gr = """strict digraph {
272 node [fontsize=10,shape=box];
276 if opts[:direction] == :bottom_up
277 gr += "edge [dir=back];"
280 #puts "@pdata is #{pdata}"
282 g = GenerateGraph.new(pdata, opts)
285 gr += g.generate_provenance_edges(k)
288 gr += g.describe_jobs
297 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
305 svg = svg.sub(/<\?xml.*?\?>/m, "")
306 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
307 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
310 def self.find_collections(sp)
312 if sp and not sp.empty?
316 c.concat(find_collections(v))
320 c.concat(find_collections(v))
323 m = GenerateGraph::collection_uuid(sp)