Added `execution_result.condition.met` field

Until today we could not search on the `met` field in the condition result. The reason for that is that this field was index as part of the condition result type only, and we disable the indexing for all condition results (to avoid mapping conflicts).

 This commit pulls the `met` condition one level higher and enables its mapping. For now (beta1) we can live with the duplication of the condition result source (were the `met` is not placed in both the condition result type and on the condition result itself). Later we should remove the duplication though.

 An example of a  "compare" condition result now looks like:

 ```
 "condition": {
    "met": true,
    "compare": {
       "met": true,
       "resolved_value": 1
    }
 }
 ```

Original commit: elastic/x-pack-elasticsearch@74a3372c25
This commit is contained in:
uboness 2015-05-20 09:39:44 +02:00
parent 2ede3c29d8
commit 8218170711
3 changed files with 26 additions and 8 deletions

View File

@ -7,6 +7,8 @@ package org.elasticsearch.watcher.condition;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
@ -83,6 +85,7 @@ public class ConditionRegistry {
* Parses the xcontent and returns the appropriate condition result. Expecting the following format:
* <code><pre>
* {
* "met" : true | false,
* "condition_type" : {
* ... // result body
* }
@ -92,17 +95,19 @@ public class ConditionRegistry {
public Condition.Result parseResult(String watchId, XContentParser parser) throws IOException {
Condition.Result result = null;
String type = null;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
type = parser.currentName();
} else if (type == null) {
currentFieldName = parser.currentName();
} else if (currentFieldName == null) {
throw new ConditionException("could not parse condition result for watch [{}]. invalid definition. expected a field indicating the condition type, but found", watchId, token);
} else if (Condition.Field.MET.match(currentFieldName)) {
// we do nothing for now here.... the condition will still parse its state in the type itself
} else {
ConditionFactory factory = factories.get(type);
ConditionFactory factory = factories.get(currentFieldName);
if (factory == null) {
throw new ConditionException("could not parse condition result for watch [{}]. un known condition type [{}]", watchId, type);
throw new ConditionException("could not parse condition result for watch [{}]. un known condition type [{}]", watchId, currentFieldName);
}
result = factory.parseResult(watchId, parser);
}
@ -113,4 +118,10 @@ public class ConditionRegistry {
return result;
}
public static void writeResult(Condition.Result result, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject()
.field(Condition.Field.MET.getPreferredName(), result.met())
.field(result.type(), result, params)
.endObject();
}
}

View File

@ -86,9 +86,8 @@ public class WatchExecutionResult implements ToXContent {
.endObject();
}
if (conditionResult != null) {
builder.startObject(Field.CONDITION.getPreferredName())
.field(conditionResult.type(), conditionResult, params)
.endObject();
builder.field(Field.CONDITION.getPreferredName());
ConditionRegistry.writeResult(conditionResult, builder, params);
}
if (throttleResult != null && throttleResult.throttle()) {
builder.field(Field.THROTTLED.getPreferredName(), throttleResult.throttle());

View File

@ -139,6 +139,14 @@
"script" : {
"type" : "object",
"enabled" : false
},
"always" : {
"type" : "object",
"enabled" : false
},
"never" : {
"type" : "object",
"enabled" : false
}
}
},