Merge branch 'master' into 10112-workflow-show
[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 requesting_container_uuid
57     get(:requesting_container_uuid)
58   end
59
60   def priority
61     @proxied.priority
62   end
63
64   # For the following properties, use value from the @container if exists
65   # This applies to a ContainerRequest with container_uuid
66
67   def started_at
68     t = get_combined(:started_at)
69     t = Time.parse(t) if (t.is_a? String)
70     t
71   end
72
73   def modified_at
74     t = get_combined(:modified_at)
75     t = Time.parse(t) if (t.is_a? String)
76     t
77   end
78
79   def finished_at
80     t = get_combined(:finished_at)
81     t = Time.parse(t) if (t.is_a? String)
82     t
83   end
84
85   def state_label
86     ec = exit_code
87     return "Failed" if (ec && ec != 0)
88
89     state = get_combined(:state)
90
91     return "Queued" if state == "Locked"
92     return "Cancelled" if ((priority == 0) and (state == "Queued"))
93     state
94   end
95
96   def exit_code
97     get_combined(:exit_code)
98   end
99
100   def docker_image
101     get_combined(:container_image)
102   end
103
104   def runtime_constraints
105     get_combined(:runtime_constraints)
106   end
107
108   def log_collection
109     if @proxied.is_a?(ContainerRequest)
110       get(:log_uuid)
111     else
112       get(:log)
113     end
114   end
115
116   def outputs
117     items = []
118     if @proxied.is_a?(ContainerRequest)
119       out = get(:output_uuid)
120     else
121       out = get(:output)
122     end
123     items << out if out
124     items
125   end
126
127   def command
128     get_combined(:command)
129   end
130
131   def cwd
132     get_combined(:cwd)
133   end
134
135   def environment
136     env = get_combined(:environment)
137     env = nil if env.andand.empty?
138     env
139   end
140
141   def mounts
142     mnt = get_combined(:mounts)
143     mnt = nil if mnt.andand.empty?
144     mnt
145   end
146
147   def output_path
148     get_combined(:output_path)
149   end
150
151   def log_object_uuids
152     [get(:uuid, @container), get(:uuid, @proxied)].compact
153   end
154
155   def render_log
156     collection = Collection.find(log_collection) rescue nil
157     if collection
158       return {log: collection, partial: 'collections/show_files', locals: {object: collection, no_checkboxes: true}}
159     end
160   end
161
162   def template_uuid
163     properties = get(:properties)
164     if properties
165       properties[:template_uuid]
166     end
167   end
168
169   # End combined properties
170
171   protected
172   def get_combined key
173     from_container = get(key, @container)
174     from_proxied = get(key, @proxied)
175
176     if from_container.is_a? Hash or from_container.is_a? Array
177       if from_container.any? then from_container else from_proxied end
178     else
179       from_container || from_proxied
180     end
181   end
182 end