Skip to content

Scripting: Switch watcher to use joda bwc time objects #35966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ to test if a response is necessary.
`ctx['watch_id']` (`String`, read-only)::
The id of the watch.

`ctx['execution_time']` (`DateTime`, read-only)::
`ctx['execution_time']` (`ZonedDateTime`, read-only)::
The start time for the watch.

`ctx['trigger']['scheduled_time']` (`DateTime`, read-only)::
`ctx['trigger']['scheduled_time']` (`ZonedDateTime`, read-only)::
The scheduled trigger time for the watch.

`ctx['trigger']['triggered_time']` (`DateTime`, read-only)::
`ctx['trigger']['triggered_time']` (`ZonedDateTime`, read-only)::
The actual trigger time for the watch.

`ctx['metadata']` (`Map`, read-only)::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ data into a new payload for use in a response to a condition.
`ctx['watch_id']` (`String`, read-only)::
The id of the watch.

`ctx['execution_time']` (`DateTime`, read-only)::
`ctx['execution_time']` (`ZonedDateTime`, read-only)::
The start time for the watch.

`ctx['trigger']['scheduled_time']` (`DateTime`, read-only)::
`ctx['trigger']['scheduled_time']` (`ZonedDateTime`, read-only)::
The scheduled trigger time for the watch.

`ctx['trigger']['triggered_time']` (`DateTime`, read-only)::
`ctx['trigger']['triggered_time']` (`ZonedDateTime`, read-only)::
The actual trigger time for the watch.

`ctx['metadata']` (`Map`, read-only)::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public final class Whitelist {
"java.util.txt",
"java.util.function.txt",
"java.util.regex.txt",
"java.util.stream.txt",
"joda.time.txt"
"java.util.stream.txt"
};

public static final List<Whitelist> BASE_WHITELISTS =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters;
import org.elasticsearch.common.time.DateUtils;
import org.joda.time.DateTime;

Expand All @@ -42,11 +44,13 @@
import java.time.temporal.TemporalUnit;
import java.time.temporal.WeekFields;
import java.util.Locale;
import java.util.Objects;

/**
* A wrapper around ZonedDateTime that exposes joda methods for backcompat.
*/
public class JodaCompatibleZonedDateTime {
private static final DateFormatter DATE_FORMATTER = DateFormatters.forPattern("strict_date_time");
private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(JodaCompatibleZonedDateTime.class));

