From 4c1c502f80b5c897d540944bb6c38d448caf433a Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Mon, 18 Aug 2014 11:23:45 +0100 Subject: [PATCH] Alerting : Fix IndexAction This commit contains fixes to the IndexAlertAction and normalizes the serialization of Alerts using ToXContent interface. Original commit: elastic/x-pack-elasticsearch@47325bdd0189d65b656854e17f4df69b23212f7d --- .../java/org/elasticsearch/alerting/Alert.java | 10 ++++++---- .../org/elasticsearch/alerting/AlertAction.java | 6 +++--- .../elasticsearch/alerting/AlertManager.java | 17 +++++++---------- .../alerting/AlertRestHandler.java | 9 +++------ .../elasticsearch/alerting/AlertTrigger.java | 11 +++++------ .../alerting/EmailAlertAction.java | 2 +- .../alerting/IndexAlertAction.java | 15 +++++++++++---- 7 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/elasticsearch/alerting/Alert.java b/src/main/java/org/elasticsearch/alerting/Alert.java index dbbee260bf0..b5551e92c2c 100644 --- a/src/main/java/org/elasticsearch/alerting/Alert.java +++ b/src/main/java/org/elasticsearch/alerting/Alert.java @@ -7,6 +7,7 @@ package org.elasticsearch.alerting; import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; @@ -17,7 +18,7 @@ import java.util.List; /** * Created by brian on 8/12/14. */ -public class Alert { +public class Alert implements ToXContent{ private final String alertName; private String queryName; private AlertTrigger trigger; @@ -121,7 +122,8 @@ public class Alert { this.running = running; } - public XContentBuilder toXContent(XContentBuilder builder) throws IOException { + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { //Note we deliberately don't serialize the version here builder.startObject(); @@ -131,13 +133,13 @@ public class Alert { builder.field(AlertManager.LASTRAN_FIELD.getPreferredName(), lastRan); builder.field(AlertManager.CURRENTLY_RUNNING.getPreferredName(), running); builder.field(AlertManager.TRIGGER_FIELD.getPreferredName()); - trigger.toXContent(builder); + trigger.toXContent(builder, params); builder.field(AlertManager.ACTION_FIELD.getPreferredName()); builder.startObject(); for (AlertAction action : actions){ builder.field(action.getActionName()); - action.toXContent(builder); + action.toXContent(builder, params); } builder.endObject(); diff --git a/src/main/java/org/elasticsearch/alerting/AlertAction.java b/src/main/java/org/elasticsearch/alerting/AlertAction.java index 13b78cd9913..a10cbb02499 100644 --- a/src/main/java/org/elasticsearch/alerting/AlertAction.java +++ b/src/main/java/org/elasticsearch/alerting/AlertAction.java @@ -5,14 +5,14 @@ */ package org.elasticsearch.alerting; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; -public interface AlertAction { +public interface AlertAction extends ToXContent { public String getActionName(); - public XContentBuilder toXContent(XContentBuilder builder) throws IOException; + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException; public boolean doAction(String alertName, AlertResult alert); } diff --git a/src/main/java/org/elasticsearch/alerting/AlertManager.java b/src/main/java/org/elasticsearch/alerting/AlertManager.java index c19c93e1435..9792361ab2d 100644 --- a/src/main/java/org/elasticsearch/alerting/AlertManager.java +++ b/src/main/java/org/elasticsearch/alerting/AlertManager.java @@ -9,7 +9,6 @@ package org.elasticsearch.alerting; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.ElasticsearchIllegalStateException; -import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; @@ -19,7 +18,6 @@ import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; -import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.Requests; import org.elasticsearch.common.Nullable; @@ -30,12 +28,11 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.index.engine.Engine; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHitField; import java.io.IOException; import java.util.ArrayList; @@ -177,7 +174,7 @@ public class AlertManager extends AbstractLifecycleComponent { XContentBuilder alertBuilder; try { alertBuilder = XContentFactory.jsonBuilder(); - alert.toXContent(alertBuilder); + alert.toXContent(alertBuilder, ToXContent.EMPTY_PARAMS); } catch (IOException ie) { throw new ElasticsearchException("Unable to serialize alert ["+ alertName + "]", ie); } @@ -190,13 +187,13 @@ public class AlertManager extends AbstractLifecycleComponent { logger.error("Failed to update in claim", ee); return false; } - synchronized (alertMap) { //Update the alert map + + synchronized (alertMap) { //Update the alert map if (alertMap.containsKey(alertName)) { alertMap.get(alertName).running(scheduleRunTime); } } return true; - } private Alert getAlertFromIndex(String alertName) { @@ -260,7 +257,7 @@ public class AlertManager extends AbstractLifecycleComponent { Alert alert = getAlertForName(alertName); alert.lastRan(fireTime); XContentBuilder alertBuilder = XContentFactory.jsonBuilder().prettyPrint(); - alert.toXContent(alertBuilder); + alert.toXContent(alertBuilder, ToXContent.EMPTY_PARAMS); UpdateRequest updateRequest = new UpdateRequest(); updateRequest.id(alertName); updateRequest.index(ALERT_INDEX); @@ -286,7 +283,7 @@ public class AlertManager extends AbstractLifecycleComponent { historyEntry.field("triggered", triggered); historyEntry.field("fireTime", fireTime.toDateTimeISO()); historyEntry.field("trigger"); - trigger.toXContent(historyEntry); + trigger.toXContent(historyEntry, ToXContent.EMPTY_PARAMS); historyEntry.field("queryRan", XContentHelper.convertToJson(triggeringQuery.bytes(),false,true)); historyEntry.field("numberOfResults", numberOfResults); if (indices != null) { @@ -352,7 +349,7 @@ public class AlertManager extends AbstractLifecycleComponent { XContentBuilder builder; try { builder = XContentFactory.jsonBuilder(); - alert.toXContent(builder); + alert.toXContent(builder, ToXContent.EMPTY_PARAMS); IndexRequest indexRequest = new IndexRequest(ALERT_INDEX, ALERT_TYPE, alertName); indexRequest.listenerThreaded(false); indexRequest.operationThreaded(false); diff --git a/src/main/java/org/elasticsearch/alerting/AlertRestHandler.java b/src/main/java/org/elasticsearch/alerting/AlertRestHandler.java index b53c8d07c59..f68679a4048 100644 --- a/src/main/java/org/elasticsearch/alerting/AlertRestHandler.java +++ b/src/main/java/org/elasticsearch/alerting/AlertRestHandler.java @@ -9,10 +9,7 @@ import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; -import org.elasticsearch.common.xcontent.XContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.*; import org.elasticsearch.rest.*; import java.io.IOException; @@ -70,7 +67,7 @@ public class AlertRestHandler implements RestHandler { boolean added = alertManager.addAlert(alert.alertName(), alert, true); if (added) { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); - alert.toXContent(builder); + alert.toXContent(builder, ToXContent.EMPTY_PARAMS); restChannel.sendResponse(new BytesRestResponse(OK, builder)); } else { restChannel.sendResponse(new BytesRestResponse(BAD_REQUEST)); @@ -103,7 +100,7 @@ public class AlertRestHandler implements RestHandler { builder.startObject(); for (Map.Entry alertEntry : alertMap.entrySet()) { builder.field(alertEntry.getKey()); - alertEntry.getValue().toXContent(builder); + alertEntry.getValue().toXContent(builder, ToXContent.EMPTY_PARAMS); } builder.endObject(); return builder; diff --git a/src/main/java/org/elasticsearch/alerting/AlertTrigger.java b/src/main/java/org/elasticsearch/alerting/AlertTrigger.java index 4d61f0eaa86..bb03ffa6473 100644 --- a/src/main/java/org/elasticsearch/alerting/AlertTrigger.java +++ b/src/main/java/org/elasticsearch/alerting/AlertTrigger.java @@ -6,16 +6,14 @@ package org.elasticsearch.alerting; import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; -/** - * Created by brian on 8/12/14. - */ -public class AlertTrigger { +public class AlertTrigger implements ToXContent { private SimpleTrigger trigger; private TriggerType triggerType; @@ -107,9 +105,10 @@ public class AlertTrigger { } } - public XContentBuilder toXContent(XContentBuilder builder) throws IOException { + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - builder.field(triggerType.toString(),trigger.toString()+value); + builder.field(triggerType.toString(), trigger.toString() + value); builder.endObject(); return builder; } diff --git a/src/main/java/org/elasticsearch/alerting/EmailAlertAction.java b/src/main/java/org/elasticsearch/alerting/EmailAlertAction.java index c9be6aa32b8..e5e9dfd8409 100644 --- a/src/main/java/org/elasticsearch/alerting/EmailAlertAction.java +++ b/src/main/java/org/elasticsearch/alerting/EmailAlertAction.java @@ -57,7 +57,7 @@ public class EmailAlertAction implements AlertAction { } @Override - public XContentBuilder toXContent(XContentBuilder builder) throws IOException { + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field("addresses"); builder.startArray(); diff --git a/src/main/java/org/elasticsearch/alerting/IndexAlertAction.java b/src/main/java/org/elasticsearch/alerting/IndexAlertAction.java index 1780083c3d2..8402daeb121 100644 --- a/src/main/java/org/elasticsearch/alerting/IndexAlertAction.java +++ b/src/main/java/org/elasticsearch/alerting/IndexAlertAction.java @@ -9,15 +9,20 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.logging.ESLogger; +import org.elasticsearch.common.logging.Loggers; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentHelper; import java.io.IOException; -public class IndexAlertAction implements AlertAction { +public class IndexAlertAction implements AlertAction, ToXContent { private final String index; private final String type; private Client client = null; + ESLogger logger = Loggers.getLogger(IndexAlertAction.class); @Inject public IndexAlertAction(String index, String type, Client client){ @@ -33,7 +38,7 @@ public class IndexAlertAction implements AlertAction { } @Override - public XContentBuilder toXContent(XContentBuilder builder) throws IOException { + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field("index", index); builder.field("type", type); @@ -47,9 +52,11 @@ public class IndexAlertAction implements AlertAction { indexRequest.index(index); indexRequest.type(type); try { - XContentBuilder resultBuilder = XContentFactory.jsonBuilder(); - alertResult.searchResponse.toXContent(resultBuilder,null); + XContentBuilder resultBuilder = XContentFactory.jsonBuilder().prettyPrint(); + resultBuilder.startObject(); + resultBuilder = alertResult.searchResponse.toXContent(resultBuilder, ToXContent.EMPTY_PARAMS); resultBuilder.field("timestamp", alertResult.fireTime); + resultBuilder.endObject(); indexRequest.source(resultBuilder); } catch (IOException ie) { throw new ElasticsearchException("Unable to create XContentBuilder",ie);