[ML] Refactor update calendar params (elastic/x-pack-elasticsearch#3714)

Original commit: elastic/x-pack-elasticsearch@a7ef33b58a
This commit is contained in:
David Kyle 2018-01-24 15:16:21 +00:00 committed by GitHub
parent e385b7dab4
commit 29ece3c79f
5 changed files with 35 additions and 38 deletions

View File

@ -17,9 +17,7 @@ import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class UpdateCalendarJobAction extends Action<UpdateCalendarJobAction.Request, PutCalendarAction.Response,
UpdateCalendarJobAction.RequestBuilder> {
@ -43,28 +41,28 @@ public class UpdateCalendarJobAction extends Action<UpdateCalendarJobAction.Requ
public static class Request extends ActionRequest {
private String calendarId;
private Set<String> jobIdsToAdd;
private Set<String> jobIdsToRemove;
private String jobIdToAdd;
private String jobIdToRemove;
public Request() {
}
public Request(String calendarId, Set<String> jobIdsToAdd, Set<String> jobIdsToRemove) {
public Request(String calendarId, String jobIdToAdd, String jobIdToRemove) {
this.calendarId = ExceptionsHelper.requireNonNull(calendarId, Calendar.ID.getPreferredName());
this.jobIdsToAdd = ExceptionsHelper.requireNonNull(jobIdsToAdd, "job_ids_to_add");
this.jobIdsToRemove = ExceptionsHelper.requireNonNull(jobIdsToRemove, "job_ids_to_remove");
this.jobIdToAdd = jobIdToAdd;
this.jobIdToRemove = jobIdToRemove;
}
public String getCalendarId() {
return calendarId;
}
public Set<String> getJobIdsToAdd() {
return jobIdsToAdd;
public String getJobIdToAdd() {
return jobIdToAdd;
}
public Set<String> getJobIdsToRemove() {
return jobIdsToRemove;
public String getJobIdToRemove() {
return jobIdToRemove;
}
@Override
@ -76,21 +74,21 @@ public class UpdateCalendarJobAction extends Action<UpdateCalendarJobAction.Requ
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
calendarId = in.readString();
jobIdsToAdd = new HashSet<>(in.readList(StreamInput::readString));
jobIdsToRemove = new HashSet<>(in.readList(StreamInput::readString));
jobIdToAdd = in.readOptionalString();
jobIdToRemove = in.readOptionalString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(calendarId);
out.writeStringList(new ArrayList<>(jobIdsToAdd));
out.writeStringList(new ArrayList<>(jobIdsToRemove));
out.writeOptionalString(jobIdToAdd);
out.writeOptionalString(jobIdToRemove);
}
@Override
public int hashCode() {
return Objects.hash(calendarId, jobIdsToAdd, jobIdsToRemove);
return Objects.hash(calendarId, jobIdToAdd, jobIdToRemove);
}
@Override
@ -102,8 +100,8 @@ public class UpdateCalendarJobAction extends Action<UpdateCalendarJobAction.Requ
return false;
}
Request other = (Request) obj;
return Objects.equals(calendarId, other.calendarId) && Objects.equals(jobIdsToAdd, other.jobIdsToAdd)
&& Objects.equals(jobIdsToRemove, other.jobIdsToRemove);
return Objects.equals(calendarId, other.calendarId) && Objects.equals(jobIdToAdd, other.jobIdToAdd)
&& Objects.equals(jobIdToRemove, other.jobIdToRemove);
}
}

View File

@ -19,6 +19,9 @@ import org.elasticsearch.xpack.core.ml.action.UpdateCalendarJobAction;
import org.elasticsearch.xpack.ml.job.JobManager;
import org.elasticsearch.xpack.core.ml.job.persistence.JobProvider;
import java.util.HashSet;
import java.util.Set;
public class TransportUpdateCalendarJobAction extends HandledTransportAction<UpdateCalendarJobAction.Request, PutCalendarAction.Response> {
private final ClusterService clusterService;
@ -39,7 +42,17 @@ public class TransportUpdateCalendarJobAction extends HandledTransportAction<Upd
@Override
protected void doExecute(UpdateCalendarJobAction.Request request, ActionListener<PutCalendarAction.Response> listener) {
jobProvider.updateCalendar(request.getCalendarId(), request.getJobIdsToAdd(), request.getJobIdsToRemove(), clusterService.state(),
Set<String> jobIdsToAdd = new HashSet<>();
if (request.getJobIdToAdd() != null && request.getJobIdToAdd().isEmpty() == false) {
jobIdsToAdd.add(request.getJobIdToAdd());
}
Set<String> jobIdsToRemove = new HashSet<>();
if (request.getJobIdToRemove() != null && request.getJobIdToRemove().isEmpty() == false) {
jobIdsToRemove.add(request.getJobIdToRemove());
}
jobProvider.updateCalendar(request.getCalendarId(), jobIdsToAdd, jobIdsToRemove, clusterService.state(),
c -> {
jobManager.updateProcessOnCalendarChanged(c.getJobIds());
listener.onResponse(new PutCalendarAction.Response(c));

View File

@ -17,7 +17,6 @@ import org.elasticsearch.xpack.core.ml.calendars.Calendar;
import org.elasticsearch.xpack.core.ml.job.config.Job;
import java.io.IOException;
import java.util.Collections;
public class RestDeleteCalendarJobAction extends BaseRestHandler {
@ -38,7 +37,7 @@ public class RestDeleteCalendarJobAction extends BaseRestHandler {
String calendarId = restRequest.param(Calendar.ID.getPreferredName());
String jobId = restRequest.param(Job.ID.getPreferredName());
UpdateCalendarJobAction.Request request =
new UpdateCalendarJobAction.Request(calendarId, Collections.emptySet(), Collections.singleton(jobId));
new UpdateCalendarJobAction.Request(calendarId, null, jobId);
return channel -> client.execute(UpdateCalendarJobAction.INSTANCE, request, new RestToXContentListener<>(channel));
}
}

View File

@ -17,7 +17,6 @@ import org.elasticsearch.xpack.core.ml.calendars.Calendar;
import org.elasticsearch.xpack.core.ml.job.config.Job;
import java.io.IOException;
import java.util.Collections;
public class RestPutCalendarJobAction extends BaseRestHandler {
@ -38,7 +37,7 @@ public class RestPutCalendarJobAction extends BaseRestHandler {
String calendarId = restRequest.param(Calendar.ID.getPreferredName());
String jobId = restRequest.param(Job.ID.getPreferredName());
UpdateCalendarJobAction.Request putCalendarRequest =
new UpdateCalendarJobAction.Request(calendarId, Collections.singleton(jobId), Collections.emptySet());
new UpdateCalendarJobAction.Request(calendarId, jobId, null);
return channel -> client.execute(UpdateCalendarJobAction.INSTANCE, putCalendarRequest, new RestToXContentListener<>(channel));
}
}

View File

@ -8,26 +8,14 @@ package org.elasticsearch.xpack.ml.action;
import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.xpack.core.ml.action.UpdateCalendarJobAction;
import java.util.HashSet;
import java.util.Set;
public class UpdateCalendarJobActionResquestTests extends AbstractStreamableTestCase<UpdateCalendarJobAction.Request> {
@Override
protected UpdateCalendarJobAction.Request createTestInstance() {
int addSize = randomIntBetween(0, 2);
Set<String> toAdd = new HashSet<>();
for (int i=0; i<addSize; i++) {
toAdd.add(randomAlphaOfLength(10));
}
int removeSize = randomIntBetween(0, 2);
Set<String> toRemove = new HashSet<>();
for (int i=0; i<removeSize; i++) {
toRemove.add(randomAlphaOfLength(10));
}
return new UpdateCalendarJobAction.Request(randomAlphaOfLength(10), toAdd, toRemove);
return new UpdateCalendarJobAction.Request(randomAlphaOfLength(10),
randomBoolean() ? null : randomAlphaOfLength(10),
randomBoolean() ? null : randomAlphaOfLength(10));
}
@Override