]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2069 refactor the filters page in order to use the new treemap
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 6 Feb 2012 18:46:58 +0000 (19:46 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 6 Feb 2012 18:47:12 +0000 (19:47 +0100)
sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/treemap_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/models/treemap2.rb
sonar-server/src/main/webapp/WEB-INF/app/views/filters/treemap.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap_container.html.erb
sonar-server/src/main/webapp/javascripts/application.js

index 080fbb4b544344567a0a626fd6681ee861f23e04..e67bb0ce48bbffc342bc3aee1001b105bada0605 100644 (file)
@@ -21,13 +21,13 @@ class FiltersController < ApplicationController
   include FiltersHelper
   helper MetricsHelper
   helper FiltersHelper
-    
+
   SECTION=Navigation::SECTION_HOME
 
   verify :method => :post, :only => [:create, :delete, :up, :down, :activate, :deactivate, :up_column, :down_column, :add_column, :delete_column, :set_sorted_column, :set_view, :set_columns, :set_page_size], :redirect_to => {:action => :index}
   before_filter :load_active_filters, :except => ['admin_console', 'treemap', 'set_view', 'set_columns']
   before_filter :login_required, :except => ['index', 'treemap']
-  before_filter :admin_required, :only => ['admin_console' ]
+  before_filter :admin_required, :only => ['admin_console']
 
   def index
     load_active_filter()
@@ -35,8 +35,8 @@ class FiltersController < ApplicationController
 
   def manage
     @shared_filters=::Filter.find(:all, :conditions => ['shared=? and (user_id<>? or user_id is null)', true, current_user.id])
-    ids=@actives.map{|af| af.filter_id}
-    @shared_filters.reject!{|f| ids.include?(f.id) }
+    ids=@actives.map { |af| af.filter_id }
+    @shared_filters.reject! { |f| ids.include?(f.id) }
   end
 
   def new
@@ -65,7 +65,7 @@ class FiltersController < ApplicationController
 
     if @filter.valid?
       @filter.save
-      
+
       # activate it by default
       current_user.active_filters.create(:filter => @filter, :user_id => current_user.id, :order_index => (current_user.active_filters.size + 1))
       flash[:notice]='Filter saved'
@@ -121,7 +121,7 @@ class FiltersController < ApplicationController
     activate_default_filters_if_needed()
     filter=::Filter.find(params[:id])
     if filter && filter.shared
-      existing=current_user.active_filters.to_a.find{|a| a.filter_id==filter.id}
+      existing=current_user.active_filters.to_a.find { |a| a.filter_id==filter.id }
       if existing.nil?
         current_user.active_filters.create(:filter => filter, :user => current_user, :order_index => current_user.active_filters.size+1)
       end
@@ -131,7 +131,7 @@ class FiltersController < ApplicationController
 
   def deactivate
     activate_default_filters_if_needed()
-    active_filter=current_user.active_filters.to_a.find{|a| a.filter_id==params[:id].to_i}
+    active_filter=current_user.active_filters.to_a.find { |a| a.filter_id==params[:id].to_i }
     if active_filter
       if active_filter.owner?
         active_filter.filter.destroy
@@ -172,7 +172,7 @@ class FiltersController < ApplicationController
         actives[index].order_index=index+1
         actives[index].save
       end
-    end   
+    end
     redirect_to :action => 'manage'
   end
 
@@ -201,9 +201,6 @@ class FiltersController < ApplicationController
   end
 
 
-
-
-
   #---------------------------------------------------------------------
   #
   # COLUMNS
@@ -293,7 +290,6 @@ class FiltersController < ApplicationController
   end
 
 
-
   #---------------------------------------------------------------------
   #
   # CUSTOMIZE DISPLAY
@@ -345,10 +341,10 @@ class FiltersController < ApplicationController
 
       else
         @snapshots=Snapshot.find(:all, :include => [:project, {:root_snapshot => :project}, {:parent_snapshot => :project}],
-          :conditions => ['snapshots.status=? AND snapshots.islast=? AND snapshots.scope=? AND projects.scope=? AND UPPER(projects.long_name) LIKE ?', 'P', true, 'PRJ', 'PRJ', "%#{params[:search].upcase}%"],
-          :order => 'projects.long_name')
+                                 :conditions => ['snapshots.status=? AND snapshots.islast=? AND snapshots.scope=? AND projects.scope=? AND UPPER(projects.long_name) LIKE ?', 'P', true, 'PRJ', 'PRJ', "%#{params[:search].upcase}%"],
+                                 :order => 'projects.long_name')
         @snapshots=select_authorized(:user, @snapshots)
