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