Merge branch '13561-collection-versions-fixes'
[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 secret_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) &&
62       @proxied.state == "Committed" &&
63       (@proxied.priority > 0 || get(:state, @container) != 'Running') &&
64       @proxied.editable?
65   end
66
67   def container_uuid
68     get(:container_uuid)
69   end
70
71   def requesting_container_uuid
72     get(:requesting_container_uuid)
73   end
74
75   def priority
76     @proxied.priority
77   end
78
79   # For the following properties, use value from the @container if exists
80   # This applies to a ContainerRequest with container_uuid
81
82   def started_at
83     t = get_combined(:started_at)
84     t = Time.parse(t) if (t.is_a? String)
85     t
86   end
87
88   def modified_at
89     t = get_combined(:modified_at)
90     t = Time.parse(t) if (t.is_a? String)
91     t
92   end
93
94   def finished_at
95     t = get_combined(:finished_at)
96     t = Time.parse(t) if (t.is_a? String)
97     t
98   end
99
100   def state_label
101     if get(:state) == 'Final' && get(:state, @container) != 'Complete'
102       # Request was finalized before its container started (or the
103       # container was cancelled)
104       return 'Cancelled'
105     end
106     state = get(:state, @container) || get(:state, @proxied)
107     case state
108     when 'Locked', 'Queued'
109       if priority == 0
110         'On hold'
111       else
112         'Queued'
113       end
114     when 'Complete'
115       if exit_code == 0
116         state
117       else
118         'Failed'
119       end
120     when 'Running'
121       if runtime_status[:error]
122         'Failing'
123       elsif runtime_status[:warning]
124         'Warning'
125       else
126         state
127       end
128     else
129       # Cancelled, or Uncommitted (no container assigned)
130       state
131     end
132   end
133
134   def runtime_status
135     return get(:runtime_status, @container) || get(:runtime_status, @proxied)
136   end
137
138   def state_bootstrap_class
139     case state_label
140     when 'Failing'
141       'danger'
142     when 'Warning'
143       'warning'
144     else
145       super
146     end
147   end
148
149   def exit_code
150     get_combined(:exit_code)
151   end
152
153   def docker_image
154     get_combined(:container_image)
155   end
156
157   def runtime_constraints
158     get_combined(:runtime_constraints)
159   end
160
161   def log_collection
162     if @proxied.is_a?(ContainerRequest)
163       get(:log_uuid)
164     else
165       get(:log)
166     end
167   end
168
169   def outputs
170     items = []
171     if @proxied.is_a?(ContainerRequest)
172       out = get(:output_uuid)
173     else
174       out = get(:output)
175     end
176     items << out if out
177     items
178   end
179
180   def command
181     get_combined(:command)
182   end
183
184   def cwd
185     get_combined(:cwd)
186   end
187
188   def environment
189     env = get_combined(:environment)
190     env = nil if env.andand.empty?
191     env
192   end
193
194   def mounts
195     mnt = get_combined(:mounts)
196     mnt = nil if mnt.andand.empty?
197     mnt
198   end
199
200   def output_path
201     get_combined(:output_path)
202   end
203
204   def log_object_uuids
205     [get(:uuid, @container), get(:uuid, @proxied)].compact
206   end
207
208   def render_log
209     collection = Collection.find(log_collection) rescue nil
210     if collection
211       return {log: collection, partial: 'collections/show_files', locals: {object: collection, no_checkboxes: true}}
212     end
213   end
214
215   def template_uuid
216     properties = get(:properties)
217     if properties
218       properties[:template_uuid]
219     end
220   end
221
222   # End combined properties
223
224   protected
225   def get_combined key
226     from_container = get(key, @container)
227     from_proxied = get(key, @proxied)
228
229     if from_container.is_a? Hash or from_container.is_a? Array
230       if from_container.any? then from_container else from_proxied end
231     else
232       from_container || from_proxied
233     end
234   end
235 end