1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 module PipelineInstancesHelper
7 def pipeline_jobs object=nil
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
16 def render_pipeline_jobs
17 pipeline_jobs.collect do |pj|
18 render_pipeline_job pj
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]
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|
37 if started_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
38 # 'j' started and ended during 'i'
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
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
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]
67 timestamps << [started_at, finished_at]
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
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
79 timestamps = merge_range timestamps, started_at, finished_at
82 timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
87 def pipeline_jobs_newschool object
91 jobuuids = object.components.values.map { |c|
92 c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
95 Job.where(uuid: jobuuids).with_count("none").each do |j|
99 object.components.each do |cname, c|
101 pj = {index: i, name: cname}
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)
110 if pj[:job][:started_at].is_a? String
111 pj[:job][:started_at] = Time.parse(pj[:job][:started_at])
113 if pj[:job][:finished_at].is_a? String
114 pj[:job][:finished_at] = Time.parse(pj[:job][:finished_at])
116 # If necessary, figure out the state based on the other fields.
117 pj[:job][:state] ||= if pj[:job][:cancelled_at]
119 elsif pj[:job][:success] == false
121 elsif pj[:job][:success] == true
123 elsif pj[:job][:running] == true
131 pj[:percent_done] = 0
132 pj[:percent_running] = 0
133 if pj[:job][:success]
136 pj[:percent_done] = 100
141 if pj[:job][:tasks_summary]
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"
151 pj[:percent_done] = 0.0
152 pj[:percent_running] = 100.0
159 case pj[:job][:state]
161 pj[:result] = 'complete'
162 pj[:labeltype] = 'success'
166 pj[:result] = 'failed'
167 pj[:labeltype] = 'danger'
170 pj[:result] = 'cancelled'
171 pj[:labeltype] = 'danger'
174 pj[:result] = 'running'
175 pj[:labeltype] = 'primary'
177 pj[:result] = 'queued'
178 pj[:labeltype] = 'default'
181 pj[:labeltype] = 'default'
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]
198 def pipeline_jobs_oldschool object
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]
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)
216 pj[:result] = 'failed'
220 if step[:warehousejob]
222 pj[:result] = 'complete'
225 elsif step[:warehousejob][:finishtime]
226 pj[:result] = 'failed'
228 elsif step[:warehousejob][:starttime]
229 pj[:result] = 'running'
231 pj[:result] = 'queued'
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)
250 def render_runtime duration, use_words, round_to_min=true
257 days = (duration / DAY).floor
258 duration -= days * DAY
262 hours = (duration / HOUR).floor
263 duration -= hours * HOUR
266 if duration >= MINUTE
267 minutes = (duration / MINUTE).floor
268 duration -= minutes * MINUTE
271 seconds = duration.floor
273 if round_to_min and seconds >= 30
280 s << "#{days} day#{'s' if days != 1}"
283 s << "#{hours} hour#{'s' if hours != 1}"
286 s << "#{minutes} minute#{'s' if minutes != 1}"
288 if not round_to_min or s.size == 0
289 s << "#{seconds} second#{'s' if seconds != 1}"
295 s += "#{days}<span class='time-label-divider'>d</span>"
299 s += "#{hours}<span class='time-label-divider'>h</span>"
302 s += "#{minutes}<span class='time-label-divider'>m</span>"
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>"
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>')