[ML] Add builder to JobUpdate (elastic/x-pack-elasticsearch#589)

Original commit: elastic/x-pack-elasticsearch@07a69eb407
This commit is contained in:
Dimitris Athanasiou 2017-02-17 11:45:16 +00:00 committed by GitHub
parent 48ee5e021d
commit d970869e00
3 changed files with 116 additions and 52 deletions

View File

@ -26,7 +26,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
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.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
@ -58,7 +57,7 @@ public class UpdateJobAction extends Action<UpdateJobAction.Request, PutJobActio
public static class Request extends AcknowledgedRequest<UpdateJobAction.Request> implements ToXContent {
public static UpdateJobAction.Request parseRequest(String jobId, XContentParser parser) {
JobUpdate update = JobUpdate.PARSER.apply(parser, null);
JobUpdate update = JobUpdate.PARSER.apply(parser, null).build();
return new UpdateJobAction.Request(jobId, update);
}

View File

@ -23,25 +23,19 @@ import java.util.Objects;
public class JobUpdate implements Writeable, ToXContent {
public static final ParseField DETECTORS = new ParseField("detectors");
@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<JobUpdate, Void> PARSER =
new ConstructingObjectParser<>("job_update", a -> new JobUpdate((String) a[0], (List<DetectorUpdate>) a[1],
(ModelDebugConfig) a[2], (AnalysisLimits) a[3], (Long) a[4], (Long) a[5], (Long) a[6], (Long) a[7],
(List<String>) a[8], (Map<String, Object>) a[9]));
public static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>("job_udpate", Builder::new);
static {
PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), Job.DESCRIPTION);
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), DetectorUpdate.PARSER, DETECTORS);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ModelDebugConfig.PARSER, Job.MODEL_DEBUG_CONFIG);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), AnalysisLimits.PARSER, Job.ANALYSIS_LIMITS);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), Job.BACKGROUND_PERSIST_INTERVAL);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), Job.RENORMALIZATION_WINDOW_DAYS);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), Job.RESULTS_RETENTION_DAYS);
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), Job.MODEL_SNAPSHOT_RETENTION_DAYS);
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), AnalysisConfig.CATEGORIZATION_FILTERS);
PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), Job.CUSTOM_SETTINGS,
ObjectParser.ValueType.OBJECT);
PARSER.declareStringOrNull(Builder::setDescription, Job.DESCRIPTION);
PARSER.declareObjectArray(Builder::setDetectorUpdates, DetectorUpdate.PARSER, DETECTORS);
PARSER.declareObject(Builder::setModelDebugConfig, ModelDebugConfig.PARSER, Job.MODEL_DEBUG_CONFIG);
PARSER.declareObject(Builder::setAnalysisLimits, AnalysisLimits.PARSER, Job.ANALYSIS_LIMITS);
PARSER.declareLong(Builder::setBackgroundPersistInterval, Job.BACKGROUND_PERSIST_INTERVAL);
PARSER.declareLong(Builder::setRenormalizationWindowDays, Job.RENORMALIZATION_WINDOW_DAYS);
PARSER.declareLong(Builder::setResultsRetentionDays, Job.RESULTS_RETENTION_DAYS);
PARSER.declareLong(Builder::setModelSnapshotRetentionDays, Job.MODEL_SNAPSHOT_RETENTION_DAYS);
PARSER.declareStringArray(Builder::setCategorizationFilters, AnalysisConfig.CATEGORIZATION_FILTERS);
PARSER.declareField(Builder::setCustomSettings, (p, c) -> p.map(), Job.CUSTOM_SETTINGS, ObjectParser.ValueType.OBJECT);
}
private final String description;
@ -55,11 +49,11 @@ public class JobUpdate implements Writeable, ToXContent {
private final List<String> categorizationFilters;
private final Map<String, Object> customSettings;
public JobUpdate(@Nullable String description, @Nullable List<DetectorUpdate> detectorUpdates,
@Nullable ModelDebugConfig modelDebugConfig, @Nullable AnalysisLimits analysisLimits,
@Nullable Long backgroundPersistInterval, @Nullable Long renormalizationWindowDays,
@Nullable Long resultsRetentionDays, @Nullable Long modelSnapshotRetentionDays,
@Nullable List<String> categorisationFilters, @Nullable Map<String, Object> customSettings) {
private JobUpdate(@Nullable String description, @Nullable List<DetectorUpdate> detectorUpdates,
@Nullable ModelDebugConfig modelDebugConfig, @Nullable AnalysisLimits analysisLimits,
@Nullable Long backgroundPersistInterval, @Nullable Long renormalizationWindowDays,
@Nullable Long resultsRetentionDays, @Nullable Long modelSnapshotRetentionDays,
@Nullable List<String> categorisationFilters, @Nullable Map<String, Object> customSettings) {
this.description = description;
this.detectorUpdates = detectorUpdates;
this.modelDebugConfig = modelDebugConfig;
@ -377,4 +371,74 @@ public class JobUpdate implements Writeable, ToXContent {
&& Objects.equals(this.rules, that.rules);
}
}
public static class Builder {
private String description;
private List<DetectorUpdate> detectorUpdates;
private ModelDebugConfig modelDebugConfig;
private AnalysisLimits analysisLimits;
private Long renormalizationWindowDays;
private Long backgroundPersistInterval;
private Long modelSnapshotRetentionDays;
private Long resultsRetentionDays;
private List<String> categorizationFilters;
private Map<String, Object> customSettings;
public Builder() {}
public Builder setDescription(String description) {
this.description = description;
return this;
}
public Builder setDetectorUpdates(List<DetectorUpdate> detectorUpdates) {
this.detectorUpdates = detectorUpdates;
return this;
}
public Builder setModelDebugConfig(ModelDebugConfig modelDebugConfig) {
this.modelDebugConfig = modelDebugConfig;
return this;
}
public Builder setAnalysisLimits(AnalysisLimits analysisLimits) {
this.analysisLimits = analysisLimits;
return this;
}
public Builder setRenormalizationWindowDays(Long renormalizationWindowDays) {
this.renormalizationWindowDays = renormalizationWindowDays;
return this;
}
public Builder setBackgroundPersistInterval(Long backgroundPersistInterval) {
this.backgroundPersistInterval = backgroundPersistInterval;
return this;
}
public Builder setModelSnapshotRetentionDays(Long modelSnapshotRetentionDays) {
this.modelSnapshotRetentionDays = modelSnapshotRetentionDays;
return this;
}
public Builder setResultsRetentionDays(Long resultsRetentionDays) {
this.resultsRetentionDays = resultsRetentionDays;
return this;
}
public Builder setCategorizationFilters(List<String> categorizationFilters) {
this.categorizationFilters = categorizationFilters;
return this;
}
public Builder setCustomSettings(Map<String, Object> customSettings) {
this.customSettings = customSettings;
return this;
}
public JobUpdate build() {
return new JobUpdate(description, detectorUpdates, modelDebugConfig, analysisLimits, backgroundPersistInterval,
renormalizationWindowDays, resultsRetentionDays, modelSnapshotRetentionDays, categorizationFilters, customSettings);
}
}
}

View File

@ -22,12 +22,12 @@ public class JobUpdateTests extends AbstractSerializingTestCase<JobUpdate> {
@Override
protected JobUpdate createTestInstance() {
String description = null;
JobUpdate.Builder update = new JobUpdate.Builder();
if (randomBoolean()) {
description = randomAsciiOfLength(20);
update.setDescription(randomAsciiOfLength(20));
}
List<JobUpdate.DetectorUpdate> detectorUpdates = null;
if (randomBoolean()) {
List<JobUpdate.DetectorUpdate> detectorUpdates = null;
int size = randomInt(10);
detectorUpdates = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
@ -44,42 +44,34 @@ public class JobUpdateTests extends AbstractSerializingTestCase<JobUpdate> {
}
detectorUpdates.add(new JobUpdate.DetectorUpdate(i, detectorDescription, detectionRules));
}
update.setDetectorUpdates(detectorUpdates);
}
ModelDebugConfig modelDebugConfig = null;
if (randomBoolean()) {
modelDebugConfig = new ModelDebugConfig(randomDouble(), randomAsciiOfLength(10));
update.setModelDebugConfig(new ModelDebugConfig(randomDouble(), randomAsciiOfLength(10)));
}
AnalysisLimits analysisLimits = null;
if (randomBoolean()) {
analysisLimits = new AnalysisLimits(randomNonNegativeLong(), randomNonNegativeLong());
update.setAnalysisLimits(new AnalysisLimits(randomNonNegativeLong(), randomNonNegativeLong()));
}
Long renormalizationWindowDays = null;
if (randomBoolean()) {
renormalizationWindowDays = randomNonNegativeLong();
update.setRenormalizationWindowDays(randomNonNegativeLong());
}
Long backgroundPersistInterval = null;
if (randomBoolean()) {
backgroundPersistInterval = randomNonNegativeLong();
update.setBackgroundPersistInterval(randomNonNegativeLong());
}
Long modelSnapshotRetentionDays = null;
if (randomBoolean()) {
modelSnapshotRetentionDays = randomNonNegativeLong();
update.setModelSnapshotRetentionDays(randomNonNegativeLong());
}
Long resultsRetentionDays = null;
if (randomBoolean()) {
resultsRetentionDays = randomNonNegativeLong();
update.setResultsRetentionDays(randomNonNegativeLong());
}
List<String> categorizationFilters = null;
if (randomBoolean()) {
categorizationFilters = Arrays.asList(generateRandomStringArray(10, 10, false));
update.setCategorizationFilters(Arrays.asList(generateRandomStringArray(10, 10, false)));
}
Map<String, Object> customSettings = null;
if (randomBoolean()) {
customSettings = Collections.singletonMap(randomAsciiOfLength(10), randomAsciiOfLength(10));
update.setCustomSettings(Collections.singletonMap(randomAsciiOfLength(10), randomAsciiOfLength(10)));
}
return new JobUpdate(description, detectorUpdates, modelDebugConfig, analysisLimits, backgroundPersistInterval,
renormalizationWindowDays, resultsRetentionDays, modelSnapshotRetentionDays, categorizationFilters, customSettings);
return update.build();
}
@Override
@ -89,7 +81,7 @@ public class JobUpdateTests extends AbstractSerializingTestCase<JobUpdate> {
@Override
protected JobUpdate parseInstance(XContentParser parser) {
return JobUpdate.PARSER.apply(parser, null);
return JobUpdate.PARSER.apply(parser, null).build();
}
public void testMergeWithJob() {
@ -108,9 +100,18 @@ public class JobUpdateTests extends AbstractSerializingTestCase<JobUpdate> {
List<String> categorizationFilters = Arrays.asList(generateRandomStringArray(10, 10, false));
Map<String, Object> customSettings = Collections.singletonMap(randomAsciiOfLength(10), randomAsciiOfLength(10));
JobUpdate update = new JobUpdate("updated_description", detectorUpdates, modelDebugConfig,
analysisLimits, randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(),
categorizationFilters, customSettings);
JobUpdate.Builder updateBuilder = new JobUpdate.Builder();
updateBuilder.setDescription("updated_description");
updateBuilder.setDetectorUpdates(detectorUpdates);
updateBuilder.setModelDebugConfig(modelDebugConfig);
updateBuilder.setAnalysisLimits(analysisLimits);
updateBuilder.setBackgroundPersistInterval(randomNonNegativeLong());
updateBuilder.setResultsRetentionDays(randomNonNegativeLong());
updateBuilder.setModelSnapshotRetentionDays(randomNonNegativeLong());
updateBuilder.setRenormalizationWindowDays(randomNonNegativeLong());
updateBuilder.setCategorizationFilters(categorizationFilters);
updateBuilder.setCustomSettings(customSettings);
JobUpdate update = updateBuilder.build();
Job.Builder jobBuilder = new Job.Builder("foo");
Detector.Builder d1 = new Detector.Builder("info_content", "domain");
@ -143,11 +144,11 @@ public class JobUpdateTests extends AbstractSerializingTestCase<JobUpdate> {
}
public void testIsAutodetectProcessUpdate() {
JobUpdate update = new JobUpdate(null, null, null, null, null, null, null, null, null, null);
JobUpdate update = new JobUpdate.Builder().build();
assertFalse(update.isAutodetectProcessUpdate());
update = new JobUpdate(null, null, new ModelDebugConfig(1.0, "ff"), null, null, null, null, null, null, null);
update = new JobUpdate.Builder().setModelDebugConfig(new ModelDebugConfig(1.0, "ff")).build();
assertTrue(update.isAutodetectProcessUpdate());
update = new JobUpdate(null, Arrays.asList(mock(JobUpdate.DetectorUpdate.class)), null, null, null, null, null, null, null, null);
update = new JobUpdate.Builder().setDetectorUpdates(Arrays.asList(mock(JobUpdate.DetectorUpdate.class))).build();
assertTrue(update.isAutodetectProcessUpdate());
}
}