From ccdaf2f116d2915938569fc556fa655fd060612b Mon Sep 17 00:00:00 2001 From: uboness Date: Tue, 2 Jun 2015 13:06:40 +0200 Subject: [PATCH] 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@f930c77d54467fb4de5680892130d9478dd72fd7 --- .../condition/compare/CompareCondition.java | 26 ++++++------------- .../compare/ExecutableCompareCondition.java | 9 ++++++- .../compare/CompareConditionSearchTests.java | 25 +++++++++++++++--- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/elasticsearch/watcher/condition/compare/CompareCondition.java b/src/main/java/org/elasticsearch/watcher/condition/compare/CompareCondition.java index e04b5c097c1..ba4929870e5 100644 --- a/src/main/java/org/elasticsearch/watcher/condition/compare/CompareCondition.java +++ b/src/main/java/org/elasticsearch/watcher/condition/compare/CompareCondition.java @@ -15,6 +15,7 @@ import org.elasticsearch.watcher.support.xcontent.WatcherXContentUtils; import java.io.IOException; import java.util.Locale; +import java.util.Map; import java.util.Objects; 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 { - private final Object resolveValue; + private final Map resolveValues; - Result(Object resolveValue, boolean met) { + Result(Map resolveValues, boolean met) { super(TYPE, met); - this.resolveValue = resolveValue; + this.resolveValues = resolveValues; } - public Object getResolveValue() { - return resolveValue; + public Map getResolveValues() { + return resolveValues; } @Override protected XContentBuilder typeXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject(type) - .field(Field.RESOLVED_VALUE.getPreferredName(), resolveValue) + .field(Field.RESOLVED_VALUES.getPreferredName(), resolveValues) .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 { - ParseField RESOLVED_VALUE = new ParseField("resolved_value"); + ParseField RESOLVED_VALUES = new ParseField("resolved_values"); } } diff --git a/src/main/java/org/elasticsearch/watcher/condition/compare/ExecutableCompareCondition.java b/src/main/java/org/elasticsearch/watcher/condition/compare/ExecutableCompareCondition.java index d396ecfebe3..3226ff3c3c9 100644 --- a/src/main/java/org/elasticsearch/watcher/condition/compare/ExecutableCompareCondition.java +++ b/src/main/java/org/elasticsearch/watcher/condition/compare/ExecutableCompareCondition.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.watcher.condition.compare; +import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.watcher.condition.ExecutableCondition; 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 java.io.IOException; +import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,6 +42,8 @@ public class ExecutableCompareCondition extends ExecutableCondition model = Variables.createCtxModel(ctx, ctx.payload()); + Map resolvedValues = new HashMap<>(); + Object configuredValue = condition.getValue(); if (configuredValue instanceof String) { @@ -49,18 +53,21 @@ public class ExecutableCompareCondition extends ExecutableCondition 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(); refresh(); @@ -59,7 +66,12 @@ public class CompareConditionSearchTests extends AbstractWatcherIntegrationTests .get(); 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 @@ -76,7 +88,12 @@ public class CompareConditionSearchTests extends AbstractWatcherIntegrationTests assertThat(condition.execute(ctx).met(), is(true)); hit.score(2f); 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 resolvedValues = result.getResolveValues(); + assertThat(resolvedValues, notNullValue()); + assertThat(resolvedValues.size(), is(1)); + assertThat(resolvedValues, hasEntry(is("ctx.payload.hits.hits.0._score"), notNullValue())); } }