]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5149 Display some stats on the "Coding Rules" page of Quality Profiles
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 17 Jun 2014 16:23:46 +0000 (18:23 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 17 Jun 2014 16:23:46 +0000 (18:23 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/views/profiles/show.html.erb
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java

index 36263b08b8de01e5192b43cd989424285dc23110..3dae58e5d850518db46384750ada58c87aed2bad 100644 (file)
  */
 package org.sonar.server.qualityprofile;
 
-import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
@@ -31,13 +31,16 @@ import org.sonar.core.qualityprofile.db.QualityProfileKey;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
+import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.search.FacetValue;
 import org.sonar.server.search.IndexClient;
+import org.sonar.server.search.QueryOptions;
 import org.sonar.server.user.UserSession;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -47,6 +50,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import static com.google.common.collect.Lists.newArrayList;
+
 public class QProfileService implements ServerComponent {
 
   private final DbClient db;
@@ -58,7 +63,7 @@ public class QProfileService implements ServerComponent {
   private final QProfileReset reset;
 
   public QProfileService(DbClient db, IndexClient index, RuleActivator ruleActivator, QProfileFactory factory, QProfileBackuper backuper,
-                         QProfileCopier copier, QProfileReset reset) {
+    QProfileCopier copier, QProfileReset reset) {
     this.db = db;
     this.index = index;
     this.ruleActivator = ruleActivator;
@@ -206,10 +211,6 @@ public class QProfileService implements ServerComponent {
     UserSession.get().checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
   }
 
-  public long countActiveRulesByProfile(QualityProfileKey key) {
-    return index.get(ActiveRuleIndex.class).countByQualityProfileKey(key);
-  }
-
   public Map<QualityProfileKey, Long> countAllActiveRules() {
     Map<QualityProfileKey, Long> counts = new HashMap<QualityProfileKey, Long>();
     for (Map.Entry<String, Long> entry : index.get(ActiveRuleIndex.class)
@@ -224,10 +225,19 @@ public class QProfileService implements ServerComponent {
   }
 
   public Map<QualityProfileKey, Multimap<String, FacetValue>> getAllProfileStats() {
-    List<QualityProfileKey> keys = Lists.newArrayList();
+    List<QualityProfileKey> keys = newArrayList();
     for (QualityProfileDto profile : this.findAll()) {
       keys.add(profile.getKey());
     }
     return index.get(ActiveRuleIndex.class).getStatsByProfileKeys(keys);
   }
+
+  public long countDeprecatedActiveRulesByProfile(QualityProfileKey key) {
+    return index.get(RuleIndex.class).search(
+      new RuleQuery()
+        .setQProfileKey(key.toString())
+        .setActivation(true)
+        .setStatuses(newArrayList(RuleStatus.DEPRECATED)),
+      new QueryOptions().setLimit(0)).getTotal();
+  }
 }
index 5c0129b34dcdf66c321b4f35f67d0ecfe69f0142..0be0572ec01962fb594608c21e92bb83a62d2bd0 100644 (file)
@@ -41,7 +41,7 @@ class ProfilesController < ApplicationController
     call_backend do
       @profile = Internal.quality_profiles.profile(params[:id].to_i)
       not_found('Profile not found') unless @profile
-      @active_rule_count = Internal.component(Java::OrgSonarServerQualityprofile::QProfileService.java_class).countActiveRulesByProfile(@profile.key())
+      @deprecated_active_rules = Internal.component(Java::OrgSonarServerQualityprofile::QProfileService.java_class).countDeprecatedActiveRulesByProfile(@profile.key())
       @stats = Internal.component(Java::OrgSonarServerQualityprofile::QProfileService.java_class).getStatsByProfile(@profile.key())
     end
     set_profile_breadcrumbs
index a4d9e3237ba9f7f03069f24740fb188db2b01fcd..2d3c5accc13579fb2ca88a46a670209ebefa3671 100644 (file)
@@ -3,17 +3,87 @@
 
   <div class="tabs-panel marginbottom10 ">
     <p>
-        <a href="<%= "#{ApplicationController.root_context}/coding_rules#qprofile=#{@profile.key().toString()}|activation=true|languages=#{@profile.language()}" -%>"><%= @active_rule_count -%> <%= message('coding_rules._rules') -%></a>
-    </p>
-    <br/>
-    <p>
-      Inheritance:<br/>
-      <%= @stats.get('inheritance') if @stats -%>
-    </p>
-    <br/>
-    <p>
-      Severity:<br/>
-      <%= @stats.get('severity') if @stats -%>
+      <% active_rules = @stats.get('countActiveRules').get(0).getValue() if @stats && @stats.get('countActiveRules') %>
+      <% active_rules ||= 0 %>
+      <a href="<%= "#{ApplicationController.root_context}/coding_rules#qprofile=#{@profile.key().toString()}|activation=true|languages=#{@profile.language()}" -%>"><%= active_rules -%> <%= message('coding_rules._rules') -%></a>
     </p>
+    <% if @stats %>
+
+      <% if @deprecated_active_rules > 0 %>
+        <br/>
+        <table class="data" style="width:120px">
+          <%
+            key = 'DEPRECATED'
+            status = key.downcase
+            value = @deprecated_active_rules
+          -%>
+          <tr>
+            <td>
+              <%= message('rules.status.' + status).upcase -%>
+            </td>
+            <td class="thin left">
+              <a href="<%= "#{ApplicationController.root_context}/coding_rules#qprofile=#{@profile.key().toString()}|activation=true|languages=#{@profile.language()}|statuses=#{key}" -%>"><%= value -%></a>
+            </td>
+          </tr>
+        </table>
+      <% end -%>
+
+      <% inheritance = @stats.get('inheritance')
+         if inheritance.size > 1
+           inheritance_stats = Hash[ *@stats.get('inheritance').collect { |v| [ v.getKey(), v ] }.flatten ] -%>
+        <br/>
+        <table class="data" style="width:120px">
+            <% if inheritance_stats['NONE'] %>
+              <tr>
+                <td>
+                  <%= message('coding_rules.filters.inheritance.not_inherited') -%>
+                </td>
+                <td class="thin left">
+                  <%= inheritance_stats['NONE'].getValue() -%>
+                </td>
+              </tr>
+            <% end -%>
+            <% if inheritance_stats['INHERITED'] %>
+              <tr>
+                <td>
+                  <%= message('coding_rules.filters.inheritance.inherited') -%>
+                </td>
+                <td class="thin left">
+                  <%= inheritance_stats['INHERITED'].getValue() -%>
+                </td>
+              </tr>
+            <% end -%>
+            <% if inheritance_stats['OVERRIDES'] %>
+              <tr>
+                <td>
+                  <%= message('coding_rules.filters.inheritance.overriden') -%>
+                </td>
+                <td class="thin left">
+                  <%= inheritance_stats['OVERRIDES'].getValue() -%>
+                </td>
+              </tr>
+            <% end -%>
+        </table>
+      <% end -%>
+
+      <br/>
+
+      <table class="data" style="width:120px">
+        <% @stats.get('severity').each do |stat|
+          key = stat.getKey()
+          severity = stat.getKey().downcase
+          value = stat.getValue()
+        -%>
+          <tr>
+            <td>
+              <i class="icon-severity-<%= severity %>"></i><%= message(severity) -%>
+            </td>
+            <td class="thin left">
+              <a href="<%= "#{ApplicationController.root_context}/coding_rules#qprofile=#{@profile.key().toString()}|activation=true|languages=#{@profile.language()}|severities=#{key}" -%>"><%= value -%></a>
+            </td>
+          </tr>
+        <% end -%>
+      </table>
+    <% end -%>
   </div>
 </div>
index 9e71e376876db8423fea663719c945f139c902bb..a535a62b898056a1013d6c794e8a9bad03d1df76 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
@@ -102,7 +103,6 @@ public class QProfileServiceMediumTest {
       XOO_PROFILE_1, XOO_PROFILE_2
     );
     assertThat(counts.values()).containsOnly(1L, 1L);
-
   }
 
   @Test
@@ -125,4 +125,24 @@ public class QProfileServiceMediumTest {
     assertThat(stats.get(XOO_PROFILE_1).get(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field()).size()).isEqualTo(1);
     assertThat(stats.get(XOO_PROFILE_1).get("countActiveRules").size()).isEqualTo(1);
   }
+
+  @Test
+  public void count_by_deprecated() throws Exception {
+    MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");
+
+    // create deprecated rule
+    RuleDto deprecatedXooRule = RuleTesting.newDto(RuleKey.of("xoo", "deprecated1"))
+      .setSeverity("MINOR").setLanguage("xoo").setStatus(RuleStatus.DEPRECATED);
+    db.ruleDao().insert(dbSession, deprecatedXooRule);
+    dbSession.commit();
+
+    // active some rules
+    service.activate(new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_1, deprecatedXooRule.getKey()))
+      .setSeverity("BLOCKER"));
+    service.activate(new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_1, XOO_RULE_1))
+      .setSeverity("BLOCKER"));
+    dbSession.commit();
+
+    assertThat(service.countDeprecatedActiveRulesByProfile(XOO_PROFILE_1)).isEqualTo(1);
+  }
 }