-        @snapshots.sort! do |s1,s2|
+        @snapshots.sort! do |s1, s2|
           if s1.qualifier==s2.qualifier
             s1.project.long_name<=>s2.project.long_name
           else
@@ -361,7 +357,6 @@ class FiltersController < ApplicationController
   end
 
 
-
   #---------------------------------------------------------------------
   #
   # TREEMAP
@@ -385,9 +380,17 @@ class FiltersController < ApplicationController
     @width=(params[:width]||'800').to_i
     @height=(params[:height]||'500').to_i
 
-    treemap_options={:period_index => @filter_context.period_index}
-    @treemap=Sonar::Treemap.new(@filter_context.measures_by_snapshot, @width, @height, @size_metric, @color_metric, treemap_options)
+    @treemap = Treemap2.new(@filter.id, @size_metric, @width, @height, {
+      :color_metric => @color_metric,
+      :period_index => @filter_context.period_index,
+      :measures_by_snapshot => @filter_context.measures_by_snapshot,
+      :browsable => false
+    })
+
+
+    #@treemap=Sonar::Treemap.new(@filter_context.measures_by_snapshot, @width, @height, @size_metric, @color_metric, treemap_options)
     render :action => "treemap", :layout => false
+
   end
 
 
@@ -403,7 +406,7 @@ class FiltersController < ApplicationController
     filter.name=params[:name]
     filter.shared=(params[:shared].present? && is_admin?)
     filter.favourites=params[:favourites].present?
-    filter.resource_id=(params[:path_id].present? ? Project.by_key(params[:path_id]).id : nil) 
+    filter.resource_id=(params[:path_id].present? ? Project.by_key(params[:path_id]).id : nil)
     filter.user_id=current_user.id
     filter.period_index=params[:period_index].to_i
     filter.criteria=[]
@@ -428,7 +431,7 @@ class FiltersController < ApplicationController
     @actives=nil
     if logged_in?
       @actives=current_user.active_filters
-      @actives=::ActiveFilter.default_active_filters if(@actives.nil? || @actives.empty?)
+      @actives=::ActiveFilter.default_active_filters if (@actives.nil? || @actives.empty?)
     else
       @actives=::ActiveFilter.for_anonymous
     end
@@ -451,9 +454,9 @@ class FiltersController < ApplicationController
   def load_active_filter()
     @active=nil
     if params[:name]
-      @active=@actives.to_a.find{|a| a.name==params[:name]}
+      @active=@actives.to_a.find { |a| a.name==params[:name] }
     elsif params[:id]
-      @active=@actives.to_a.find{|a| a.filter.id==params[:id].to_i}
+      @active=@actives.to_a.find { |a| a.filter.id==params[:id].to_i }
     end
 
     if @active.nil? && !@actives.empty?
index ad96d43989b80674cd9e93fff192cdce64fa9ee5..6a8151f57d802ed83713248a9412ff6614b2ddc7 100644 (file)
@@ -56,6 +56,7 @@ class TreemapController < ApplicationController
     render :update do |page|
       page.replace_html  "tm-#{html_id}", :partial => 'treemap', :object => treemap
       page.replace_html  "tm-gradient-#{html_id}", :partial => 'gradient', :locals => {:metric => color_metric}
+      page.hide "tm-loading-#{html_id}"
     end
   end
 
index cb17402b0c42aa474d34c09792bdf973804b593d..abf87c48569debaca0db192ad06744a89d969e9d 100644 (file)
@@ -32,7 +32,8 @@ class Treemap2
 
     @color_metric = options[:color_metric]
     @root_snapshot = options[:root_snapshot]
