Skip to content

Commit ba2465c

Browse files
committed
HLRC: ML PUT Calendar (#33362)
1 parent c51acad commit ba2465c

20 files changed

+999
-239
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.client.ml.GetRecordsRequest;
4444
import org.elasticsearch.client.ml.OpenJobRequest;
4545
import org.elasticsearch.client.ml.PostDataRequest;
46+
import org.elasticsearch.client.ml.PutCalendarRequest;
4647
import org.elasticsearch.client.ml.PutDatafeedRequest;
4748
import org.elasticsearch.client.ml.PutJobRequest;
4849
import org.elasticsearch.client.ml.UpdateJobRequest;
@@ -346,4 +347,16 @@ static Request getInfluencers(GetInfluencersRequest getInfluencersRequest) throw
346347
request.setEntity(createEntity(getInfluencersRequest, REQUEST_BODY_CONTENT_TYPE));
347348
return request;
348349
}
350+
351+
static Request putCalendar(PutCalendarRequest putCalendarRequest) throws IOException {
352+
String endpoint = new EndpointBuilder()
353+
.addPathPartAsIs("_xpack")
354+
.addPathPartAsIs("ml")
355+
.addPathPartAsIs("calendars")
356+
.addPathPart(putCalendarRequest.getCalendar().getId())
357+
.build();
358+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
359+
request.setEntity(createEntity(putCalendarRequest, REQUEST_BODY_CONTENT_TYPE));
360+
return request;
361+
}
349362
}

client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java

Lines changed: 240 additions & 221 deletions
Large diffs are not rendered by default.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.ml;
21+
22+
import org.elasticsearch.action.ActionRequest;
23+
import org.elasticsearch.action.ActionRequestValidationException;
24+
import org.elasticsearch.client.ml.calendars.Calendar;
25+
import org.elasticsearch.common.xcontent.ToXContentObject;
26+
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
28+
import java.io.IOException;
29+
import java.util.Objects;
30+
31+
/**
32+
* Request to create a new Machine Learning calendar
33+
*/
34+
public class PutCalendarRequest extends ActionRequest implements ToXContentObject {
35+
36+
private final Calendar calendar;
37+
38+
public PutCalendarRequest(Calendar calendar) {
39+
this.calendar = calendar;
40+
}
41+
42+
public Calendar getCalendar() {
43+
return calendar;
44+
}
45+
46+
@Override
47+
public ActionRequestValidationException validate() {
48+
return null;
49+
}
50+
51+
@Override
52+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
53+
calendar.toXContent(builder, params);
54+
return builder;
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Objects.hash(calendar);
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (obj == null) {
65+
return false;
66+
}
67+
if (getClass() != obj.getClass()) {
68+
return false;
69+
}
70+
PutCalendarRequest other = (PutCalendarRequest) obj;
71+
return Objects.equals(calendar, other.calendar);
72+
}
73+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.ml;
21+
22+
import org.elasticsearch.client.ml.calendars.Calendar;
23+
import org.elasticsearch.common.Strings;
24+
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
import org.elasticsearch.common.xcontent.XContentParser;
27+
28+
import java.io.IOException;
29+
import java.util.Objects;
30+
31+
public class PutCalendarResponse implements ToXContentObject {
32+
33+
public static PutCalendarResponse fromXContent(XContentParser parser) throws IOException {
34+
return new PutCalendarResponse(Calendar.PARSER.parse(parser, null));
35+
}
36+
37+
private final Calendar calendar;
38+
39+
PutCalendarResponse(Calendar calendar) {
40+
this.calendar = calendar;
41+
}
42+
43+
public Calendar getCalendar() {
44+
return calendar;
45+
}
46+
47+
@Override
48+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
49+
calendar.toXContent(builder, params);
50+
return builder;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(calendar);
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj) {
60+
if (this == obj) {
61+
return true;
62+
}
63+
64+
if (obj == null || getClass() != obj.getClass()) {
65+
return false;
66+
}
67+
68+
PutCalendarResponse other = (PutCalendarResponse) obj;
69+
return Objects.equals(calendar, other.calendar);
70+
}
71+
72+
@Override
73+
public final String toString() {
74+
return Strings.toString(this);
75+
}
76+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.ml.calendars;
20+
21+
import org.elasticsearch.common.Nullable;
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.XContentBuilder;
26+
27+
import java.io.IOException;
28+
import java.util.Collections;
29+
import java.util.List;
30+
import java.util.Objects;
31+
32+
/**
33+
* A simple calendar object for scheduled (special) events.
34+
* The calendar consists of a name an a list of job Ids or job groups
35+
* the events are stored separately and reference the calendar.
36+
*/
37+
public class Calendar implements ToXContentObject {
38+
39+
public static final String CALENDAR_TYPE = "calendar";
40+
41+
public static final ParseField JOB_IDS = new ParseField("job_ids");
42+
public static final ParseField ID = new ParseField("calendar_id");
43+
public static final ParseField DESCRIPTION = new ParseField("description");
44+
45+
@SuppressWarnings("unchecked")
46+
public static final ConstructingObjectParser<Calendar, Void> PARSER =
47+
new ConstructingObjectParser<>(CALENDAR_TYPE, true, a ->
48+
new Calendar((String) a[0], (List<String>) a[1], (String) a[2]));
49+
50+
static {
51+
PARSER.declareString(ConstructingObjectParser.constructorArg(), ID);
52+
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), JOB_IDS);
53+
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DESCRIPTION);
54+
}
55+
56+
private final String id;
57+
private final List<String> jobIds;
58+
private final String description;
59+
60+
/**
61+
* {@code jobIds} can be a mix of job groups and job Ids
62+
* @param id The calendar Id
63+
* @param jobIds List of job Ids or job groups
64+
* @param description An optional description
65+
*/
66+
public Calendar(String id, List<String> jobIds, @Nullable String description) {
67+
this.id = Objects.requireNonNull(id, ID.getPreferredName() + " must not be null");
68+
this.jobIds = Collections.unmodifiableList(Objects.requireNonNull(jobIds, JOB_IDS.getPreferredName() + " must not be null"));
69+
this.description = description;
70+
}
71+
72+
public String getId() {
73+
return id;
74+
}
75+
76+
public List<String> getJobIds() {
77+
return jobIds;
78+
}
79+
80+
@Nullable
81+
public String getDescription() {
82+
return description;
83+
}
84+
85+
@Override
86+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
87+
builder.startObject();
88+
builder.field(ID.getPreferredName(), id);
89+
builder.field(JOB_IDS.getPreferredName(), jobIds);
90+
if (description != null) {
91+
builder.field(DESCRIPTION.getPreferredName(), description);
92+
}
93+
builder.endObject();
94+
return builder;
95+
}
96+
97+
@Override
98+
public boolean equals(Object obj) {
99+
if (obj == this) {
100+
return true;
101+
}
102+
103+
if (obj == null || getClass() != obj.getClass()) {
104+
return false;
105+
}
106+
107+
Calendar other = (Calendar) obj;
108+
return id.equals(other.id) && jobIds.equals(other.jobIds) && Objects.equals(description, other.description);
109+
}
110+
111+
@Override
112+
public int hashCode() {
113+
return Objects.hash(id, jobIds, description);
114+
}
115+
}

0 commit comments

Comments
 (0)