From ce0d9d820b55d519101f0bae2853f0b2dcc8594d Mon Sep 17 00:00:00 2001 From: Ed Savage Date: Tue, 19 Jan 2021 17:11:10 +0000 Subject: [PATCH] [ML] Send autodetect config updates as JSON Send configuration updates to model plot, scheduled events, detector rules and filters as JSON formatted strings. Remove any now unneeded supporting code. --- .../ml/integration/DetectionRulesIT.java | 1 + .../ml/integration/ScheduledEventsIT.java | 2 + .../writer/AutodetectControlMsgWriter.java | 73 ++++++++++++------ .../autodetect/writer/MlFilterWriter.java | 49 ------------ .../writer/ModelPlotConfigWriter.java | 52 ------------- .../writer/ScheduledEventsWriter.java | 61 --------------- .../autodetect/writer/WriterConstants.java | 17 ---- .../AutodetectControlMsgWriterTests.java | 32 ++++---- .../writer/MlFilterWriterTests.java | 38 --------- .../writer/ModelPlotConfigWriterTests.java | 77 ------------------- .../writer/ScheduledEventsWriterTests.java | 55 ------------- 11 files changed, 68 insertions(+), 389 deletions(-) delete mode 100644 x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriter.java delete mode 100644 x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriter.java delete mode 100644 x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriter.java delete mode 100644 x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/WriterConstants.java delete mode 100644 x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriterTests.java delete mode 100644 x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriterTests.java delete mode 100644 x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java index c62893b142dae..b1cacea65867b 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java @@ -122,6 +122,7 @@ public void testCondition() throws Exception { assertThat(records.get(0).getByFieldValue(), equalTo("low")); } + @AwaitsFix(bugUrl = "https://github.com/elastic/ml-cpp/issues/1253") public void testScope() throws Exception { MlFilter safeIps = MlFilter.builder("safe_ips").setItems("111.111.111.111", "222.222.222.222").build(); assertThat(putMlFilter(safeIps).getFilter(), equalTo(safeIps)); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java index b682abe3a7f9e..6f496af7a2ac9 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java @@ -195,6 +195,7 @@ public void testScheduledEventWithInterimResults() throws IOException { /** * Test an open job picks up changes to scheduled events/calendars */ + @AwaitsFix(bugUrl = "https://github.com/elastic/ml-cpp/issues/1253") public void testAddEventsToOpenJob() throws Exception { TimeValue bucketSpan = TimeValue.timeValueMinutes(30); Job.Builder job = createJob("scheduled-events-add-events-to-open-job", bucketSpan); @@ -263,6 +264,7 @@ public void testAddEventsToOpenJob() throws Exception { /** * An open job that later gets added to a calendar, should take the scheduled events into account */ + @AwaitsFix(bugUrl = "https://github.com/elastic/ml-cpp/issues/1253") public void testAddOpenedJobToGroupWithCalendar() throws Exception { TimeValue bucketSpan = TimeValue.timeValueMinutes(30); String groupName = "opened-calendar-job-group"; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java index 8efebf613dc14..17729525e8538 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java @@ -7,7 +7,6 @@ import org.elasticsearch.common.Strings; 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.json.JsonXContent; @@ -23,9 +22,9 @@ import java.io.IOException; import java.io.OutputStream; -import java.io.StringWriter; import java.util.List; import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; /** * A writer for sending control messages to the C++ autodetect process. @@ -84,6 +83,21 @@ public class AutodetectControlMsgWriter extends AbstractControlMsgWriter { */ private static AtomicLong ms_FlushNumber = new AtomicLong(1); + /** + * This field name must match that in the api::CAnomalyJobConfig C++ class. + */ + private static final String DETECTOR_INDEX = "detector_index"; + + /** + * This field name must match that in the api::CAnomalyJobConfig C++ class. + */ + private static final String CUSTOM_RULES = "custom_rules"; + + /** + * This field name must match that in the api::CAnomalyJobConfig C++ class. + */ + private static final String DETECTOR_RULES = "detector_rules"; + /** * Construct the control message writer with a LengthEncodedWriter * @@ -192,10 +206,16 @@ private void writeControlCodeFollowedByTimeRange(String code, String start, Stri } public void writeUpdateModelPlotMessage(ModelPlotConfig modelPlotConfig) throws IOException { - StringWriter configWriter = new StringWriter(); - configWriter.append(UPDATE_MESSAGE_CODE).append("[modelPlotConfig]\n"); - new ModelPlotConfigWriter(modelPlotConfig, configWriter).write(); - writeMessage(configWriter.toString()); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(UPDATE_MESSAGE_CODE); + try (XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { + jsonBuilder.startObject(); + jsonBuilder.field(ModelPlotConfig.TYPE_FIELD.getPreferredName(), modelPlotConfig); + jsonBuilder.endObject(); + String msg = Strings.toString(jsonBuilder); + stringBuilder.append(msg); + } + writeMessage(stringBuilder.toString()); } public void writeCategorizationStopOnWarnMessage(boolean isStopOnWarn) throws IOException { @@ -204,37 +224,44 @@ public void writeCategorizationStopOnWarnMessage(boolean isStopOnWarn) throws IO public void writeUpdateDetectorRulesMessage(int detectorIndex, List rules) throws IOException { StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(UPDATE_MESSAGE_CODE).append("[detectorRules]\n"); - stringBuilder.append("detectorIndex=").append(Integer.toString(detectorIndex)).append("\n"); - - stringBuilder.append("rulesJson="); + stringBuilder.append(UPDATE_MESSAGE_CODE); try (XContentBuilder builder = JsonXContent.contentBuilder()) { - builder.startArray(); - for (DetectionRule rule : rules) { - rule.toXContent(builder, ToXContent.EMPTY_PARAMS); - } - builder.endArray(); + builder.startObject(); + builder.field(DETECTOR_RULES).startObject(); + builder.field(DETECTOR_INDEX, detectorIndex); + builder.field(CUSTOM_RULES, rules); + builder.endObject(); + builder.endObject(); stringBuilder.append(Strings.toString(builder)); } - writeMessage(stringBuilder.toString()); } public void writeUpdateFiltersMessage(List filters) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(UPDATE_MESSAGE_CODE).append("[filters]\n"); - new MlFilterWriter(filters, stringBuilder).write(); + stringBuilder.append(UPDATE_MESSAGE_CODE); + try (XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { + jsonBuilder.startObject().field(MlFilter.RESULTS_FIELD.getPreferredName(), filters).endObject(); + String msg = Strings.toString(jsonBuilder); + stringBuilder.append(msg); + } writeMessage(stringBuilder.toString()); } public void writeUpdateScheduledEventsMessage(List events, TimeValue bucketSpan) throws IOException { StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(UPDATE_MESSAGE_CODE).append("[scheduledEvents]\n"); - if (events.isEmpty()) { - stringBuilder.append("clear = true\n"); - } else { - new ScheduledEventsWriter(events, bucketSpan, stringBuilder).write(); + stringBuilder.append(UPDATE_MESSAGE_CODE); + + List scheduledEventToRuleWriters = events.stream() + .map(x -> new ScheduledEventToRuleWriter(x.getDescription(), x.toDetectionRule(bucketSpan))) + .collect(Collectors.toList()); + + try (XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { + jsonBuilder.startObject().field(ScheduledEvent.RESULTS_FIELD.getPreferredName(), scheduledEventToRuleWriters).endObject(); + String msg = Strings.toString(jsonBuilder); + stringBuilder.append(msg); } writeMessage(stringBuilder.toString()); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriter.java deleted file mode 100644 index d832c197e0e3e..0000000000000 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriter.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.job.process.autodetect.writer; - -import org.elasticsearch.xpack.core.ml.job.config.MlFilter; - -import java.io.IOException; -import java.util.Collection; -import java.util.Objects; - -import static org.elasticsearch.xpack.ml.job.process.autodetect.writer.WriterConstants.EQUALS; -import static org.elasticsearch.xpack.ml.job.process.autodetect.writer.WriterConstants.NEW_LINE; - -public class MlFilterWriter { - - private static final String FILTER_PREFIX = "filter."; - - private final Collection filters; - private final StringBuilder buffer; - - public MlFilterWriter(Collection filters, StringBuilder buffer) { - this.filters = Objects.requireNonNull(filters); - this.buffer = Objects.requireNonNull(buffer); - } - - public void write() throws IOException { - for (MlFilter filter : filters) { - - StringBuilder filterAsJson = new StringBuilder(); - filterAsJson.append('['); - boolean first = true; - for (String item : filter.getItems()) { - if (first) { - first = false; - } else { - filterAsJson.append(','); - } - filterAsJson.append('"'); - filterAsJson.append(item); - filterAsJson.append('"'); - } - filterAsJson.append(']'); - buffer.append(FILTER_PREFIX).append(filter.getId()).append(EQUALS).append(filterAsJson).append(NEW_LINE); - } - } -} diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriter.java deleted file mode 100644 index 8744bb789a591..0000000000000 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriter.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.job.process.autodetect.writer; - -import org.elasticsearch.xpack.core.ml.job.config.ModelPlotConfig; - -import java.io.IOException; -import java.io.Writer; -import java.util.Objects; - -import static org.elasticsearch.xpack.ml.job.process.autodetect.writer.WriterConstants.EQUALS; -import static org.elasticsearch.xpack.ml.job.process.autodetect.writer.WriterConstants.NEW_LINE; - -public class ModelPlotConfigWriter { - - private static final double BOUNDS_PERCENTILE_DEFAULT = 95.0; - private static final double BOUNDS_PERCENTILE_DISABLE_VALUE = -1.0; - - private final ModelPlotConfig modelPlotConfig; - private final Writer writer; - - public ModelPlotConfigWriter(ModelPlotConfig modelPlotConfig, Writer writer) { - this.modelPlotConfig = Objects.requireNonNull(modelPlotConfig); - this.writer = Objects.requireNonNull(writer); - } - - public void write() throws IOException { - StringBuilder contents = new StringBuilder(); - - contents.append("boundspercentile") - .append(EQUALS) - .append(modelPlotConfig.isEnabled() ? - BOUNDS_PERCENTILE_DEFAULT : BOUNDS_PERCENTILE_DISABLE_VALUE) - .append(NEW_LINE); - - String terms = modelPlotConfig.getTerms(); - contents.append(ModelPlotConfig.TERMS_FIELD.getPreferredName()) - .append(EQUALS) - .append(terms == null ? "" : terms) - .append(NEW_LINE); - - contents.append(ModelPlotConfig.ANNOTATIONS_ENABLED_FIELD.getPreferredName()) - .append(EQUALS) - .append(modelPlotConfig.annotationsEnabled()) - .append(NEW_LINE); - - writer.write(contents.toString()); - } -} diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriter.java deleted file mode 100644 index b48b258d356cb..0000000000000 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriter.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.job.process.autodetect.writer; - -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; - -import java.io.IOException; -import java.util.Collection; -import java.util.Objects; - -import static org.elasticsearch.xpack.ml.job.process.autodetect.writer.WriterConstants.EQUALS; -import static org.elasticsearch.xpack.ml.job.process.autodetect.writer.WriterConstants.NEW_LINE; - -public class ScheduledEventsWriter { - - private static final String SCHEDULED_EVENT_PREFIX = "scheduledevent."; - private static final String SCHEDULED_EVENT_DESCRIPTION_SUFFIX = ".description"; - private static final String RULES_SUFFIX = ".rules"; - - private final Collection scheduledEvents; - private final TimeValue bucketSpan; - private final StringBuilder buffer; - - public ScheduledEventsWriter(Collection scheduledEvents, TimeValue bucketSpan, StringBuilder buffer) { - this.scheduledEvents = Objects.requireNonNull(scheduledEvents); - this.bucketSpan = Objects.requireNonNull(bucketSpan); - this.buffer = Objects.requireNonNull(buffer); - } - - public void write() throws IOException { - int eventIndex = 0; - for (ScheduledEvent event: scheduledEvents) { - - StringBuilder eventContent = new StringBuilder(); - eventContent.append(SCHEDULED_EVENT_PREFIX).append(eventIndex) - .append(SCHEDULED_EVENT_DESCRIPTION_SUFFIX).append(EQUALS) - .append(event.getDescription()) - .append(NEW_LINE); - - eventContent.append(SCHEDULED_EVENT_PREFIX).append(eventIndex).append(RULES_SUFFIX).append(EQUALS); - try (XContentBuilder contentBuilder = XContentFactory.jsonBuilder()) { - contentBuilder.startArray(); - event.toDetectionRule(bucketSpan).toXContent(contentBuilder, null); - contentBuilder.endArray(); - eventContent.append(Strings.toString(contentBuilder)); - } - - eventContent.append(NEW_LINE); - buffer.append(eventContent.toString()); - - ++eventIndex; - } - } -} diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/WriterConstants.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/WriterConstants.java deleted file mode 100644 index db48e76b8a65b..0000000000000 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/WriterConstants.java +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.job.process.autodetect.writer; - -/** - * Common constants for process writer classes - */ -public final class WriterConstants { - public static final char NEW_LINE = '\n'; - public static final String EQUALS = " = "; - - private WriterConstants() { - } -} diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java index 7804d459e9995..5170a47d5b4b1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java @@ -187,7 +187,7 @@ public void testWriteUpdateModelPlotMessage() throws IOException { inOrder.verify(lengthEncodedWriter).writeNumFields(4); inOrder.verify(lengthEncodedWriter, times(3)).writeField(""); inOrder.verify(lengthEncodedWriter) - .writeField("u[modelPlotConfig]\nboundspercentile = 95.0\nterms = foo,bar\nannotations_enabled = false\n"); + .writeField("u{\"model_plot_config\":{\"enabled\":true,\"terms\":\"foo,bar\",\"annotations_enabled\":false}}"); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -201,11 +201,10 @@ public void testWriteUpdateDetectorRulesMessage() throws IOException { InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(4); inOrder.verify(lengthEncodedWriter, times(3)).writeField(""); - inOrder.verify(lengthEncodedWriter).writeField("u[detectorRules]\ndetectorIndex=2\n" + - "rulesJson=[{\"actions\":[\"skip_result\"],\"conditions\":" + - "[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}," + - "{\"actions\":[\"skip_result\"],\"conditions\":[" + - "{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}]"); + inOrder.verify(lengthEncodedWriter).writeField("u{\"detector_rules\":{\"detector_index\":2," + + "\"custom_rules\":[{\"actions\":[\"skip_result\"]," + + "\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}," + + "{\"actions\":[\"skip_result\"],\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}]}}"); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -220,7 +219,8 @@ public void testWriteUpdateFiltersMessage() throws IOException { InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(2); inOrder.verify(lengthEncodedWriter, times(1)).writeField(""); - inOrder.verify(lengthEncodedWriter).writeField("u[filters]\nfilter.filter_1 = [\"a\"]\nfilter.filter_2 = [\"b\",\"c\"]\n"); + inOrder.verify(lengthEncodedWriter).writeField("u{\"filters\":[{\"filter_id\":\"filter_1\",\"items\":[\"a\"]}," + + "{\"filter_id\":\"filter_2\",\"items\":[\"b\",\"c\"]}]}"); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -246,15 +246,13 @@ public void testWriteUpdateScheduledEventsMessage() throws IOException { inOrder.verify(lengthEncodedWriter, times(1)).writeField(""); ArgumentCaptor capturedMessage = ArgumentCaptor.forClass(String.class); inOrder.verify(lengthEncodedWriter).writeField(capturedMessage.capture()); - assertThat(capturedMessage.getValue(), equalTo("u[scheduledEvents]\n" - + "scheduledevent.0.description = new year\n" - + "scheduledevent.0.rules = [{\"actions\":[\"skip_result\",\"skip_model_update\"]," - + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5147648E9}," - + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5148512E9}]}]\n" - + "scheduledevent.1.description = Jan maintenance day\n" - + "scheduledevent.1.rules = [{\"actions\":[\"skip_result\",\"skip_model_update\"]," - + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5151968E9}," - + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5152832E9}]}]\n")); + assertThat(capturedMessage.getValue(), equalTo("u{\"events\":[{\"description\":\"new year\"," + + "\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," + + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5147648E9}," + + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5148512E9}]}]}," + + "{\"description\":\"Jan maintenance day\",\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," + + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5151968E9}," + + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5152832E9}]}]}]}")); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -266,7 +264,7 @@ public void testWriteUpdateScheduledEventsMessage_GivenEmpty() throws IOExceptio InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(2); inOrder.verify(lengthEncodedWriter, times(1)).writeField(""); - inOrder.verify(lengthEncodedWriter).writeField("u[scheduledEvents]\nclear = true\n"); + inOrder.verify(lengthEncodedWriter).writeField("u{\"events\":[]}"); verifyNoMoreInteractions(lengthEncodedWriter); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriterTests.java deleted file mode 100644 index 12ceb12f46223..0000000000000 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/MlFilterWriterTests.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.job.process.autodetect.writer; - -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.core.ml.job.config.MlFilter; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; - -public class MlFilterWriterTests extends ESTestCase { - - public void testWrite_GivenEmpty() throws IOException { - StringBuilder buffer = new StringBuilder(); - new MlFilterWriter(Collections.emptyList(), buffer).write(); - - assertThat(buffer.toString().isEmpty(), is(true)); - } - - public void testWrite() throws IOException { - List filters = new ArrayList<>(); - filters.add(MlFilter.builder("filter_1").setItems("a", "b").build()); - filters.add(MlFilter.builder("filter_2").setItems("c", "d").build()); - - StringBuilder buffer = new StringBuilder(); - new MlFilterWriter(filters, buffer).write(); - - assertThat(buffer.toString(), equalTo("filter.filter_1 = [\"a\",\"b\"]\nfilter.filter_2 = [\"c\",\"d\"]\n")); - } -} \ No newline at end of file diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriterTests.java deleted file mode 100644 index 4682658b43df6..0000000000000 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ModelPlotConfigWriterTests.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.job.process.autodetect.writer; - -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.core.ml.job.config.ModelPlotConfig; -import org.junit.After; -import org.junit.Before; -import org.mockito.Mockito; - -import java.io.IOException; -import java.io.OutputStreamWriter; - -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -public class ModelPlotConfigWriterTests extends ESTestCase { - private OutputStreamWriter writer; - - @Before - public void setUpMocks() { - writer = Mockito.mock(OutputStreamWriter.class); - } - - @After - public void verifyNoMoreWriterInteractions() { - verifyNoMoreInteractions(writer); - } - - public void testWrite_GivenEnabledConfigWithoutTerms() throws IOException { - ModelPlotConfig modelPlotConfig = new ModelPlotConfig(); - ModelPlotConfigWriter writer = new ModelPlotConfigWriter(modelPlotConfig, this.writer); - - writer.write(); - - verify(this.writer).write("boundspercentile = 95.0\nterms = \nannotations_enabled = true\n"); - } - - public void testWrite_GivenEnabledConfigWithTerms() throws IOException { - ModelPlotConfig modelPlotConfig = new ModelPlotConfig(true, "foo,bar", false); - ModelPlotConfigWriter writer = new ModelPlotConfigWriter(modelPlotConfig, this.writer); - - writer.write(); - - verify(this.writer).write("boundspercentile = 95.0\nterms = foo,bar\nannotations_enabled = false\n"); - } - - public void testWrite_GivenDisabledConfigWithTerms() throws IOException { - ModelPlotConfig modelPlotConfig = new ModelPlotConfig(false, "foo,bar", false); - ModelPlotConfigWriter writer = new ModelPlotConfigWriter(modelPlotConfig, this.writer); - - writer.write(); - - verify(this.writer).write("boundspercentile = -1.0\nterms = foo,bar\nannotations_enabled = false\n"); - } - - public void testWrite_GivenEnabledConfigWithEnabledAnnotations() throws IOException { - ModelPlotConfig modelPlotConfig = new ModelPlotConfig(true, null, true); - ModelPlotConfigWriter writer = new ModelPlotConfigWriter(modelPlotConfig, this.writer); - - writer.write(); - - verify(this.writer).write("boundspercentile = 95.0\nterms = \nannotations_enabled = true\n"); - } - - public void testWrite_GivenDisabledConfigWithEnabledAnnotations() throws IOException { - ModelPlotConfig modelPlotConfig = new ModelPlotConfig(false, null, true); - ModelPlotConfigWriter writer = new ModelPlotConfigWriter(modelPlotConfig, this.writer); - - writer.write(); - - verify(this.writer).write("boundspercentile = -1.0\nterms = \nannotations_enabled = true\n"); - } -} diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java deleted file mode 100644 index ad9ab67468083..0000000000000 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.ml.job.process.autodetect.writer; - -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; - -import java.io.IOException; -import java.time.Instant; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; - -public class ScheduledEventsWriterTests extends ESTestCase { - - public void testWrite_GivenEmpty() throws IOException { - StringBuilder buffer = new StringBuilder(); - new ScheduledEventsWriter(Collections.emptyList(), TimeValue.timeValueHours(1), buffer).write(); - - assertThat(buffer.toString().isEmpty(), is(true)); - } - - public void testWrite() throws IOException { - List events = new ArrayList<>(); - events.add(new ScheduledEvent.Builder().description("Black Friday") - .startTime(Instant.ofEpochMilli(1511395200000L)) - .endTime(Instant.ofEpochMilli(1515369600000L)) - .calendarId("calendar_id").build()); - events.add(new ScheduledEvent.Builder().description("Blue Monday") - .startTime(Instant.ofEpochMilli(1519603200000L)) - .endTime(Instant.ofEpochMilli(1519862400000L)) - .calendarId("calendar_id").build()); - - StringBuilder buffer = new StringBuilder(); - new ScheduledEventsWriter(events, TimeValue.timeValueHours(1), buffer).write(); - - String expectedString = "scheduledevent.0.description = Black Friday\n" + - "scheduledevent.0.rules = [{\"actions\":[\"skip_result\",\"skip_model_update\"]," + - "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5113952E9}," + - "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5153696E9}]}]\n" + - "scheduledevent.1.description = Blue Monday\n" + - "scheduledevent.1.rules = [{\"actions\":[\"skip_result\",\"skip_model_update\"]," + - "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5196032E9}," + - "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5198624E9}]}]" + - "\n"; - assertThat(buffer.toString(), equalTo(expectedString)); - } -}