HLRC: ML Add Job to Calendar API (#35666)

This commit is contained in:
Benjamin Trent 2018-11-19 11:41:49 -06:00 committed by GitHub
parent aaeb47d1f7
commit 214bc96738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 309 additions and 0 deletions

View File

@ -51,6 +51,7 @@ import org.elasticsearch.client.ml.GetRecordsRequest;
import org.elasticsearch.client.ml.OpenJobRequest;
import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.client.ml.PreviewDatafeedRequest;
import org.elasticsearch.client.ml.PutCalendarJobRequest;
import org.elasticsearch.client.ml.PutCalendarRequest;
import org.elasticsearch.client.ml.PutDatafeedRequest;
import org.elasticsearch.client.ml.PutFilterRequest;
@ -485,6 +486,18 @@ final class MLRequestConverters {
return request;
}
static Request putCalendarJob(PutCalendarJobRequest putCalendarJobRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("calendars")
.addPathPart(putCalendarJobRequest.getCalendarId())
.addPathPartAsIs("jobs")
.addPathPart(Strings.collectionToCommaDelimitedString(putCalendarJobRequest.getJobIds()))
.build();
return new Request(HttpPut.METHOD_NAME, endpoint);
}
static Request deleteCalendar(DeleteCalendarRequest deleteCalendarRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")

View File

@ -63,6 +63,7 @@ import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.client.ml.PostDataResponse;
import org.elasticsearch.client.ml.PreviewDatafeedRequest;
import org.elasticsearch.client.ml.PreviewDatafeedResponse;
import org.elasticsearch.client.ml.PutCalendarJobRequest;
import org.elasticsearch.client.ml.PutCalendarRequest;
import org.elasticsearch.client.ml.PutCalendarResponse;
import org.elasticsearch.client.ml.PutDatafeedRequest;
@ -1217,6 +1218,47 @@ public final class MachineLearningClient {
Collections.emptySet());
}
/**
* Adds Machine Learning Job(s) to a calendar
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-put-calendar-job.html">
* ML Put calendar job documentation</a>
*
* @param request The request
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return The {@link PutCalendarResponse} containing the updated calendar
* @throws IOException when there is a serialization issue sending the request or receiving the response
*/
public PutCalendarResponse putCalendarJob(PutCalendarJobRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::putCalendarJob,
options,
PutCalendarResponse::fromXContent,
Collections.emptySet());
}
/**
* Adds Machine Learning Job(s) to a calendar, notifies listener when completed
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-put-calendar-job.html">
* ML Put calendar job documentation</a>
*
* @param request The request
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener Listener to be notified upon request completion
*/
public void putCalendarJobAsync(PutCalendarJobRequest request, RequestOptions options, ActionListener<PutCalendarResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::putCalendarJob,
options,
PutCalendarResponse::fromXContent,
listener,
Collections.emptySet());
}
/**
* Deletes the given Machine Learning Calendar
* <p>

View File

@ -0,0 +1,88 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.ml;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* Request class for adding Machine Learning Jobs to an existing calendar
*/
public class PutCalendarJobRequest extends ActionRequest {
private final List<String> jobIds;
private final String calendarId;
/**
* Create a new request referencing an existing Calendar and which JobIds to add
* to it.
*
* @param calendarId The non-null ID of the calendar
* @param jobIds JobIds to add to the calendar, cannot be empty, or contain null values.
* It can be a list of jobs or groups.
*/
public PutCalendarJobRequest(String calendarId, String... jobIds) {
this.calendarId = Objects.requireNonNull(calendarId, "[calendar_id] must not be null.");
if (jobIds.length == 0) {
throw new InvalidParameterException("jobIds must not be empty.");
}
if (Arrays.stream(jobIds).anyMatch(Objects::isNull)) {
throw new NullPointerException("jobIds must not contain null values.");
}
this.jobIds = Arrays.asList(jobIds);
}
public List<String> getJobIds() {
return jobIds;
}
public String getCalendarId() {
return calendarId;
}
@Override
public ActionRequestValidationException validate() {
return null;
}
@Override
public int hashCode() {
return Objects.hash(jobIds, calendarId);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
PutCalendarJobRequest that = (PutCalendarJobRequest) other;
return Objects.equals(jobIds, that.jobIds) &&
Objects.equals(calendarId, that.calendarId);
}
}

