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@47325bdd01
This commit is contained in:
parent
26e053eaed
commit
4c1c502f80
|
@ -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();
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
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);
|
||||
|
|
|
@ -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<String, Alert> alertEntry : alertMap.entrySet()) {
|
||||
builder.field(alertEntry.getKey());
|
||||
alertEntry.getValue().toXContent(builder);
|
||||
alertEntry.getValue().toXContent(builder, ToXContent.EMPTY_PARAMS);
|
||||
}
|
||||
builder.endObject();
|
||||
return builder;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue