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