From d31fe9f6989700980d6d57362ec334e5e4d5c2f3 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 19 Jan 2015 10:39:56 +0100 Subject: [PATCH] SONAR-6019 Add debt characteristics facet --- .../sonar/server/rule/index/RuleIndex.java | 31 +++++++++++++++++++ .../java/org/sonar/server/search/Result.java | 12 +++++-- .../rule/ws/RulesWebServiceMediumTest.java | 1 + .../search_debt_rule.json | 15 +++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java index 45e40be5d32..e623303fb8a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -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 { 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 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; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/Result.java b/server/sonar-server/src/main/java/org/sonar/server/search/Result.java index 844f31211fc..e65e6ed4234 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/Result.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/Result.java @@ -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 { @@ -79,7 +83,11 @@ public class Result { } 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; diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java index bc4fab4772f..4e6b9cbfde7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java @@ -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"); } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json index 474f4f219b5..cd218765544 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json @@ -15,4 +15,19 @@ "defaultDebtRemFnCoeff": "1h", "defaultDebtRemFnOffset": "15min" } +], +"facets": [ + { + "property": "debt_characteristics", + "values": [ + { + "val": "RELIABILITY", + "count": 1 + }, + { + "val": "HARD_RELIABILITY", + "count": 1 + } + ] + } ]} -- 2.39.5