Merge branch '2411-check-copyright'
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
index 1ed182c35f8482e39537e0591c849f8538d5934f..a5b26f0d6d3d5534fd19e1df76d45ac97e7aadd8 100644 (file)
@@ -1,31 +1,43 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 class ContainerWorkUnit < ProxyWorkUnit
   attr_accessor :container
+  attr_accessor :child_proxies
 
-  def initialize proxied, label
-    super
+  def initialize proxied, label, parent, child_objects=nil
+    super proxied, label, parent
     if @proxied.is_a?(ContainerRequest)
       container_uuid = get(:container_uuid)
       if container_uuid
-        @container = Container.where(uuid: container_uuid).first
+        @container = Container.find(container_uuid)
       end
     end
+    @child_proxies = child_objects
   end
 
   def children
-    return self.my_children if self.my_children
-
-    container_uuid = nil
-    container_uuid = if @proxied.is_a?(Container) then uuid else get(:container_uuid) end
+    return @my_children if @my_children
 
     items = []
+    container_uuid = if @proxied.is_a?(Container) then uuid else get(:container_uuid) end
     if container_uuid
-      reqs = ContainerRequest.where(requesting_container_uuid: container_uuid).results
-      reqs.each do |cr|
-        items << cr.work_unit(cr.name || 'this container')
+      cols = ContainerRequest.columns.map(&:name) - %w(id updated_at mounts)
+      my_children = @child_proxies || ContainerRequest.select(cols).where(requesting_container_uuid: container_uuid).results if !my_children
+      my_child_containers = my_children.map(&:container_uuid).compact.uniq
+      grandchildren = {}
+      my_child_containers.each { |c| grandchildren[c] = []} if my_child_containers.any?
+      reqs = ContainerRequest.select(cols).where(requesting_container_uuid: my_child_containers).results if my_child_containers.any?
+      reqs.each {|cr| grandchildren[cr.requesting_container_uuid] << cr} if reqs
+
+      my_children.each do |cr|
+        items << cr.work_unit(cr.name || 'this container', child_objects=grandchildren[cr.container_uuid])
       end
     end
 
-    self.my_children = items
+    @child_proxies = nil #no need of this any longer
+    @my_children = items
   end
 
   def title
@@ -46,13 +58,21 @@ class ContainerWorkUnit < ProxyWorkUnit
   end
 
   def can_cancel?
-    @proxied.is_a?(ContainerRequest) && state_label.in?(["Queued", "Locked", "Running"]) && priority > 0
+    @proxied.is_a?(ContainerRequest) && @proxied.state == "Committed" && @proxied.priority > 0 && @proxied.editable?
   end
 
   def container_uuid
     get(:container_uuid)
   end
 
+  def requesting_container_uuid
+    get(:requesting_container_uuid)
+  end
+
+  def priority
+    @proxied.priority
+  end
+
   # For the following properties, use value from the @container if exists
   # This applies to a ContainerRequest with container_uuid
 
@@ -75,7 +95,18 @@ class ContainerWorkUnit < ProxyWorkUnit
   end
 
   def state_label
-    get_combined(:state)
+    ec = exit_code
+    return "Failed" if (ec && ec != 0)
+
+    state = get_combined(:state)
+
+    return "Queued" if state == "Locked"
+    return "Cancelled" if ((priority == 0) and (state == "Queued"))
+    state
+  end
+
+  def exit_code
+    get_combined(:exit_code)
   end
 
   def docker_image
@@ -86,17 +117,22 @@ class ContainerWorkUnit < ProxyWorkUnit
     get_combined(:runtime_constraints)
   end
 
-  def priority
-    get_combined(:priority)
-  end
-
   def log_collection
-    get_combined(:log)
+    if @proxied.is_a?(ContainerRequest)
+      get(:log_uuid)
+    else
+      get(:log)
+    end
   end
 
   def outputs
     items = []
-    items << get_combined(:output) if get_combined(:output)
+    if @proxied.is_a?(ContainerRequest)
+      out = get(:output_uuid)
+    else
+      out = get(:output)
+    end
+    items << out if out
     items
   end
 
@@ -125,14 +161,7 @@ class ContainerWorkUnit < ProxyWorkUnit
   end
 
   def log_object_uuids
-    [get_combined(:uuid), get(:uuid)].uniq
-  end
-
-  def live_log_lines(limit=2000)
-    event_types = ["stdout", "stderr", "arv-mount", "crunch-run"]
-    log_lines = Log.where(event_type: event_types, object_uuid: log_object_uuids).order("id DESC").limit(limit)
-    log_lines.results.reverse.
-      flat_map { |log| log.properties[:text].split("\n") rescue [] }
+    [get(:uuid, @container), get(:uuid, @proxied)].compact
   end
 
   def render_log
@@ -145,14 +174,21 @@ class ContainerWorkUnit < ProxyWorkUnit
   def template_uuid
     properties = get(:properties)
     if properties
-      properties[:workflow_uuid]
+      properties[:template_uuid]
     end
   end
 
-  # End combined propeties
+  # End combined properties
 
   protected
   def get_combined key
-    get(key, @container) || get(key, @proxied)
+    from_container = get(key, @container)
+    from_proxied = get(key, @proxied)
+
+    if from_container.is_a? Hash or from_container.is_a? Array
+      if from_container.any? then from_container else from_proxied end
+    else
+      from_container || from_proxied
+    end
   end
 end