]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2032 Delete the measures on metric 'violations' and rule priority
authorsimonbrandhof <simon.brandhof@gmail.com>
Mon, 6 Dec 2010 22:14:24 +0000 (22:14 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Mon, 6 Dec 2010 22:14:24 +0000 (22:14 +0000)
plugins/sonar-core-gwt/src/main/java/org/sonar/plugins/core/violationsviewer/client/ViolationsViewer.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ViolationsDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ViolationsDecoratorTest.java
sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/drilldown_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/models/drilldown.rb
sonar-server/src/main/webapp/WEB-INF/app/views/drilldown/_rule_priority.erb
sonar-server/src/main/webapp/WEB-INF/app/views/drilldown/violations.html.erb
sonar-server/src/main/webapp/WEB-INF/db/migrate/164_delete_measures_on_violations_and_priority.rb [new file with mode: 0644]

index 2ad7caa8bbbefc61af3bbb8f7fdb804c2eaf8bd4..2afccc73f57354d794210f6504015cdbcf160758 100644 (file)
@@ -38,7 +38,6 @@ import org.sonar.wsclient.services.ResourceQuery;
 
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.List;
 
 public class ViolationsViewer extends Page {
   public static final String GWT_ID = "org.sonar.plugins.core.violationsviewer.ViolationsViewer";
@@ -119,8 +118,9 @@ public class ViolationsViewer extends Page {
   }
 
   private void loadRulePriorities() {
-    final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.VIOLATIONS)
-        .setExcludeRulePriorities(false);
+    final ResourceQuery query = ResourceQuery.createForResource(resource, Metrics.BLOCKER_VIOLATIONS,
+        Metrics.CRITICAL_VIOLATIONS, Metrics.MAJOR_VIOLATIONS, Metrics.MINOR_VIOLATIONS, Metrics.INFO_VIOLATIONS)
+        .setExcludeRulePriorities(true);
     Sonar.getInstance().find(query, new AbstractCallback<Resource>(loading) {
       @Override
       protected void doOnResponse(Resource resource) {
@@ -135,26 +135,23 @@ public class ViolationsViewer extends Page {
     final Grid grid = new Grid(1, 10);
     header.setWidget(0, 0, grid);
 
-    List<Measure> measures = resource.getMeasures();
-    displayRulePriority(grid, 0, "BLOCKER", measures);
-    displayRulePriority(grid, 2, "CRITICAL", measures);
-    displayRulePriority(grid, 4, "MAJOR", measures);
-    displayRulePriority(grid, 6, "MINOR", measures);
-    displayRulePriority(grid, 8, "INFO", measures);
+    displayRulePriority(grid, 0, "BLOCKER", resource.getMeasure(Metrics.BLOCKER_VIOLATIONS));
+    displayRulePriority(grid, 2, "CRITICAL", resource.getMeasure(Metrics.CRITICAL_VIOLATIONS));
+    displayRulePriority(grid, 4, "MAJOR", resource.getMeasure(Metrics.MAJOR_VIOLATIONS));
+    displayRulePriority(grid, 6, "MINOR", resource.getMeasure(Metrics.MINOR_VIOLATIONS));
+    displayRulePriority(grid, 8, "INFO", resource.getMeasure(Metrics.INFO_VIOLATIONS));
   }
 
-  private void displayRulePriority(final Grid grid, final int column, final String priority, final List<Measure> measures) {
+  private void displayRulePriority(final Grid grid, final int column, final String priority, final Measure measure) {
     String value = "0";
-    for (Measure measure : measures) {
-      if (priority.equals(measure.getRulePriority())) {
-        value = measure.getFormattedValue();
-        filterBox.addItem(priority + " (" + value + ")", priority);
-        if (priority.equals(defaultFilter)) {
-          filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
-        }
-        continue;
+    if (measure != null) {
+      value = measure.getFormattedValue();
+      filterBox.addItem(priority + " (" + value + ")", priority);
+      if (priority.equals(defaultFilter)) {
+        filterBox.setSelectedIndex(filterBox.getItemCount() - 1);
       }
     }
+
     grid.setHTML(0, column, Icons.forPriority(priority).getHTML());
     grid.setHTML(0, column + 1, value);
     grid.getCellFormatter().setStyleName(0, column, "thin metric right");
index c52501beec9de18feca8b9706affaa055f386da2..79bbf2da1adcb2c08bbc5b257e4d76c351017946 100644 (file)
@@ -79,10 +79,10 @@ public class ViolationsDecorator implements Decorator {
 
   private void saveViolationsByPriority(DecoratorContext context) {
     for (RulePriority priority : RulePriority.values()) {
-      Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.rulePriority(CoreMetrics.VIOLATIONS, priority));
+      Metric metric = getMetricForPriority(priority);
+      Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.metric(metric));
       double sum = MeasureUtils.sum(true, children) + priorities.count(priority);
-      context.saveMeasure(RuleMeasure.createForPriority(CoreMetrics.VIOLATIONS, priority, sum));
-      context.saveMeasure(new Measure(getMetricForPriority(priority), sum));
+      context.saveMeasure(new Measure(metric, sum));
     }
   }
 
index 7ea119a24ac57012f86f4567254419e5c57b0b62..866d02d4d19c7bb871eb2b585a7490c7667a5c09 100644 (file)
@@ -115,12 +115,6 @@ public class ViolationsDecoratorTest {
 
     decorator.decorate(resource, context);
 
-    verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.VIOLATIONS, null, RulePriority.BLOCKER, 0.0)));
-    verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.VIOLATIONS, null, RulePriority.CRITICAL, 2.0)));
-    verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.VIOLATIONS, null, RulePriority.MAJOR, 1.0)));
-    verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.VIOLATIONS, null, RulePriority.MINOR, 1.0)));
-    verify(context).saveMeasure(argThat(new IsRuleMeasure(CoreMetrics.VIOLATIONS, null, RulePriority.INFO, 0.0)));
-
     verify(context).saveMeasure(argThat(new IsMeasure(CoreMetrics.BLOCKER_VIOLATIONS, 0.0)));
     verify(context).saveMeasure(argThat(new IsMeasure(CoreMetrics.CRITICAL_VIOLATIONS, 2.0)));
     verify(context).saveMeasure(argThat(new IsMeasure(CoreMetrics.MAJOR_VIOLATIONS, 1.0)));
