*/
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;
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;
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;
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;
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)
}
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();
+ }
}
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
<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>
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;
XOO_PROFILE_1, XOO_PROFILE_2
);
assertThat(counts.values()).containsOnly(1L, 1L);
-
}
@Test
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);
+ }
}