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