Merge branch '12018-tool-docs'
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class ContainerWorkUnit < ProxyWorkUnit
6   attr_accessor :container
7   attr_accessor :child_proxies
8
9   def initialize proxied, label, parent, child_objects=nil
10     super proxied, label, parent
11     if @proxied.is_a?(ContainerRequest)
12       container_uuid = get(:container_uuid)
13       if container_uuid
14         @container = Container.find(container_uuid)
15       end
16     end
17     @child_proxies = child_objects
18   end
19
20   def children
21     return @my_children if @my_children
22
23     items = []
24     container_uuid = if @proxied.is_a?(Container) then uuid else get(:container_uuid) end
25     if container_uuid
26       cols = ContainerRequest.columns.map(&:name) - %w(id updated_at mounts)
27       my_children = @child_proxies || ContainerRequest.select(cols).where(requesting_container_uuid: container_uuid).results if !my_children
28       my_child_containers = my_children.map(&:container_uuid).compact.uniq
29       grandchildren = {}
30       my_child_containers.each { |c| grandchildren[c] = []} if my_child_containers.any?
31       reqs = ContainerRequest.select(cols).where(requesting_container_uuid: my_child_containers).results if my_child_containers.any?
32       reqs.each {|cr| grandchildren[cr.requesting_container_uuid] << cr} if reqs
33
34       my_children.each do |cr|
35         items << cr.work_unit(cr.name || 'this container', child_objects=grandchildren[cr.container_uuid])
36       end
37     end
38
39     @child_proxies = nil #no need of this any longer
40     @my_children = items
41   end
42
43   def title
44     "container"
45   end
46
47   def uri
48     uuid = get(:uuid)
49
50     return nil unless uuid
51
52     if @proxied.class.respond_to? :table_name
53       "/#{@proxied.class.table_name}/#{uuid}"
54     else
55       resource_class = ArvadosBase.resource_class_for_uuid(uuid)
56       "#{resource_class.table_name}/#{uuid}" if resource_class
57     end
58   end
59
60   def can_cancel?
61     @proxied.is_a?(ContainerRequest) && @proxied.state == "Committed" && @proxied.priority > 0 && @proxied.editable?
62   end
63
64   def container_uuid
65     get(:container_uuid)
66   end
67
68   def requesting_container_uuid
69     get(:requesting_container_uuid)
70   end
71
72   def priority
73     @proxied.priority
74   end
75
76   # For the following properties, use value from the @container if exists
77   # This applies to a ContainerRequest with container_uuid
78
79   def started_at
80     t = get_combined(:started_at)
81     t = Time.parse(t) if (t.is_a? String)
82     t
83   end
84
85   def modified_at
86     t = get_combined(:modified_at)
87     t = Time.parse(t) if (t.is_a? String)
88     t
89   end
90
91   def finished_at
92     t = get_combined(:finished_at)
93     t = Time.parse(t) if (t.is_a? String)
94     t
95   end
96
97   def state_label
98     ec = exit_code
99     return "Failed" if (ec && ec != 0)
100
101     state = get_combined(:state)
102
103     return "Queued" if state == "Locked"
104     return "Cancelled" if ((priority == 0) and (state == "Queued"))
105     state
106   end
107
108   def exit_code
109     get_combined(:exit_code)
110   end
111
112   def docker_image
113     get_combined(:container_image)
114   end
115
116   def runtime_constraints
117     get_combined(:runtime_constraints)
118   end
119
120   def log_collection
121     if @proxied.is_a?(ContainerRequest)
122       get(:log_uuid)
123     else
124       get(:log)
125     end
126   end
127
128   def outputs
129     items = []
130     if @proxied.is_a?(ContainerRequest)
131       out = get(:output_uuid)
132     else
133       out = get(:output)
134     end
135     items << out if out
136     items
137   end
138
139   def command
140     get_combined(:command)
141   end
142
143   def cwd
144     get_combined(:cwd)
145   end
146
147   def environment
148     env = get_combined(:environment)
149     env = nil if env.andand.empty?
150     env
151   end
152
153   def mounts
154     mnt = get_combined(:mounts)
155     mnt = nil if mnt.andand.empty?
156     mnt
157   end
158
159   def output_path
160     get_combined(:output_path)
161   end
162
163   def log_object_uuids
164     [get(:uuid, @container), get(:uuid, @proxied)].compact
165   end
166
167   def render_log
168     collection = Collection.find(log_collection) rescue nil
169     if collection
170       return {log: collection, partial: 'collections/show_files', locals: {object: collection, no_checkboxes: true}}
171     end
172   end
173
174   def template_uuid
175     properties = get(:properties)
176     if properties
177       properties[:template_uuid]
178     end
179   end
180
181   # End combined properties
182
183   protected
184   def get_combined key
185     from_container = get(key, @container)
186     from_proxied = get(key, @proxied)
187
188     if from_container.is_a? Hash or from_container.is_a? Array
189       if from_container.any? then from_container else from_proxied end
190     else
191       from_container || from_proxied
192     end
193   end
194 end