From bb8357055cd77a7cbdb3d94ed5ec74fa7b7215f8 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Wed, 11 Jun 2014 17:29:42 +0200 Subject: [PATCH] SONAR-5007 - Added count by IndexField for *Index classes --- .../qualityprofile/QProfileService.java | 8 ++++- .../org/sonar/server/search/BaseIndex.java | 22 ++++++++++++ .../ActiveRuleBackendMediumTest.java | 35 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) 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 fe5a7dda15f..76a8025f8f0 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 @@ -29,6 +29,7 @@ import org.sonar.core.qualityprofile.db.QualityProfileDto; 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; @@ -41,6 +42,7 @@ import java.io.StringWriter; import java.io.Writer; import java.util.Collection; import java.util.List; +import java.util.Map; public class QProfileService implements ServerComponent { @@ -201,7 +203,11 @@ 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 countAllActiveRules() { + return index.get(ActiveRuleIndex.class).countByField(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY); + } } diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index 90782b19576..883f9ef14f8 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -36,6 +36,8 @@ import org.elasticsearch.index.query.FilterBuilder; 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; @@ -541,4 +543,24 @@ public abstract class BaseIndex, KEY extends Serial .setTypes(this.getIndexType()) .get().getCount(); } + + public Map countByField(IndexField indexField) { + Map counts = new HashMap(); + 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; + } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java index cb4143f7da3..c2cd3c79f87 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java @@ -36,11 +36,13 @@ import org.sonar.core.rule.RuleDto; 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; @@ -250,9 +252,42 @@ public class ActiveRuleBackendMediumTest { // 1. Assert by profileKey assertThat(index.countByQualityProfileKey(profileDto1.getKey())).isEqualTo(1); + + // 2. Assert by term aggregation; + Map 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 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()) -- 2.39.5