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]
27 def pipeline_jobs_newschool object
31 jobuuids = object.components.values.map { |c|
32 c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
35 Job.where(uuid: jobuuids).each do |j|
39 object.components.each do |cname, c|
41 pj = {index: i, name: cname}
46 if c[:job] and c[:job][:uuid] and job[c[:job][:uuid]]
47 pj[:job] = job[c[:job][:uuid]]
49 pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
52 pj[:percent_running] = 0
56 pj[:percent_done] = 100
61 if pj[:job][:tasks_summary]
63 ts = pj[:job][:tasks_summary]
64 denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
65 pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
66 pj[:percent_done] = 100.0 * ts[:done].to_f / denom
67 pj[:percent_running] = 100.0 * ts[:running].to_f / denom
68 pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
71 pj[:percent_done] = 0.0
72 pj[:percent_running] = 100.0
79 pj[:result] = 'complete'
80 pj[:labeltype] = 'success'
83 elsif pj[:job][:finished_at]
84 pj[:result] = 'failed'
85 pj[:labeltype] = 'danger'
87 elsif pj[:job][:started_at]
88 pj[:result] = 'running'
89 pj[:labeltype] = 'primary'
91 pj[:result] = 'queued'
92 pj[:labeltype] = 'default'
95 pj[:labeltype] = 'default'
98 pj[:job_id] = pj[:job][:uuid]
99 pj[:script] = pj[:job][:script] || c[:script]
100 pj[:repository] = pj[:job][:script] || c[:repository]
101 pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
102 pj[:script_version] = pj[:job][:script_version] || c[:script_version]
103 pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
104 pj[:output] = pj[:job][:output]
105 pj[:output_uuid] = c[:output_uuid]
106 pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
112 def pipeline_jobs_oldschool object
114 object.components[:steps].each_with_index do |step, i|
115 pj = {index: i, name: step[:name]}
116 if step[:complete] and step[:complete] != 0
117 if step[:output_data_locator]
123 if step[:progress] and
124 (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
125 pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
130 pj[:result] = 'failed'
134 if step[:warehousejob]
136 pj[:result] = 'complete'
139 elsif step[:warehousejob][:finishtime]
140 pj[:result] = 'failed'
142 elsif step[:warehousejob][:starttime]
143 pj[:result] = 'running'
145 pj[:result] = 'queued'
148 pj[:progress_detail] = (step[:progress] rescue nil)
149 pj[:job_id] = (step[:warehousejob][:id] rescue nil)
150 pj[:job_link] = pj[:job_id]
151 pj[:script] = step[:function]
152 pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
153 pj[:output] = step[:output_data_locator]
154 pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
160 def runtime duration, long
165 hours = (duration / 3600).floor
166 duration -= hours * 3600
169 minutes = (duration / 60).floor
170 duration -= minutes * 60
172 duration = duration.floor
177 s += "#{hours} hour#{'s' if hours != 1} "
180 s += "#{minutes} minute#{'s' if minutes != 1} "
182 s += "#{duration} second#{'s' if duration != 1}"
184 s = "#{hours}:#{minutes.to_s.rjust(2, '0')}:#{duration.to_s.rjust(2, '0')}"
189 def determine_wallclock_runtime jobs
193 timestamps.each_index do |i|
194 if started_at = j[:started_at]
195 finished_at = (if j[:finished_at] then j[:finished_at] else Time.now end)
197 if started_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
198 # 'j' started and ended during 'i'
203 if started_at < timestamps[i][0] and finished_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
204 # 'j' started before 'i' and ended during 'i'
205 # move start time of 'i' back
206 timestamps[i][0] = started_at
211 if started_at >= timestamps[i][0] and started_at <= timestamps[i][1]
212 # 'j' started during 'i'
213 # move end time of 'i' back
214 timestamps[i][1] = finished_at
219 if finished_at < timestamps[i][0]
220 # 'j' finished before 'i' started, so insert before 'i'
225 if started_at > timestamps[i][1]
226 # 'j' started after 'i' finished, so insert after 'i'
234 timestamps.insert insert_at, [started_at, finished_at]
237 timestamps.map { |t| t[1] - t[0] }.reduce(:+)