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