Skip to content

Commit

Permalink
[ML] Improve error when no available field exists for rule scope (#32550
Browse files Browse the repository at this point in the history
)

Closes #32542
  • Loading branch information
dimitris-athanasiou committed Aug 1, 2018
1 parent 7b111e8 commit f330d53
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public boolean isEmpty() {
public void validate(Set<String> validKeys) {
Optional<String> invalidKey = scope.keySet().stream().filter(k -> !validKeys.contains(k)).findFirst();
if (invalidKey.isPresent()) {
if (validKeys.isEmpty()) {
throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS,
invalidKey.get()));
}
throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD,
invalidKey.get(), validKeys));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public final class Messages {
"Invalid detector rule: function {0} only supports conditions that apply to time";
public static final String JOB_CONFIG_DETECTION_RULE_REQUIRES_SCOPE_OR_CONDITION =
"Invalid detector rule: at least scope or a condition is required";
public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS =
"Invalid detector rule: scope field ''{0}'' is invalid; detector has no available fields for scoping";
public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD =
"Invalid detector rule: scope field ''{0}'' is invalid; select from {1}";
public static final String JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION = "field_name cannot be used with function ''{0}''";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.test.AbstractWireSerializingTestCase;

import java.util.Collections;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -53,6 +55,17 @@ public void testValidate_GivenMultipleValidFields() {
scope.validate(Sets.newHashSet("foo", "bar", "foobar"));
}

public void testValidate_GivenNoAvailableFieldsForScope() {
RuleScope scope = RuleScope.builder()
.include("foo", "filter1")
.build();
assertThat(scope.isEmpty(), is(false));

ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> scope.validate(Collections.emptySet()));
assertThat(e.getMessage(), equalTo("Invalid detector rule: scope field 'foo' is invalid; " +
"detector has no available fields for scoping"));
}

public void testValidate_GivenMultipleFieldsIncludingInvalid() {
RuleScope scope = RuleScope.builder()
.include("foo", "filter1")
Expand Down

0 comments on commit f330d53

Please sign in to comment.