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