4031: Find for collection dependencies even in the middle of script parameter
[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               if resource_class_for_uuid(value_info[:value]) == Link
68                 # Use the link target, not the link itself, as script
69                 # parameter; but keep the link info around as well.
70                 link = Link.find value_info[:value]
71                 value_info[:value] = link.head_uuid
72                 value_info[:link_uuid] = link.uuid
73                 value_info[:link_name] = link.name
74               else
75                 # Delete stale link_uuid and link_name data.
76                 value_info[:link_uuid] = nil
77                 value_info[:link_name] = nil
78               end
79             end
80           end
81         end
82       end
83     end
84     super
85   end
86
87   def graph(pipelines)
88     return nil, nil if params['tab_pane'] != "Graph"
89
90     provenance = {}
91     pips = {}
92     n = 1
93
94     # When comparing more than one pipeline, "pips" stores bit fields that
95     # indicates which objects are part of which pipelines.
96
97     pipelines.each do |p|
98       collections = []
99       hashes = []
100       jobs = []
101
102       p[:components].each do |k, v|
103         provenance["component_#{p[:uuid]}_#{k}"] = v
104
105         collections << v[:output_uuid] if v[:output_uuid]
106         jobs << v[:job][:uuid] if v[:job]
107       end
108
109       jobs = jobs.compact.uniq
110       if jobs.any?
111         Job.where(uuid: jobs).each do |j|
112           job_uuid = j.uuid
113
114           provenance[job_uuid] = j
115           pips[job_uuid] = 0 unless pips[job_uuid] != nil
116           pips[job_uuid] |= n
117
118           hashes << j[:output] if j[:output]
119           ProvenanceHelper::find_collections(j) do |hash, uuid|
120             collections << uuid if uuid
121             hashes << hash if hash
122           end
123
124           if j[:script_version]
125             script_uuid = j[:script_version]
126             provenance[script_uuid] = {:uuid => script_uuid}
127             pips[script_uuid] = 0 unless pips[script_uuid] != nil
128             pips[script_uuid] |= n
129           end
130         end
131       end
132
133       hashes = hashes.compact.uniq
134       if hashes.any?
135         Collection.where(portable_data_hash: hashes).each do |c|
136           hash_uuid = c.portable_data_hash
137           provenance[hash_uuid] = c
138           pips[hash_uuid] = 0 unless pips[hash_uuid] != nil
139           pips[hash_uuid] |= n
140         end
141       end
142
143       collections = collections.compact.uniq
144       if collections.any?
145         Collection.where(uuid: collections).each do |c|
146           collection_uuid = c.uuid
147           provenance[collection_uuid] = c
148           pips[collection_uuid] = 0 unless pips[collection_uuid] != nil
149           pips[collection_uuid] |= n
150         end
151       end
152
153       n = n << 1
154     end
155
156     return provenance, pips
157   end
158
159   def show
160     @pipelines = [@object]
161
162     if params[:compare]
163       PipelineInstance.where(uuid: params[:compare]).each do |p|
164         @pipelines << p
165       end
166     end
167
168     provenance, pips = graph(@pipelines)
169     if provenance
170       @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
171         :request => request,
172         :all_script_parameters => true,
173         :combine_jobs => :script_and_version,
174         :pips => pips,
175         :only_components => true,
176         :no_docker => true,
177         :no_log => true}
178     end
179
180     super
181   end
182
183   def compare
184     @breadcrumb_page_name = 'compare'
185
186     @rows = []          # each is {name: S, components: [...]}
187
188     if params['tab_pane'] == "Compare" or params['tab_pane'].nil?
189       # Build a table: x=pipeline y=component
190       @objects.each_with_index do |pi, pi_index|
191         pipeline_jobs(pi).each do |component|
192           # Find a cell with the same name as this component but no
193           # entry for this pipeline
194           target_row = nil
195           @rows.each_with_index do |row, row_index|
196             if row[:name] == component[:name] and !row[:components][pi_index]
197               target_row = row
198             end
199           end
200           if !target_row
201             target_row = {name: component[:name], components: []}
202             @rows << target_row
203           end
204           target_row[:components][pi_index] = component
205         end
206       end
207
208       @rows.each do |row|
209         # Build a "normal" pseudo-component for this row by picking the
210         # most common value for each attribute. If all values are
211         # equally common, there is no "normal".
212         normal = {}              # attr => most common value
213         highscore = {}           # attr => how common "normal" is
214         score = {}               # attr => { value => how common }
215         row[:components].each do |pj|
216           next if pj.nil?
217           pj.each do |k,v|
218             vstr = for_comparison v
219             score[k] ||= {}
220             score[k][vstr] = (score[k][vstr] || 0) + 1
221             highscore[k] ||= 0
222             if score[k][vstr] == highscore[k]
223               # tie for first place = no "normal"
224               normal.delete k
225             elsif score[k][vstr] == highscore[k] + 1
226               # more pipelines have v than anything else
227               highscore[k] = score[k][vstr]
228               normal[k] = vstr
229             end
230           end
231         end
232
233         # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
234         row[:components].each do |pj|
235           next if pj.nil?
236           pj[:is_normal] = {}
237           pj.each do |k,v|
238             pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
239           end
240         end
241       end
242     end
243
244     if params['tab_pane'] == "Graph"
245       provenance, pips = graph(@objects)
246
247       @pipelines = @objects
248
249       if provenance
250         @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
251           :request => request,
252           :all_script_parameters => true,
253           :combine_jobs => :script_and_version,
254           :script_version_nodes => true,
255           :pips => pips }
256       end
257     end
258
259     @object = @objects.first
260
261     show
262   end
263
264   def show_pane_list
265     panes = %w(Components Log Graph Advanced)
266     if @object and @object.state.in? ['New', 'Ready']
267       panes = %w(Inputs) + panes - %w(Log)
268     end
269     if not @object.components.values.any? { |x| x[:job] rescue false }
270       panes -= ['Graph']
271     end
272     panes
273   end
274
275   def compare_pane_list
276     %w(Compare Graph)
277   end
278
279   def index
280     @limit = 20
281     super
282   end
283
284   protected
285   def for_comparison v
286     if v.is_a? Hash or v.is_a? Array
287       v.to_json
288     else
289       v.to_s
290     end
291   end
292
293   def find_objects_by_uuid
294     @objects = model_class.where(uuid: params[:uuids])
295   end
296
297 end