Merge branch 'master' into 2756-eventbus-in-workbench
[arvados.git] / apps / workbench / app / helpers / pipeline_instances_helper.rb
1 module PipelineInstancesHelper
2
3   def pipeline_jobs object=nil
4     object ||= @object
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
9     end
10   end
11
12   def render_pipeline_jobs
13     pipeline_jobs.collect do |pj|
14       render_pipeline_job pj
15     end
16   end
17
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]
22     pj
23   end
24
25   def pipieline_log_history(job_uuids)
26     results = []
27
28     log_history = Log.where(event_type: 'transient-log-entry',
29                             object_uuid: job_uuids).order('id DESC').limit(5).all
30     if !log_history.results.empty?
31       results = log_history.results.reverse
32     end
33
34     return results
35   end
36
37   protected
38
39   def pipeline_jobs_newschool object
40     ret = []
41     i = -1
42
43     object.components.each do |cname, c|
44       i += 1
45       pj = {index: i, name: cname}
46       pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
47       pj[:percent_done] = 0
48       pj[:percent_running] = 0
49       if pj[:job][:success]
50         if pj[:job][:output]
51           pj[:progress] = 1.0
52           pj[:percent_done] = 100
53         else
54           pj[:progress] = 0.0
55         end
56       else
57         if pj[:job][:tasks_summary]
58           begin
59             ts = pj[:job][:tasks_summary]
60             denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
61             pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
62             pj[:percent_done] = 100.0 * ts[:done].to_f / denom
63             pj[:percent_running] = 100.0 * ts[:running].to_f / denom
64             pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
65           rescue
66             pj[:progress] = 0.5
67             pj[:percent_done] = 0.0
68             pj[:percent_running] = 100.0
69           end
70         else
71           pj[:progress] = 0.0
72         end
73       end
74       if pj[:job][:success]
75         pj[:result] = 'complete'
76         pj[:complete] = true
77         pj[:progress] = 1.0
78       elsif pj[:job][:finished_at]
79         pj[:result] = 'failed'
80         pj[:failed] = true
81       elsif pj[:job][:started_at]
82         pj[:result] = 'running'
83       elsif pj[:job][:uuid]
84         pj[:result] = 'queued'
85       else
86         pj[:result] = 'none'
87       end
88       pj[:job_id] = pj[:job][:uuid]
89       pj[:script] = pj[:job][:script] || c[:script]
90       pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
91       pj[:script_version] = pj[:job][:script_version] || c[:script_version]
92       pj[:output] = pj[:job][:output]
93       pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
94       ret << pj
95     end
96     ret
97   end
98
99   def pipeline_jobs_oldschool object
100     ret = []
101     object.components[:steps].each_with_index do |step, i|
102       pj = {index: i, name: step[:name]}
103       if step[:complete] and step[:complete] != 0
104         if step[:output_data_locator]
105           pj[:progress] = 1.0
106         else
107           pj[:progress] = 0.0
108         end
109       else
110         if step[:progress] and
111             (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
112           pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
113         else
114           pj[:progress] = 0.0
115         end
116         if step[:failed]
117           pj[:result] = 'failed'
118           pj[:failed] = true
119         end
120       end
121       if step[:warehousejob]
122         if step[:complete]
123           pj[:result] = 'complete'
124           pj[:complete] = true
125           pj[:progress] = 1.0
126         elsif step[:warehousejob][:finishtime]
127           pj[:result] = 'failed'
128           pj[:failed] = true
129         elsif step[:warehousejob][:starttime]
130           pj[:result] = 'running'
131         else
132           pj[:result] = 'queued'
133         end
134       end
135       pj[:progress_detail] = (step[:progress] rescue nil)
136       pj[:job_id] = (step[:warehousejob][:id] rescue nil)
137       pj[:job_link] = pj[:job_id]
138       pj[:script] = step[:function]
139       pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
140       pj[:output] = step[:output_data_locator]
141       pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
142       ret << pj
143     end
144     ret
145   end
146 end