Merge branch '3899-crunch-use-job-state' closes #3899
[arvados.git] / apps / workbench / app / helpers / pipeline_instances_helper.rb
1 module PipelineInstancesHelper
2
3   def pipeline_jobs object=nil
4     object ||= @object
5     if object.components[:steps].is_a? Array
6       pipeline_jobs_oldschool object
7     elsif object.components.is_a? Hash
8       pipeline_jobs_newschool object
9     end
10   end
11
12   def render_pipeline_jobs
13     pipeline_jobs.collect do |pj|
14       render_pipeline_job pj
15     end
16   end
17
18   def render_pipeline_job pj
19     pj[:progress_bar] = render partial: 'job_progress', locals: {:j => pj[:job]}
20     pj[:output_link] = link_to_if_arvados_object pj[:output]
21     pj[:job_link] = link_to_if_arvados_object pj[:job][:uuid]
22     pj
23   end
24
25   # Merge (started_at, finished_at) time range into the list of time ranges in
26   # timestamps (timestamps must be sorted and non-overlapping).  
27   # return the updated timestamps list.
28   def merge_range timestamps, started_at, finished_at
29     # in the comments below, 'i' is the entry in the timestamps array and 'j'
30     # is the started_at, finished_at range which is passed in.
31     timestamps.each_index do |i|
32       if started_at
33         if started_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
34           # 'j' started and ended during 'i'
35           return timestamps
36         end
37
38         if started_at < timestamps[i][0] and finished_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
39           # 'j' started before 'i' and finished during 'i'
40           # re-merge range between when 'j' started and 'i' finished
41           finished_at = timestamps[i][1]
42           timestamps.delete_at i
43           return merge_range timestamps, started_at, finished_at
44         end
45
46         if started_at >= timestamps[i][0] and started_at <= timestamps[i][1]
47           # 'j' started during 'i' and finished sometime after
48           # move end time of 'i' back
49           # re-merge range between when 'i' started and 'j' finished
50           started_at = timestamps[i][0]
51           timestamps.delete_at i
52           return merge_range timestamps, started_at, finished_at
53         end
54
55         if finished_at < timestamps[i][0]
56           # 'j' finished before 'i' started, so insert before 'i'
57           timestamps.insert i, [started_at, finished_at]
58           return timestamps
59         end
60       end
61     end
62
63     timestamps << [started_at, finished_at]
64   end
65   
66   # Accept a list of objects with [:started_at] and [:finshed_at] keys and
67   # merge overlapping ranges to compute the time spent running after periods of
68   # overlapping execution are factored out.
69   def determine_wallclock_runtime jobs
70     timestamps = []
71     jobs.each do |j|
72       insert_at = 0
73       started_at = j[:started_at]
74       finished_at = (if j[:finished_at] then j[:finished_at] else Time.now end)
75       if started_at
76         timestamps = merge_range timestamps, started_at, finished_at
77       end
78     end
79     timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
80   end
81
82   protected
83
84   def pipeline_jobs_newschool object
85     ret = []
86     i = -1
87
88     jobuuids = object.components.values.map { |c|
89       c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
90     }.compact
91     job = {}
92     Job.where(uuid: jobuuids).each do |j|
93       job[j[:uuid]] = j
94     end
95
96     object.components.each do |cname, c|
97       i += 1
98       pj = {index: i, name: cname}
99       if not c.is_a?(Hash)
100         ret << pj
101         next
102       end
103       if c[:job] and c[:job][:uuid] and job[c[:job][:uuid]]
104         pj[:job] = job[c[:job][:uuid]]
105       else
106         pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
107
108         # If necessary, figure out the state based on the other fields.
109         pj[:job][:state] ||= if pj[:job][:cancelled_at]
110                                "Cancelled"
111                              elsif pj[:job][:success] == false
112                                "Failed"
113                              elsif pj[:job][:success] == true
114                                "Complete"
115                              elsif pj[:job][:running] == true
116                                "Running"
117                              else
118                                "Queued"
119                              end
120       end
121       pj[:percent_done] = 0
122       pj[:percent_running] = 0
123       if pj[:job][:success]
124         if pj[:job][:output]
125           pj[:progress] = 1.0
126           pj[:percent_done] = 100
127         else
128           pj[:progress] = 0.0
129         end
130       else
131         if pj[:job][:tasks_summary]
132           begin
133             ts = pj[:job][:tasks_summary]
134             denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
135             pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
136             pj[:percent_done] = 100.0 * ts[:done].to_f / denom
137             pj[:percent_running] = 100.0 * ts[:running].to_f / denom
138             pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
139           rescue
140             pj[:progress] = 0.5
141             pj[:percent_done] = 0.0
142             pj[:percent_running] = 100.0
143           end
144         else
145           pj[:progress] = 0.0
146         end
147       end
148
149       case pj[:job][:state]
150         when 'Complete'
151         pj[:result] = 'complete'
152         pj[:labeltype] = 'success'
153         pj[:complete] = true
154         pj[:progress] = 1.0
155       when 'Failed'
156         pj[:result] = 'failed'
157         pj[:labeltype] = 'danger'
158         pj[:failed] = true
159       when 'Cancelled'
160         pj[:result] = 'cancelled'
161         pj[:labeltype] = 'danger'
162         pj[:failed] = true
163       when 'Running'
164         pj[:result] = 'running'
165         pj[:labeltype] = 'primary'
166       when 'Queued'
167         pj[:result] = 'queued'
168         pj[:labeltype] = 'default'
169       else
170         pj[:result] = 'none'
171         pj[:labeltype] = 'default'
172       end
173
174       pj[:job_id] = pj[:job][:uuid]
175       pj[:script] = pj[:job][:script] || c[:script]
176       pj[:repository] = pj[:job][:script] || c[:repository]
177       pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
178       pj[:script_version] = pj[:job][:script_version] || c[:script_version]
179       pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
180       pj[:output] = pj[:job][:output]
181       pj[:output_uuid] = c[:output_uuid]
182       pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
183       ret << pj
184     end
185     ret
186   end
187
188   def pipeline_jobs_oldschool object
189     ret = []
190     object.components[:steps].each_with_index do |step, i|
191       pj = {index: i, name: step[:name]}
192       if step[:complete] and step[:complete] != 0
193         if step[:output_data_locator]
194           pj[:progress] = 1.0
195         else
196           pj[:progress] = 0.0
197         end
198       else
199         if step[:progress] and
200             (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
201           pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
202         else
203           pj[:progress] = 0.0
204         end
205         if step[:failed]
206           pj[:result] = 'failed'
207           pj[:failed] = true
208         end
209       end
210       if step[:warehousejob]
211         if step[:complete]
212           pj[:result] = 'complete'
213           pj[:complete] = true
214           pj[:progress] = 1.0
215         elsif step[:warehousejob][:finishtime]
216           pj[:result] = 'failed'
217           pj[:failed] = true
218         elsif step[:warehousejob][:starttime]
219           pj[:result] = 'running'
220         else
221           pj[:result] = 'queued'
222         end
223       end
224       pj[:progress_detail] = (step[:progress] rescue nil)
225       pj[:job_id] = (step[:warehousejob][:id] rescue nil)
226       pj[:job_link] = pj[:job_id]
227       pj[:script] = step[:function]
228       pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
229       pj[:output] = step[:output_data_locator]
230       pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
231       ret << pj
232     end
233     ret
234   end
235
236   MINUTE = 60
237   HOUR = 60 * MINUTE
238   DAY = 24 * HOUR
239
240   def render_runtime duration, use_words, round_to_min=true
241     days = 0
242     hours = 0
243     minutes = 0
244     seconds = 0
245
246     if duration >= DAY
247       days = (duration / DAY).floor
248       duration -= days * DAY
249     end
250
251     if duration >= HOUR
252       hours = (duration / HOUR).floor
253       duration -= hours * HOUR
254     end
255
256     if duration >= MINUTE
257       minutes = (duration / MINUTE).floor
258       duration -= minutes * MINUTE
259     end
260
261     seconds = duration.floor
262
263     if round_to_min and seconds >= 30
264       minutes += 1
265     end    
266
267     if use_words
268       s = []
269       if days > 0 then
270         s << "#{days} day#{'s' if days != 1}"
271       end
272       if hours > 0 then
273         s << "#{hours} hour#{'s' if hours != 1}"
274       end
275       if minutes > 0 then
276         s << "#{minutes} minute#{'s' if minutes != 1}"
277       end
278       if not round_to_min or s.size == 0
279         s << "#{seconds} second#{'s' if seconds != 1}"
280       end
281       s = s * " "
282     else
283       s = ""
284       if days > 0
285         s += "#{days}<span class='time-label-divider'>d</span> "
286       end
287
288       if (hours > 0)
289         s += "#{hours}<span class='time-label-divider'>h</span>"
290       end
291
292       s += "#{minutes}<span class='time-label-divider'>m</span>"
293
294       if not round_to_min
295         s += "#{seconds}<span class='time-label-divider'>s</span>"
296       end
297     end
298
299     raw(s)
300   end
301
302 end