Merge branch '10484-count-clients' closes #10484
[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] if pj[:job]
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       started_at = (j.started_at if j.respond_to?(:started_at)) || (j[:started_at] if j.is_a?(Hash))
73       finished_at = (j.finished_at if j.respond_to?(:finished_at)) || (j[:finished_at] if j.is_a?(Hash)) || Time.now
74       if started_at
75         timestamps = merge_range timestamps, started_at, finished_at
76       end
77     end
78     timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
79   end
80
81   protected
82
83   def pipeline_jobs_newschool object
84     ret = []
85     i = -1
86
87     jobuuids = object.components.values.map { |c|
88       c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
89     }.compact
90     job = {}
91     Job.where(uuid: jobuuids).each do |j|
92       job[j[:uuid]] = j
93     end
94
95     object.components.each do |cname, c|
96       i += 1
97       pj = {index: i, name: cname}
98       if not c.is_a?(Hash)
99         ret << pj
100         next
101       end
102       if c[:job] and c[:job][:uuid] and job[c[:job][:uuid]]
103         pj[:job] = job[c[:job][:uuid]]
104       elsif c[:job].is_a?(Hash)
105         pj[:job] = c[:job]
106         if pj[:job][:started_at].is_a? String
107           pj[:job][:started_at] = Time.parse(pj[:job][:started_at])
108         end
109         if pj[:job][:finished_at].is_a? String
110           pj[:job][:finished_at] = Time.parse(pj[:job][:finished_at])
111         end
112         # If necessary, figure out the state based on the other fields.
113         pj[:job][:state] ||= if pj[:job][:cancelled_at]
114                                "Cancelled"
115                              elsif pj[:job][:success] == false
116                                "Failed"
117                              elsif pj[:job][:success] == true
118                                "Complete"
119                              elsif pj[:job][:running] == true
120                                "Running"
121                              else
122                                "Queued"
123                              end
124       else
125         pj[:job] = {}
126       end
127       pj[:percent_done] = 0
128       pj[:percent_running] = 0
129       if pj[:job][:success]
130         if pj[:job][:output]
131           pj[:progress] = 1.0
132           pj[:percent_done] = 100
133         else
134           pj[:progress] = 0.0
135         end
136       else
137         if pj[:job][:tasks_summary]
138           begin
139             ts = pj[:job][:tasks_summary]
140             denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
141             pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
142             pj[:percent_done] = 100.0 * ts[:done].to_f / denom
143             pj[:percent_running] = 100.0 * ts[:running].to_f / denom
144             pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
145           rescue
146             pj[:progress] = 0.5
147             pj[:percent_done] = 0.0
148             pj[:percent_running] = 100.0
149           end
150         else
151           pj[:progress] = 0.0
152         end
153       end
154
155       case pj[:job][:state]
156         when 'Complete'
157         pj[:result] = 'complete'
158         pj[:labeltype] = 'success'
159         pj[:complete] = true
160         pj[:progress] = 1.0
161       when 'Failed'
162         pj[:result] = 'failed'
163         pj[:labeltype] = 'danger'
164         pj[:failed] = true
165       when 'Cancelled'
166         pj[:result] = 'cancelled'
167         pj[:labeltype] = 'danger'
168         pj[:failed] = true
169       when 'Running'
170         pj[:result] = 'running'
171         pj[:labeltype] = 'primary'
172       when 'Queued'
173         pj[:result] = 'queued'
174         pj[:labeltype] = 'default'
175       else
176         pj[:result] = 'none'
177         pj[:labeltype] = 'default'
178       end
179
180       pj[:job_id] = pj[:job][:uuid]
181       pj[:script] = pj[:job][:script] || c[:script]
182       pj[:repository] = pj[:job][:script] || c[:repository]
183       pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
184       pj[:script_version] = pj[:job][:script_version] || c[:script_version]
185       pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
186       pj[:output] = pj[:job][:output]
187       pj[:output_uuid] = c[:output_uuid]
188       pj[:finished_at] = pj[:job][:finished_at]
189       ret << pj
190     end
191     ret
192   end
193
194   def pipeline_jobs_oldschool object
195     ret = []
196     object.components[:steps].each_with_index do |step, i|
197       pj = {index: i, name: step[:name]}
198       if step[:complete] and step[:complete] != 0
199         if step[:output_data_locator]
200           pj[:progress] = 1.0
201         else
202           pj[:progress] = 0.0
203         end
204       else
205         if step[:progress] and
206             (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
207           pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
208         else
209           pj[:progress] = 0.0
210         end
211         if step[:failed]
212           pj[:result] = 'failed'
213           pj[:failed] = true
214         end
215       end
216       if step[:warehousejob]
217         if step[:complete]
218           pj[:result] = 'complete'
219           pj[:complete] = true
220           pj[:progress] = 1.0
221         elsif step[:warehousejob][:finishtime]
222           pj[:result] = 'failed'
223           pj[:failed] = true
224         elsif step[:warehousejob][:starttime]
225           pj[:result] = 'running'
226         else
227           pj[:result] = 'queued'
228         end
229       end
230       pj[:progress_detail] = (step[:progress] rescue nil)
231       pj[:job_id] = (step[:warehousejob][:id] rescue nil)
232       pj[:job_link] = pj[:job_id]
233       pj[:script] = step[:function]
234       pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
235       pj[:output] = step[:output_data_locator]
236       pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
237       ret << pj
238     end
239     ret
240   end
241
242   MINUTE = 60
243   HOUR = 60 * MINUTE
244   DAY = 24 * HOUR
245
246   def render_runtime duration, use_words, round_to_min=true
247     days = 0
248     hours = 0
249     minutes = 0
250     seconds = 0
251
252     if duration >= DAY
253       days = (duration / DAY).floor
254       duration -= days * DAY
255     end
256
257     if duration >= HOUR
258       hours = (duration / HOUR).floor
259       duration -= hours * HOUR
260     end
261
262     if duration >= MINUTE
263       minutes = (duration / MINUTE).floor
264       duration -= minutes * MINUTE
265     end
266
267     seconds = duration.floor
268
269     if round_to_min and seconds >= 30
270       minutes += 1
271     end
272
273     if use_words
274       s = []
275       if days > 0 then
276         s << "#{days} day#{'s' if days != 1}"
277       end
278       if hours > 0 then
279         s << "#{hours} hour#{'s' if hours != 1}"
280       end
281       if minutes > 0 then
282         s << "#{minutes} minute#{'s' if minutes != 1}"
283       end
284       if not round_to_min or s.size == 0
285         s << "#{seconds} second#{'s' if seconds != 1}"
286       end
287       s = s * " "
288     else
289       s = ""
290       if days > 0
291         s += "#{days}<span class='time-label-divider'>d</span>"
292       end
293
294       if (hours > 0)
295         s += "#{hours}<span class='time-label-divider'>h</span>"
296       end
297
298       s += "#{minutes}<span class='time-label-divider'>m</span>"
299
300       if not round_to_min or (days == 0 and hours == 0 and minutes == 0)
301         s += "#{seconds}<span class='time-label-divider'>s</span>"
302       end
303     end
304
305     raw(s)
306   end
307
308   def render_unreadable_inputs_present
309     if current_user and controller.class.name.eql?('PipelineInstancesController') and unreadable_inputs_present?
310       raw('<div class="alert alert-danger unreadable-inputs-present">' +
311             '<p>One or more inputs provided are not readable by you. ' +
312               'Please correct these before you can run the pipeline.</p></div>')
313     end
314   end
315 end