Skip to content

Commit 542ee5f

Browse files
authored
Format Watcher.status.lastChecked and lastMetCondition (#38788) backport#38626
Change the formatting for Watcher.status.lastCheck and lastMetCondition to be the same as Watcher.status.state.timestamp. These should all have only millisecond precision closes #38619 backport #38626
1 parent a9178b3 commit 542ee5f

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/WatchStatus.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public ZonedDateTime lastChecked() {
8080
return lastChecked;
8181
}
8282

83+
public ZonedDateTime lastMetCondition() {
84+
return lastMetCondition;
85+
}
86+
8387
public ActionStatus actionStatus(String actionId) {
8488
return actions.get(actionId);
8589
}
@@ -252,10 +256,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
252256
builder.field(Field.STATE.getPreferredName(), state, params);
253257
}
254258
if (lastChecked != null) {
255-
builder.timeField(Field.LAST_CHECKED.getPreferredName(), lastChecked);
259+
writeDate(Field.LAST_CHECKED.getPreferredName(), builder, lastChecked);
256260
}
257261
if (lastMetCondition != null) {
258-
builder.timeField(Field.LAST_MET_CONDITION.getPreferredName(), lastMetCondition);
262+
writeDate(Field.LAST_MET_CONDITION.getPreferredName(), builder, lastMetCondition);
259263
}
260264
if (actions != null) {
261265
builder.startObject(Field.ACTIONS.getPreferredName());

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherTestUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.xpack.core.watcher.common.secret.Secret;
2222
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
2323
import org.elasticsearch.xpack.core.watcher.execution.Wid;
24+
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
2425
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
2526
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
2627
import org.elasticsearch.xpack.core.watcher.watch.Payload;
@@ -52,6 +53,7 @@
5253
import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;
5354
import org.elasticsearch.xpack.watcher.trigger.schedule.ScheduleTrigger;
5455
import org.elasticsearch.xpack.watcher.trigger.schedule.ScheduleTriggerEvent;
56+
import org.hamcrest.Matcher;
5557

5658
import javax.mail.internet.AddressException;
5759
import java.io.IOException;
@@ -69,6 +71,7 @@
6971
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
7072
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
7173
import static org.elasticsearch.test.ESTestCase.randomFrom;
74+
import static org.hamcrest.Matchers.is;
7275

7376
public final class WatcherTestUtils {
7477

@@ -188,4 +191,13 @@ public static Watch createTestWatch(String watchName, Client client, HttpClient
188191
public static SearchType getRandomSupportedSearchType() {
189192
return randomFrom(SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH);
190193
}
194+
195+
public static Matcher<String> isSameDate(ZonedDateTime zonedDateTime) {
196+
/*
197+
When comparing timestamps returned from _search/.watcher-history* the same format of date has to be used
198+
during serialisation to json on index time.
199+
The toString of ZonedDateTime is omitting the millisecond part when is 0. This was not the case in joda.
200+
*/
201+
return is(WatcherDateTimeUtils.formatDate(zonedDateTime));
202+
}
191203
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/HistoryIntegrationTests.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
import org.elasticsearch.xpack.core.watcher.actions.ActionStatus;
1818
import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder;
1919
import org.elasticsearch.xpack.core.watcher.input.Input;
20-
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
2120
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
2221
import org.elasticsearch.xpack.core.watcher.watch.WatchStatus;
2322
import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateRequest;
2423
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
24+
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;
2525
import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;
26-
import org.hamcrest.Matcher;
2726

28-
import java.time.ZonedDateTime;
2927
import java.util.Locale;
3028

3129
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@@ -152,7 +150,6 @@ public void testPayloadInputWithDotsInFieldNameWorks() throws Exception {
152150
}
153151
}
154152

155-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/38693")
156153
public void testThatHistoryContainsStatus() throws Exception {
157154
watcherClient().preparePutWatch("test_watch")
158155
.setSource(watchBuilder()
@@ -176,10 +173,12 @@ public void testThatHistoryContainsStatus() throws Exception {
176173
assertThat(active, is(status.state().isActive()));
177174

178175
String timestamp = source.getValue("status.state.timestamp");
179-
assertThat(timestamp, isSameDate(status.state().getTimestamp()));
176+
assertThat(timestamp, WatcherTestUtils.isSameDate(status.state().getTimestamp()));
180177

181178
String lastChecked = source.getValue("status.last_checked");
182-
assertThat(lastChecked, isSameDate(status.lastChecked()));
179+
assertThat(lastChecked, WatcherTestUtils.isSameDate(status.lastChecked()));
180+
String lastMetCondition = source.getValue("status.last_met_condition");
181+
assertThat(lastMetCondition, WatcherTestUtils.isSameDate(status.lastMetCondition()));
183182

184183
Integer version = source.getValue("status.version");
185184
int expectedVersion = (int) (status.version() - 1);
@@ -202,12 +201,4 @@ public void testThatHistoryContainsStatus() throws Exception {
202201
}
203202

204203

205-
private Matcher<String> isSameDate(ZonedDateTime zonedDateTime) {
206-
/*
207-
When comparing timestamps returned from _search/.watcher-history* the same format of date has to be used
208-
during serialisation to json on index time.
209-
The toString of ZonedDateTime is omitting the millisecond part when is 0. This was not the case in joda.
210-
*/
211-
return is(WatcherDateTimeUtils.formatDate(zonedDateTime));
212-
}
213204
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchStatusIntegrationTests.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@
1212
import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchResponse;
1313
import org.elasticsearch.xpack.watcher.condition.NeverCondition;
1414
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
15+
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;
16+
import org.hamcrest.FeatureMatcher;
17+
import org.hamcrest.Matcher;
18+
19+
import java.time.ZonedDateTime;
20+
import java.time.temporal.ChronoField;
1521

1622
import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.loggingAction;
1723
import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder;
1824
import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput;
1925
import static org.elasticsearch.xpack.watcher.trigger.TriggerBuilders.schedule;
2026
import static org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule.Interval.Unit.SECONDS;
2127
import static org.elasticsearch.xpack.watcher.trigger.schedule.Schedules.interval;
28+
import static org.hamcrest.Matchers.equalTo;
2229
import static org.hamcrest.Matchers.is;
2330
import static org.hamcrest.Matchers.notNullValue;
31+
import static org.hamcrest.Matchers.nullValue;
2432

2533
public class WatchStatusIntegrationTests extends AbstractWatcherIntegrationTestCase {
2634

27-
@AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/38619")
2835
public void testThatStatusGetsUpdated() {
2936
WatcherClient watcherClient = watcherClient();
3037
watcherClient.preparePutWatch("_name")
@@ -44,10 +51,24 @@ public void testThatStatusGetsUpdated() {
4451
GetResponse getResponse = client().prepareGet(".watches", "doc", "_name").get();
4552
getResponse.getSource();
4653
XContentSource source = new XContentSource(getResponse.getSourceAsBytesRef(), XContentType.JSON);
54+
4755
String lastChecked = source.getValue("status.last_checked");
56+
assertThat(lastChecked, WatcherTestUtils.isSameDate(getWatchResponse.getStatus().lastChecked()));
57+
assertThat(getWatchResponse.getStatus().lastChecked(), isMillisResolution());
58+
// not started yet, so both nulls
59+
String lastMetCondition = source.getValue("status.last_met_condition");
60+
assertThat(lastMetCondition, is(nullValue()));
61+
assertThat(getWatchResponse.getStatus().lastMetCondition(), is(nullValue()));
62+
}
4863

49-
assertThat(lastChecked, is(notNullValue()));
50-
assertThat(getWatchResponse.getStatus().lastChecked().toString(), is(lastChecked));
64+
private Matcher<ZonedDateTime> isMillisResolution() {
65+
return new FeatureMatcher<ZonedDateTime,Boolean>(equalTo(true), "has millisecond precision", "precission") {
66+
@Override
67+
protected Boolean featureValueOf(ZonedDateTime actual) {
68+
//if date has millisecond precision its nanosecond field will be rounded to millis (equal millis * 10^6)
69+
return actual.getNano() == actual.get(ChronoField.MILLI_OF_SECOND) * 1000_000;
70+
}
71+
};
5172
}
5273

5374
}

0 commit comments

Comments
 (0)