1 module PipelineInstancesHelper
3 def pipeline_jobs object=nil
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
12 def render_pipeline_jobs
13 pipeline_jobs.collect do |pj|
14 render_pipeline_job pj
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]
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|
33 if started_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
34 # 'j' started and ended during 'i'
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
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
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]
63 timestamps << [started_at, finished_at]
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
73 started_at = j[:started_at]
74 finished_at = (if j[:finished_at] then j[:finished_at] else Time.now end)
76 timestamps = merge_range timestamps, started_at, finished_at
79 timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
84 def pipeline_jobs_newschool object
88 jobuuids = object.components.values.map { |c|
89 c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
92 Job.where(uuid: jobuuids).each do |j|
96 object.components.each do |cname, c|
98 pj = {index: i, name: cname}
103 if c[:job] and c[:job][:uuid] and job[c[:job][:uuid]]
104 pj[:job] = job[c[:job][:uuid]]
106 pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
108 pj[:percent_done] = 0
109 pj[:percent_running] = 0
110 if pj[:job][:success]
113 pj[:percent_done] = 100
118 if pj[:job][:tasks_summary]
120 ts = pj[:job][:tasks_summary]
121 denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
122 pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
123 pj[:percent_done] = 100.0 * ts[:done].to_f / denom
124 pj[:percent_running] = 100.0 * ts[:running].to_f / denom
125 pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
128 pj[:percent_done] = 0.0
129 pj[:percent_running] = 100.0
135 if pj[:job][:success]
136 pj[:result] = 'complete'
137 pj[:labeltype] = 'success'
140 elsif pj[:job][:finished_at]
141 pj[:result] = 'failed'
142 pj[:labeltype] = 'danger'
144 elsif pj[:job][:started_at]
145 pj[:result] = 'running'
146 pj[:labeltype] = 'primary'
147 elsif pj[:job][:uuid]
148 pj[:result] = 'queued'
149 pj[:labeltype] = 'default'
152 pj[:labeltype] = 'default'
155 pj[:job_id] = pj[:job][:uuid]
156 pj[:script] = pj[:job][:script] || c[:script]
157 pj[:repository] = pj[:job][:script] || c[:repository]
158 pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
159 pj[:script_version] = pj[:job][:script_version] || c[:script_version]
160 pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
161 pj[:output] = pj[:job][:output]
162 pj[:output_uuid] = c[:output_uuid]
163 pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
169 def pipeline_jobs_oldschool object
171 object.components[:steps].each_with_index do |step, i|
172 pj = {index: i, name: step[:name]}
173 if step[:complete] and step[:complete] != 0
174 if step[:output_data_locator]
180 if step[:progress] and
181 (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
182 pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
187 pj[:result] = 'failed'
191 if step[:warehousejob]
193 pj[:result] = 'complete'
196 elsif step[:warehousejob][:finishtime]
197 pj[:result] = 'failed'
199 elsif step[:warehousejob][:starttime]
200 pj[:result] = 'running'
202 pj[:result] = 'queued'
205 pj[:progress_detail] = (step[:progress] rescue nil)
206 pj[:job_id] = (step[:warehousejob][:id] rescue nil)
207 pj[:job_link] = pj[:job_id]
208 pj[:script] = step[:function]
209 pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
210 pj[:output] = step[:output_data_locator]
211 pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
221 def render_runtime duration, use_words, round_to_min=true
228 days = (duration / DAY).floor
229 duration -= days * DAY
233 hours = (duration / HOUR).floor
234 duration -= hours * HOUR
237 if duration >= MINUTE
238 minutes = (duration / MINUTE).floor
239 duration -= minutes * MINUTE
242 seconds = duration.floor
244 if round_to_min and seconds >= 30
251 s << "#{days} day#{'s' if days != 1}"
254 s << "#{hours} hour#{'s' if hours != 1}"
257 s << "#{minutes} minute#{'s' if minutes != 1}"
259 if not round_to_min or s.size == 0
260 s << "#{seconds} second#{'s' if seconds != 1}"
266 s += "#{days}<span class='time-label-divider'>d</span> "
270 s += "#{hours}<span class='time-label-divider'>h</span>"
273 s += "#{minutes}<span class='time-label-divider'>m</span>"
276 s += "#{seconds}<span class='time-label-divider'>s</span>"