4084: Move all pane state into $pane, flatten panes with multiple entry points.
[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     count = {}
101     provenance = {}
102     pips = {}
103     n = 1
104
105     pipelines.each do |p|
106       collections = []
107
108       p.components.each do |k, v|
109         j = v[:job] || next
110
111         uuid = j[:uuid].intern
112         provenance[uuid] = j
113         pips[uuid] = 0 unless pips[uuid] != nil
114         pips[uuid] |= n
115
116         collections << j[:output]
117         ProvenanceHelper::find_collections(j[:script_parameters]).each do |k|
118           collections << k
119         end
120
121         uuid = j[:script_version].intern
122         provenance[uuid] = {:uuid => uuid}
123         pips[uuid] = 0 unless pips[uuid] != nil
124         pips[uuid] |= n
125       end
126
127       Collection.where(uuid: collections.compact).each do |c|
128         uuid = c.uuid.intern
129         provenance[uuid] = c
130         pips[uuid] = 0 unless pips[uuid] != nil
131         pips[uuid] |= n
132       end
133
134       n = n << 1
135     end
136
137     return provenance, pips
138   end
139
140   def show
141     @pipelines = [@object]
142
143     if params[:compare]
144       PipelineInstance.where(uuid: params[:compare]).each do |p|
145         @pipelines << p
146       end
147     end
148
149     provenance, pips = graph(@pipelines)
150     if provenance
151       @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
152         :request => request,
153         :all_script_parameters => true,
154         :combine_jobs => :script_and_version,
155         :script_version_nodes => true,
156         :pips => pips }
157     end
158
159     super
160   end
161
162   def compare
163     @breadcrumb_page_name = 'compare'
164
165     @rows = []          # each is {name: S, components: [...]}
166
167     if params['tab_pane'] == "Compare" or params['tab_pane'].nil?
168       # Build a table: x=pipeline y=component
169       @objects.each_with_index do |pi, pi_index|
170         pipeline_jobs(pi).each do |component|
171           # Find a cell with the same name as this component but no
172           # entry for this pipeline
173           target_row = nil
174           @rows.each_with_index do |row, row_index|
175             if row[:name] == component[:name] and !row[:components][pi_index]
176               target_row = row
177             end
178           end
179           if !target_row
180             target_row = {name: component[:name], components: []}
181             @rows << target_row
182           end
183           target_row[:components][pi_index] = component
184         end
185       end
186
187       @rows.each do |row|
188         # Build a "normal" pseudo-component for this row by picking the
189         # most common value for each attribute. If all values are
190         # equally common, there is no "normal".
191         normal = {}              # attr => most common value
192         highscore = {}           # attr => how common "normal" is
193         score = {}               # attr => { value => how common }
194         row[:components].each do |pj|
195           next if pj.nil?
196           pj.each do |k,v|
197             vstr = for_comparison v
198             score[k] ||= {}
199             score[k][vstr] = (score[k][vstr] || 0) + 1
200             highscore[k] ||= 0
201             if score[k][vstr] == highscore[k]
202               # tie for first place = no "normal"
203               normal.delete k
204             elsif score[k][vstr] == highscore[k] + 1
205               # more pipelines have v than anything else
206               highscore[k] = score[k][vstr]
207               normal[k] = vstr
208             end
209           end
210         end
211
212         # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
213         row[:components].each do |pj|
214           next if pj.nil?
215           pj[:is_normal] = {}
216           pj.each do |k,v|
217             pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
218           end
219         end
220       end
221     end
222
223     if params['tab_pane'] == "Graph"
224       provenance, pips = graph(@objects)
225
226       @pipelines = @objects
227
228       if provenance
229         @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
230           :request => request,
231           :all_script_parameters => true,
232           :combine_jobs => :script_and_version,
233           :script_version_nodes => true,
234           :pips => pips }
235       end
236     end
237
238     @object = @objects.first
239
240     show
241   end
242
243   def show_pane_list
244     panes = %w(Components Log Graph Advanced)
245     if @object and @object.state.in? ['New', 'Ready']
246       panes = %w(Inputs) + panes - %w(Log)
247     end
248     if not @object.components.values.any? { |x| x[:job] rescue false }
249       panes -= ['Graph']
250     end
251     panes
252   end
253
254   def compare_pane_list
255     %w(Compare Graph)
256   end
257
258   def index
259     @limit = 20
260     super
261   end
262
263   protected
264   def for_comparison v
265     if v.is_a? Hash or v.is_a? Array
266       v.to_json
267     else
268       v.to_s
269     end
270   end
271
272   def find_objects_by_uuid
273     @objects = model_class.where(uuid: params[:uuids])
274   end
275
276 end