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 gr += "\"#{node}\" [label=\"#{node}\"];\n"
153 gr += edge(job_uuid(job), node, {:label => prefix})
156 return '' if sp.empty?
157 m = GenerateGraph::collection_uuid(sp)
158 if m and (@pdata[m.intern] or (not @opts[:pdata_only]))
159 gr += edge(job_uuid(job), m, {:label => prefix})
160 gr += generate_provenance_edges(m)
161 elsif @opts[:all_script_parameters]
162 gr += "\"#{sp}\" [label=\"#{sp}\"];\n"
163 gr += edge(job_uuid(job), sp, {:label => prefix})
169 def generate_provenance_edges(uuid)
171 m = GenerateGraph::collection_uuid(uuid)
174 uuid = uuid.intern if uuid
176 if (not uuid) or uuid.empty? or @visited[uuid]
180 if not @pdata[uuid] then
181 return describe_node(uuid)
183 @visited[uuid] = true
187 # uuid is a collection
188 if not Collection.is_empty_blob_locator? uuid.to_s
189 @pdata.each do |k, job|
190 if job[:output] == uuid.to_s
191 extra = { label: 'output' }
192 gr += edge(uuid, job_uuid(job), extra)
193 gr += generate_provenance_edges(job[:uuid])
195 if job[:log] == uuid.to_s
196 gr += edge(uuid, job_uuid(job), {:label => "log"})
197 gr += generate_provenance_edges(job[:uuid])
201 gr += describe_node(uuid)
203 # uuid is something else
204 rsc = ArvadosBase::resource_class_for_uuid uuid.to_s
209 gr += script_param_edges(job, "", job[:script_parameters])
211 if @opts[:script_version_nodes]
212 gr += describe_node(job[:script_version])
213 gr += edge(job_uuid(job), job[:script_version], {:label => "script_version"})
219 gr += describe_node(uuid)
223 @pdata.each do |k, link|
224 if link[:head_uuid] == uuid.to_s and link[:link_class] == "provenance"
225 href = url_for ({:controller => Link.to_s.tableize,
227 :id => link[:uuid] })
229 gr += describe_node(link[:tail_uuid])
230 gr += edge(link[:head_uuid], link[:tail_uuid], {:label => link[:name], :href => href})
231 gr += generate_provenance_edges(link[:tail_uuid])
241 href = url_for ({:controller => Job.to_s.tableize,
244 gr += "\"#{k}\" [href=\"#{href}?"
248 gr += "uuid%5b%5d=#{u[:uuid]}&"
249 n |= @opts[:pips][u[:uuid].intern] if @opts[:pips] and @opts[:pips][u[:uuid].intern]
254 if @opts[:combine_jobs] == :script_only
255 gr += "#{v[0][:script]}"
256 elsif @opts[:combine_jobs] == :script_and_version
257 gr += "#{v[0][:script]}" # Just show the name but the nodes will be distinct
259 gr += "#{v[0][:script]}\\n#{v[0][:finished_at]}"
261 gr += "\",#{determine_fillcolor n}];\n"
268 def self.create_provenance_graph(pdata, svgId, opts={})
269 if pdata.is_a? Array or pdata.is_a? ArvadosResourceList
272 p2[k[:uuid].intern] = k if k[:uuid]
277 unless pdata.is_a? Hash
278 raise "create_provenance_graph accepts Array or Hash for pdata only, pdata is #{pdata.class}"
281 gr = """strict digraph {
282 node [fontsize=10,shape=box];
286 if opts[:direction] == :bottom_up
287 gr += "edge [dir=back];"
290 g = GenerateGraph.new(pdata, opts)
293 gr += g.generate_provenance_edges(k)
296 gr += g.describe_jobs
303 Open3.popen2("dot", "-Tsvg") do |stdin, stdout, wait_thr|
311 svg = svg.sub(/<\?xml.*?\?>/m, "")
312 svg = svg.sub(/<!DOCTYPE.*?>/m, "")
313 svg = svg.sub(/<svg /, "<svg id=\"#{svgId}\" ")
316 def self.find_collections(sp)
321 c.concat(find_collections(v))
325 c.concat(find_collections(v))
329 m = GenerateGraph::collection_uuid(sp)