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 node_value = encode_quotes node
153 gr += "\"#{node_value}\" [label=\"#{node_value}\"];\n"
154 gr += edge(job_uuid(job), node_value, {:label => prefix})
157 return '' if sp.empty?
158 m = GenerateGraph::collection_uuid(sp)
159 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
160 gr += edge(job_uuid(job), m, {:label => prefix})
161 gr += generate_provenance_edges(m)
162 elsif @opts[:all_script_parameters]
163 sp_value = encode_quotes sp
164 gr += "\"#{sp_value}\" [label=\"#{sp_value}\"];\n"
165 gr += edge(job_uuid(job), sp_value, {:label => prefix})
171 def generate_provenance_edges(uuid)
173 m = GenerateGraph::collection_uuid(uuid)
176 uuid = uuid.intern if uuid
178 if (not uuid) or uuid.empty? or @visited[uuid]
182 if not @pdata[uuid] then
183 return describe_node(uuid)
185 @visited[uuid] = true
189 # uuid is a collection
190 if not Collection.is_empty_blob_locator? uuid.to_s
191 @pdata.each do |k, job|
192 if job[:output] == uuid.to_s
193 extra = { label: 'output' }
194 gr += edge(uuid, job_uuid(job), extra)
195 gr += generate_provenance_edges(job[:uuid])
197 if job[:log] == uuid.to_s
198 gr += edge(uuid, job_uuid(job), {:label => "log"})
199 gr += generate_provenance_edges(job[:uuid])
203 gr += describe_node(uuid)
205 # uuid is something else
206 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
211 gr += script_param_edges(job, "", job[:script_parameters])
213 if @opts[:script_version_nodes]
214 gr += describe_node(job[:script_version])
215 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
221 gr += describe_node(uuid)
225 @pdata.each do |k, link|
226 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
227 href = url_for ({:controller => Link.to_s.tableize,
229 :id => link[:uuid] })
231 gr += describe_node(link[:tail_uuid])
232 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
233 gr += generate_provenance_edges(link[:tail_uuid])
243 href = url_for ({:controller => Job.to_s.tableize,
246 gr += "\"#{k}\" [href=\"#{href}?"
250 gr += "uuid%5b%5d=#{u[:uuid]}&"
251 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
256 if @opts[:combine_jobs] == :script_only
257 gr += "#{v[0][:script]}"
258 elsif @opts[:combine_jobs] == :script_and_version
259 gr += "#{v[0][:script]}" # Just show the name but the nodes will be distinct
261 gr += "#{v[0][:script]}\\n#{v[0][:finished_at]}"
263 gr += "\",#{determine_fillcolor n}];\n"
268 def encode_quotes value
269 value.andand.gsub("\"", "\\\"")
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)