Merge branch 'master' into 4156-cli-tests
[arvados.git] / apps / workbench / app / controllers / pipeline_instances_controller.rb
1 class PipelineInstancesController < ApplicationController
2   skip_before_filter :find_object_by_uuid, only: :compare
3   before_filter :find_objects_by_uuid, only: :compare
4   include PipelineInstancesHelper
5   include PipelineComponentsHelper
6
7   def copy
8     template = PipelineTemplate.find?(@object.pipeline_template_uuid)
9
10     source = @object
11     @object = PipelineInstance.new
12     @object.pipeline_template_uuid = source.pipeline_template_uuid
13
14     if params['components'] == 'use_latest' and template
15       @object.components = template.components.deep_dup
16       @object.components.each do |cname, component|
17         # Go through the script parameters of each component
18         # that are marked as user input and copy them over.
19         # Skip any components that are not present in the
20         # source instance (there's nothing to copy)
21         if source.components.include? cname
22           component[:script_parameters].each do |pname, val|
23             if val.is_a? Hash and val[:dataclass]
24               # this is user-inputtable, so check the value from the source pipeline
25               srcvalue = source.components[cname][:script_parameters][pname]
26               if not srcvalue.nil?
27                 component[:script_parameters][pname] = srcvalue
28               end
29             end
30           end
31         end
32       end
33     else
34       @object.components = source.components.deep_dup
35     end
36
37     if params['script'] == 'use_same'
38       # Go through each component and copy the script_version from each job.
39       @object.components.each do |cname, component|
40         if source.components.include? cname and source.components[cname][:job]
41           component[:script_version] = source.components[cname][:job][:script_version]
42         end
43       end
44     end
45
46     @object.components.each do |cname, component|
47       component.delete :job
48     end
49     @object.state = 'New'
50
51     # set owner_uuid to that of source, provided it is a project and wriable by current user
52     current_project = Group.find(source.owner_uuid) rescue nil
53     if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
54       @object.owner_uuid = source.owner_uuid
55     end
56
57     super
58   end
59
60   def update
61     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
62     if (components = @updates[:components])
63       components.each do |cname, component|
64         if component[:script_parameters]
65           component[:script_parameters].each do |param, value_info|
66             if value_info.is_a? Hash
67               value_info_partitioned = value_info[:value].partition('/') if value_info[:value].andand.class.eql?(String)
68               value_info_value = value_info_partitioned ? value_info_partitioned[0] : value_info[:value]
69               value_info_class = resource_class_for_uuid value_info_value
70               if value_info_class == Link
71                 # Use the link target, not the link itself, as script
72                 # parameter; but keep the link info around as well.
73                 link = Link.find value_info[:value]
74                 value_info[:value] = link.head_uuid
75                 value_info[:link_uuid] = link.uuid
76                 value_info[:link_name] = link.name
77               else
78                 # Delete stale link_uuid and link_name data.
79                 value_info[:link_uuid] = nil
80                 value_info[:link_name] = nil
81               end
82               if value_info_class == Collection
83                 # to ensure reproducibility, the script_parameter for a
84                 # collection should be the portable_data_hash
85                 # keep the collection name and uuid for human-readability
86                 obj = Collection.find value_info_value
87                 if value_info_partitioned
88                   value_info[:value] = obj.portable_data_hash + value_info_partitioned[1] + value_info_partitioned[2]
89                   value_info[:selection_name] = obj.name ? obj.name + value_info_partitioned[1] + value_info_partitioned[2] : obj.name
90                 else
91                   value_info[:value] = obj.portable_data_hash
92                   value_info[:selection_name] = obj.name
93                 end
94                 value_info[:selection_uuid] = obj.uuid
95               end
96             end
97           end
98         end
99       end
100     end
101     super
102   end
103
104   def graph(pipelines)
105     return nil, nil if params['tab_pane'] != "Graph"
106
107     provenance = {}
108     pips = {}
109     n = 1
110
111     # When comparing more than one pipeline, "pips" stores bit fields that
112     # indicates which objects are part of which pipelines.
113
114     pipelines.each do |p|
115       collections = []
116       hashes = []
117       jobs = []
118
119       p[:components].each do |k, v|
120         provenance["component_#{p[:uuid]}_#{k}"] = v
121
122         collections << v[:output_uuid] if v[:output_uuid]
123         jobs << v[:job][:uuid] if v[:job]
124       end
125
126       jobs = jobs.compact.uniq
127       if jobs.any?
128         Job.where(uuid: jobs).each do |j|
129           job_uuid = j.uuid
130
131           provenance[job_uuid] = j
132           pips[job_uuid] = 0 unless pips[job_uuid] != nil
133           pips[job_uuid] |= n
134
135           hashes << j[:output] if j[:output]
136           ProvenanceHelper::find_collections(j) do |hash, uuid|
137             collections << uuid if uuid
138             hashes << hash if hash
139           end
140
141           if j[:script_version]
142             script_uuid = j[:script_version]
143             provenance[script_uuid] = {:uuid => script_uuid}
144             pips[script_uuid] = 0 unless pips[script_uuid] != nil
145             pips[script_uuid] |= n
146           end
147         end
148       end
149
150       hashes = hashes.compact.uniq
151       if hashes.any?
152         Collection.where(portable_data_hash: hashes).each do |c|
153           hash_uuid = c.portable_data_hash
154           provenance[hash_uuid] = c
155           pips[hash_uuid] = 0 unless pips[hash_uuid] != nil
156           pips[hash_uuid] |= n
157         end
158       end
159
160       collections = collections.compact.uniq
161       if collections.any?
162         Collection.where(uuid: collections).each do |c|
163           collection_uuid = c.uuid
164           provenance[collection_uuid] = c
165           pips[collection_uuid] = 0 unless pips[collection_uuid] != nil
166           pips[collection_uuid] |= n
167         end
168       end
169
170       n = n << 1
171     end
172
173     return provenance, pips
174   end
175
176   def show
177     @pipelines = [@object]
178
179     if params[:compare]
180       PipelineInstance.where(uuid: params[:compare]).each do |p|
181         @pipelines << p
182       end
183     end
184
185     provenance, pips = graph(@pipelines)
186     if provenance
187       @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
188         :request => request,
189         :all_script_parameters => true,
190         :combine_jobs => :script_and_version,
191         :pips => pips,
192         :only_components => true,
193         :no_docker => true,
194         :no_log => true}
195     end
196
197     super
198   end
199
200   def compare
201     @breadcrumb_page_name = 'compare'
202
203     @rows = []          # each is {name: S, components: [...]}
204
205     if params['tab_pane'] == "Compare" or params['tab_pane'].nil?
206       # Build a table: x=pipeline y=component
207       @objects.each_with_index do |pi, pi_index|
208         pipeline_jobs(pi).each do |component|
209           # Find a cell with the same name as this component but no
210           # entry for this pipeline
211           target_row = nil
212           @rows.each_with_index do |row, row_index|
213             if row[:name] == component[:name] and !row[:components][pi_index]
214               target_row = row
215             end
216           end
217           if !target_row
218             target_row = {name: component[:name], components: []}
219             @rows << target_row
220           end
221           target_row[:components][pi_index] = component
222         end
223       end
224
225       @rows.each do |row|
226         # Build a "normal" pseudo-component for this row by picking the
227         # most common value for each attribute. If all values are
228         # equally common, there is no "normal".
229         normal = {}              # attr => most common value
230         highscore = {}           # attr => how common "normal" is
231         score = {}               # attr => { value => how common }
232         row[:components].each do |pj|
233           next if pj.nil?
234           pj.each do |k,v|
235             vstr = for_comparison v
236             score[k] ||= {}
237             score[k][vstr] = (score[k][vstr] || 0) + 1
238             highscore[k] ||= 0
239             if score[k][vstr] == highscore[k]
240               # tie for first place = no "normal"
241               normal.delete k
242             elsif score[k][vstr] == highscore[k] + 1
243               # more pipelines have v than anything else
244               highscore[k] = score[k][vstr]
245               normal[k] = vstr
246             end
247           end
248         end
249
250         # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
251         row[:components].each do |pj|
252           next if pj.nil?
253           pj[:is_normal] = {}
254           pj.each do |k,v|
255             pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
256           end
257         end
258       end
259     end
260
261     if params['tab_pane'] == "Graph"
262       provenance, pips = graph(@objects)
263
264       @pipelines = @objects
265
266       if provenance
267         @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
268           :request => request,
269           :all_script_parameters => true,
270           :combine_jobs => :script_and_version,
271           :script_version_nodes => true,
272           :pips => pips }
273       end
274     end
275
276     @object = @objects.first
277
278     show
279   end
280
281   def show_pane_list
282     panes = %w(Components Log Graph Advanced)
283     if @object and @object.state.in? ['New', 'Ready']
284       panes = %w(Inputs) + panes - %w(Log)
285     end
286     if not @object.components.values.any? { |x| x[:job] rescue false }
287       panes -= ['Graph']
288     end
289     panes
290   end
291
292   def compare_pane_list
293     %w(Compare Graph)
294   end
295
296   protected
297   def for_comparison v
298     if v.is_a? Hash or v.is_a? Array
299       v.to_json
300     else
301       v.to_s
302     end
303   end
304
305   def load_filters_and_paging_params
306     params[:limit] = 20
307     super
308   end
309
310   def find_objects_by_uuid
311     @objects = model_class.where(uuid: params[:uuids])
312   end
313 end