private void addStatusFacetIfNeeded(RuleQuery query, QueryContext options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
if (options.facets().contains(FACET_STATUSES)) {
Collection<RuleStatus> statusesFromQuery = query.getStatuses();
- Object[] selectedStatuses = statusesFromQuery == null ? new Object[0] : statusesFromQuery.toArray();
- AggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "__top")
- .filter(stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.STATUS.field())
- .mustNot(FilterBuilders.termFilter(RuleNormalizer.RuleField.STATUS.field(), RuleStatus.REMOVED.toString())))
+ BoolFilterBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.STATUS.field());
+ AggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter")
+ .filter(facetFilter)
.subAggregation(
- AggregationBuilders.terms(FACET_STATUSES + "__values").field(RuleNormalizer.RuleField.STATUS.field()))
- .subAggregation(
- AggregationBuilders.terms(FACET_STATUSES + "__selected").field(RuleNormalizer.RuleField.STATUS.field())
- .include(Joiner.on('|').join(selectedStatuses)));
-
- aggregations.put(FACET_STATUSES, statuses);
+ AggregationBuilders
+ .terms(FACET_STATUSES)
+ .field(RuleNormalizer.RuleField.STATUS.field())
+ .include(Joiner.on('|').join(ALL_STATUSES_EXCEPT_REMOVED))
+ .exclude(RuleStatus.REMOVED.toString())
+ .size(ALL_STATUSES_EXCEPT_REMOVED.size()));
+
+ aggregations.put(FACET_STATUSES, AggregationBuilders.global(FACET_STATUSES).subAggregation(statuses));
}
}
} else if (Terms.class.isAssignableFrom(aggregation.getClass())) {
Terms termAggregation = (Terms) aggregation;
for (Terms.Bucket value : termAggregation.getBuckets()) {
- String facetName = aggregation.getName().replace("_selected", "");
- if (aggregation.getName().contains("__") && !aggregation.getName().startsWith("__")) {
+ String facetName = aggregation.getName();
+ if (facetName.contains("__") && !facetName.startsWith("__")) {
facetName = facetName.substring(0, facetName.indexOf("__"));
}
+ facetName = facetName.replace("_selected", "");
this.facets.put(facetName, new FacetValue(value.getKey(), value.getDocCount()));
}
} else if (HasAggregations.class.isAssignableFrom(aggregation.getClass())) {
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.ws.WebService;
public class RulesWebServiceMediumTest {
@ClassRule
- public static ServerTester tester = new ServerTester();
+ public static ServerTester tester = new ServerTester()
+ .setProperty("sonar.log.profilingLevel", "FULL")
+ .setProperty("sonar.search.httpPort", "9010");
private static final String API_ENDPOINT = "api/rules";
private static final String API_SEARCH_METHOD = "search";
request.execute().assertJson(this.getClass(), "statuses_facet.json", false);
}
+ @Test
+ public void statuses_facet_should_be_sticky() throws Exception {
+ ruleDao.insert(session, RuleTesting.newXooX1());
+ ruleDao.insert(session, RuleTesting.newXooX2().setStatus(RuleStatus.BETA));
+ ruleDao.insert(session, RuleTesting.newXooX3().setStatus(RuleStatus.DEPRECATED));
+ session.commit();
+
+ WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
+ request.setParam(SearchAction.PARAM_STATUSES, "DEPRECATED");
+ request.setParam(SearchAction.PARAM_FACETS, "statuses");
+ request.execute().assertJson(this.getClass(), "statuses_facet_sticky.json", false);
+ }
+
@Test
public void sort_by_name() throws Exception {
ruleDao.insert(session, RuleTesting.newXooX1().setName("Dodgy - Consider returning a zero length array rather than null "));
--- /dev/null
+{"total": 1, "p": 1, "ps": 100, "rules": [
+ {"key": "xoo:x3"}
+],
+"facets": [
+ {
+ "property": "statuses",
+ "values": [
+ {
+ "val": "BETA",
+ "count": 1
+ },{
+ "val": "DEPRECATED",
+ "count": 1
+ },{
+ "val": "READY",
+ "count": 1
+ }
+ ]
+ }
+]}