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