Merge branch '11629-groups-contents-memory'
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
index 9796ef8a23c88f7e64773e074f9f28c170027807..84fc1f8f0978e16a86bd34fde8d07a6d2d9cd7c1 100644 (file)
@@ -1,59 +1,68 @@
 class ContainerWorkUnit < ProxyWorkUnit
-  attr_accessor :related
+  attr_accessor :container
 
-  def initialize proxied, label
+  def initialize proxied, label, parent
     super
     if @proxied.is_a?(ContainerRequest)
       container_uuid = get(:container_uuid)
       if container_uuid
-        @related = Container.where(uuid: container_uuid).first rescue nil
+        @container = Container.where(uuid: container_uuid).first
       end
     end
   end
 
   def children
-    return self.my_children if self.my_children
+    return @my_children if @my_children
 
-    items = []
-
-    if @proxied.is_a?(Container)
-      crs = {}
-      reqs = ContainerRequest.where(requesting_container_uuid: uuid).results
-      reqs.each { |cr| crs[cr.container_uuid] = cr.name }
-
-      containers = Container.where(uuid: crs.keys).results
-      containers.each do |c|
-        items << c.work_unit(crs[c.uuid] || 'this container')
-      end
+    container_uuid = nil
+    container_uuid = if @proxied.is_a?(Container) then uuid else get(:container_uuid) end
 
-      self.my_children = items
-    else
-      container_uuid = get(:container_uuid)
-      if container_uuid
-        reqs = ContainerRequest.where(requesting_container_uuid: container_uuid).results
-        reqs.each do |cr|
-          items << cr.work_unit(cr.name || 'this container')
-        end
+    items = []
+    if container_uuid
+      reqs = ContainerRequest.where(requesting_container_uuid: container_uuid).results
+      reqs.each do |cr|
+        items << cr.work_unit(cr.name || 'this container')
       end
     end
 
-    self.my_children = items
+    @my_children = items
   end
 
   def title
     "container"
   end
 
+  def uri
+    uuid = get(:uuid)
+
+    return nil unless uuid
+
+    if @proxied.class.respond_to? :table_name
+      "/#{@proxied.class.table_name}/#{uuid}"
+    else
+      resource_class = ArvadosBase.resource_class_for_uuid(uuid)
+      "#{resource_class.table_name}/#{uuid}" if resource_class
+    end
+  end
+
   def can_cancel?
-    @proxied.is_a?(ContainerRequest) && state_label.in?(["Queued", "Locked", "Running"])
+    @proxied.is_a?(ContainerRequest) && @proxied.state == "Committed" && @proxied.priority > 0 && @proxied.editable?
   end
 
   def container_uuid
     get(:container_uuid)
   end
 
-  # For the following properties, use value from the @related container if exists
-  # This applies to a ContainerRequest in Committed or Final state with container_uuid
+  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
 
   def started_at
     t = get_combined(:started_at)
@@ -74,7 +83,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
@@ -85,17 +105,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
 
@@ -123,10 +148,35 @@ class ContainerWorkUnit < ProxyWorkUnit
     get_combined(:output_path)
   end
 
-  # End combined propeties
+  def log_object_uuids
+    [get(:uuid, @container), get(:uuid, @proxied)].compact
+  end
+
+  def render_log
+    collection = Collection.find(log_collection) rescue nil
+    if collection
+      return {log: collection, partial: 'collections/show_files', locals: {object: collection, no_checkboxes: true}}
+    end
+  end
+
+  def template_uuid
+    properties = get(:properties)
+    if properties
+      properties[:template_uuid]
+    end
+  end
+
+  # End combined properties
 
   protected
   def get_combined key
-    get(key, @related) || 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