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