1 module ProvenanceHelper
4 def initialize(pdata, opts)
11 def self.collection_uuid(uuid)
12 m = CollectionsHelper.match(uuid)
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)
118 k = prefix + "::" + k.to_s
120 gr += script_param_edges(job, k.to_s, v)
126 if GenerateGraph::collection_uuid(v)
127 gr += script_param_edges(job, "#{prefix}[#{i}]", v)
128 elsif @opts[:all_script_parameters]
129 node += "', '" unless node == ""
130 node = "['" if node == ""
138 #id = "#{job[:uuid]}_#{prefix}"
139 gr += "\"#{node}\" [label=\"#{node}\"];\n"
140 gr += edge(job_uuid(job), node, {:label => prefix})
143 return '' if sp.empty?
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})
158 def generate_provenance_edges(uuid)
160 m = GenerateGraph::collection_uuid(uuid)
163 uuid = uuid.intern if uuid
165 if (not uuid) or uuid.empty? or @visited[uuid]
167 #puts "already @visited #{uuid}"
171 if not @pdata[uuid] then
172 return describe_node(uuid)
174 @visited[uuid] = true
177 #puts "visiting #{uuid}"
180 # uuid is a collection
181 gr += describe_node(uuid)
183 if m == :"d41d8cd98f00b204e9800998ecf8427e+0"
184 # empty collection, don't follow any further
188 @pdata.each do |k, job|
189 if job[:output] == uuid.to_s
190 gr += edge(uuid, job_uuid(job), {:label => "output"})
191 gr += generate_provenance_edges(job[:uuid])
193 if job[:log] == uuid.to_s
194 gr += edge(uuid, job_uuid(job), {:label => "log"})
195 gr += generate_provenance_edges(job[:uuid])
199 # uuid is something else
200 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
205 gr += script_param_edges(job, "", job[:script_parameters])
207 if @opts[:script_version_nodes]
208 gr += describe_node(job[:script_version])
209 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
213 gr += describe_node(uuid)
217 @pdata.each do |k, link|
218 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
219 gr += describe_node(link[:tail_uuid])
220 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => "/links/#{link[:uuid]}"})
221 gr += generate_provenance_edges(link[:tail_uuid])
225 #puts "finished #{uuid}"
233 gr += "\"#{k}\" [href=\"/jobs?"
237 gr += "uuid%5b%5d=#{u[:uuid]}&"
238 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
243 if @opts[:combine_jobs] == :script_only
244 gr += uuid = "#{v[0][:script]}"
245 elsif @opts[:combine_jobs] == :script_and_version
246 gr += uuid = "#{v[0][:script]}"
248 gr += uuid = "#{v[0][:script]}\\n#{v[0][:finished_at]}"
250 gr += "\",#{determine_fillcolor n}];\n"
257 def self.create_provenance_graph(pdata, svgId, opts={})
258 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
261 p2[k[:uuid].intern] = k if k[:uuid]
266 unless pdata.is_a? Hash
267 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
270 gr = """strict digraph {
271 node [fontsize=10,shape=box];
275 if opts[:direction] == :bottom_up
276 gr += "edge [dir=back];"
279 #puts "@pdata is #{pdata}"
281 g = GenerateGraph.new(pdata, opts)
284 gr += g.generate_provenance_edges(k)
287 gr += g.describe_jobs
296 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
304 svg = svg.sub(/<\?xml.*?\?>/m, "")
305 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
306 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
309 def self.find_collections(sp)
314 c.concat(find_collections(v))
318 c.concat(find_collections(v))
322 m = GenerateGraph::collection_uuid(sp)