From f14133323d409ab2d0ab70ea3c5625fe66d75148 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 6 Feb 2012 19:46:58 +0100 Subject: [PATCH] SONAR-2069 refactor the filters page in order to use the new treemap --- .../app/controllers/filters_controller.rb | 47 +++++++++-------- .../app/controllers/treemap_controller.rb | 1 + .../webapp/WEB-INF/app/models/treemap2.rb | 50 +++++++++---------- .../app/views/filters/treemap.html.erb | 7 +-- .../app/views/treemap/_treemap.html.erb | 3 +- .../views/treemap/_treemap_container.html.erb | 7 ++- .../main/webapp/javascripts/application.js | 3 +- 7 files changed, 60 insertions(+), 58 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb index 080fbb4b544..e67bb0ce48b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb @@ -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? diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/treemap_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/treemap_controller.rb index ad96d43989b..6a8151f57d8 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/treemap_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/treemap_controller.rb @@ -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 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/treemap2.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/treemap2.rb index cb17402b0c4..abf87c48569 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/treemap2.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/treemap2.rb @@ -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 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/filters/treemap.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/filters/treemap.html.erb index 3b6601281b4..9dc29b9844a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/filters/treemap.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/filters/treemap.html.erb @@ -9,7 +9,7 @@ - <%= message('color') -%>: <%= render :partial => 'components/treemap_gradient', :locals => {:color_metric => @color_metric} %> + <%= message('color') -%>: <%= render :partial => 'treemap/gradient', :locals => {:metric => @color_metric} %>
<%= 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 @@
<% end %> -
- <%= @treemap.generate_html() %> + +
+ <%= render :partial => 'treemap/treemap' -%>
\ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap.html.erb index 61e67e7c8a4..d82c2b92b87 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap.html.erb @@ -1,9 +1,8 @@ -
+
<%= treemap.generate_html() -%>
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap_container.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap_container.html.erb index b6b533af1fd..211bc5981c4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap_container.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/treemap/_treemap_container.html.erb @@ -5,14 +5,14 @@ <%= message('size') -%>
<%= 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)" %> <%= message('color') -%>
<%= 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' -%> @@ -20,8 +20,7 @@
- -
+
Left click to zoom in. Right click to zoom out.
diff --git a/sonar-server/src/main/webapp/javascripts/application.js b/sonar-server/src/main/webapp/javascripts/application.js index eb13eac4303..c79ebabffaa 100644 --- a/sonar-server/src/main/webapp/javascripts/application.js +++ b/sonar-server/src/main/webapp/javascripts/application.js @@ -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; -- 2.39.5