Core: Use reading/writing search request infra also in alert action

Original commit: elastic/x-pack-elasticsearch@14dbbfe164
This commit is contained in:
Martijn van Groningen 2014-11-13 19:22:41 +01:00
parent eb3f123ad6
commit cabdcd862b
4 changed files with 24 additions and 31 deletions

View File

@ -8,6 +8,7 @@ package org.elasticsearch.alerts.actions;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertUtils;
import org.elasticsearch.alerts.triggers.AlertTrigger;
import org.elasticsearch.common.io.stream.DataOutputStreamOutput;
import org.elasticsearch.common.joda.time.DateTime;
@ -192,14 +193,12 @@ public class AlertActionEntry implements ToXContent{
historyEntry.field(AlertActionManager.SCHEDULED_FIRE_TIME_FIELD, scheduledTime.toDateTimeISO());
historyEntry.field("trigger", trigger, params);
ByteArrayOutputStream out;
if (searchRequest != null) {
out = new ByteArrayOutputStream();
searchRequest.writeTo(new DataOutputStreamOutput(new DataOutputStream(out)));
historyEntry.field("request_binary", out.toByteArray());
historyEntry.field("request");
AlertUtils.writeSearchRequest(searchRequest, historyEntry);
}
if (searchResponse != null) {
out = new ByteArrayOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
searchResponse.writeTo(new DataOutputStreamOutput(new DataOutputStream(out)));
historyEntry.field("response_binary", out.toByteArray());
// Serializing it as xcontent allows the search response to be encapsulated in a doc as a json object

View File

@ -9,7 +9,6 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.alerts.*;
@ -48,7 +47,7 @@ public class AlertActionManager extends AbstractComponent {
public static final String SCHEDULED_FIRE_TIME_FIELD = "scheduled_fire_time";
public static final String ERROR_MESSAGE = "errorMsg";
public static final String TRIGGER_FIELD = "trigger";
public static final String REQUEST = "request_binary";
public static final String REQUEST = "request";
public static final String RESPONSE = "response_binary";
public static final String ACTIONS_FIELD = "actions";
@ -199,6 +198,9 @@ public class AlertActionManager extends AbstractComponent {
case TRIGGER_FIELD:
entry.setTrigger(triggerManager.instantiateAlertTrigger(parser));
break;
case REQUEST:
entry.setSearchRequest(AlertUtils.readSearchRequest(parser));
break;
case "response":
// Ignore this, the binary form is already read
parser.skipChildren();
@ -221,11 +223,6 @@ public class AlertActionManager extends AbstractComponent {
case SCHEDULED_FIRE_TIME_FIELD:
entry.setScheduledTime(DateTime.parse(parser.text()));
break;
case REQUEST:
SearchRequest request = new SearchRequest();
request.readFrom(new BytesStreamInput(parser.binaryValue(), false));
entry.setSearchRequest(request);
break;
case RESPONSE:
SearchResponse response = new SearchResponse();
response.readFrom(new BytesStreamInput(parser.binaryValue(), false));

View File

@ -30,8 +30,9 @@
"errorMsg": {
"type": "string"
},
"request_binary" : {
"type" : "binary"
"request" : {
"type" : "object",
"dynamic" : true
},
"response_binary" : {
"type" : "binary"

View File

@ -11,50 +11,45 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.AlertManager;
import org.elasticsearch.alerts.AlertUtils;
import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.alerts.transport.actions.get.GetAlertRequest;
import org.elasticsearch.alerts.transport.actions.get.GetAlertResponse;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
import org.elasticsearch.alerts.triggers.AlertTrigger;
import org.elasticsearch.alerts.triggers.ScriptedTrigger;
import org.elasticsearch.alerts.triggers.TriggerResult;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.joda.time.DateTimeZone;
import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.internal.InternalSearchHit;
import org.elasticsearch.search.internal.InternalSearchHits;
import org.elasticsearch.search.internal.InternalSearchResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.core.Is.is;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
*/
@ -107,7 +102,8 @@ public class AlertActionsTest extends ElasticsearchIntegrationTest {
BytesStreamOutput out = new BytesStreamOutput();
SearchRequest searchRequest = new SearchRequest("test123");
searchRequest.writeTo(out);
builder.field(AlertActionManager.REQUEST, out.bytes());
builder.field(AlertActionManager.REQUEST);
AlertUtils.writeSearchRequest(searchRequest, builder);
SearchResponse searchResponse = new SearchResponse(
new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 10, 0), null, null, null, false, false),
null, 1, 1, 0, new ShardSearchFailure[0]