index 6ab6aa2a9025c84cf673aaa58da835d46e4aef8e..8051214b04e5fa5bd3ed02080209859767987049 100644 (file)
@@ -30,7 +30,7 @@ import java.sql.Statement;
 public class SchemaMigration {
 
   public final static int VERSION_UNKNOWN = -1;
-  public static final int LAST_VERSION = 163;
+  public static final int LAST_VERSION = 164;
 
   public final static String TABLE_NAME = "schema_migrations";
 
index 2266645512150a3bd2a2959dd1a80ebb8e857967..dcbd183408f6ffad6c793090a30f86c28797a0c4 100644 (file)
@@ -62,7 +62,15 @@ class DrilldownController < ApplicationController
   end
 
   def violations
-    @metric = select_metric(params[:metric], 'violations')
+    @rule=Rule.by_key_or_id(params[:rule])
+
+    if @rule.nil? && params[:priority]
+      @metric = Metric::by_key("#{params[:priority].downcase}_violations")
+      @priority_id=Sonar::RulePriority.id(params[:priority])
+    else
+      @metric = Metric::by_key('violations')
+      @priority_id=nil
+    end
 
     # selected resources
     if params[:rids]
@@ -76,12 +84,8 @@ class DrilldownController < ApplicationController
     @selected_rids=@selected_rids.map{|r|r.to_i}
 
 
-    # options
-    @rule=Rule.by_key_or_id(params[:rule])
-    @priority_id = (params[:priority] ? Sonar::RulePriority.id(params[:priority]) : nil)
-
-    options={}
-    options[:rule_priority_id]=@priority_id
+    # options for Drilldown
+    options={:exclude_zero_value => true}
     if @rule
       params[:rule]=@rule.key  # workaround for SONAR-1767 : the javascript hash named "rp" in the HTML source must contain the rule key, but not the rule id
       options[:rule_id]=@rule.id
index 3fd9db9b5c00b161ae5debc504b20713fa392161..d661c6af2b7bfb206f3bd9c910330e8ad44193cf 100644 (file)
@@ -44,18 +44,20 @@ class DrilldownColumn
   def initialize(snapshot, metric, scope, selected_resource_ids, options)
     @scope=scope
     @snapshot = snapshot
-    order='project_measures.value'
+
+    value_column = (options[:variation_index] ? "variation_value_#{options[:variation_index]}" : 'value')
+    order="project_measures.#{value_column}"
     if metric.direction<0
       order += ' DESC'
     end
 
     conditions='snapshots.root_snapshot_id=:root_sid AND snapshots.islast=:islast AND snapshots.scope=:scope AND snapshots.path LIKE :path AND project_measures.metric_id=:metric_id'
 
-    if metric.key=='violations'
-      conditions += ' AND project_measures.value>0'
+    if options[:exclude_zero_value]
+      conditions += " AND project_measures.#{value_column}<>0"
     end
 
-    values={
+    condition_values={
       :root_sid => (snapshot.root_snapshot_id || snapshot.id),
       :islast=>true,
       :scope => scope,
@@ -64,29 +66,23 @@ class DrilldownColumn
 
     if options[:rule_id]
       conditions += ' AND project_measures.rule_id=:rule'
-      values[:rule]=options[:rule_id]
+      condition_values[:rule]=options[:rule_id]
     else
-      conditions += ' AND project_measures.rule_id IS NULL'
+      conditions += ' AND project_measures.rule_id IS NULL AND project_measures.rule_priority IS NULL '
     end
 
-    if options[:rule_priority_id]
-      conditions += ' AND project_measures.rule_priority=:priority'
-      values[:priority]=options[:rule_priority_id]
-    elsif options[:rule_id].nil?
-      conditions += ' AND project_measures.rule_priority IS NULL'
-    end
 
     if options[:characteristic]
       conditions += ' AND project_measures.characteristic_id=:characteristic_id'
-      values[:characteristic_id]=options[:characteristic].id
+      condition_values[:characteristic_id]=options[:characteristic].id
     else
       conditions += ' AND project_measures.characteristic_id IS NULL'
     end
 
     @measures=ProjectMeasure.find(:all,
-      :select => 'project_measures.id,project_measures.metric_id,project_measures.value,project_measures.text_value,project_measures.alert_status,project_measures.snapshot_id',
+      :select => "project_measures.id,project_measures.metric_id,project_measures.#{value_column},project_measures.text_value,project_measures.alert_status,project_measures.snapshot_id",
       :joins => :snapshot,
-      :conditions => [conditions,values],
+      :conditions => [conditions, condition_values],
       :order => order,
       :limit => 200)
 
index 3931990aa4ad1c79cb3ade637e8c5b77c26ca961..d15af616eaa3e056f475e154d36d9b246ae72e24 100644 (file)
@@ -1,15 +1,10 @@
-<tr class="<%= css -%> <%= 'selected' if @priority_id==priority_id -%>">
+<tr class="<%= css -%> <%= 'selected' if Sonar::RulePriority.to_s(priority_id)==params[:priority] -%>">
        <td><%= image_tag "priority/#{priority_id}.png" %></td>
-    <td>
-               <%= link_to label,
-                  {:controller => 'drilldown', :action => 'violations', :id => @project.id, :priority => Sonar::RulePriority.to_s(priority_id)} %>
-
-    </td>
-    <td style="padding-left: 10px;" align="right">
-           <% measure = measures.select{|m| m.rule_priority==priority_id}.first %>
-        <%= format_measure(measure) -%>
-      </td>
-      <td align="left">
-           <%= barchart(:width => 60, :percent => (measure ? (100 * measure.value / max).to_i : 0), :color => '#777') if max>0  %>
-      </td>
+  <td><%= link_to label, {:controller => 'drilldown', :action => 'violations', :id => @project.id, :priority => Sonar::RulePriority.to_s(priority_id)} %></td>
+  <td style="padding-left: 10px;" align="right">
+         <%= format_measure(measure) -%>
+  </td>
+  <td align="left">
+    <%= barchart(:width => 60, :percent => (measure ? (100 * measure.value / max).to_i : 0), :color => '#777') if max>0  %>
+  </td>
 </tr>
\ No newline at end of file
index 7732df8577ca4419ee1a150d0f2591850c06d526..090af3bdb3a62f8675a7cc0e399e955686486070 100644 (file)
@@ -6,20 +6,25 @@
 <tr>
   <td align="left" width="1%" nowrap class="column first">
 
-       <% 
-               rule_priority_measures = @snapshot.rule_priority_measures(Metric::VIOLATIONS)
+       <%
                max = 0
-               rule_priority_measures.each do |m|
-                       max = m.value if m.value and m.value>max
-               end
+               blocker_violations=@snapshot.measure('blocker_violations')
+               critical_violations=@snapshot.measure('critical_violations')
+               major_violations=@snapshot.measure('major_violations')
+               minor_violations=@snapshot.measure('minor_violations')
+               info_violations=@snapshot.measure('info_violations')
+
+         [blocker_violations, critical_violations, major_violations, minor_violations, info_violations].each do |m|
+           max = m.value if m && m.value && m.value>max
+         end
        %>
     <h3>Severity</h3>
     <table class="spacedicon" style="border: 1px solid #ccc;">
-         <%= render :partial => 'rule_priority', :locals => {:label => 'Blocker', :css => 'even', :priority_id => Sonar::RulePriority::PRIORITY_BLOCKER, :max => max, :measures => rule_priority_measures }%>
-       <%= render :partial => 'rule_priority', :locals => {:label => 'Critical', :css => 'odd', :priority_id => Sonar::RulePriority::PRIORITY_CRITICAL, :max => max, :measures => rule_priority_measures }%>
-         <%= render :partial => 'rule_priority', :locals => {:label => 'Major', :css => 'even', :priority_id => Sonar::RulePriority::PRIORITY_MAJOR, :max => max, :measures => rule_priority_measures }%>
-         <%= render :partial => 'rule_priority', :locals => {:label => 'Minor', :css => 'odd', :priority_id => Sonar::RulePriority::PRIORITY_MINOR, :max => max, :measures => rule_priority_measures }%>
-         <%= render :partial => 'rule_priority', :locals => {:label => 'Info', :css => 'even', :priority_id => Sonar::RulePriority::PRIORITY_INFO, :max => max, :measures => rule_priority_measures }%>
+         <%= render :partial => 'rule_priority', :locals => {:label => 'Blocker', :css => 'even', :priority_id => Sonar::RulePriority::PRIORITY_BLOCKER, :max => max, :measure => blocker_violations }%>
+       <%= render :partial => 'rule_priority', :locals => {:label => 'Critical', :css => 'odd', :priority_id => Sonar::RulePriority::PRIORITY_CRITICAL, :max => max, :measure => critical_violations }%>
+         <%= render :partial => 'rule_priority', :locals => {:label => 'Major', :css => 'even', :priority_id => Sonar::RulePriority::PRIORITY_MAJOR, :max => max, :measure => major_violations }%>
+         <%= render :partial => 'rule_priority', :locals => {:label => 'Minor', :css => 'odd', :priority_id => Sonar::RulePriority::PRIORITY_MINOR, :max => max, :measure => minor_violations }%>
+         <%= render :partial => 'rule_priority', :locals => {:label => 'Info', :css => 'even', :priority_id => Sonar::RulePriority::PRIORITY_INFO, :max => max, :measure => info_violations }%>
     </table>
   </td>
   <td class="column" align="left" style="white-space: normal;">
@@ -27,7 +32,7 @@
    <div class="scrollable">
        <table class="spacedicon" width="100%" id="col_rules">
              <%
-               rule_measures=@snapshot.rule_measures(Metric.by_key(Metric::VIOLATIONS), @priority_id)
+               rule_measures=@snapshot.rule_measures(Metric.by_key('violations'), @priority_id)
                max=0
                rule_index=0
                        rule_measures.each { |m| max=m.value if m.value>max }
 <% if @priority_id %>
 <%= Sonar::RulePriority.to_s(@priority_id) %> <%= link_to 'clear', {:overwrite_params => {:priority => nil}} %>
 <% else %>
-Any <%= @filter %>
+Any severity
 <% end %>&nbsp;&raquo;&nbsp;
 <% if @rule %>
 <%= h @rule.name %> <%= link_to 'clear', {:overwrite_params => {:rule => nil}} %>
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/164_delete_measures_on_violations_and_priority.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/164_delete_measures_on_violations_and_priority.rb
new file mode 100644 (file)
index 0000000..b3178df
--- /dev/null
@@ -0,0 +1,31 @@
+#
+# Sonar, entreprise quality control tool.
+# Copyright (C) 2009 SonarSource SA
+# mailto:contact AT sonarsource DOT com
+#
+# Sonar is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 3 of the License, or (at your option) any later version.
+#
+# Sonar is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Sonar; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+#
+
+#
+# Sonar 2.5
+#
+class DeleteMeasuresOnViolationsAndPriority < ActiveRecord::Migration
+
+  def self.up
+    puts "If the following step fails, please execute the SQL request 'DELETE FROM PROJECT_MEASURES WHERE RULE_ID IS NULL AND RULE_PRIORITY IS NOT NULL' and restart Sonar."
+    ProjectMeasure.delete_all('rule_id is null and rule_priority is not null')
+  end
+
+end