]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 - Updated getProfileStats
authorStephane Gamard <stephane.gamard@searchbox.com>
Mon, 16 Jun 2014 12:17:33 +0000 (14:17 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Mon, 16 Jun 2014 12:22:07 +0000 (14:22 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java

index 7829d94e450bf67db4baa8b8ee899c8e6fb8e57b..50b3788eaf77375486dcfe92f0d00570bb682a16 100644 (file)
@@ -42,12 +42,15 @@ import com.google.common.collect.Multimap;
 import org.elasticsearch.action.count.CountRequestBuilder;
 import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.common.collect.ImmutableList;
 import org.elasticsearch.common.settings.ImmutableSettings;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.index.query.FilterBuilders;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.aggregations.Aggregation;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
+import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.core.cluster.WorkQueue;
 import org.sonar.core.profiling.Profiling;
@@ -181,34 +184,36 @@ public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, Active
   }
 
   public Multimap<String, FacetValue> getStatsByProfileKey(QualityProfileKey key) {
+    return getStatsByProfileKey(ImmutableList.of(key)).get(key);
+  }
+
+  public Map<QualityProfileKey, Multimap<String, FacetValue>> getStatsByProfileKey(List<QualityProfileKey> keys) {
+
+    String[] stringKeys = new String[keys.size()];
+    for (int i = 0; i < keys.size(); i++) {
+      stringKeys[i] = keys.get(i).toString();
+    }
 
     SearchResponse response = getClient().prepareSearch(this.getIndexName())
       .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
-        FilterBuilders.termsFilter(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key.toString())))
+        FilterBuilders.termsFilter(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), stringKeys)))
       .addAggregation(AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field())
         .field(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field())
         .subAggregation(AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())
           .field(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())
           .subAggregation(AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field())
             .field(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field()))))
-      .addAggregation(AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())
-        .field(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field()))
-      .addAggregation(AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field())
-        .field(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field()))
-
       .setSize(0)
       .setTypes(this.getIndexType())
       .get();
 
-    return this.processAggregations(response.getAggregations());
-  }
-
-  public Map<QualityProfileKey, Multimap<String, FacetValue>> getStatsByProfileKey(List<QualityProfileKey> keys) {
-    //TODO Optimize in a single request.
     Map<QualityProfileKey, Multimap<String, FacetValue>> stats = new HashMap<QualityProfileKey, Multimap<String, FacetValue>>();
-    for (QualityProfileKey key : keys) {
-      stats.put(key, getStatsByProfileKey(key));
+    Aggregation aggregation = response.getAggregations().get(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field());
+    for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
+      stats.put(QualityProfileKey.parse(value.getKey())
+        , this.processAggregations(value.getAggregations()));
     }
+
     return stats;
   }
 }
index 7c722f94f4740160f9a7eee3e247534b1d4fcedc..e4f51b4b6d81500b96fe8b21905a7796cb5113e9 100644 (file)
@@ -23,7 +23,6 @@ import com.google.common.collect.Multimap;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.core.permission.GlobalPermissions;
@@ -106,7 +105,6 @@ public class QProfileServiceMediumTest {
   }
 
   @Test
-  @Ignore
   public void stat_for_all_profiles() {
     MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");
 
@@ -119,6 +117,8 @@ public class QProfileServiceMediumTest {
     dbSession.commit();
 
     Map<QualityProfileKey, Multimap<String, FacetValue>> stats = service.getAllProfileStats();
+    System.out.println("stats = " + stats);
+
     assertThat(stats.size()).isEqualTo(2);
     assertThat(stats.get(XOO_PROFILE_1).size()).isEqualTo(1);
   }