9687: Updated state_label method changes to something more simple and compact. Added...
[arvados.git] / apps / workbench / app / models / container_work_unit.rb
1 class ContainerWorkUnit < ProxyWorkUnit
2   attr_accessor :container
3
4   def initialize proxied, label
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 self.my_children if self.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     self.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) && state_label.in?(["Queued", "Locked", "Running"]) && priority > 0
50   end
51
52   def container_uuid
53     get(:container_uuid)
54   end
55
56   # For the following properties, use value from the @container if exists
57   # This applies to a ContainerRequest with container_uuid
58
59   def started_at
60     t = get_combined(:started_at)
61     t = Time.parse(t) if (t.is_a? String)
62     t
63   end
64
65   def modified_at
66     t = get_combined(:modified_at)
67     t = Time.parse(t) if (t.is_a? String)
68     t
69   end
70
71   def finished_at
72     t = get_combined(:finished_at)
73     t = Time.parse(t) if (t.is_a? String)
74     t
75   end
76
77   def state_label
78     exit_code = get_combined(:exit_code)
79     "Failed" if (exit_code && exit_code != 0)
80     get_combined(:state)
81   end
82
83   def docker_image
84     get_combined(:container_image)
85   end
86
87   def runtime_constraints
88     get_combined(:runtime_constraints)
89   end
90
91   def priority
92     get_combined(:priority)
93   end
94
95   def log_collection
96     get_combined(:log)
97   end
98
99   def outputs
100     items = []
101     items << get_combined(:output) if get_combined(:output)
102     items
103   end
104
105   def command
106     get_combined(:command)
107   end
108
109   def cwd
110     get_combined(:cwd)
111   end
112
113   def environment
114     env = get_combined(:environment)
115     env = nil if env.andand.empty?
116     env
117   end
118
119   def mounts
120     mnt = get_combined(:mounts)
121     mnt = nil if mnt.andand.empty?
122     mnt
123   end
124
125   def output_path
126     get_combined(:output_path)
127   end
128
129   def log_object_uuids
130     [get_combined(:uuid), get(:uuid)].uniq
131   end
132
133   def live_log_lines(limit=2000)
134     event_types = ["stdout", "stderr", "arv-mount", "crunch-run"]
135     log_lines = Log.where(event_type: event_types, object_uuid: log_object_uuids).order("id DESC").limit(limit)
136     log_lines.results.reverse.
137       flat_map { |log| log.properties[:text].split("\n") rescue [] }
138   end
139
140   def render_log
141     collection = Collection.find(log_collection) rescue nil
142     if collection
143       return {log: collection, partial: 'collections/show_files', locals: {object: collection, no_checkboxes: true}}
144     end
145   end
146
147   # End combined propeties
148
149   protected
150   def get_combined key
151     get(key, @container) || get(key, @proxied)
152   end
153 end