From 8218170711295578dba12ce213865d8acdfee426 Mon Sep 17 00:00:00 2001 From: uboness Date: Wed, 20 May 2015 09:39:44 +0200 Subject: [PATCH] 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@74a3372c25c41499cae201ef926096a479fbc008 --- .../watcher/condition/ConditionRegistry.java | 21 ++++++++++++++----- .../execution/WatchExecutionResult.java | 5 ++--- src/main/resources/watch_history.json | 8 +++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/elasticsearch/watcher/condition/ConditionRegistry.java b/src/main/java/org/elasticsearch/watcher/condition/ConditionRegistry.java index feeab65dfdb..328322f4cbd 100644 --- a/src/main/java/org/elasticsearch/watcher/condition/ConditionRegistry.java +++ b/src/main/java/org/elasticsearch/watcher/condition/ConditionRegistry.java @@ -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: *
      *     {
+     *         "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();
+    }
 }
diff --git a/src/main/java/org/elasticsearch/watcher/execution/WatchExecutionResult.java b/src/main/java/org/elasticsearch/watcher/execution/WatchExecutionResult.java
index d4654988901..09e78f353c8 100644
--- a/src/main/java/org/elasticsearch/watcher/execution/WatchExecutionResult.java
+++ b/src/main/java/org/elasticsearch/watcher/execution/WatchExecutionResult.java
@@ -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());
diff --git a/src/main/resources/watch_history.json b/src/main/resources/watch_history.json
index 9bc67c50a50..85d57560952 100644
--- a/src/main/resources/watch_history.json
+++ b/src/main/resources/watch_history.json
@@ -139,6 +139,14 @@
                 "script" : {
                   "type" : "object",
                   "enabled" : false
+                },
+                "always" : {
+                  "type" : "object",
+                  "enabled" : false
+                },
+                "never" : {
+                  "type" : "object",
+                  "enabled" : false
                 }
               }
             },