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