-    @measures = options[:measures] # pre-computed measures, for example by filters
+    @measures_by_snapshot = options[:measures_by_snapshot] # pre-computed measures, for example by filters
+    @browsable = options[:browsable]
     if options[:period_index] && options[:period_index]>0
       @period_index = options[:period_index]
     end
@@ -57,8 +58,8 @@ class Treemap2
 
   protected
 
-  def measures
-    @measures ||=
+  def measures_by_snapshot
+    @measures_by_snapshot ||=
       begin
         metric_ids=[@size_metric.id]
         metric_ids << @color_metric.id if @color_metric && @color_metric.id!=@size_metric.id
@@ -73,36 +74,33 @@ class Treemap2
           sql_conditions<<" AND snapshots.scope='PRJ' and snapshots.qualifier='TRK'"
         end
 
-        ProjectMeasure.find(:all, :include => {:snapshot => :project}, :conditions => [sql_conditions].concat(sql_values))
+        hash = {}
+        ProjectMeasure.find(:all, :include => {:snapshot => :project}, :conditions => [sql_conditions].concat(sql_values)).each do |m|
+          hash[m.snapshot]||={}
+          hash[m.snapshot][m.metric]=m
+        end
+        hash
       end
   end
 
   def build_tree(node)
-    color_measures_by_sid={}
-    if @color_metric
-      measures.each do |measure|
-        color_measures_by_sid[measure.snapshot_id]=measure if measure.metric_id==@color_metric.id
-      end
-    end
-
-    measures.each do |measure|
-      if measure.metric_id==@size_metric.id
-        color_measure = color_measures_by_sid[measure.snapshot_id]
-        resource = measure.snapshot.project
-        child = Treemap::Node.new(:id => "#{@id}-#{@id_counter += 1}",
-                                  :size => size_value(measure),
-                                  :label => resource.name(false),
-                                  :title => escape_javascript(resource.name(true)),
-                                  :tooltip => tooltip(resource, measure, color_measure),
-                                  :color => html_color(color_measure),
-                                  :rid => resource.id,
-                                  :browsable => resource.display_dashboard?)
-        node.add_child(child)
-      end
+    measures_by_snapshot.each_pair do |snapshot, measures|
+      size_measure=measures[size_metric]
+      color_measure=(color_metric ? measures[color_metric] : nil)
+      resource = snapshot.project
+      child = Treemap::Node.new(:id => "#{@id}-#{@id_counter += 1}",
+                                :size => size_value(size_measure),
+                                :label => resource.name(false),
+                                :title => escape_javascript(resource.name(true)),
+                                :tooltip => tooltip(resource, size_measure, color_measure),
+                                :color => html_color(color_measure),
+                                :rid => resource.copy_resource_id || resource.id,
+                                :browsable => @browsable && resource.display_dashboard?)
+      node.add_child(child)
     end
   end
 
-  def tooltip(resource,size_measure, color_measure)
+  def tooltip(resource, size_measure, color_measure)
     html=CGI::escapeHTML(resource.name(true))
     html += " - #{CGI::escapeHTML(@size_metric.short_name)}: #{CGI::escapeHTML(size_measure.formatted_value)}"
     if color_measure
index 3b6601281b4103b709d14ddacee1344c733e6489..9dc29b9844a1e51d97ef684d99dd547ea7839f7c 100644 (file)
@@ -9,7 +9,7 @@
     </td>
     <td class="sep"> </td>
     <td>
-      <span class="comments"><%= message('color') -%>: <%= render :partial => 'components/treemap_gradient', :locals => {:color_metric => @color_metric} %></span>
+      <span class="comments"><%= message('color') -%>: <%= render :partial => 'treemap/gradient', :locals => {:metric => @color_metric} %></span>
       <br/>
       <%= select_tag 'color_metric', options_grouped_by_domain(Sonar::TreemapBuilder.color_metrics, @color_metric.key),
         :id => 'color_metric', :class => 'small', :onchange => "load_treemap(this.form.size_metric.value,this.form.color_metric.value, false);return false;" %>
@@ -19,6 +19,7 @@
 </form>
 <br/>
 <% end %>
-<div class="treemap" style="height:<%= @treemap.height %>px">
-  <%= @treemap.generate_html() %>
+
+<div class="treemap">
+  <%=  render :partial => 'treemap/treemap' -%>
 </div>
