1 class PipelineInstancesController < ApplicationController
2 skip_before_filter :find_object_by_uuid, only: :compare
3 before_filter :find_objects_by_uuid, only: :compare
4 skip_around_filter :require_thread_api_token, if: proc { |ctrl|
5 Rails.configuration.anonymous_user_token and
6 'show' == ctrl.action_name
9 include PipelineInstancesHelper
10 include PipelineComponentsHelper
13 template = PipelineTemplate.find?(@object.pipeline_template_uuid)
16 @object = PipelineInstance.new
17 @object.pipeline_template_uuid = source.pipeline_template_uuid
19 if params['components'] == 'use_latest' and template
20 @object.components = template.components.deep_dup
21 @object.components.each do |cname, component|
22 # Go through the script parameters of each component
23 # that are marked as user input and copy them over.
24 # Skip any components that are not present in the
25 # source instance (there's nothing to copy)
26 if source.components.include? cname
27 component[:script_parameters].each do |pname, val|
28 if val.is_a? Hash and val[:dataclass]
29 # this is user-inputtable, so check the value from the source pipeline
30 srcvalue = source.components[cname][:script_parameters][pname]
32 component[:script_parameters][pname] = srcvalue
39 @object.components = source.components.deep_dup
42 if params['script'] == 'use_same'
43 # Go through each component and copy the script_version from each job.
44 @object.components.each do |cname, component|
45 if source.components.include? cname and source.components[cname][:job]
46 component[:script_version] = source.components[cname][:job][:script_version]
51 @object.components.each do |cname, component|
56 # set owner_uuid to that of source, provided it is a project and writable by current user
57 current_project = Group.find(source.owner_uuid) rescue nil
58 if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
59 @object.owner_uuid = source.owner_uuid
66 @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
67 if (components = @updates[:components])
68 components.each do |cname, component|
69 if component[:script_parameters]
70 component[:script_parameters].each do |param, value_info|
71 if value_info.is_a? Hash
72 value_info_partitioned = value_info[:value].partition('/') if value_info[:value].andand.class.eql?(String)
73 value_info_value = value_info_partitioned ? value_info_partitioned[0] : value_info[:value]
74 value_info_class = resource_class_for_uuid value_info_value
75 if value_info_class == Link
76 # Use the link target, not the link itself, as script
77 # parameter; but keep the link info around as well.
78 link = Link.find value_info[:value]
79 value_info[:value] = link.head_uuid
80 value_info[:link_uuid] = link.uuid
81 value_info[:link_name] = link.name
83 # Delete stale link_uuid and link_name data.
84 value_info[:link_uuid] = nil
85 value_info[:link_name] = nil
87 if value_info_class == Collection
88 # to ensure reproducibility, the script_parameter for a
89 # collection should be the portable_data_hash
90 # keep the collection name and uuid for human-readability
91 obj = Collection.find value_info_value
92 if value_info_partitioned
93 value_info[:value] = obj.portable_data_hash + value_info_partitioned[1] + value_info_partitioned[2]
94 value_info[:selection_name] = obj.name ? obj.name + value_info_partitioned[1] + value_info_partitioned[2] : obj.name
96 value_info[:value] = obj.portable_data_hash
97 value_info[:selection_name] = obj.name
99 value_info[:selection_uuid] = obj.uuid
110 return nil, nil if params['tab_pane'] != "Graph"
116 # When comparing more than one pipeline, "pips" stores bit fields that
117 # indicates which objects are part of which pipelines.
119 pipelines.each do |p|
124 p[:components].each do |k, v|
125 provenance["component_#{p[:uuid]}_#{k}"] = v
127 collections << v[:output_uuid] if v[:output_uuid]
128 jobs << v[:job][:uuid] if v[:job]
131 jobs = jobs.compact.uniq
133 Job.where(uuid: jobs).each do |j|
136 provenance[job_uuid] = j
137 pips[job_uuid] = 0 unless pips[job_uuid] != nil
140 hashes << j[:output] if j[:output]
141 ProvenanceHelper::find_collections(j) do |hash, uuid|
142 collections << uuid if uuid
143 hashes << hash if hash
146 if j[:script_version]
147 script_uuid = j[:script_version]
148 provenance[script_uuid] = {:uuid => script_uuid}
149 pips[script_uuid] = 0 unless pips[script_uuid] != nil
150 pips[script_uuid] |= n
155 hashes = hashes.compact.uniq
157 Collection.where(portable_data_hash: hashes).each do |c|
158 hash_uuid = c.portable_data_hash
159 provenance[hash_uuid] = c
160 pips[hash_uuid] = 0 unless pips[hash_uuid] != nil
165 collections = collections.compact.uniq
167 Collection.where(uuid: collections).each do |c|
168 collection_uuid = c.uuid
169 provenance[collection_uuid] = c
170 pips[collection_uuid] = 0 unless pips[collection_uuid] != nil
171 pips[collection_uuid] |= n
178 return provenance, pips
182 # the #show action can also be called by #compare, which does its own work to set up @pipelines
183 unless defined? @pipelines
184 @pipelines = [@object]
187 provenance, pips = graph(@pipelines)
189 @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
191 :direction => :top_down,
192 :all_script_parameters => true,
193 :combine_jobs => :script_and_version,
195 :only_components => true,
204 @breadcrumb_page_name = 'compare'
206 @rows = [] # each is {name: S, components: [...]}
208 if params['tab_pane'] == "Compare" or params['tab_pane'].nil?
209 # Build a table: x=pipeline y=component
210 @objects.each_with_index do |pi, pi_index|
211 pipeline_jobs(pi).each do |component|
212 # Find a cell with the same name as this component but no
213 # entry for this pipeline
215 @rows.each_with_index do |row, row_index|
216 if row[:name] == component[:name] and !row[:components][pi_index]
221 target_row = {name: component[:name], components: []}
224 target_row[:components][pi_index] = component
229 # Build a "normal" pseudo-component for this row by picking the
230 # most common value for each attribute. If all values are
231 # equally common, there is no "normal".
232 normal = {} # attr => most common value
233 highscore = {} # attr => how common "normal" is
234 score = {} # attr => { value => how common }
235 row[:components].each do |pj|
238 vstr = for_comparison v
240 score[k][vstr] = (score[k][vstr] || 0) + 1
242 if score[k][vstr] == highscore[k]
243 # tie for first place = no "normal"
245 elsif score[k][vstr] == highscore[k] + 1
246 # more pipelines have v than anything else
247 highscore[k] = score[k][vstr]
253 # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
254 row[:components].each do |pj|
258 pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
264 if params['tab_pane'] == "Graph"
265 @pipelines = @objects
268 @object = @objects.first
274 panes = %w(Components Log Graph Advanced)
275 if @object and @object.state.in? ['New', 'Ready']
276 panes = %w(Inputs) + panes - %w(Log)
278 if not @object.components.values.any? { |x| x[:job] rescue false }
284 def compare_pane_list
288 helper_method :unreadable_inputs_present?
289 def unreadable_inputs_present?
290 unless @unreadable_inputs_present.nil?
291 return @unreadable_inputs_present
296 @object.components.each do |k, component|
298 component[:script_parameters].andand.each do |p, tv|
299 if (tv.is_a? Hash) and ((tv[:dataclass] == "Collection") || (tv[:dataclass] == "File"))
308 split = value.split '/'
309 if CollectionsHelper.match(split[0])
310 input_pdhs << split[0]
312 input_uuids << split[0]
319 input_pdhs = input_pdhs.uniq
320 input_uuids = input_uuids.uniq
322 preload_collections_for_objects input_uuids if input_uuids.any?
323 preload_for_pdhs input_pdhs if input_pdhs.any?
325 @unreadable_inputs_present = false
326 input_uuids.each do |uuid|
327 if !collections_for_object(uuid).any?
328 @unreadable_inputs_present = true
332 if !@unreadable_inputs_present
333 input_pdhs.each do |pdh|
334 if !collection_for_pdh(pdh).any?
335 @unreadable_inputs_present = true
341 @unreadable_inputs_present
346 if params[:return_to]
347 redirect_to params[:return_to]
355 if v.is_a? Hash or v.is_a? Array
362 def load_filters_and_paging_params
367 def find_objects_by_uuid
368 @objects = model_class.where(uuid: params[:uuids])