From 829a11b68ec30a5a764fd130518bfece786af9cc Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Wed, 26 Oct 2011 15:07:45 +0200 Subject: [PATCH] SONAR-1928 Improve performances by splitting the SQL query in 2 --- .../core/widgets/hotspot_metric.html.erb | 44 ++++++++++-------- .../hotspot_most_violated_resources.html.erb | 45 +++++++++++-------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_metric.html.erb b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_metric.html.erb index 894d54a5c8b..94769ac1b4e 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_metric.html.erb +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_metric.html.erb @@ -13,21 +13,30 @@ end snapshots = nil - if metric.numeric? - snapshots_conditions = ["snapshots.scope = 'FIL'", "project_measures.rule_id IS NULL", "project_measures.characteristic_id IS NULL"] - snapshots_values = {} - snapshots_conditions << "snapshots.path LIKE :path" - snapshots_values[:path] = "#{@snapshot.path}#{@snapshot.id}.%" - snapshots_conditions << "project_measures.metric_id = :m_id" - snapshots_values[:m_id] = metric.id - snapshots_conditions << "snapshots.root_project_id = :root_id" - snapshots_values[:root_id] = @snapshot.root_project_id - - snapshots=Snapshot.find(:all, - :conditions => [snapshots_conditions.join(' AND '), snapshots_values], - :include => ['project', 'measures'], - :order => "project_measures.value #{'DESC' if metric.direction<0}", - :limit => limit) + if metric.numeric? + snapshots_conditions=["snapshots.scope = 'FIL'", "snapshots.islast=:islast"] + snapshots_values={:islast => true} + snapshots_conditions << '(snapshots.id=:sid OR (snapshots.root_snapshot_id=:root_sid AND snapshots.path LIKE :path))' + snapshots_values[:sid]=@snapshot.id + snapshots_values[:root_sid] = (@snapshot.root_snapshot_id || @snapshot.id) + snapshots_values[:path]="#{@snapshot.path}#{@snapshot.id}.%" + + measures_conditions = ["project_measures.rule_id IS NULL", "project_measures.characteristic_id IS NULL"] + measures_values = {} + measures_conditions << "project_measures.metric_id = :m_id" + measures_values[:m_id] = metric.id + + measures=ProjectMeasure.find(:all, + :joins => :snapshot, + :conditions => [ (snapshots_conditions + measures_conditions).join(' AND '), snapshots_values.merge(measures_values)], + :order => "project_measures.value #{'DESC' if metric.direction<0}", + :limit => limit) + + snapshots=Snapshot.find(measures.map {|m| m.snapshot_id}, :include => 'project') + snapshots_by_id = {} + snapshots.each do |s| + snapshots_by_id[s.id]=s + end end %> @@ -53,9 +62,8 @@ else metric_max_value = snapshots.first.measure(metric).value end - snapshots.each do |s| - measure = s.measure(metric) - resource = s.resource + measures.each do |measure| + resource = snapshots_by_id[measure.snapshot_id].resource %> diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_most_violated_resources.html.erb b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_most_violated_resources.html.erb index 8685fc47ce8..3f457c7dfbf 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_most_violated_resources.html.erb +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_most_violated_resources.html.erb @@ -6,23 +6,30 @@ metric = Metric.find(:first, :conditions => "name = 'weighted_violations'") - snapshots_conditions = ["snapshots.scope = 'FIL'"] - snapshots_values = {} + snapshots_conditions=["snapshots.scope = 'FIL'", "snapshots.islast=:islast"] + snapshots_values={:islast => true} snapshots_conditions << "(snapshots.qualifier = 'CLA' OR snapshots.qualifier = 'FIL' OR snapshots.qualifier = 'TRK')" - snapshots_conditions << "project_measures.rule_id IS NULL" - snapshots_conditions << "project_measures.characteristic_id IS NULL" - snapshots_conditions << "snapshots.path LIKE :path" - snapshots_values[:path] = "#{@snapshot.path}#{@snapshot.id}.%" - snapshots_conditions << "project_measures.metric_id = :m_id" - snapshots_values[:m_id] = metric.id - snapshots_conditions << "snapshots.root_project_id = :root_id" - snapshots_values[:root_id] = @snapshot.root_project_id - - snapshots=Snapshot.find(:all, - :conditions => [snapshots_conditions.join(' AND '), snapshots_values], - :include => ['project', 'measures'], - :order => "project_measures.value DESC", - :limit => limit) + snapshots_conditions << '(snapshots.id=:sid OR (snapshots.root_snapshot_id=:root_sid AND snapshots.path LIKE :path))' + snapshots_values[:sid]=@snapshot.id + snapshots_values[:root_sid] = (@snapshot.root_snapshot_id || @snapshot.id) + snapshots_values[:path]="#{@snapshot.path}#{@snapshot.id}.%" + + measures_conditions = ["project_measures.rule_id IS NULL", "project_measures.characteristic_id IS NULL"] + measures_values = {} + measures_conditions << "project_measures.metric_id = :m_id" + measures_values[:m_id] = metric.id + + measures=ProjectMeasure.find(:all, + :joins => :snapshot, + :conditions => [ (snapshots_conditions + measures_conditions).join(' AND '), snapshots_values.merge(measures_values)], + :order => "project_measures.value #{'DESC' if metric.direction<0}", + :limit => limit) + + snapshots=Snapshot.find(measures.map {|m| m.snapshot_id}, :include => 'project') + snapshots_by_id = {} + snapshots.each do |s| + snapshots_by_id[s.id]=s + end %>
@@ -36,10 +43,10 @@ <% - snapshots.each do |s| - resource = s.resource + measures.each do |measure| + resource = snapshots_by_id[measure.snapshot_id].resource violations_per_severity={} - s.measure(metric).text_value.split(';').each do |part| + measure.text_value.split(';').each do |part| fields=part.split('=') violations_per_severity[fields[0]]=fields[1] end -- 2.39.5