Merge branch 'master' into origin-2035-arv-mount-tags-folders
[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
26   protected
27
28   def pipeline_jobs_newschool object
29     ret = []
30     i = -1
31
32     object.components.each do |cname, c|
33       i += 1
34       pj = {index: i, name: cname}
35       pj[:job] = c[:job].is_a?(Hash) ? c[:job] : {}
36       pj[:percent_done] = 0
37       pj[:percent_running] = 0
38       if pj[:job][:success]
39         if pj[:job][:output]
40           pj[:progress] = 1.0
41           pj[:percent_done] = 100
42         else
43           pj[:progress] = 0.0
44         end
45       else
46         if pj[:job][:tasks_summary]
47           begin
48             ts = pj[:job][:tasks_summary]
49             denom = ts[:done].to_f + ts[:running].to_f + ts[:todo].to_f
50             pj[:progress] = (ts[:done].to_f + ts[:running].to_f/2) / denom
51             pj[:percent_done] = 100.0 * ts[:done].to_f / denom
52             pj[:percent_running] = 100.0 * ts[:running].to_f / denom
53             pj[:progress_detail] = "#{ts[:done]} done #{ts[:running]} run #{ts[:todo]} todo"
54           rescue
55             pj[:progress] = 0.5
56             pj[:percent_done] = 0.0
57             pj[:percent_running] = 100.0
58           end
59         else
60           pj[:progress] = 0.0
61         end
62       end
63       if pj[:job][:success]
64         pj[:result] = 'complete'
65         pj[:complete] = true
66         pj[:progress] = 1.0
67       elsif pj[:job][:finished_at]
68         pj[:result] = 'failed'
69         pj[:failed] = true
70       elsif pj[:job][:started_at]
71         pj[:result] = 'running'
72       elsif pj[:job][:uuid]
73         pj[:result] = 'queued'
74       else
75         pj[:result] = 'none'
76       end
77       pj[:job_id] = pj[:job][:uuid]
78       pj[:script] = pj[:job][:script] || c[:script]
79       pj[:script_parameters] = pj[:job][:script_parameters] || c[:script_parameters]
80       pj[:script_version] = pj[:job][:script_version] || c[:script_version]
81       pj[:output] = pj[:job][:output]
82       pj[:finished_at] = (Time.parse(pj[:job][:finished_at]) rescue nil)
83       ret << pj
84     end
85     ret
86   end
87
88   def pipeline_jobs_oldschool object
89     ret = []
90     object.components[:steps].each_with_index do |step, i|
91       pj = {index: i, name: step[:name]}
92       if step[:complete] and step[:complete] != 0
93         if step[:output_data_locator]
94           pj[:progress] = 1.0
95         else
96           pj[:progress] = 0.0
97         end
98       else
99         if step[:progress] and
100             (re = step[:progress].match /^(\d+)\+(\d+)\/(\d+)$/)
101           pj[:progress] = (((re[1].to_f + re[2].to_f/2) / re[3].to_f) rescue 0.5)
102         else
103           pj[:progress] = 0.0
104         end
105         if step[:failed]
106           pj[:result] = 'failed'
107           pj[:failed] = true
108         end
109       end
110       if step[:warehousejob]
111         if step[:complete]
112           pj[:result] = 'complete'
113           pj[:complete] = true
114           pj[:progress] = 1.0
115         elsif step[:warehousejob][:finishtime]
116           pj[:result] = 'failed'
117           pj[:failed] = true
118         elsif step[:warehousejob][:starttime]
119           pj[:result] = 'running'
120         else
121           pj[:result] = 'queued'
122         end
123       end
124       pj[:progress_detail] = (step[:progress] rescue nil)
125       pj[:job_id] = (step[:warehousejob][:id] rescue nil)
126       pj[:job_link] = pj[:job_id]
127       pj[:script] = step[:function]
128       pj[:script_version] = (step[:warehousejob][:revision] rescue nil)
129       pj[:output] = step[:output_data_locator]
130       pj[:finished_at] = (Time.parse(step[:warehousejob][:finishtime]) rescue nil)
131       ret << pj
132     end
133     ret
134   end
135 end