From: Julien Lancelot Date: Tue, 17 Jun 2014 16:23:46 +0000 (+0200) Subject: SONAR-5149 Display some stats on the "Coding Rules" page of Quality Profiles X-Git-Tag: 4.4-RC1~375 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=81f15087e56ee50062935f020aeb72ebdf431693;p=sonarqube.git SONAR-5149 Display some stats on the "Coding Rules" page of Quality Profiles --- 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 countAllActiveRules() { Map counts = new HashMap(); for (Map.Entry entry : index.get(ActiveRuleIndex.class) @@ -224,10 +225,19 @@ public class QProfileService implements ServerComponent { } public Map> getAllProfileStats() { - List keys = Lists.newArrayList(); + List 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 @@

- "><%= @active_rule_count -%> <%= message('coding_rules._rules') -%> -

-
-

- Inheritance:
- <%= @stats.get('inheritance') if @stats -%> -

-
-

- Severity:
- <%= @stats.get('severity') if @stats -%> + <% active_rules = @stats.get('countActiveRules').get(0).getValue() if @stats && @stats.get('countActiveRules') %> + <% active_rules ||= 0 %> + "><%= active_rules -%> <%= message('coding_rules._rules') -%>

+ <% if @stats %> + + <% if @deprecated_active_rules > 0 %> +
+ + <% + key = 'DEPRECATED' + status = key.downcase + value = @deprecated_active_rules + -%> + + + + +
+ <%= message('rules.status.' + status).upcase -%> + + "><%= value -%> +
+ <% end -%> + + <% inheritance = @stats.get('inheritance') + if inheritance.size > 1 + inheritance_stats = Hash[ *@stats.get('inheritance').collect { |v| [ v.getKey(), v ] }.flatten ] -%> +
+ + <% if inheritance_stats['NONE'] %> + + + + + <% end -%> + <% if inheritance_stats['INHERITED'] %> + + + + + <% end -%> + <% if inheritance_stats['OVERRIDES'] %> + + + + + <% end -%> +
+ <%= message('coding_rules.filters.inheritance.not_inherited') -%> + + <%= inheritance_stats['NONE'].getValue() -%> +
+ <%= message('coding_rules.filters.inheritance.inherited') -%> + + <%= inheritance_stats['INHERITED'].getValue() -%> +
+ <%= message('coding_rules.filters.inheritance.overriden') -%> + + <%= inheritance_stats['OVERRIDES'].getValue() -%> +
+ <% end -%> + +
+ + + <% @stats.get('severity').each do |stat| + key = stat.getKey() + severity = stat.getKey().downcase + value = stat.getValue() + -%> + + + + + <% end -%> +
+ <%= message(severity) -%> + + "><%= value -%> +
+ <% end -%>
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); + } }