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