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.RuleQuery;
import org.sonar.server.search.IndexClient;
import org.sonar.server.user.UserSession;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
public class QProfileService implements ServerComponent {
UserSession.get().checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
}
- public Long countActiveRulesByProfile(QualityProfileKey key) {
+ public long countActiveRulesByProfile(QualityProfileKey key) {
return index.get(ActiveRuleIndex.class).countByQualityProfileKey(key);
}
+
+ public Map<String, Long> countAllActiveRules() {
+ return index.get(ActiveRuleIndex.class).countByField(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY);
+ }
}
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.aggregations.AggregationBuilders;
+import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.core.cluster.WorkQueue;
.setTypes(this.getIndexType())
.get().getCount();
}
+
+ public Map<String, Long> countByField(IndexField indexField) {
+ Map<String, Long> counts = new HashMap<String, Long>();
+ Terms values = getClient().prepareSearch(this.getIndexName())
+ .setTypes(this.getIndexType())
+ .setQuery(QueryBuilders.matchAllQuery())
+ .setSize(0)
+ .addAggregation(AggregationBuilders
+ .terms(indexField.field())
+ .field(indexField.field())
+ .order(Terms.Order.count(false))
+ .size(Integer.MAX_VALUE)
+ .minDocCount(0)).get()
+ .getAggregations().get(indexField.field());
+
+ for (Terms.Bucket value : values.getBuckets()) {
+ counts.put(value.getKey(), value.getDocCount());
+ }
+ return counts;
+ }
}
import org.sonar.core.rule.RuleParamDto;
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.RuleTesting;
import org.sonar.server.tester.ServerTester;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
// 1. Assert by profileKey
assertThat(index.countByQualityProfileKey(profileDto1.getKey())).isEqualTo(1);
+
+ // 2. Assert by term aggregation;
+ Map<String, Long> counts = index.countByField(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY);
+ assertThat(counts).hasSize(2);
+ assertThat(counts.values()).containsOnly(1L, 1L);
+ assertThat(counts.keySet()).containsOnly(profileDto1.getKey().toString(), profileDto2.getKey().toString());
+ }
+
+ @Test
+ public void count_all_by_index_field() {
+ QualityProfileDto profileDto1 = QualityProfileDto.createFor("p1", "java");
+ QualityProfileDto profileDto2 = QualityProfileDto.createFor("p2", "java");
+ db.qualityProfileDao().insert(dbSession, profileDto1, profileDto2);
+
+ RuleKey ruleKey = RuleKey.of("javascript", "S001");
+ RuleDto ruleDto = newRuleDto(ruleKey);
+ db.ruleDao().insert(dbSession, ruleDto);
+
+ ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(profileDto1, ruleDto).setSeverity(Severity.MAJOR);
+ ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(profileDto2, ruleDto).setSeverity(Severity.MAJOR);
+ db.activeRuleDao().insert(dbSession, activeRule1, activeRule2);
+ dbSession.commit();
+
+ // 0. Test base case
+ assertThat(index.countAll()).isEqualTo(2);
+
+ // 1. Assert by term aggregation;
+ Map<String, Long> counts = index.countByField(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY);
+ assertThat(counts).hasSize(2);
+ assertThat(counts.values()).containsOnly(1L, 1L);
+ assertThat(counts.keySet()).containsOnly(profileDto1.getKey().toString(), profileDto2.getKey().toString());
}
+
+
private RuleDto newRuleDto(RuleKey ruleKey) {
return new RuleDto()
.setRuleKey(ruleKey.rule())