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 def merge_range timestamps, started_at, finished_at
26 timestamps.each_index do |i|
28 if started_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
29 # 'j' started and ended during 'i'
33 if started_at < timestamps[i][0] and finished_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
34 # 'j' started before 'i' and finished during 'i'
35 # re-merge range between when 'j' started and 'i' finished
36 finished_at = timestamps[i][1]
37 timestamps.delete_at i
38 return merge_range timestamps, started_at, finished_at
41 if started_at >= timestamps[i][0] and started_at <= timestamps[i][1]
42 # 'j' started during 'i' and finished sometime after
43 # move end time of 'i' back
44 # re-merge range between when 'i' started and 'j' finished
45 started_at = timestamps[i][0]
46 timestamps.delete_at i
47 return merge_range timestamps, started_at, finished_at
50 if finished_at < timestamps[i][0]
51 # 'j' finished before 'i' started, so insert before 'i'
52 timestamps.insert i, [started_at, finished_at]
58 timestamps << [started_at, finished_at]
61 def determine_wallclock_runtime jobs
66 started_at = j[:started_at]
67 finished_at = (if j[:finished_at] then j[:finished_at] else Time.now end)
69 timestamps = merge_range timestamps, started_at, finished_at
72 timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
77 def pipeline_jobs_newschool object
81 jobuuids = object.components.values.map { |c|
82 c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
85 Job.where(uuid: jobuuids).each do |j|
89 object.components.each do |cname, c|
91 pj = {index: i, name: cname}
96 if c[:job] and c[:job][:uuid] and job[c[:job][:uuid]]
97 pj[:job] = job[c[:job][:uuid]]
99 pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
101 pj[:percent_done] = 0
102 pj[:percent_running] = 0
103 if pj[:job][:success]
106 pj[:percent_done] = 100
111 if pj[:job][:tasks_summary]
113 ts = pj[:job][:tasks_summary]
114 denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
115 pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
116 pj[:percent_done] = 100.0 * ts[:done].to_f / denom
117 pj[:percent_running] = 100.0 * ts[:running].to_f / denom
118 pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
121 pj[:percent_done] = 0.0
122 pj[:percent_running] = 100.0
128 if pj[:job][:success]
129 pj[:result] = 'complete'
130 pj[:labeltype] = 'success'
133 elsif pj[:job][:finished_at]
134 pj[:result] = 'failed'
135 pj[:labeltype] = 'danger'
137 elsif pj[:job][:started_at]
138 pj[:result] = 'running'
139 pj[:labeltype] = 'primary'
140 elsif pj[:job][:uuid]
141 pj[:result] = 'queued'
142 pj[:labeltype] = 'default'
145 pj[:labeltype] = 'default'
148 pj[:job_id] = pj[:job][:uuid]
149 pj[:script] = pj[:job][:script] || c[:script]
150 pj[:repository] = pj[:job][:script] || c[:repository]
151 pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
152 pj[:script_version] = pj[:job][:script_version] || c[:script_version]
153 pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
154 pj[:output] = pj[:job][:output]
155 pj[:output_uuid] = c[:output_uuid]
156 pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
162 def pipeline_jobs_oldschool object
164 object.components[:steps].each_with_index do |step, i|
165 pj = {index: i, name: step[:name]}
166 if step[:complete] and step[:complete] != 0
167 if step[:output_data_locator]
173 if step[:progress] and
174 (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
175 pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
180 pj[:result] = 'failed'
184 if step[:warehousejob]
186 pj[:result] = 'complete'
189 elsif step[:warehousejob][:finishtime]
190 pj[:result] = 'failed'
192 elsif step[:warehousejob][:starttime]
193 pj[:result] = 'running'
195 pj[:result] = 'queued'
198 pj[:progress_detail] = (step[:progress] rescue nil)
199 pj[:job_id] = (step[:warehousejob][:id] rescue nil)
200 pj[:job_link] = pj[:job_id]
201 pj[:script] = step[:function]
202 pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
203 pj[:output] = step[:output_data_locator]
204 pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
210 def runtime duration, long
215 hours = (duration / 3600).floor
216 duration -= hours * 3600
219 minutes = (duration / 60).floor
220 duration -= minutes * 60
222 duration = duration.floor
227 s += "#{hours} hour#{'s' if hours != 1} "
230 s += "#{minutes} minute#{'s' if minutes != 1} "
232 s += "#{duration} second#{'s' if duration != 1}"
234 s = "#{hours}:#{minutes.to_s.rjust(2, '0')}:#{duration.to_s.rjust(2, '0')}"