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