Skip to content

Commit 4e182b9

Browse files
authored
[ML] adding datafeed_config to job in high level rest client (#75338)
1 parent b2b80f7 commit 4e182b9

File tree

5 files changed

+84
-14
lines changed

5 files changed

+84
-14
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package org.elasticsearch.client.ml.datafeed;
99

1010
import org.elasticsearch.action.support.IndicesOptions;
11-
import org.elasticsearch.client.ml.job.config.Job;
1211
import org.elasticsearch.common.xcontent.ParseField;
1312
import org.elasticsearch.common.bytes.BytesArray;
1413
import org.elasticsearch.common.bytes.BytesReference;
@@ -43,6 +42,7 @@
4342
public class DatafeedConfig implements ToXContentObject {
4443

4544
public static final ParseField ID = new ParseField("datafeed_id");
45+
public static final ParseField JOB_ID = new ParseField("job_id");
4646
public static final ParseField QUERY_DELAY = new ParseField("query_delay");
4747
public static final ParseField FREQUENCY = new ParseField("frequency");
4848
public static final ParseField INDEXES = new ParseField("indexes");
@@ -61,7 +61,7 @@ public class DatafeedConfig implements ToXContentObject {
6161

6262
static {
6363
PARSER.declareString(ConstructingObjectParser.constructorArg(), ID);
64-
PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID);
64+
PARSER.declareString(ConstructingObjectParser.constructorArg(), JOB_ID);
6565

6666
PARSER.declareStringArray(Builder::setIndices, INDEXES);
6767
PARSER.declareStringArray(Builder::setIndices, INDICES);
@@ -189,7 +189,7 @@ public Map<String, Object> getRuntimeMappings() {
189189
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
190190
builder.startObject();
191191
builder.field(ID.getPreferredName(), id);
192-
builder.field(Job.ID.getPreferredName(), jobId);
192+
builder.field(JOB_ID.getPreferredName(), jobId);
193193
if (queryDelay != null) {
194194
builder.field(QUERY_DELAY.getPreferredName(), queryDelay.getStringRep());
195195
}
@@ -312,7 +312,7 @@ public static class Builder {
312312

313313
public Builder(String id, String jobId) {
314314
this.id = Objects.requireNonNull(id, ID.getPreferredName());
315-
this.jobId = Objects.requireNonNull(jobId, Job.ID.getPreferredName());
315+
this.jobId = Objects.requireNonNull(jobId, JOB_ID.getPreferredName());
316316
}
317317

318318
public Builder(DatafeedConfig config) {

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Job.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.client.ml.job.config;
99

1010
import org.elasticsearch.client.common.TimeUtil;
11+
import org.elasticsearch.client.ml.datafeed.DatafeedConfig;
1112
import org.elasticsearch.common.xcontent.ParseField;
1213
import org.elasticsearch.common.Strings;
1314
import org.elasticsearch.core.TimeValue;
@@ -24,6 +25,7 @@
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Objects;
28+
import java.util.Optional;
2729

2830
/**
2931
* This class represents a configured and created Job. The creation time is set
@@ -60,6 +62,7 @@ public class Job implements ToXContentObject {
6062
public static final ParseField DELETING = new ParseField("deleting");
6163
public static final ParseField ALLOW_LAZY_OPEN = new ParseField("allow_lazy_open");
6264
public static final ParseField BLOCKED = new ParseField("blocked");
65+
public static final ParseField DATAFEED_CONFIG = new ParseField("datafeed_config");
6366

6467
public static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>("job_details", true, Builder::new);
6568

@@ -92,6 +95,7 @@ public class Job implements ToXContentObject {
9295
PARSER.declareBoolean(Builder::setDeleting, DELETING);
9396
PARSER.declareBoolean(Builder::setAllowLazyOpen, ALLOW_LAZY_OPEN);
9497
PARSER.declareObject(Builder::setBlocked, Blocked.PARSER, BLOCKED);
98+
PARSER.declareObject(Builder::setDatafeed, DatafeedConfig.PARSER, DATAFEED_CONFIG);
9599
}
96100

97101
private final String jobId;
@@ -116,14 +120,15 @@ public class Job implements ToXContentObject {
116120
private final Boolean deleting;
117121
private final Boolean allowLazyOpen;
118122
private final Blocked blocked;
123+
private final DatafeedConfig datafeedConfig;
119124

120125
private Job(String jobId, String jobType, List<String> groups, String description,
121126
Date createTime, Date finishedTime,
122127
AnalysisConfig analysisConfig, AnalysisLimits analysisLimits, DataDescription dataDescription,
123128
ModelPlotConfig modelPlotConfig, Long renormalizationWindowDays, TimeValue backgroundPersistInterval,
124129
Long modelSnapshotRetentionDays, Long dailyModelSnapshotRetentionAfterDays, Long resultsRetentionDays,
125130
Map<String, Object> customSettings, String modelSnapshotId, String resultsIndexName, Boolean deleting,
126-
Boolean allowLazyOpen, Blocked blocked) {
131+
Boolean allowLazyOpen, Blocked blocked, DatafeedConfig datafeedConfig) {
127132

128133
this.jobId = jobId;
129134
this.jobType = jobType;
@@ -146,6 +151,7 @@ private Job(String jobId, String jobType, List<String> groups, String descriptio
146151
this.deleting = deleting;
147152
this.allowLazyOpen = allowLazyOpen;
148153
this.blocked = blocked;
154+
this.datafeedConfig = datafeedConfig;
149155
}
150156

151157
/**
@@ -286,6 +292,14 @@ public Blocked getBlocked() {
286292
return blocked;
287293
}
288294

295+
/**
296+
* The currently configured datafeed for the job
297+
* @return Optional of the datafeed config. Will be none if a datafeed is not configured for this job
298+
*/
299+
public Optional<DatafeedConfig> getDatafeedConfig() {
300+
return Optional.ofNullable(datafeedConfig);
301+
}
302+
289303
@Override
290304
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
291305
builder.startObject();
@@ -350,6 +364,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
350364
if (blocked != null) {
351365
builder.field(BLOCKED.getPreferredName(), blocked);
352366
}
367+
if (datafeedConfig != null) {
368+
builder.field(DATAFEED_CONFIG.getPreferredName(), datafeedConfig, params);
369+
}
353370
builder.endObject();
354371
return builder;
355372
}
@@ -385,15 +402,16 @@ public boolean equals(Object other) {
385402
&& Objects.equals(this.resultsIndexName, that.resultsIndexName)
386403
&& Objects.equals(this.deleting, that.deleting)
387404
&& Objects.equals(this.allowLazyOpen, that.allowLazyOpen)
388-
&& Objects.equals(this.blocked, that.blocked);
405+
&& Objects.equals(this.blocked, that.blocked)
406+
&& Objects.equals(this.datafeedConfig, that.datafeedConfig);
389407
}
390408

391409
@Override
392410
public int hashCode() {
393411
return Objects.hash(jobId, jobType, groups, description, createTime, finishedTime,
394412
analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays,
395413
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
396-
customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen, blocked);
414+
customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen, blocked, datafeedConfig);
397415
}
398416

399417
@Override
@@ -428,6 +446,7 @@ public static class Builder {
428446
private Boolean deleting;
429447
private Boolean allowLazyOpen;
430448
private Blocked blocked;
449+
private DatafeedConfig.Builder datafeedConfig;
431450

432451
private Builder() {
433452
}
@@ -458,6 +477,7 @@ public Builder(Job job) {
458477
this.deleting = job.getDeleting();
459478
this.allowLazyOpen = job.getAllowLazyOpen();
460479
this.blocked = job.getBlocked();
480+
this.datafeedConfig = job.getDatafeedConfig().isPresent() ? new DatafeedConfig.Builder(job.datafeedConfig) : null;
461481
}
462482

463483
public Builder setId(String id) {
@@ -569,6 +589,11 @@ Builder setBlocked(Blocked blocked) {
569589
return this;
570590
}
571591

592+
public Builder setDatafeed(DatafeedConfig.Builder datafeed) {
593+
this.datafeedConfig = datafeed;
594+
return this;
595+
}
596+
572597
/**
573598
* Builds a job.
574599
*
@@ -581,7 +606,8 @@ public Job build() {
581606
id, jobType, groups, description, createTime, finishedTime,
582607
analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays,
583608
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
584-
customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen, blocked);
609+
customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen, blocked,
610+
datafeedConfig == null ? null : datafeedConfig.build());
585611
}
586612
}
587613
}

client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,38 @@ public void testGetJob() throws Exception {
279279
assertThat(response.jobs().stream().map(Job::getId).collect(Collectors.toList()), hasItems(jobId1, jobId2));
280280
}
281281

282+
public void testGetJobWithDatafeed() throws Exception {
283+
String jobId = "hlrc-job-with-datafeed";
284+
285+
Job job = buildJob(jobId);
286+
MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
287+
machineLearningClient.putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
288+
289+
String datafeedId = "datafeed-" + jobId;
290+
DatafeedConfig datafeedConfig = DatafeedConfig.builder(datafeedId, jobId).setIndices("some_data_index").build();
291+
292+
execute(new PutDatafeedRequest(datafeedConfig), machineLearningClient::putDatafeed, machineLearningClient::putDatafeedAsync);
293+
294+
// Test getting specific job
295+
GetJobResponse response = execute(new GetJobRequest(jobId), machineLearningClient::getJob, machineLearningClient::getJobAsync);
296+
assertThat(response.jobs(), hasSize(1));
297+
assertThat(response.jobs().get(0).getDatafeedConfig().orElse(null), is(notNullValue()));
298+
}
299+
300+
public void testPutJobWithDatafeed() throws Exception {
301+
String jobId = "hlrc-put-job-with-datafeed";
302+
303+
Job.Builder job = buildJobBuilder(jobId).setDatafeed(DatafeedConfig.builder(jobId, jobId).setIndices("some_data_index"));
304+
305+
MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
306+
machineLearningClient.putJob(new PutJobRequest(job.build()), RequestOptions.DEFAULT);
307+
308+
// Test getting specific job
309+
GetJobResponse response = execute(new GetJobRequest(jobId), machineLearningClient::getJob, machineLearningClient::getJobAsync);
310+
assertThat(response.jobs(), hasSize(1));
311+
assertThat(response.jobs().get(0).getDatafeedConfig().orElse(null), is(notNullValue()));
312+
}
313+
282314
public void testDeleteJob_GivenWaitForCompletionIsTrue() throws Exception {
283315
String jobId = randomValidJobId();
284316
Job job = buildJob(jobId);
@@ -2810,6 +2842,10 @@ private static Job buildJobForExpiredDataTests(String jobId) {
28102842
}
28112843

28122844
public static Job buildJob(String jobId) {
2845+
return buildJobBuilder(jobId).build();
2846+
}
2847+
2848+
public static Job.Builder buildJobBuilder(String jobId) {
28132849
Job.Builder builder = new Job.Builder(jobId);
28142850
builder.setDescription(randomAlphaOfLength(10));
28152851

@@ -2828,8 +2864,7 @@ public static Job buildJob(String jobId) {
28282864
dataDescription.setTimeFormat(DataDescription.EPOCH_MS);
28292865
dataDescription.setTimeField("timestamp");
28302866
builder.setDataDescription(dataDescription);
2831-
2832-
return builder.build();
2867+
return builder;
28332868
}
28342869

28352870
private void putJob(Job job) throws IOException {

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedConfigTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public static DatafeedConfig createRandom() {
4040
return createRandomBuilder().build();
4141
}
4242

43-
public static DatafeedConfig.Builder createRandomBuilder() {
43+
public static DatafeedConfig.Builder createRandomBuilder(String datafeedId, String jobId) {
4444
long bucketSpanMillis = 3600000;
45-
DatafeedConfig.Builder builder = constructBuilder();
45+
DatafeedConfig.Builder builder = constructBuilder(datafeedId, jobId);
4646
builder.setIndices(randomStringList(1, 10));
4747
if (randomBoolean()) {
4848
try {
@@ -119,6 +119,10 @@ public static DatafeedConfig.Builder createRandomBuilder() {
119119
return builder;
120120
}
121121

122+
public static DatafeedConfig.Builder createRandomBuilder() {
123+
return createRandomBuilder(randomValidDatafeedId(), randomAlphaOfLength(10));
124+
}
125+
122126
public static List<String> randomStringList(int min, int max) {
123127
int size = scaledRandomIntBetween(min, max);
124128
List<String> list = new ArrayList<>();
@@ -175,8 +179,8 @@ public static String randomValidDatafeedId() {
175179
return generator.ofCodePointsLength(random(), 10, 10);
176180
}
177181

178-
private static DatafeedConfig.Builder constructBuilder() {
179-
return new DatafeedConfig.Builder(randomValidDatafeedId(), randomAlphaOfLength(10));
182+
private static DatafeedConfig.Builder constructBuilder(String datafeedId, String jobId) {
183+
return new DatafeedConfig.Builder(datafeedId, jobId);
180184
}
181185

182186
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package org.elasticsearch.client.ml.job.config;
99

1010
import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
11+
12+
import org.elasticsearch.client.ml.datafeed.DatafeedConfigTests;
1113
import org.elasticsearch.common.settings.Settings;
1214
import org.elasticsearch.core.TimeValue;
1315
import org.elasticsearch.common.xcontent.DeprecationHandler;
@@ -163,6 +165,9 @@ public static Job.Builder createRandomizedJobBuilder() {
163165
if (randomBoolean()) {
164166
builder.setBlocked(BlockedTests.createRandom());
165167
}
168+
if (randomBoolean()) {
169+
builder.setDatafeed(DatafeedConfigTests.createRandomBuilder(jobId, jobId));
170+
}
166171
return builder;
167172
}
168173

0 commit comments

Comments
 (0)