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