From 8b83d7499414bc3e60c767551fd96c9bcd3480b8 Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Fri, 13 Feb 2015 17:07:46 -0800 Subject: [PATCH] Fix DateTime issues We were using DateTime without a timezone to pick the history index to write the alert runs to. This caused tests to fail because we use UTC internally (as we should) Original commit: elastic/x-pack-elasticsearch@6d6f57fb9e96931680657763d287ab04c33510e9 --- .../alerts/actions/index/IndexAction.java | 12 +++++++++--- .../java/org/elasticsearch/alerts/BootStrapTest.java | 7 +++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/elasticsearch/alerts/actions/index/IndexAction.java b/src/main/java/org/elasticsearch/alerts/actions/index/IndexAction.java index 204391c0146..345669fc695 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/index/IndexAction.java +++ b/src/main/java/org/elasticsearch/alerts/actions/index/IndexAction.java @@ -23,6 +23,8 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; /** */ @@ -66,9 +68,13 @@ public class IndexAction extends Action { try { IndexResponse response = client.index(indexRequest).actionGet(); - //@TODO this doesn't seem to work for index response - //return new Result(new Payload.Simple(responseToData(response)), null, true); - return new Result(new Payload.Simple(), null, response.isCreated()); + Map data = new HashMap<>(); + data.put("created", response.isCreated()); + data.put("id", response.getId()); + data.put("version", response.getVersion()); + data.put("type", response.getType()); + data.put("index", response.getIndex()); + return new Result(new Payload.Simple(data), null, response.isCreated()); } catch (ElasticsearchException e) { logger.error("failed to index result for alert [{}]", e, ctx.alert().name()); return new Result(null, "failed to build index request. " + e.getMessage(), false); diff --git a/src/test/java/org/elasticsearch/alerts/BootStrapTest.java b/src/test/java/org/elasticsearch/alerts/BootStrapTest.java index 378aca5c2cc..a218d9711b4 100644 --- a/src/test/java/org/elasticsearch/alerts/BootStrapTest.java +++ b/src/test/java/org/elasticsearch/alerts/BootStrapTest.java @@ -22,11 +22,11 @@ import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsResponse; import org.elasticsearch.alerts.trigger.search.ScriptSearchTrigger; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.joda.time.DateTime; +import org.elasticsearch.common.joda.time.DateTimeZone; 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.index.VersionType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.script.ScriptService; import org.elasticsearch.test.junit.annotations.TestLogging; @@ -87,7 +87,7 @@ public class BootStrapTest extends AbstractAlertingTests { new Alert.Status() ); - DateTime scheduledFireTime = new DateTime(); + DateTime scheduledFireTime = new DateTime(DateTimeZone.UTC); FiredAlert entry = new FiredAlert(alert, scheduledFireTime, scheduledFireTime, FiredAlert.State.AWAITS_RUN); String actionHistoryIndex = HistoryStore.getAlertHistoryIndexNameForTime(scheduledFireTime); @@ -114,7 +114,7 @@ public class BootStrapTest extends AbstractAlertingTests { @Test @TestLogging("alerts.actions:DEBUG") public void testBootStrapManyHistoryIndices() throws Exception { - DateTime now = new DateTime(); + DateTime now = new DateTime(DateTimeZone.UTC); long numberOfAlertHistoryIndices = randomIntBetween(2,8); long numberOfAlertHistoryEntriesPerIndex = randomIntBetween(5,10); SearchRequest searchRequest = createTriggerSearchRequest("my-index").source(searchSource().query(termQuery("field", "value"))); @@ -149,7 +149,6 @@ public class BootStrapTest extends AbstractAlertingTests { IndexResponse indexResponse = client().prepareIndex(actionHistoryIndex, HistoryStore.ALERT_HISTORY_TYPE, entry.id()) .setConsistencyLevel(WriteConsistencyLevel.ALL) .setSource(XContentFactory.jsonBuilder().value(entry)) - .setVersionType(VersionType.INTERNAL) .get(); assertTrue(indexResponse.isCreated()); }