From 497147c30db1a37fe8d2c5c1f6445cee9e1e9976 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Fri, 10 Feb 2017 08:44:08 +0100 Subject: [PATCH] Watcher: Include watch status in history (elastic/elasticsearch#4875) In order to help watcher UI easily gather the status of a watch and its actions, we should write the watch status in the history as well. This commit is doing exactly that. Tests ensure, that the status is not searchable, as in the watch mapping itself. This also requires the history template to be changed, thus the counter of the template had to be increased as well. Some minor refactorings by making ctors private that dont need to be public have been happening as well. Closes elastic/elasticsearch#4735 Original commit: elastic/x-pack-elasticsearch@df8352ceb5519c1cba06999db180bec275403524 --- .../xpack/watcher/actions/ActionStatus.java | 20 +++---- .../execution/WatchExecutionResult.java | 5 +- .../xpack/watcher/history/WatchRecord.java | 28 ++++++---- .../support/WatcherIndexTemplateRegistry.java | 2 +- .../execute/TransportExecuteWatchAction.java | 2 + .../src/main/resources/watch_history.json | 5 ++ .../WatcherIndexTemplateRegistryTests.java | 1 - .../integration/HistoryIntegrationTests.java | 53 +++++++++++++++++++ .../test/watcher/execute_watch/10_basic.yaml | 1 + 9 files changed, 89 insertions(+), 28 deletions(-) diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionStatus.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionStatus.java index 542d622ed7b..0e0c24f7e86 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionStatus.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionStatus.java @@ -330,18 +330,14 @@ public class ActionStatus implements ToXContentObject { Execution execution = (Execution) o; - if (successful != execution.successful) return false; - if (!timestamp.equals(execution.timestamp)) return false; - return !(reason != null ? !reason.equals(execution.reason) : execution.reason != null); - + return Objects.equals(successful, execution.successful) && + Objects.equals(timestamp, execution.timestamp) && + Objects.equals(reason, execution.reason); } @Override public int hashCode() { - int result = timestamp.hashCode(); - result = 31 * result + (successful ? 1 : 0); - result = 31 * result + (reason != null ? reason.hashCode() : 0); - return result; + return Objects.hash(timestamp, successful, reason); } @Override @@ -436,16 +432,12 @@ public class ActionStatus implements ToXContentObject { if (o == null || getClass() != o.getClass()) return false; Throttle throttle = (Throttle) o; - - if (!timestamp.equals(throttle.timestamp)) return false; - return reason.equals(throttle.reason); + return Objects.equals(timestamp, throttle.timestamp) && Objects.equals(reason, throttle.reason); } @Override public int hashCode() { - int result = timestamp.hashCode(); - result = 31 * result + reason.hashCode(); - return result; + return Objects.hash(timestamp, reason); } @Override diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/execution/WatchExecutionResult.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/execution/WatchExecutionResult.java index 275f963dadd..666b59d5cef 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/execution/WatchExecutionResult.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/execution/WatchExecutionResult.java @@ -33,8 +33,9 @@ public class WatchExecutionResult implements ToXContentObject { context.actionsResults()); } - WatchExecutionResult(DateTime executionTime, long executionDurationMs, Input.Result inputResult, Condition.Result conditionResult, - @Nullable Transform.Result transformResult, Map actionsResults) { + private WatchExecutionResult(DateTime executionTime, long executionDurationMs, Input.Result inputResult, + Condition.Result conditionResult, @Nullable Transform.Result transformResult, + Map actionsResults) { this.executionTime = executionTime; this.inputResult = inputResult; this.conditionResult = conditionResult; diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/history/WatchRecord.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/history/WatchRecord.java index 3cdc6f00970..851b8534ffb 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/history/WatchRecord.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/history/WatchRecord.java @@ -33,6 +33,7 @@ import java.util.Objects; public abstract class WatchRecord implements ToXContentObject { protected final Wid id; + protected final Watch watch; protected final TriggerEvent triggerEvent; protected final ExecutionState state; @@ -44,8 +45,8 @@ public abstract class WatchRecord implements ToXContentObject { @Nullable protected final Map metadata; @Nullable protected final WatchExecutionResult executionResult; - public WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state, Map vars, ExecutableInput input, - Condition condition, Map metadata, WatchExecutionResult executionResult) { + private WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state, Map vars, ExecutableInput input, + Condition condition, Map metadata, Watch watch, WatchExecutionResult executionResult) { this.id = id; this.triggerEvent = triggerEvent; this.state = state; @@ -54,24 +55,26 @@ public abstract class WatchRecord implements ToXContentObject { this.condition = condition; this.metadata = metadata; this.executionResult = executionResult; + this.watch = watch; } - public WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state) { - this(id, triggerEvent, state, Collections.emptyMap(), null, null, null, null); + private WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state) { + this(id, triggerEvent, state, Collections.emptyMap(), null, null, null, null, null); } - public WatchRecord(WatchRecord record, ExecutionState state) { - this(record.id, record.triggerEvent, state, record.vars, record.input, record.condition, record.metadata, record.executionResult); + private WatchRecord(WatchRecord record, ExecutionState state) { + this(record.id, record.triggerEvent, state, record.vars, record.input, record.condition, record.metadata, record.watch, + record.executionResult); } - public WatchRecord(WatchExecutionContext context, ExecutionState state) { + private WatchRecord(WatchExecutionContext context, ExecutionState state) { this(context.id(), context.triggerEvent(), state, context.vars(), context.watch().input(), context.watch().condition(), - context.watch().metadata(), null); + context.watch().metadata(), context.watch(), null); } - public WatchRecord(WatchExecutionContext context, WatchExecutionResult executionResult) { + private WatchRecord(WatchExecutionContext context, WatchExecutionResult executionResult) { this(context.id(), context.triggerEvent(), getState(executionResult), context.vars(), context.watch().input(), - context.watch().condition(), context.watch().metadata(), executionResult); + context.watch().condition(), context.watch().metadata(), context.watch(), executionResult); } private static ExecutionState getState(WatchExecutionResult executionResult) { @@ -125,6 +128,10 @@ public abstract class WatchRecord implements ToXContentObject { builder.field(Field.WATCH_ID.getPreferredName(), id.watchId()); builder.field(Field.STATE.getPreferredName(), state.id()); + if (watch != null && watch.status() != null) { + builder.field("_status", watch.status(), params); + } + builder.field(Field.TRIGGER_EVENT.getPreferredName()); triggerEvent.recordXContent(builder, params); @@ -179,6 +186,7 @@ public abstract class WatchRecord implements ToXContentObject { ParseField TRIGGER_EVENT = new ParseField("trigger_event"); ParseField MESSAGES = new ParseField("messages"); ParseField STATE = new ParseField("state"); + ParseField STATUS = new ParseField("_status"); ParseField VARS = new ParseField("vars"); ParseField METADATA = new ParseField("metadata"); ParseField EXECUTION_RESULT = new ParseField("result"); diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java index 5f596ac3c54..c85a04315e9 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java @@ -37,7 +37,7 @@ import static java.util.Collections.unmodifiableMap; public class WatcherIndexTemplateRegistry extends AbstractComponent implements ClusterStateListener { private static final String FORBIDDEN_INDEX_SETTING = "index.mapper.dynamic"; - public static final String INDEX_TEMPLATE_VERSION = "2"; + public static final String INDEX_TEMPLATE_VERSION = "3"; public static final String HISTORY_TEMPLATE_NAME = "watch_history_" + INDEX_TEMPLATE_VERSION; public static final String TRIGGERED_TEMPLATE_NAME = "triggered_watches"; diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java index 23be0a469ed..a45af893df4 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java @@ -90,6 +90,8 @@ public class TransportExecuteWatchAction extends WatcherTransportAction