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