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
65 started_at = j[:started_at]
66 finished_at = (if j[:finished_at] then j[:finished_at] else Time.now end)
68 timestamps = merge_range timestamps, started_at, finished_at
71 timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
76 def pipeline_jobs_newschool object
80 jobuuids = object.components.values.map { |c|
81 c[:job][:uuid] if c.is_a?(Hash) and c[:job].is_a?(Hash)
84 Job.where(uuid: jobuuids).each do |j|
88 object.components.each do |cname, c|
90 pj = {index: i, name: cname}
95 if c[:job] and c[:job][:uuid] and job[c[:job][:uuid]]
96 pj[:job] = job[c[:job][:uuid]]
98 pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
100 pj[:percent_done] = 0
101 pj[:percent_running] = 0
102 if pj[:job][:success]
105 pj[:percent_done] = 100
110 if pj[:job][:tasks_summary]
112 ts = pj[:job][:tasks_summary]
113 denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
114 pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
115 pj[:percent_done] = 100.0 * ts[:done].to_f / denom
116 pj[:percent_running] = 100.0 * ts[:running].to_f / denom
117 pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
120 pj[:percent_done] = 0.0
121 pj[:percent_running] = 100.0
127 if pj[:job][:success]
128 pj[:result] = 'complete'
129 pj[:labeltype] = 'success'
132 elsif pj[:job][:finished_at]
133 pj[:result] = 'failed'
134 pj[:labeltype] = 'danger'
136 elsif pj[:job][:started_at]
137 pj[:result] = 'running'
138 pj[:labeltype] = 'primary'
139 elsif pj[:job][:uuid]
140 pj[:result] = 'queued'
141 pj[:labeltype] = 'default'
144 pj[:labeltype] = 'default'
147 pj[:job_id] = pj[:job][:uuid]
148 pj[:script] = pj[:job][:script] || c[:script]
149 pj[:repository] = pj[:job][:script] || c[:repository]
150 pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
151 pj[:script_version] = pj[:job][:script_version] || c[:script_version]
152 pj[:nondeterministic] = pj[:job][:nondeterministic] || c[:nondeterministic]
153 pj[:output] = pj[:job][:output]
154 pj[:output_uuid] = c[:output_uuid]
155 pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
161 def pipeline_jobs_oldschool object
163 object.components[:steps].each_with_index do |step, i|
164 pj = {index: i, name: step[:name]}
165 if step[:complete] and step[:complete] != 0
166 if step[:output_data_locator]
172 if step[:progress] and
173 (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
174 pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
179 pj[:result] = 'failed'
183 if step[:warehousejob]
185 pj[:result] = 'complete'
188 elsif step[:warehousejob][:finishtime]
189 pj[:result] = 'failed'
191 elsif step[:warehousejob][:starttime]
192 pj[:result] = 'running'
194 pj[:result] = 'queued'
197 pj[:progress_detail] = (step[:progress] rescue nil)
198 pj[:job_id] = (step[:warehousejob][:id] rescue nil)
199 pj[:job_link] = pj[:job_id]
200 pj[:script] = step[:function]
201 pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
202 pj[:output] = step[:output_data_locator]
203 pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
209 def runtime duration, long
214 hours = (duration / 3600).floor
215 duration -= hours * 3600
218 minutes = (duration / 60).floor
219 duration -= minutes * 60
221 duration = duration.floor
226 s += "#{hours} hour#{'s' if hours != 1} "
229 s += "#{minutes} minute#{'s' if minutes != 1} "
231 s += "#{duration} second#{'s' if duration != 1}"
233 s = "#{hours}:#{minutes.to_s.rjust(2, '0')}:#{duration.to_s.rjust(2, '0')}"