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)
31 bgcolor = "style=filled,fillcolor=\"#88ff88\""
33 bgcolor = "style=filled,fillcolor=\"#8888ff\""
35 bgcolor = "style=filled,fillcolor=\"#88ffff\""
40 def describe_node(uuid)
41 bgcolor = determine_fillcolor @opts[:pips][uuid] if @opts[:pips]
43 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
45 href = "/#{rsc.to_s.underscore.pluralize rsc}/#{uuid}"
47 #"\"#{uuid}\" [label=\"#{rsc}\\n#{uuid}\",href=\"#{href}\"];\n"
50 if uuid == :"d41d8cd98f00b204e9800998ecf8427e+0"
53 return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
57 if @pdata[uuid][:name]
58 return "\"#{uuid}\" [label=\"#{@pdata[uuid][:name]}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
61 if @pdata[uuid].respond_to? :files
62 files = @pdata[uuid].files
63 elsif @pdata[uuid][:files]
64 files = @pdata[uuid][:files]
70 while i < 3 and i < files.length
71 label += "\\n" unless label == ""
76 label += "\\n⋮"
78 return "\"#{uuid}\" [label=\"#{label}\",href=\"#{href}\",shape=oval,#{bgcolor}];\n"
82 return "\"#{uuid}\" [label=\"#{rsc}\",href=\"#{href}\",#{bgcolor}];\n"
85 "\"#{uuid}\" [#{bgcolor}];\n"
89 if @opts[:combine_jobs] == :script_only
90 uuid = "#{job[:script]}"
91 elsif @opts[:combine_jobs] == :script_and_version
92 uuid = "#{job[:script]}_#{job[:script_version]}"
94 uuid = "#{job[:uuid]}"
97 @jobs[uuid] = [] unless @jobs[uuid]
98 @jobs[uuid] << job unless @jobs[uuid].include? job
103 def edge(tail, head, extra)
104 if @opts[:direction] == :bottom_up
105 gr = "\"#{tail}\" -> \"#{head}\""
107 gr = "\"#{head}\" -> \"#{tail}\""
112 gr += "#{k}=\"#{v}\","
120 def script_param_edges(job, prefix, sp)
122 if sp and not sp.empty?
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 == ""
147 #id = "#{job[:uuid]}_#{prefix}"
148 gr += "\"#{node}\" [label=\"#{node}\"];\n"
149 gr += edge(job_uuid(job), node, {:label => prefix})
152 m = GenerateGraph::collection_uuid(sp)
153 #puts "#{m} pdata is #{@pdata[m.intern]}"
154 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
155 gr += edge(job_uuid(job), m, {:label => prefix})
156 gr += generate_provenance_edges(m)
157 elsif @opts[:all_script_parameters]
158 #id = "#{job[:uuid]}_#{prefix}"
159 gr += "\"#{sp}\" [label=\"#{sp}\"];\n"
160 gr += edge(job_uuid(job), sp, {:label => prefix})
167 def generate_provenance_edges(uuid)
169 m = GenerateGraph::collection_uuid(uuid)
172 uuid = uuid.intern if uuid
174 if (not uuid) or uuid.empty? or @visited[uuid]
176 #puts "already @visited #{uuid}"
180 if not @pdata[uuid] then
181 return describe_node(uuid)
183 @visited[uuid] = true
186 #puts "visiting #{uuid}"
189 # uuid is a collection
190 gr += describe_node(uuid)
192 if m == :"d41d8cd98f00b204e9800998ecf8427e+0"
193 # empty collection, don't follow any further
197 @pdata.each do |k, job|
198 if job[:output] == uuid.to_s
199 gr += edge(uuid, job_uuid(job), {:label => "output"})
200 gr += generate_provenance_edges(job[:uuid])
202 if job[:log] == uuid.to_s
203 gr += edge(uuid, job_uuid(job), {:label => "log"})
204 gr += generate_provenance_edges(job[: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"})
222 gr += describe_node(uuid)
226 @pdata.each do |k, link|
227 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
228 gr += describe_node(link[:tail_uuid])
229 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => "/links/#{link[:uuid]}"})
230 gr += generate_provenance_edges(link[:tail_uuid])
234 #puts "finished #{uuid}"
242 gr += "\"#{k}\" [href=\"/jobs?"
246 gr += "uuid%5b%5d=#{u[:uuid]}&"
247 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
252 if @opts[:combine_jobs] == :script_only
253 gr += uuid = "#{v[0][:script]}"
254 elsif @opts[:combine_jobs] == :script_and_version
255 gr += uuid = "#{v[0][:script]}"
257 gr += uuid = "#{v[0][:script]}\\n#{v[0][:finished_at]}"
259 gr += "\",#{determine_fillcolor n}];\n"
266 def self.create_provenance_graph(pdata, svgId, opts={})
267 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
270 p2[k[:uuid].intern] = k if k[:uuid]
275 unless pdata.is_a? Hash
276 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
279 gr = """strict digraph {
280 node [fontsize=10,shape=box];
284 if opts[:direction] == :bottom_up
285 gr += "edge [dir=back];"
288 #puts "@pdata is #{pdata}"
290 g = GenerateGraph.new(pdata, opts)
293 gr += g.generate_provenance_edges(k)
296 gr += g.describe_jobs
305 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
313 svg = svg.sub(/<\?xml.*?\?>/m, "")
314 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
315 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
318 def self.find_collections(sp)
320 if sp and not sp.empty?
324 c.concat(find_collections(v))
328 c.concat(find_collections(v))
331 m = GenerateGraph::collection_uuid(sp)