\ No newline at end of file
index 61e67e7c8a48770f3d2a36ded89d51729d0206eb..d82c2b92b8727a89d33eac656304e1b7c7d2d423 100644 (file)
@@ -1,9 +1,8 @@
-<div style="width: <%= treemap.width -%>px; height:<%= treemap.height %>px;">
+<div class="treemap" style="width: <%= treemap.width -%>px; height:<%= treemap.height %>px;">
   <%= treemap.generate_html() -%>
 </div>
 <script>
   for (var i = 1; i <= <%= treemap.id_counter -%>; i++) {
     addTmEvent(<%= treemap.id -%>, i);
   }
-  $('tm-loading-<%= treemap.id -%>').hide();
 </script>
index b6b533af1fd1190dc59875736237d1aec8feb8cd..211bc5981c4154dd4c329de4ce20130a3c00ff62 100644 (file)
@@ -5,14 +5,14 @@
         <span class="comments"><%= message('size') -%></span>
         <br/>
         <%= select_tag "size", options_grouped_by_domain(Sonar::TreemapBuilder.size_metrics, (size_metric ? size_metric.key : nil), :include_empty => true),
-                       :id => "tm-size-#{treemap_id}", :class => 'small spacer-right', :onchange => "refreshTm(#{treemap_id}, #{resource_id})" %>
+                       :id => "tm-size-#{treemap_id}", :class => 'small spacer-right', :onchange => "refreshTm(#{treemap_id}, null)" %>
       </td>
       <td valign="top" class="thin nowrap">
         <span class="comments"><%= message('color') -%></span>
         <span id="tm-gradient-<%= treemap_id -%>" class="note"></span>
         <br/>
         <%= select_tag 'color', options_grouped_by_domain(Sonar::TreemapBuilder.color_metrics, (color_metric ? color_metric.key : nil), :include_empty => true),
-                         :id => "tm-color-#{treemap_id}", :class => 'small', :onchange => "refreshTm(#{treemap_id}, #{resource_id})" %>
+                         :id => "tm-color-#{treemap_id}", :class => 'small', :onchange => "refreshTm(#{treemap_id}, null)" %>
         <%= image_tag 'loading.gif', :id => "tm-loading-#{treemap_id}", :style => 'vertical-align: top;display: none' -%>
       </td>
       <td></td>
@@ -20,8 +20,7 @@
   </table>
   <input type="hidden" id="tm-h-<%= treemap_id -%>" value="<%= heightInPercents -%>"/>
 </div>
-<div id="tm-error-<%= treemap_id -%>" class="error" style="display:none"></div>
-<div id="tm-<%= treemap_id -%>" class="treemap spacer-bottom"></div>
+<div id="tm-<%= treemap_id -%>" class="spacer-bottom"></div>
 
 <div style="margin: 5px 0 0 0" class="notes">
   <div style="float: right"><span>Left click to zoom in. Right click to zoom out.</span></div>
index eb13eac4303f0551f9936c8610b9be1d2ac2e8e1..c79ebabffaa11a989f1323c319c0e00abd5cfdee 100644 (file)
@@ -186,6 +186,7 @@ function refreshTm(treemap_id, resource_id) {
   var color = $F('tm-color-' + treemap_id);
   var width = $('tm-' + treemap_id).getWidth() - 10;
   var height = Math.round(width * parseFloat($F('tm-h-' + treemap_id) / 100.0));
+  var rid = (resource_id!=null ? resource_id : context[context.length-1][0]);
 
   context = treemapContexts[treemap_id];
   var output = '';
@@ -196,7 +197,7 @@ function refreshTm(treemap_id, resource_id) {
   $('tm-loading-' + treemap_id).show();
 
   new Ajax.Request(
-    baseUrl + '/treemap/index?id=' + treemap_id + '&width=' + width + '&height=' + height + '&size_metric=' + size + '&color_metric=' + color + '&resource=' + resource_id,
+    baseUrl + '/treemap/index?id=' + treemap_id + '&width=' + width + '&height=' + height + '&size_metric=' + size + '&color_metric=' + color + '&resource=' + rid,
     {asynchronous:true, evalScripts:true});
 
   return false;