]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Added count by IndexField for *Index classes
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 11 Jun 2014 15:29:42 +0000 (17:29 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 11 Jun 2014 15:29:54 +0000 (17:29 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java

index fe5a7dda15f43e0bd369f2be8cfba016a2fb055d..76a8025f8f0a5960623d562b6ea5df745fe0840e 100644 (file)
@@ -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);
+  }
 }
index 90782b195769f48f92998a72a549e72e3b718c8c..883f9ef14f8c352b813fa13bfd20e41aa39e51f1 100644 (file)
@@ -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;
+  }
 }
index cb4143f7da360a6d72384192aa03a914fc9587e3..c2cd3c79f87107d20d2cc6ac11bc03724788ff82 100644 (file)
@@ -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())