import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import org.apache.commons.collections.CollectionUtils;
public static final String FACET_DEBT_CHARACTERISTICS = "debt_characteristics";
public static final String FACET_OLD_DEFAULT = "true";
- public static final List<String> ALL_STATUSES = ImmutableList.copyOf(Collections2.transform(Arrays.asList(RuleStatus.values()), new Function<RuleStatus, String>() {
- @Override
- public String apply(RuleStatus input) {
- return input.toString();
- }
- }));
+ public static final List<String> ALL_STATUSES_EXCEPT_REMOVED = ImmutableList.copyOf(
+ Collections2.filter(
+ Collections2.transform(
+ Arrays.asList(RuleStatus.values()),
+ new Function<RuleStatus, String>() {
+ @Override
+ public String apply(RuleStatus input) {
+ return input.toString();
+ }
+ }),
+ new Predicate<String>() {
+ @Override
+ public boolean apply(String input) {
+ return !RuleStatus.REMOVED.toString().equals(input);
+ }
+ }));
public RuleIndex(RuleNormalizer normalizer, SearchClient client) {
super(IndexDefinition.RULE, normalizer, client);
addDefaultFacets(query, options, queryBuilder, filters, aggregations, stickyFacetBuilder);
- if (options.facets().contains(FACET_STATUSES)) {
- aggregations.put(FACET_STATUSES,
- stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.STATUS.field(), FACET_STATUSES, ALL_STATUSES.toArray()));
- }
+ addStatusFacetIfNeeded(query, options, aggregations, stickyFacetBuilder);
if (options.facets().contains(FACET_SEVERITIES)) {
aggregations.put(FACET_SEVERITIES,
stickyFacetBuilder.buildStickyFacet(RuleNormalizer.RuleField.SEVERITY.field(), FACET_SEVERITIES, Severity.ALL.toArray()));
}
+ addCharacteristicsFacetIfNeeded(query, options, aggregations, stickyFacetBuilder);
+
+ return aggregations;
+
+ }
+
+ 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())))
+ .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);
+ }
+ }
+
+ private void addCharacteristicsFacetIfNeeded(RuleQuery query, QueryContext options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
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()))
+ .filter(stickyFacetBuilder.getStickyFacetFilter("debtCharacteristics"))
.subAggregation(
AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_top").field(RuleNormalizer.RuleField.CHARACTERISTIC.field())
.size(characsSize))
.include(Joiner.on('|').join(selectedChars))
.size(characsSize));
AggregationBuilder debtSubChar = AggregationBuilders.filter(FACET_DEBT_CHARACTERISTICS + "__subchars")
- .filter(stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field()))
+ .filter(stickyFacetBuilder.getStickyFacetFilter("debtCharacteristics"))
.subAggregation(
AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__subchars_top").field(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())
.size(subCharacsSize))
.subAggregation(debtSubChar);
aggregations.put(FACET_DEBT_CHARACTERISTICS, debtCharTopLevel);
}
-
- return aggregations;
-
}
protected void addDefaultFacets(RuleQuery query, QueryContext options, QueryBuilder queryBuilder, Map<String, FilterBuilder> filters,
result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json");
}
+ @Test
+ public void debt_characteristics_is_sticky_facet() throws Exception {
+ insertDebtCharacteristics(session);
+
+ ruleDao.insert(session, RuleTesting.newXooX1()
+ .setDefaultSubCharacteristicId(hardReliabilityId)
+ .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setDefaultRemediationCoefficient("1h")
+ .setDefaultRemediationOffset("15min")
+
+ .setSubCharacteristicId(null)
+ .setRemediationFunction(null)
+ .setRemediationCoefficient(null)
+ .setRemediationOffset(null)
+ );
+ ruleDao.insert(session, RuleTesting.newXooX2()
+ .setDefaultSubCharacteristicId(hardReliabilityId)
+ .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setDefaultRemediationCoefficient("1h")
+ .setDefaultRemediationOffset("15min")
+
+ .setSubCharacteristicId(softReliabilityId)
+ .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setRemediationCoefficient("30min")
+ .setRemediationOffset("5min")
+ );
+ session.commit();
+
+ 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("debt_characteristics", "SOFT_RELIABILITY");
+ request.setParam(SearchAction.PARAM_FACETS, "debt_characteristics");
+ WsTester.Result result = request.execute();
+ result.assertJson(this.getClass(), "search_debt_rules_sticky.json");
+ }
+
@Test
public void search_template_rules() throws Exception {
RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true);
}
@Test
- public void statuses_facet_should_have_all_statuses() throws Exception {
+ public void statuses_facet_should_have_all_statuses_except_removed() throws Exception {
WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
request.setParam(SearchAction.PARAM_FACETS, "statuses");
request.execute().assertJson(this.getClass(), "statuses_facet.json", false);
--- /dev/null
+{"total": 1, "p": 1, "ps": 100, "rules": [
+ {
+ "key": "xoo:x2",
+ "debtChar": "RELIABILITY",
+ "debtCharName": "Reliability",
+ "debtSubChar": "SOFT_RELIABILITY",
+ "debtSubCharName": "Soft Reliability",
+ "debtRemFnType": "LINEAR_OFFSET",
+ "debtRemFnCoeff": "30min",
+ "debtRemFnOffset": "5min",
+ "debtOverloaded": true,
+ "defaultDebtChar": "RELIABILITY",
+ "defaultDebtSubChar": "HARD_RELIABILITY",
+ "defaultDebtRemFnType": "LINEAR_OFFSET",
+ "defaultDebtRemFnCoeff": "1h",
+ "defaultDebtRemFnOffset": "15min"
+ }
+],
+"facets": [
+ {
+ "property": "debt_characteristics",
+ "values": [
+ {
+ "val": "RELIABILITY",
+ "count": 2
+ },
+ {
+ "val": "SOFT_RELIABILITY",
+ "count": 1
+ },
+ {
+ "val": "HARD_RELIABILITY",
+ "count": 1
+ }
+ ]
+ }
+]}