9767: add a link to workflow in the container_request#show page.
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
index 2fe1ff06698c452aee44c0e7df143e6a185af36d..e56c0655234cb598aecaf917abb3c3cca59f3156 100644 (file)
@@ -1,12 +1,12 @@
 class ContainerWorkUnit < ProxyWorkUnit
-  attr_accessor :related
+  attr_accessor :container
 
   def initialize proxied, label
     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
@@ -14,26 +14,14 @@ class ContainerWorkUnit < ProxyWorkUnit
   def children
     return self.my_children if self.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
 
@@ -44,6 +32,19 @@ class ContainerWorkUnit < ProxyWorkUnit
     "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"]) && priority > 0
   end
@@ -52,8 +53,8 @@ class ContainerWorkUnit < ProxyWorkUnit
     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
+  # 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)
@@ -123,10 +124,35 @@ class ContainerWorkUnit < ProxyWorkUnit
     get_combined(:output_path)
   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 [] }
+  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 propeties
 
   protected
   def get_combined key
-    get(key, @related) || get(key, @proxied)
+    get(key, @container) || get(key, @proxied)
   end
 end