summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-06-17 18:23:46 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-06-17 18:23:46 +0200
commit81f15087e56ee50062935f020aeb72ebdf431693 (patch)
treefc784743507aacfcced5fd022a04ac8589eeb4cf
parent5deaa414aab4576845a34679a19ada7c8ed037c1 (diff)
downloadsonarqube-81f15087e56ee50062935f020aeb72ebdf431693.tar.gz
sonarqube-81f15087e56ee50062935f020aeb72ebdf431693.zip
SONAR-5149 Display some stats on the "Coding Rules" page of Quality Profiles
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java24
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb2
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/profiles/show.html.erb92
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java22
4 files changed, 120 insertions, 20 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
index 36263b08b8d..3dae58e5d85 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
@@ -19,10 +19,10 @@
*/
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();
+ }
}
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
index 5c0129b34dc..0be0572ec01 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
@@ -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
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/show.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/show.html.erb
index a4d9e3237ba..2d3c5accc13 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/show.html.erb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/show.html.erb
@@ -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>
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java
index 9e71e376876..a535a62b898 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java
@@ -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);
+ }
}