Merge branch 'master' into 3296-user-profile
[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     @object = @object.dup
9     @object.components.each do |cname, component|
10       component.delete :job
11     end
12     @object.state = 'New'
13     super
14   end
15
16   def update
17     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
18     if (components = @updates[:components])
19       components.each do |cname, component|
20         if component[:script_parameters]
21           component[:script_parameters].each do |param, value_info|
22             if value_info.is_a? Hash
23               if resource_class_for_uuid(value_info[:value]) == Link
24                 # Use the link target, not the link itself, as script
25                 # parameter; but keep the link info around as well.
26                 link = Link.find value_info[:value]
27                 value_info[:value] = link.head_uuid
28                 value_info[:link_uuid] = link.uuid
29                 value_info[:link_name] = link.name
30               else
31                 # Delete stale link_uuid and link_name data.
32                 value_info[:link_uuid] = nil
33                 value_info[:link_name] = nil
34               end
35             end
36           end
37         end
38       end
39     end
40     super
41   end
42
43   def graph(pipelines)
44     return nil, nil if params['tab_pane'] != "Graph"
45
46     count = {}
47     provenance = {}
48     pips = {}
49     n = 1
50
51     pipelines.each do |p|
52       collections = []
53
54       p.components.each do |k, v|
55         j = v[:job] || next
56
57         # The graph is interested in whether the component is
58         # indicated as persistent, more than whether the job
59         # satisfying it (which could have been reused, or someone
60         # else's) is.
61         j[:output_is_persistent] = v[:output_is_persistent]
62
63         uuid = j[:uuid].intern
64         provenance[uuid] = j
65         pips[uuid] = 0 unless pips[uuid] != nil
66         pips[uuid] |= n
67
68         collections << j[:output]
69         ProvenanceHelper::find_collections(j[:script_parameters]).each do |k|
70           collections << k
71         end
72
73         uuid = j[:script_version].intern
74         provenance[uuid] = {:uuid => uuid}
75         pips[uuid] = 0 unless pips[uuid] != nil
76         pips[uuid] |= n
77       end
78
79       Collection.where(uuid: collections.compact).each do |c|
80         uuid = c.uuid.intern
81         provenance[uuid] = c
82         pips[uuid] = 0 unless pips[uuid] != nil
83         pips[uuid] |= n
84       end
85
86       n = n << 1
87     end
88
89     return provenance, pips
90   end
91
92   def show
93     @pipelines = [@object]
94
95     if params[:compare]
96       PipelineInstance.where(uuid: params[:compare]).each do |p|
97         @pipelines << p
98       end
99     end
100
101     provenance, pips = graph(@pipelines)
102     if provenance
103       @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
104         :request => request,
105         :all_script_parameters => true,
106         :combine_jobs => :script_and_version,
107         :script_version_nodes => true,
108         :pips => pips }
109     end
110
111     super
112   end
113
114   def compare
115     @breadcrumb_page_name = 'compare'
116
117     @rows = []          # each is {name: S, components: [...]}
118
119     if params['tab_pane'] == "Compare" or params['tab_pane'].nil?
120       # Build a table: x=pipeline y=component
121       @objects.each_with_index do |pi, pi_index|
122         pipeline_jobs(pi).each do |component|
123           # Find a cell with the same name as this component but no
124           # entry for this pipeline
125           target_row = nil
126           @rows.each_with_index do |row, row_index|
127             if row[:name] == component[:name] and !row[:components][pi_index]
128               target_row = row
129             end
130           end
131           if !target_row
132             target_row = {name: component[:name], components: []}
133             @rows << target_row
134           end
135           target_row[:components][pi_index] = component
136         end
137       end
138
139       @rows.each do |row|
140         # Build a "normal" pseudo-component for this row by picking the
141         # most common value for each attribute. If all values are
142         # equally common, there is no "normal".
143         normal = {}              # attr => most common value
144         highscore = {}           # attr => how common "normal" is
145         score = {}               # attr => { value => how common }
146         row[:components].each do |pj|
147           next if pj.nil?
148           pj.each do |k,v|
149             vstr = for_comparison v
150             score[k] ||= {}
151             score[k][vstr] = (score[k][vstr] || 0) + 1
152             highscore[k] ||= 0
153             if score[k][vstr] == highscore[k]
154               # tie for first place = no "normal"
155               normal.delete k
156             elsif score[k][vstr] == highscore[k] + 1
157               # more pipelines have v than anything else
158               highscore[k] = score[k][vstr]
159               normal[k] = vstr
160             end
161           end
162         end
163
164         # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
165         row[:components].each do |pj|
166           next if pj.nil?
167           pj[:is_normal] = {}
168           pj.each do |k,v|
169             pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
170           end
171         end
172       end
173     end
174
175     if params['tab_pane'] == "Graph"
176       provenance, pips = graph(@objects)
177
178       @pipelines = @objects
179
180       if provenance
181         @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
182           :request => request,
183           :all_script_parameters => true,
184           :combine_jobs => :script_and_version,
185           :script_version_nodes => true,
186           :pips => pips }
187       end
188     end
189
190     @object = @objects.first
191
192     show
193   end
194
195   def show_pane_list
196     panes = %w(Components Graph Advanced)
197     if @object and @object.state.in? ['New', 'Ready']
198       panes = %w(Inputs) + panes
199     end
200     if not @object.components.values.any? { |x| x[:job] rescue false }
201       panes -= ['Graph']
202     end
203     panes
204   end
205
206   def compare_pane_list
207     %w(Compare Graph)
208   end
209
210   def index
211     @limit = 20
212     super
213   end
214
215   protected
216   def for_comparison v
217     if v.is_a? Hash or v.is_a? Array
218       v.to_json
219     else
220       v.to_s
221     end
222   end
223
224   def find_objects_by_uuid
225     @objects = model_class.where(uuid: params[:uuids])
226   end
227
228 end