]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6019 Add debt characteristics facet
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 19 Jan 2015 09:39:56 +0000 (10:39 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 19 Jan 2015 09:58:00 +0000 (10:58 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/search/Result.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json

index 45e40be5d32741728d4d55b28be8330f07fb94a7..e623303fb8a89b14b3bfeb642915176f176d9ee6 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.rule.index;
 
 import com.google.common.base.Function;
+import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
@@ -41,6 +42,7 @@ import org.elasticsearch.index.query.SimpleQueryStringBuilder;
 import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.aggregations.AggregationBuilder;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
+import org.elasticsearch.search.aggregations.bucket.global.GlobalBuilder;
 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 import org.elasticsearch.search.sort.FieldSortBuilder;
 import org.elasticsearch.search.sort.SortBuilders;
@@ -356,6 +358,35 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
         stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.SEVERITY.field(), FACET_SEVERITIES, Severity.ALL.toArray()));
     }
 
+    if (options.facets().contains(FACET_DEBT_CHARACTERISTICS)) {
+      int characsSize = 10;
+      int subCharacsSize = 300;
+      Collection<String> characsFromQuery = query.getDebtCharacteristics();
+      Object[] selectedChars = characsFromQuery == null ? new Object[0] : characsFromQuery.toArray();
+      AggregationBuilder debtChar = AggregationBuilders.filter(FACET_DEBT_CHARACTERISTICS + "__chars")
+        .filter(stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field()))
+        .subAggregation(
+          AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_top").field(RuleNormalizer.RuleField.CHARACTERISTIC.field())
+            .size(characsSize))
+        .subAggregation(
+          AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_selected").field(RuleNormalizer.RuleField.CHARACTERISTIC.field())
+            .include(Joiner.on('|').join(selectedChars))
+            .size(characsSize));
+      AggregationBuilder debtSubChar = AggregationBuilders.filter(FACET_DEBT_CHARACTERISTICS + "__subchars")
+        .filter(stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field()))
+        .subAggregation(
+          AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__subchars_top").field(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())
+            .size(subCharacsSize))
+        .subAggregation(
+          AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_selected").field(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())
+            .include(Joiner.on('|').join(selectedChars))
+            .size(subCharacsSize));
+      GlobalBuilder debtCharTopLevel = AggregationBuilders.global(FACET_DEBT_CHARACTERISTICS)
+        .subAggregation(debtChar)
+        .subAggregation(debtSubChar);
+      aggregations.put(FACET_DEBT_CHARACTERISTICS, debtCharTopLevel);
+    }
+
     return aggregations;
 
   }
index 844f31211fc30a75e61aab3691971301ea212729..e65e6ed42341c189045244d40933f37f7324fd39 100644 (file)
@@ -35,7 +35,11 @@ import org.slf4j.LoggerFactory;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 
 public class Result<K> {
 
@@ -79,7 +83,11 @@ public class Result<K> {
     } else if (Terms.class.isAssignableFrom(aggregation.getClass())) {
       Terms termAggregation = (Terms) aggregation;
       for (Terms.Bucket value : termAggregation.getBuckets()) {
-        this.facets.put(aggregation.getName().replace("_selected",""), new FacetValue(value.getKey(), value.getDocCount()));
+        String facetName = aggregation.getName().replace("_selected", "");
+        if (aggregation.getName().contains("__")) {
+          facetName = facetName.substring(0, facetName.indexOf("__"));
+        }
+        this.facets.put(facetName, new FacetValue(value.getKey(), value.getDocCount()));
       }
     } else if (HasAggregations.class.isAssignableFrom(aggregation.getClass())) {
       HasAggregations hasAggregations = (HasAggregations) aggregation;
index bc4fab4772f67a703285dee99a1f881344dde3eb..4e6b9cbfde7fffd17f3d52f9c6e66998e89f57d2 100644 (file)
@@ -195,6 +195,7 @@ public class RulesWebServiceMediumTest {
     MockUserSession.set();
     WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
     request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn");
+    request.setParam(SearchAction.PARAM_FACETS, "debt_characteristics");
     WsTester.Result result = request.execute();
     result.assertJson(this.getClass(), "search_debt_rule.json");
   }
index 474f4f219b5e15d43a57623002861b9c06f6ffea..cd2187655448a99fefb1ac63583f0df1c89d06cb 100644 (file)
     "defaultDebtRemFnCoeff": "1h",
     "defaultDebtRemFnOffset": "15min"
   }
+],
+"facets": [
+  {
+    "property": "debt_characteristics",
+    "values": [
+      {
+        "val": "RELIABILITY",
+        "count": 1
+      },
+      {
+        "val": "HARD_RELIABILITY",
+        "count": 1
+      }
+    ]
+  }
 ]}