View File

@ -47,6 +47,7 @@ import org.elasticsearch.client.ml.GetRecordsRequest;
import org.elasticsearch.client.ml.OpenJobRequest;
import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.client.ml.PreviewDatafeedRequest;
import org.elasticsearch.client.ml.PutCalendarJobRequest;
import org.elasticsearch.client.ml.PutCalendarRequest;
import org.elasticsearch.client.ml.PutDatafeedRequest;
import org.elasticsearch.client.ml.PutFilterRequest;
@ -518,6 +519,16 @@ public class MLRequestConvertersTests extends ESTestCase {
}
}
public void testPutCalendarJob() throws IOException {
String calendarId = randomAlphaOfLength(10);
String job1 = randomAlphaOfLength(5);
String job2 = randomAlphaOfLength(5);
PutCalendarJobRequest putCalendarJobRequest = new PutCalendarJobRequest(calendarId, job1, job2);
Request request = MLRequestConverters.putCalendarJob(putCalendarJobRequest);
assertEquals(HttpPut.METHOD_NAME, request.getMethod());
assertEquals("/_xpack/ml/calendars/" + calendarId + "/jobs/" + job1 + "," + job2, request.getEndpoint());
}
public void testGetCalendars() throws IOException {
GetCalendarsRequest getCalendarsRequest = new GetCalendarsRequest();
String expectedEndpoint = "/_xpack/ml/calendars";

View File

@ -58,6 +58,7 @@ import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.client.ml.PostDataResponse;
import org.elasticsearch.client.ml.PreviewDatafeedRequest;
import org.elasticsearch.client.ml.PreviewDatafeedResponse;
import org.elasticsearch.client.ml.PutCalendarJobRequest;
import org.elasticsearch.client.ml.PutCalendarRequest;
import org.elasticsearch.client.ml.PutCalendarResponse;
import org.elasticsearch.client.ml.PutDatafeedRequest;
@ -826,6 +827,26 @@ public class MachineLearningIT extends ESRestHighLevelClientTestCase {
assertThat(putCalendarResponse.getCalendar(), equalTo(calendar));
}
public void testPutCalendarJob() throws IOException {
Calendar calendar = new Calendar("put-calendar-job-id", Collections.singletonList("put-calendar-job-0"), null);
MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
PutCalendarResponse putCalendarResponse =
machineLearningClient.putCalendar(new PutCalendarRequest(calendar), RequestOptions.DEFAULT);
assertThat(putCalendarResponse.getCalendar().getJobIds(), containsInAnyOrder( "put-calendar-job-0"));
String jobId1 = "put-calendar-job-1";
String jobId2 = "put-calendar-job-2";
PutCalendarJobRequest putCalendarJobRequest = new PutCalendarJobRequest(calendar.getId(), jobId1, jobId2);
putCalendarResponse = execute(putCalendarJobRequest,
machineLearningClient::putCalendarJob,
machineLearningClient::putCalendarJobAsync);
assertThat(putCalendarResponse.getCalendar().getJobIds(), containsInAnyOrder(jobId1, jobId2, "put-calendar-job-0"));
}
public void testGetCalendars() throws Exception {
Calendar calendar1 = CalendarTests.testInstance();
Calendar calendar2 = CalendarTests.testInstance();

View File

@ -76,6 +76,7 @@ import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.client.ml.PostDataResponse;
import org.elasticsearch.client.ml.PreviewDatafeedRequest;
import org.elasticsearch.client.ml.PreviewDatafeedResponse;
import org.elasticsearch.client.ml.PutCalendarJobRequest;
import org.elasticsearch.client.ml.PutCalendarRequest;
import org.elasticsearch.client.ml.PutCalendarResponse;
import org.elasticsearch.client.ml.PutDatafeedRequest;
@ -2083,6 +2084,58 @@ public class MlClientDocumentationIT extends ESRestHighLevelClientTestCase {
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
public void testPutCalendarJob() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();
Calendar calendar = new Calendar("holidays", Collections.singletonList("job_1"), "A calendar for public holidays");
PutCalendarRequest putRequest = new PutCalendarRequest(calendar);
client.machineLearning().putCalendar(putRequest, RequestOptions.DEFAULT);
{
// tag::put-calendar-job-request
PutCalendarJobRequest request = new PutCalendarJobRequest("holidays", // <1>
"job_2", "job_group_1"); // <2>
// end::put-calendar-job-request
// tag::put-calendar-job-execute
PutCalendarResponse response = client.machineLearning().putCalendarJob(request, RequestOptions.DEFAULT);
// end::put-calendar-job-execute
// tag::put-calendar-job-response
Calendar updatedCalendar = response.getCalendar(); // <1>
// end::put-calendar-job-response
assertThat(updatedCalendar.getJobIds(), containsInAnyOrder("job_1", "job_2", "job_group_1"));
}
{
PutCalendarJobRequest request = new PutCalendarJobRequest("holidays", "job_4");
// tag::put-calendar-job-execute-listener
ActionListener<PutCalendarResponse> listener =
new ActionListener<PutCalendarResponse>() {
@Override
public void onResponse(PutCalendarResponse putCalendarsResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::put-calendar-job-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::put-calendar-job-execute-async
client.machineLearning().putCalendarJobAsync(request, RequestOptions.DEFAULT, listener); // <1>
// end::put-calendar-job-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
public void testGetCalendar() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();

View File

@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.ml;
import org.elasticsearch.test.ESTestCase;
public class PutCalendarJobRequestTests extends ESTestCase {
public void testWithNullId() {
NullPointerException ex = expectThrows(NullPointerException.class,
() -> new PutCalendarJobRequest(null, "job1"));
assertEquals("[calendar_id] must not be null.", ex.getMessage());
}
public void testSetJobIds() {
String calendarId = randomAlphaOfLength(10);
NullPointerException ex = expectThrows(NullPointerException.class,
() ->new PutCalendarJobRequest(calendarId, "job1", null));
assertEquals("jobIds must not contain null values.", ex.getMessage());
IllegalArgumentException illegalArgumentException =
expectThrows(IllegalArgumentException.class, () -> new PutCalendarJobRequest(calendarId));
assertEquals("jobIds must not be empty.", illegalArgumentException.getMessage());
}
}

View File

@ -0,0 +1,36 @@
--
:api: put-calendar-job
:request: PutCalendarJobRequest
:response: PutCalendarResponse
--
[id="{upid}-{api}"]
=== Put Calendar Job API
Adds {ml} jobs to an existing {ml} calendar.
The API accepts a +{request}+ and responds
with a +{response}+ object.
[id="{upid}-{api}-request"]
==== Put Calendar Job Request
A +{request}+ is constructed referencing a non-null
calendar ID, and JobIDs to which to add to the calendar
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request]
--------------------------------------------------
<1> The ID of the calendar to which to add the jobs
<2> The JobIds to add to the calendar
[id="{upid}-{api}-response"]
==== Put Calendar Response
The returned +{response}+ contains the updated Calendar:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
--------------------------------------------------
<1> The updated Calendar
include::../execution.asciidoc[]

View File

@ -266,6 +266,7 @@ The Java High Level REST Client supports the following Machine Learning APIs:
* <<{upid}-get-categories>>
* <<{upid}-get-calendars>>
* <<{upid}-put-calendar>>
* <<{upid}-put-calendar-job>>
* <<{upid}-delete-calendar>>
* <<{upid}-put-filter>>
* <<{upid}-get-filters>>
@ -300,6 +301,7 @@ include::ml/get-influencers.asciidoc[]
include::ml/get-categories.asciidoc[]
include::ml/get-calendars.asciidoc[]
include::ml/put-calendar.asciidoc[]
include::ml/put-calendar-job.asciidoc[]
include::ml/delete-calendar.asciidoc[]
include::ml/put-filter.asciidoc[]
include::ml/get-model-snapshots.asciidoc[]