Merge branch 'master' into 3901-component-rendering-errors
[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       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       else
114         pj[:job] = {}
115       end
116       pj[:percent_done] = 0
117       pj[:percent_running] = 0
118       if pj[:job][:success]
119         if pj[:job][:output]
120           pj[:progress] = 1.0
121           pj[:percent_done] = 100
122         else
123           pj[:progress] = 0.0
124         end
125       else
126         if pj[:job][:tasks_summary]
127           begin
128             ts = pj[:job][:tasks_summary]
129             denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
130             pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
131             pj[:percent_done] = 100.0 * ts[:done].to_f / denom
132             pj[:percent_running] = 100.0 * ts[:running].to_f / denom
133             pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
134           rescue
135             pj[:progress] = 0.5
136             pj[:percent_done] = 0.0
137             pj[:percent_running] = 100.0
138           end
139         else
140           pj[:progress] = 0.0
141         end
142       end
143       if pj[:job][:success]
144         pj[:result] = 'complete'
145         pj[:labeltype] = 'success'
146         pj[:complete] = true
147         pj[:progress] = 1.0
148       elsif pj[:job][:finished_at]
149         pj[:result] = 'failed'
150         pj[:labeltype] = 'danger'
151         pj[:failed] = true
152       elsif pj[:job][:started_at]
153         pj[:result] = 'running'
154         pj[:labeltype] = 'primary'
155       elsif pj[:job][:uuid]
156         pj[:result] = 'queued'
157         pj[:labeltype] = 'default'
158       else
159         pj[:result] = 'none'
160         pj[:labeltype] = 'default'
161       end
162
163       pj[:job_id] = pj[:job][:uuid]
164       pj[:script] = pj[:job][:script] || c[:script]
165       pj[:repository] = pj[:job][:script] || c[:repository]
166       pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
167       pj[:script_version] = pj[:job][:script_version] || c[:script_version]
168       pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
169       pj[:output] = pj[:job][:output]
170       pj[:output_uuid] = c[:output_uuid]
171       pj[:finished_at] = pj[:job][:finished_at]
172       ret << pj
173     end
174     ret
175   end
176
177   def pipeline_jobs_oldschool object
178     ret = []
179     object.components[:steps].each_with_index do |step, i|
180       pj = {index: i, name: step[:name]}
181       if step[:complete] and step[:complete] != 0
182         if step[:output_data_locator]
183           pj[:progress] = 1.0
184         else
185           pj[:progress] = 0.0
186         end
187       else
188         if step[:progress] and
189             (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
190           pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
191         else
192           pj[:progress] = 0.0
193         end
194         if step[:failed]
195           pj[:result] = 'failed'
196           pj[:failed] = true
197         end
198       end
199       if step[:warehousejob]
200         if step[:complete]
201           pj[:result] = 'complete'
202           pj[:complete] = true
203           pj[:progress] = 1.0
204         elsif step[:warehousejob][:finishtime]
205           pj[:result] = 'failed'
206           pj[:failed] = true
207         elsif step[:warehousejob][:starttime]
208           pj[:result] = 'running'
209         else
210           pj[:result] = 'queued'
211         end
212       end
213       pj[:progress_detail] = (step[:progress] rescue nil)
214       pj[:job_id] = (step[:warehousejob][:id] rescue nil)
215       pj[:job_link] = pj[:job_id]
216       pj[:script] = step[:function]
217       pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
218       pj[:output] = step[:output_data_locator]
219       pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
220       ret << pj
221     end
222     ret
223   end
224
225   MINUTE = 60
226   HOUR = 60 * MINUTE
227   DAY = 24 * HOUR
228
229   def render_runtime duration, use_words, round_to_min=true
230     days = 0
231     hours = 0
232     minutes = 0
233     seconds = 0
234
235     if duration >= DAY
236       days = (duration / DAY).floor
237       duration -= days * DAY
238     end
239
240     if duration >= HOUR
241       hours = (duration / HOUR).floor
242       duration -= hours * HOUR
243     end
244
245     if duration >= MINUTE
246       minutes = (duration / MINUTE).floor
247       duration -= minutes * MINUTE
248     end
249
250     seconds = duration.floor
251
252     if round_to_min and seconds >= 30
253       minutes += 1
254     end    
255
256     if use_words
257       s = []
258       if days > 0 then
259         s << "#{days} day#{'s' if days != 1}"
260       end
261       if hours > 0 then
262         s << "#{hours} hour#{'s' if hours != 1}"
263       end
264       if minutes > 0 then
265         s << "#{minutes} minute#{'s' if minutes != 1}"
266       end
267       if not round_to_min or s.size == 0
268         s << "#{seconds} second#{'s' if seconds != 1}"
269       end
270       s = s * " "
271     else
272       s = ""
273       if days > 0
274         s += "#{days}<span class='time-label-divider'>d</span> "
275       end
276
277       if (hours > 0)
278         s += "#{hours}<span class='time-label-divider'>h</span>"
279       end
280
281       s += "#{minutes}<span class='time-label-divider'>m</span>"
282
283       if not round_to_min
284         s += "#{seconds}<span class='time-label-divider'>s</span>"
285       end
286     end
287
288     raw(s)
289   end
290
291 end