compare condition result

The `compare` condition result will now hold all resolved values, keyed by their associated parameter place holder strings.

Original commit: elastic/x-pack-elasticsearch@f930c77d54
This commit is contained in:
uboness 2015-06-02 13:06:40 +02:00
parent a9658cca77
commit ccdaf2f116
3 changed files with 37 additions and 23 deletions

View File

@ -15,6 +15,7 @@ import org.elasticsearch.watcher.support.xcontent.WatcherXContentUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import static org.elasticsearch.common.joda.time.DateTimeZone.UTC; import static org.elasticsearch.common.joda.time.DateTimeZone.UTC;
@ -125,21 +126,21 @@ public class CompareCondition implements Condition {
public static class Result extends Condition.Result { public static class Result extends Condition.Result {
private final Object resolveValue; private final Map<String, Object> resolveValues;
Result(Object resolveValue, boolean met) { Result(Map<String, Object> resolveValues, boolean met) {
super(TYPE, met); super(TYPE, met);
this.resolveValue = resolveValue; this.resolveValues = resolveValues;
} }
public Object getResolveValue() { public Map<String, Object> getResolveValues() {
return resolveValue; return resolveValues;
} }
@Override @Override
protected XContentBuilder typeXContent(XContentBuilder builder, Params params) throws IOException { protected XContentBuilder typeXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject(type) return builder.startObject(type)
.field(Field.RESOLVED_VALUE.getPreferredName(), resolveValue) .field(Field.RESOLVED_VALUES.getPreferredName(), resolveValues)
.endObject(); .endObject();
} }
} }
@ -298,18 +299,7 @@ public class CompareCondition implements Condition {
} }
} }
public static class EvaluationException extends CompareConditionException {
public EvaluationException(String msg, Object... args) {
super(msg, args);
}
public EvaluationException(String msg, Throwable cause, Object... args) {
super(msg, cause, args);
}
}
interface Field extends Condition.Field { interface Field extends Condition.Field {
ParseField RESOLVED_VALUE = new ParseField("resolved_value"); ParseField RESOLVED_VALUES = new ParseField("resolved_values");
} }
} }

View File

@ -5,6 +5,7 @@
*/ */
package org.elasticsearch.watcher.condition.compare; package org.elasticsearch.watcher.condition.compare;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.watcher.condition.ExecutableCondition; import org.elasticsearch.watcher.condition.ExecutableCondition;
import org.elasticsearch.watcher.execution.WatchExecutionContext; import org.elasticsearch.watcher.execution.WatchExecutionContext;
@ -14,6 +15,7 @@ import org.elasticsearch.watcher.support.clock.Clock;
import org.elasticsearch.watcher.support.xcontent.MapPath; import org.elasticsearch.watcher.support.xcontent.MapPath;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -40,6 +42,8 @@ public class ExecutableCompareCondition extends ExecutableCondition<CompareCondi
public CompareCondition.Result execute(WatchExecutionContext ctx) throws IOException { public CompareCondition.Result execute(WatchExecutionContext ctx) throws IOException {
Map<String, Object> model = Variables.createCtxModel(ctx, ctx.payload()); Map<String, Object> model = Variables.createCtxModel(ctx, ctx.payload());
Map<String, Object> resolvedValues = new HashMap<>();
Object configuredValue = condition.getValue(); Object configuredValue = condition.getValue();
if (configuredValue instanceof String) { if (configuredValue instanceof String) {
@ -49,18 +53,21 @@ public class ExecutableCompareCondition extends ExecutableCondition<CompareCondi
if (matcher.matches()) { if (matcher.matches()) {
String dateMath = matcher.group(1); String dateMath = matcher.group(1);
configuredValue = WatcherDateTimeUtils.parseDateMath(dateMath, UTC, clock); configuredValue = WatcherDateTimeUtils.parseDateMath(dateMath, UTC, clock);
resolvedValues.put(dateMath, WatcherDateTimeUtils.formatDate((DateTime) configuredValue));
} else { } else {
// checking if the given value is a path expression // checking if the given value is a path expression
matcher = PATH_PATTERN.matcher((String) configuredValue); matcher = PATH_PATTERN.matcher((String) configuredValue);
if (matcher.matches()) { if (matcher.matches()) {
String configuredPath = matcher.group(1); String configuredPath = matcher.group(1);
configuredValue = MapPath.eval(configuredPath, model); configuredValue = MapPath.eval(configuredPath, model);
resolvedValues.put(configuredPath, configuredValue);
} }
} }
} }
Object resolvedValue = MapPath.eval(condition.getPath(), model); Object resolvedValue = MapPath.eval(condition.getPath(), model);
resolvedValues.put(condition.getPath(), resolvedValue);
return new CompareCondition.Result(resolvedValue, condition.getOp().eval(resolvedValue, configuredValue)); return new CompareCondition.Result(resolvedValues, condition.getOp().eval(resolvedValue, configuredValue));
} }
} }

