Checkpoint, working on form to set component inputs.
[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         uuid = j[:uuid].intern
19         provenance[uuid] = j
20         pips[uuid] = 0 unless pips[uuid] != nil
21         pips[uuid] |= n
22
23         collections << j[:output]
24         ProvenanceHelper::find_collections(j[:script_parameters]).each do |k|
25           collections << k
26         end
27
28         uuid = j[:script_version].intern
29         provenance[uuid] = {:uuid => uuid}
30         pips[uuid] = 0 unless pips[uuid] != nil
31         pips[uuid] |= n
32       end
33
34       Collection.where(uuid: collections.compact).each do |c|
35         uuid = c.uuid.intern
36         provenance[uuid] = c
37         pips[uuid] = 0 unless pips[uuid] != nil
38         pips[uuid] |= n
39       end
40       
41       n = n << 1
42     end
43
44     return provenance, pips
45   end
46
47   def show
48     if @object.components.empty? and @object.pipeline_template_uuid
49       template = PipelineTemplate.find(@object.pipeline_template_uuid)
50       @object.components= template.components
51       @object.save
52     end
53
54     @pipelines = [@object]
55
56     if params[:compare]
57       PipelineInstance.where(uuid: params[:compare]).each do |p|
58         @pipelines << p
59       end
60     end
61
62     provenance, pips = graph(@pipelines)
63
64     @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
65       :all_script_parameters => true, 
66       :combine_jobs => :script_and_version,
67       :script_version_nodes => true,
68       :pips => pips }
69     super
70   end
71
72   def compare
73     @breadcrumb_page_name = 'compare'
74
75     @rows = []          # each is {name: S, components: [...]}
76
77     # Build a table: x=pipeline y=component
78     @objects.each_with_index do |pi, pi_index|
79       pipeline_jobs(pi).each do |component|
80         # Find a cell with the same name as this component but no
81         # entry for this pipeline
82         target_row = nil
83         @rows.each_with_index do |row, row_index|
84           if row[:name] == component[:name] and !row[:components][pi_index]
85             target_row = row
86           end
87         end
88         if !target_row
89           target_row = {name: component[:name], components: []}
90           @rows << target_row
91         end
92         target_row[:components][pi_index] = component
93       end
94     end
95
96     @rows.each do |row|
97       # Build a "normal" pseudo-component for this row by picking the
98       # most common value for each attribute. If all values are
99       # equally common, there is no "normal".
100       normal = {}              # attr => most common value
101       highscore = {}           # attr => how common "normal" is
102       score = {}               # attr => { value => how common }
103       row[:components].each do |pj|
104         next if pj.nil?
105         pj.each do |k,v|
106           vstr = for_comparison v
107           score[k] ||= {}
108           score[k][vstr] = (score[k][vstr] || 0) + 1
109           highscore[k] ||= 0
110           if score[k][vstr] == highscore[k]
111             # tie for first place = no "normal"
112             normal.delete k
113           elsif score[k][vstr] == highscore[k] + 1
114             # more pipelines have v than anything else
115             highscore[k] = score[k][vstr]
116             normal[k] = vstr
117           end
118         end
119       end
120
121       # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
122       row[:components].each do |pj|
123         next if pj.nil?
124         pj[:is_normal] = {}
125         pj.each do |k,v|
126           pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
127         end
128       end
129     end
130
131     provenance, pips = graph(@objects)
132
133     @pipelines = @objects
134
135     @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
136       :all_script_parameters => true, 
137       :combine_jobs => :script_and_version,
138       :script_version_nodes => true,
139       :pips => pips }
140   end
141
142   def show_pane_list
143     %w(Components Graph Attributes Metadata JSON API)
144   end
145
146   def compare_pane_list 
147     %w(Compare Graph)
148   end 
149
150   protected
151   def for_comparison v
152     if v.is_a? Hash or v.is_a? Array
153       v.to_json
154     else
155       v.to_s
156     end
157   end
158
159   def find_objects_by_uuid
160     @objects = model_class.where(uuid: params[:uuids])
161   end
162
163 end