aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-06-11 17:29:42 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-06-11 17:29:54 +0200
commitbb8357055cd77a7cbdb3d94ed5ec74fa7b7215f8 (patch)
treed6df59b841cdf6821cba2afe389c81b7b966836f
parent16a2b3ddf4a8fd7cd0d4a2da4dbfe1b0d16ec716 (diff)
downloadsonarqube-bb8357055cd77a7cbdb3d94ed5ec74fa7b7215f8.tar.gz
sonarqube-bb8357055cd77a7cbdb3d94ed5ec74fa7b7215f8.zip
SONAR-5007 - Added count by IndexField for *Index classes
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java8
-rw-r--r--sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java22
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java35
3 files changed, 64 insertions, 1 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 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<String, Long> 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<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
.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;
+ }
}
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<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())