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