Merge branch 'master' into 2756-eventbus-in-workbench
[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     @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       :request => request,
66       :all_script_parameters => true, 
67       :combine_jobs => :script_and_version,
68       :script_version_nodes => true,
69       :pips => pips }
70     super
71   end
72
73   def compare
74     @breadcrumb_page_name = 'compare'
75
76     @rows = []          # each is {name: S, components: [...]}
77
78     # Build a table: x=pipeline y=component
79     @objects.each_with_index do |pi, pi_index|
80       pipeline_jobs(pi).each do |component|
81         # Find a cell with the same name as this component but no
82         # entry for this pipeline
83         target_row = nil
84         @rows.each_with_index do |row, row_index|
85           if row[:name] == component[:name] and !row[:components][pi_index]
86             target_row = row
87           end
88         end
89         if !target_row
90           target_row = {name: component[:name], components: []}
91           @rows << target_row
92         end
93         target_row[:components][pi_index] = component
94       end
95     end
96
97     @rows.each do |row|
98       # Build a "normal" pseudo-component for this row by picking the
99       # most common value for each attribute. If all values are
100       # equally common, there is no "normal".
101       normal = {}              # attr => most common value
102       highscore = {}           # attr => how common "normal" is
103       score = {}               # attr => { value => how common }
104       row[:components].each do |pj|
105         next if pj.nil?
106         pj.each do |k,v|
107           vstr = for_comparison v
108           score[k] ||= {}
109           score[k][vstr] = (score[k][vstr] || 0) + 1
110           highscore[k] ||= 0
111           if score[k][vstr] == highscore[k]
112             # tie for first place = no "normal"
113             normal.delete k
114           elsif score[k][vstr] == highscore[k] + 1
115             # more pipelines have v than anything else
116             highscore[k] = score[k][vstr]
117             normal[k] = vstr
118           end
119         end
120       end
121
122       # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
123       row[:components].each do |pj|
124         next if pj.nil?
125         pj[:is_normal] = {}
126         pj.each do |k,v|
127           pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
128         end
129       end
130     end
131
132     provenance, pips = graph(@objects)
133
134     @pipelines = @objects
135
136     @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
137       :request => request,
138       :all_script_parameters => true, 
139       :combine_jobs => :script_and_version,
140       :script_version_nodes => true,
141       :pips => pips }
142   end
143
144   def show_pane_list
145     panes = %w(Components Graph Attributes Metadata JSON API)
146     if @object and @object.state.in? ['New', 'Ready']
147       panes = %w(Inputs) + panes
148     end
149     panes
150   end
151
152   def compare_pane_list 
153     %w(Compare Graph)
154   end 
155
156   def index
157     @limit = 20
158     super
159   end
160
161   protected
162   def for_comparison v
163     if v.is_a? Hash or v.is_a? Array
164       v.to_json
165     else
166       v.to_s
167     end
168   end
169
170   def find_objects_by_uuid
171     @objects = model_class.where(uuid: params[:uuids])
172   end
173
174 end