1 class PipelineInstancesController < ApplicationController
2 skip_before_filter :find_object_by_uuid, only: :compare
3 before_filter :find_objects_by_uuid, only: :compare
4 if Rails.configuration.anonymous_user_token
5 skip_around_filter :require_thread_api_token, only: :show
8 include PipelineInstancesHelper
9 include PipelineComponentsHelper
12 template = PipelineTemplate.find?(@object.pipeline_template_uuid)
15 @object = PipelineInstance.new
16 @object.pipeline_template_uuid = source.pipeline_template_uuid
18 if params['components'] == 'use_latest' and template
19 @object.components = template.components.deep_dup
20 @object.components.each do |cname, component|
21 # Go through the script parameters of each component
22 # that are marked as user input and copy them over.
23 # Skip any components that are not present in the
24 # source instance (there's nothing to copy)
25 if source.components.include? cname
26 component[:script_parameters].each do |pname, val|
27 if val.is_a? Hash and val[:dataclass]
28 # this is user-inputtable, so check the value from the source pipeline
29 srcvalue = source.components[cname][:script_parameters][pname]
31 component[:script_parameters][pname] = srcvalue
38 @object.components = source.components.deep_dup
41 if params['script'] == 'use_same'
42 # Go through each component and copy the script_version from each job.
43 @object.components.each do |cname, component|
44 if source.components.include? cname and source.components[cname][:job]
45 component[:script_version] = source.components[cname][:job][:script_version]
50 @object.components.each do |cname, component|
55 # set owner_uuid to that of source, provided it is a project and wriable by current user
56 current_project = Group.find(source.owner_uuid) rescue nil
57 if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
58 @object.owner_uuid = source.owner_uuid
65 @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
66 if (components = @updates[:components])
67 components.each do |cname, component|
68 if component[:script_parameters]
69 component[:script_parameters].each do |param, value_info|
70 if value_info.is_a? Hash
71 value_info_partitioned = value_info[:value].partition('/') if value_info[:value].andand.class.eql?(String)
72 value_info_value = value_info_partitioned ? value_info_partitioned[0] : value_info[:value]
73 value_info_class = resource_class_for_uuid value_info_value
74 if value_info_class == Link
75 # Use the link target, not the link itself, as script
76 # parameter; but keep the link info around as well.
77 link = Link.find value_info[:value]
78 value_info[:value] = link.head_uuid
79 value_info[:link_uuid] = link.uuid
80 value_info[:link_name] = link.name
82 # Delete stale link_uuid and link_name data.
83 value_info[:link_uuid] = nil
84 value_info[:link_name] = nil
86 if value_info_class == Collection
87 # to ensure reproducibility, the script_parameter for a
88 # collection should be the portable_data_hash
89 # keep the collection name and uuid for human-readability
90 obj = Collection.find value_info_value
91 if value_info_partitioned
92 value_info[:value] = obj.portable_data_hash + value_info_partitioned[1] + value_info_partitioned[2]
93 value_info[:selection_name] = obj.name ? obj.name + value_info_partitioned[1] + value_info_partitioned[2] : obj.name
95 value_info[:value] = obj.portable_data_hash
96 value_info[:selection_name] = obj.name
98 value_info[:selection_uuid] = obj.uuid
109 return nil, nil if params['tab_pane'] != "Graph"
115 # When comparing more than one pipeline, "pips" stores bit fields that
116 # indicates which objects are part of which pipelines.
118 pipelines.each do |p|
123 p[:components].each do |k, v|
124 provenance["component_#{p[:uuid]}_#{k}"] = v
126 collections << v[:output_uuid] if v[:output_uuid]
127 jobs << v[:job][:uuid] if v[:job]
130 jobs = jobs.compact.uniq
132 Job.where(uuid: jobs).each do |j|
135 provenance[job_uuid] = j
136 pips[job_uuid] = 0 unless pips[job_uuid] != nil
139 hashes << j[:output] if j[:output]
140 ProvenanceHelper::find_collections(j) do |hash, uuid|
141 collections << uuid if uuid
142 hashes << hash if hash
145 if j[:script_version]
146 script_uuid = j[:script_version]
147 provenance[script_uuid] = {:uuid => script_uuid}
148 pips[script_uuid] = 0 unless pips[script_uuid] != nil
149 pips[script_uuid] |= n
154 hashes = hashes.compact.uniq
156 Collection.where(portable_data_hash: hashes).each do |c|
157 hash_uuid = c.portable_data_hash
158 provenance[hash_uuid] = c
159 pips[hash_uuid] = 0 unless pips[hash_uuid] != nil
164 collections = collections.compact.uniq
166 Collection.where(uuid: collections).each do |c|
167 collection_uuid = c.uuid
168 provenance[collection_uuid] = c
169 pips[collection_uuid] = 0 unless pips[collection_uuid] != nil
170 pips[collection_uuid] |= n
177 return provenance, pips
181 # the #show action can also be called by #compare, which does its own work to set up @pipelines
182 unless defined? @pipelines
183 @pipelines = [@object]
186 provenance, pips = graph(@pipelines)
188 @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
190 :all_script_parameters => true,
191 :combine_jobs => :script_and_version,
193 :only_components => true,
202 @breadcrumb_page_name = 'compare'
204 @rows = [] # each is {name: S, components: [...]}
206 if params['tab_pane'] == "Compare" or params['tab_pane'].nil?
207 # Build a table: x=pipeline y=component
208 @objects.each_with_index do |pi, pi_index|
209 pipeline_jobs(pi).each do |component|
210 # Find a cell with the same name as this component but no
211 # entry for this pipeline
213 @rows.each_with_index do |row, row_index|
214 if row[:name] == component[:name] and !row[:components][pi_index]
219 target_row = {name: component[:name], components: []}
222 target_row[:components][pi_index] = component
227 # Build a "normal" pseudo-component for this row by picking the
228 # most common value for each attribute. If all values are
229 # equally common, there is no "normal".
230 normal = {} # attr => most common value
231 highscore = {} # attr => how common "normal" is
232 score = {} # attr => { value => how common }
233 row[:components].each do |pj|
236 vstr = for_comparison v
238 score[k][vstr] = (score[k][vstr] || 0) + 1
240 if score[k][vstr] == highscore[k]
241 # tie for first place = no "normal"
243 elsif score[k][vstr] == highscore[k] + 1
244 # more pipelines have v than anything else
245 highscore[k] = score[k][vstr]
251 # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
252 row[:components].each do |pj|
256 pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
262 if params['tab_pane'] == "Graph"
263 @pipelines = @objects
266 @object = @objects.first
272 panes = %w(Components Log Graph Advanced)
273 if @object and @object.state.in? ['New', 'Ready']
274 panes = %w(Inputs) + panes - %w(Log)
276 if not @object.components.values.any? { |x| x[:job] rescue false }
282 def compare_pane_list
288 if v.is_a? Hash or v.is_a? Array
295 def load_filters_and_paging_params
300 def find_objects_by_uuid
301 @objects = model_class.where(uuid: params[:uuids])