Call super in pipeline_instances#show to make preview/js/json
[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     @pipelines = [@object]
49
50     if params[:compare]
51       PipelineInstance.where(uuid: params[:compare]).each do |p|
52         @pipelines << p
53       end
54     end
55
56     provenance, pips = graph(@pipelines)
57
58     @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
59       :all_script_parameters => true, 
60       :combine_jobs => :script_and_version,
61       :script_version_nodes => true,
62       :pips => pips }
63     super
64   end
65
66   def compare
67     @breadcrumb_page_name = 'compare'
68
69     @rows = []          # each is {name: S, components: [...]}
70
71     # Build a table: x=pipeline y=component
72     @objects.each_with_index do |pi, pi_index|
73       pipeline_jobs(pi).each do |component|
74         # Find a cell with the same name as this component but no
75         # entry for this pipeline
76         target_row = nil
77         @rows.each_with_index do |row, row_index|
78           if row[:name] == component[:name] and !row[:components][pi_index]
79             target_row = row
80           end
81         end
82         if !target_row
83           target_row = {name: component[:name], components: []}
84           @rows << target_row
85         end
86         target_row[:components][pi_index] = component
87       end
88     end
89
90     @rows.each do |row|
91       # Build a "normal" pseudo-component for this row by picking the
92       # most common value for each attribute. If all values are
93       # equally common, there is no "normal".
94       normal = {}              # attr => most common value
95       highscore = {}           # attr => how common "normal" is
96       score = {}               # attr => { value => how common }
97       row[:components].each do |pj|
98         pj.each do |k,v|
99           vstr = for_comparison v
100           score[k] ||= {}
101           score[k][vstr] = (score[k][vstr] || 0) + 1
102           highscore[k] ||= 0
103           if score[k][vstr] == highscore[k]
104             # tie for first place = no "normal"
105             normal.delete k
106           elsif score[k][vstr] == highscore[k] + 1
107             # more pipelines have v than anything else
108             highscore[k] = score[k][vstr]
109             normal[k] = vstr
110           end
111         end
112       end
113
114       # Add a hash in component[:is_normal]: { attr => is_the_value_normal? }
115       row[:components].each do |pj|
116         pj[:is_normal] = {}
117         pj.each do |k,v|
118           pj[:is_normal][k] = (normal.has_key?(k) && normal[k] == for_comparison(v))
119         end
120       end
121     end
122
123     provenance, pips = graph(@objects)
124
125     @prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
126       :all_script_parameters => true, 
127       :combine_jobs => :script_and_version,
128       :script_version_nodes => true,
129       :pips => pips }
130   end
131
132   protected
133   def for_comparison v
134     if v.is_a? Hash or v.is_a? Array
135       v.to_json
136     else
137       v.to_s
138     end
139   end
140
141   def find_objects_by_uuid
142     @objects = model_class.where(uuid: params[:uuids])
143   end
144
145 end