View File

@ -21,8 +21,10 @@ import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTests;
import org.elasticsearch.watcher.watch.Payload; import org.elasticsearch.watcher.watch.Payload;
import org.junit.Test; import org.junit.Test;
import java.util.Map;
import static org.elasticsearch.watcher.test.WatcherTestUtils.mockExecutionContext; import static org.elasticsearch.watcher.test.WatcherTestUtils.mockExecutionContext;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
@ -49,7 +51,12 @@ public class CompareConditionSearchTests extends AbstractWatcherIntegrationTests
ExecutableCompareCondition condition = new ExecutableCompareCondition(new CompareCondition("ctx.payload.aggregations.rate.buckets.0.doc_count", CompareCondition.Op.GTE, 5), logger, SystemClock.INSTANCE); ExecutableCompareCondition condition = new ExecutableCompareCondition(new CompareCondition("ctx.payload.aggregations.rate.buckets.0.doc_count", CompareCondition.Op.GTE, 5), logger, SystemClock.INSTANCE);
WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response)); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response));
assertThat(condition.execute(ctx).met(), is(false)); CompareCondition.Result result = condition.execute(ctx);
assertThat(result.met(), is(false));
Map<String, Object> resolvedValues = result.getResolveValues();
assertThat(resolvedValues, notNullValue());
assertThat(resolvedValues.size(), is(1));
assertThat(resolvedValues, hasEntry("ctx.payload.aggregations.rate.buckets.0.doc_count", (Object) 4));
client().prepareIndex("my-index", "my-type").setTimestamp("2005-01-01T00:40").setSource("{}").get(); client().prepareIndex("my-index", "my-type").setTimestamp("2005-01-01T00:40").setSource("{}").get();
refresh(); refresh();
@ -59,7 +66,12 @@ public class CompareConditionSearchTests extends AbstractWatcherIntegrationTests
.get(); .get();
ctx = mockExecutionContext("_name", new Payload.XContent(response)); ctx = mockExecutionContext("_name", new Payload.XContent(response));
assertThat(condition.execute(ctx).met(), is(true)); result = condition.execute(ctx);
assertThat(result.met(), is(true));
resolvedValues = result.getResolveValues();
assertThat(resolvedValues, notNullValue());
assertThat(resolvedValues.size(), is(1));
assertThat(resolvedValues, hasEntry("ctx.payload.aggregations.rate.buckets.0.doc_count", (Object) 5));
} }
@Test @Test
@ -76,7 +88,12 @@ public class CompareConditionSearchTests extends AbstractWatcherIntegrationTests
assertThat(condition.execute(ctx).met(), is(true)); assertThat(condition.execute(ctx).met(), is(true));
hit.score(2f); hit.score(2f);
when(ctx.payload()).thenReturn(new Payload.XContent(response)); when(ctx.payload()).thenReturn(new Payload.XContent(response));
assertThat(condition.execute(ctx).met(), is(false)); CompareCondition.Result result = condition.execute(ctx);
assertThat(result.met(), is(false));
Map<String, Object> resolvedValues = result.getResolveValues();
assertThat(resolvedValues, notNullValue());
assertThat(resolvedValues.size(), is(1));
assertThat(resolvedValues, hasEntry(is("ctx.payload.hits.hits.0._score"), notNullValue()));
} }
} }