Expand Down Expand Up @@ -75,7 +79,10 @@ public ZonedDateTime getZonedDateTime() {

@Override
public boolean equals(Object o) {
return dt.equals(o);
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JodaCompatibleZonedDateTime that = (JodaCompatibleZonedDateTime) o;
return Objects.equals(dt, that.dt);
}

@Override
Expand All @@ -85,7 +92,7 @@ public int hashCode() {

@Override
public String toString() {
return dt.toString();
return DATE_FORMATTER.format(dt);
}

public boolean isAfter(ZonedDateTime o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ void assertMethodDeprecation(Runnable assertions, String oldMethod, String newMe
assertDeprecation(assertions, "Use of the joda time method [" + oldMethod + "] is deprecated. Use [" + newMethod + "] instead.");
}

public void testEquals() {
assertThat(javaTime, equalTo(javaTime));
}

public void testToString() {
assertThat(javaTime.toString(), equalTo(jodaTime.toString()));
}

public void testDayOfMonth() {
assertThat(javaTime.getDayOfMonth(), equalTo(jodaTime.getDayOfMonth()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

Expand All @@ -35,6 +36,9 @@ public static DateTime convertToDate(Object value, Clock clock) {
if (value instanceof DateTime) {
return (DateTime) value;
}
if (value instanceof JodaCompatibleZonedDateTime) {
return new DateTime(((JodaCompatibleZonedDateTime) value).toInstant().toEpochMilli(), DateTimeZone.UTC);
}
if (value instanceof String) {
return parseDateMath((String) value, DateTimeZone.UTC, clock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
import org.joda.time.DateTime;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -25,7 +28,8 @@ public TriggerEvent(String jobName, DateTime triggeredTime) {
this.jobName = jobName;
this.triggeredTime = triggeredTime;
this.data = new HashMap<>();
data.put(Field.TRIGGERED_TIME.getPreferredName(), triggeredTime);
data.put(Field.TRIGGERED_TIME.getPreferredName(),
new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(triggeredTime.getMillis()), ZoneOffset.UTC));
}

public String jobName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
*/
package org.elasticsearch.xpack.watcher.support;

import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.watch.Payload;

import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -34,7 +37,8 @@ public static Map<String, Object> createCtx(WatchExecutionContext ctx, Payload p
Map<String, Object> ctxModel = new HashMap<>();
ctxModel.put(ID, ctx.id().value());
ctxModel.put(WATCH_ID, ctx.id().watchId());
ctxModel.put(EXECUTION_TIME, ctx.executionTime());
ctxModel.put(EXECUTION_TIME,
new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(ctx.executionTime().getMillis()), ZoneOffset.UTC));
ctxModel.put(TRIGGER, ctx.triggerEvent().data());
if (payload != null) {
ctxModel.put(PAYLOAD, payload.data());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;

public class ScheduleTriggerEvent extends TriggerEvent {

Expand All @@ -28,7 +31,8 @@ public ScheduleTriggerEvent(DateTime triggeredTime, DateTime scheduledTime) {
public ScheduleTriggerEvent(String jobName, DateTime triggeredTime, DateTime scheduledTime) {
super(jobName, triggeredTime);
this.scheduledTime = scheduledTime;
data.put(Field.SCHEDULED_TIME.getPreferredName(), scheduledTime);
data.put(Field.SCHEDULED_TIME.getPreferredName(),
new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(scheduledTime.getMillis()), ZoneOffset.UTC));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.common.secret.Secret;
Expand Down Expand Up @@ -52,6 +53,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -129,6 +132,7 @@ public void testExecute() throws Exception {
Map<String, Object> metadata = MapBuilder.<String, Object>newMapBuilder().put("_key", "_val").map();

DateTime now = DateTime.now(DateTimeZone.UTC);
JodaCompatibleZonedDateTime jodaJavaNow = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(now.getMillis()), ZoneOffset.UTC);

Wid wid = new Wid("watch1", now);
WatchExecutionContext ctx = mockExecutionContextBuilder("watch1")
Expand All @@ -139,14 +143,14 @@ public void testExecute() throws Exception {
.buildMock();

Map<String, Object> triggerModel = new HashMap<>();
triggerModel.put("triggered_time", now);
triggerModel.put("scheduled_time", now);
triggerModel.put("triggered_time", jodaJavaNow);
triggerModel.put("scheduled_time", jodaJavaNow);
Map<String, Object> ctxModel = new HashMap<>();
ctxModel.put("id", ctx.id().value());
ctxModel.put("watch_id", "watch1");
ctxModel.put("payload", data);
ctxModel.put("metadata", metadata);
ctxModel.put("execution_time", now);
ctxModel.put("execution_time", jodaJavaNow);
ctxModel.put("trigger", triggerModel);
ctxModel.put("vars", emptyMap());
Map<String, Object> expectedModel = singletonMap("ctx", ctxModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
Expand All @@ -35,6 +36,8 @@
import org.junit.Before;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -78,6 +81,7 @@ public void testExecute() throws Exception {
Map<String, Object> metadata = MapBuilder.<String, Object>newMapBuilder().put("_key", "_val").map();

DateTime now = DateTime.now(DateTimeZone.UTC);
JodaCompatibleZonedDateTime jodaJavaNow = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(now.getMillis()), ZoneOffset.UTC);

Wid wid = new Wid(randomAlphaOfLength(5), now);
WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId())
Expand All @@ -88,14 +92,14 @@ public void testExecute() throws Exception {
.buildMock();

Map<String, Object> triggerModel = new HashMap<>();
triggerModel.put("triggered_time", now);
triggerModel.put("scheduled_time", now);
triggerModel.put("triggered_time", jodaJavaNow);
triggerModel.put("scheduled_time", jodaJavaNow);
Map<String, Object> ctxModel = new HashMap<>();
ctxModel.put("id", ctx.id().value());
ctxModel.put("watch_id", wid.watchId());
ctxModel.put("payload", data);
ctxModel.put("metadata", metadata);
ctxModel.put("execution_time", now);
ctxModel.put("execution_time", jodaJavaNow);
ctxModel.put("trigger", triggerModel);
ctxModel.put("vars", Collections.emptyMap());
Map<String, Object> expectedModel = singletonMap("ctx", ctxModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.common.SuppressLoggerChecks;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
Expand All @@ -22,6 +23,8 @@
import org.junit.Before;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -55,18 +58,19 @@ public void init() throws IOException {

public void testExecute() throws Exception {
final DateTime now = DateTime.now(UTC);
JodaCompatibleZonedDateTime jodaJavaNow = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(now.getMillis()), ZoneOffset.UTC);

WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContextBuilder("_watch_id")
.time("_watch_id", now)
.buildMock();

Map<String, Object> triggerModel = new HashMap<>();
triggerModel.put("scheduled_time", now);
triggerModel.put("triggered_time", now);
triggerModel.put("scheduled_time", jodaJavaNow);
triggerModel.put("triggered_time", jodaJavaNow);
Map<String, Object> ctxModel = new HashMap<>();
ctxModel.put("id", ctx.id().value());
ctxModel.put("watch_id", "_watch_id");
ctxModel.put("execution_time", now);
ctxModel.put("execution_time", jodaJavaNow);
ctxModel.put("payload", emptyMap());
ctxModel.put("metadata", emptyMap());
ctxModel.put("vars", emptyMap());
Expand Down
Loading