Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / helpers / provenance_helper.rb
index 31d0203c24adbc8a7cc8967b075a44005dbe9d0c..cef5cc7ee816db5e906537e6da643b33223466f7 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 module ProvenanceHelper
 
   class GenerateGraph
@@ -37,9 +41,15 @@ module ProvenanceHelper
           return "\"#{uuid}\" [label=\"(empty collection)\"];\n"
         end
 
-        href = url_for ({:controller => Collection.to_s.tableize,
-                          :action => :show,
-                          :id => uuid.to_s })
+        if describe_opts[:col_uuid]
+          href = url_for ({:controller => Collection.to_s.tableize,
+                           :action => :show,
+                           :id => describe_opts[:col_uuid].to_s })
+        else
+          href = url_for ({:controller => Collection.to_s.tableize,
+                           :action => :show,
+                           :id => uuid.to_s })
+        end
 
         return "\"#{uuid}\" [label=\"#{encode_quotes(describe_opts[:label] || (@pdata[uuid] and @pdata[uuid][:name]) || uuid)}\",shape=box,href=\"#{href}\",#{bgcolor}];\n"
       else
@@ -104,53 +114,6 @@ module ProvenanceHelper
       gr
     end
 
-    def cr_edges cr
-      uuid = cr[:uuid]
-      gr = ""
-
-      ProvenanceHelper::find_collections cr[:mounts], 'mounts' do |col_hash, col_uuid, key|
-        if col_uuid
-          gr += describe_node(col_uuid)
-          gr += edge(col_uuid, uuid, {:label => key})
-        else
-          gr += describe_node(col_hash)
-          gr += edge(col_hash, uuid, {:label => key})
-        end
-      end
-
-      [
-        [true, :requesting_container_uuid, 'requesting_container'],
-        [true, :container_image, 'container_image'],
-        [false, :output_uuid, 'output'],
-        [false, :log_uuid, 'log']
-      ].each do |is_input, attr, label|
-        if cr[attr]
-          if attr == :container_image && cr[:container_uuid]
-            # Extract the container_image's UUID from the CR's container
-            cont = Container.find(cr[:container_uuid])
-            gr += describe_node(cont[:container_image], {label: cr[attr]})
-            # If container_image, then is_input is true
-            edge_from = cont[:container_image]
-            edge_to = uuid
-          else
-            # Default case
-            gr += describe_node(cr[attr])
-
-            if is_input
-              edge_from = cr[attr]
-              edge_to = uuid
-            else
-              edge_from = uuid
-              edge_to = cr[attr]
-            end
-          end
-          gr += edge(edge_from, edge_to, {label: label})
-        end
-      end
-
-      gr
-    end
-
     def job_edges job, edge_opts={}
       uuid = job_uuid(job)
       gr = ""
@@ -188,6 +151,83 @@ module ProvenanceHelper
       gr
     end
 
+    def cr_edges cont, edge_opts={}
+      uuid = cont[:uuid]
+      gr = ""
+
+      gr += describe_node(cont[:uuid], {href: {controller: 'container_requests',
+                                             id: cont[:uuid]},
+                                        shape: 'oval',
+                                        label: cont[:name]})
+
+      ProvenanceHelper::find_collections cont[:mounts] do |collection_hash, collection_uuid, key|
+        if @opts[:pdh_to_uuid] and @opts[:pdh_to_uuid][collection_hash]
+          collection_uuid = @opts[:pdh_to_uuid][collection_hash].uuid
+          collection_hash = nil
+        end
+        if collection_uuid and @pdata[collection_uuid]
+          gr += describe_node(collection_uuid)
+          gr += edge(collection_uuid, uuid, {:label => key})
+        elsif collection_hash and @pdata[collection_hash]
+          gr += describe_node(collection_hash)
+          gr += edge(collection_hash, uuid, {:label => key})
+        end
+      end
+
+      if cont[:container_image] and !@opts[:no_docker] and @pdata[cont[:container_image]]
+        gr += describe_node(cont[:container_image], {label: cont[:container_image]})
+        gr += edge(cont[:container_image], uuid, {label: "docker_image"})
+      end
+
+      if cont[:output_uuid] and !edge_opts[:no_output] and @pdata[cont[:output_uuid]]
+        gr += describe_node(cont[:output_uuid])
+        gr += edge(uuid, cont[:output_uuid], {label: "output" })
+      end
+
+      if cont[:log_uuid] and !edge_opts[:no_log] and @pdata[cont[:log_uuid]]
+        gr += describe_node(cont[:log_uuid])
+        gr += edge(uuid, cont[:log_uuid], {label: "log"})
+      end
+
+      gr
+    end
+
+    def container_edges cont, edge_opts={}
+      uuid = cont[:uuid]
+      gr = ""
+
+      gr += describe_node(cont[:uuid], {href: {controller: 'containers',
+                                             id: cont[:uuid]},
+                                      shape: 'oval'})
+
+      ProvenanceHelper::find_collections cont[:mounts] do |collection_hash, collection_uuid, key|
+        if collection_uuid and @pdata[collection_uuid]
+          gr += describe_node(collection_uuid)
+          gr += edge(collection_uuid, uuid, {:label => key})
+        elsif collection_hash and @pdata[collection_hash]
+          gr += describe_node(collection_hash)
+          gr += edge(collection_hash, uuid, {:label => key})
+        end
+      end
+
+      if cont[:container_image] and !@opts[:no_docker] and @pdata[cont[:container_image]]
+        gr += describe_node(cont[:container_image], {label: cont[:container_image]})
+        gr += edge(cont[:container_image], uuid, {label: "docker_image"})
+      end
+
+      if cont[:output] and !edge_opts[:no_output] and @pdata[cont[:output]]
+        gr += describe_node(cont[:output])
+        gr += edge(uuid, cont[:output], {label: "output" })
+      end
+
+      if cont[:log] and !edge_opts[:no_log] and @pdata[cont[:log]]
+        gr += describe_node(cont[:log])
+        gr += edge(uuid, cont[:log], {label: "log"})
+      end
+
+      gr
+    end
+
     def generate_provenance_edges(uuid)
       gr = ""
       m = GenerateGraph::collection_uuid(uuid)
@@ -234,10 +274,9 @@ module ProvenanceHelper
         elsif rsc == ContainerRequest
           cr = @pdata[uuid]
           gr += cr_edges cr if cr
-          gr += describe_node(uuid, {href: {controller: 'container_requests',
-                                            id: uuid},
-                                     label: @pdata[uuid][:name],
-                                     shape: 'oval'})
+        elsif rsc == Container
+          cr = @pdata[uuid]
+          gr += container_edges cr if cr
         end
       end
 
@@ -312,9 +351,8 @@ module ProvenanceHelper
 node [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
 edge [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
 """
-
-    if opts[:direction] == :bottom_up
-      gr += "edge [dir=back];"
+    if ["LR", "RL"].include? opts[:direction]
+      gr += "rankdir=#{opts[:direction]};"
     end
 
     begin
@@ -383,4 +421,17 @@ edge [fontsize=10,fontname=\"Helvetica,Arial,sans-serif\"];
       end
     end
   end
+
+  def self.cr_input_pdhs cr
+    pdhs = []
+    input_obj = cr[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content] || cr[:mounts]
+    if input_obj
+      find_collections input_obj do |col_hash, col_uuid, key|
+        if col_hash
+          pdhs << col_hash
+        end
+      end
+    end
+    pdhs
+  end
 end