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