Skip to content

Commit

Permalink
HLRC: ML PUT Calendar (#33362)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkyle authored Sep 14, 2018
1 parent bcbbbdf commit b04faa0
Show file tree
Hide file tree
Showing 20 changed files with 999 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.elasticsearch.client.ml.GetRecordsRequest;
import org.elasticsearch.client.ml.OpenJobRequest;
import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.client.ml.PutCalendarRequest;
import org.elasticsearch.client.ml.PutDatafeedRequest;
import org.elasticsearch.client.ml.PutJobRequest;
import org.elasticsearch.client.ml.UpdateJobRequest;
Expand Down Expand Up @@ -327,4 +328,16 @@ static Request getInfluencers(GetInfluencersRequest getInfluencersRequest) throw
request.setEntity(createEntity(getInfluencersRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}

static Request putCalendar(PutCalendarRequest putCalendarRequest) throws IOException {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("calendars")
.addPathPart(putCalendarRequest.getCalendar().getId())
.build();
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
request.setEntity(createEntity(putCalendarRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 org.elasticsearch.client.ml.calendars.Calendar;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

/**
* Request to create a new Machine Learning calendar
*/
public class PutCalendarRequest extends ActionRequest implements ToXContentObject {

private final Calendar calendar;

public PutCalendarRequest(Calendar calendar) {
this.calendar = calendar;
}

public Calendar getCalendar() {
return calendar;
}

@Override
public ActionRequestValidationException validate() {
return null;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
calendar.toXContent(builder, params);
return builder;
}

@Override
public int hashCode() {
return Objects.hash(calendar);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
PutCalendarRequest other = (PutCalendarRequest) obj;
return Objects.equals(calendar, other.calendar);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.client.ml.calendars.Calendar;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

public class PutCalendarResponse implements ToXContentObject {

public static PutCalendarResponse fromXContent(XContentParser parser) throws IOException {
return new PutCalendarResponse(Calendar.PARSER.parse(parser, null));
}

private final Calendar calendar;

PutCalendarResponse(Calendar calendar) {
this.calendar = calendar;
}

public Calendar getCalendar() {
return calendar;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
calendar.toXContent(builder, params);
return builder;
}

@Override
public int hashCode() {
return Objects.hash(calendar);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

PutCalendarResponse other = (PutCalendarResponse) obj;
return Objects.equals(calendar, other.calendar);
}

@Override
public final String toString() {
return Strings.toString(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.calendars;

import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* A simple calendar object for scheduled (special) events.
* The calendar consists of a name an a list of job Ids or job groups
* the events are stored separately and reference the calendar.
*/
public class Calendar implements ToXContentObject {

public static final String CALENDAR_TYPE = "calendar";

public static final ParseField JOB_IDS = new ParseField("job_ids");
public static final ParseField ID = new ParseField("calendar_id");
public static final ParseField DESCRIPTION = new ParseField("description");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<Calendar, Void> PARSER =
new ConstructingObjectParser<>(CALENDAR_TYPE, true, a ->
new Calendar((String) a[0], (List<String>) a[1], (String) a[2]));

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), ID);
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), JOB_IDS);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DESCRIPTION);
}

private final String id;
private final List<String> jobIds;
private final String description;

/**
* {@code jobIds} can be a mix of job groups and job Ids
* @param id The calendar Id
* @param jobIds List of job Ids or job groups
* @param description An optional description
*/
public Calendar(String id, List<String> jobIds, @Nullable String description) {
this.id = Objects.requireNonNull(id, ID.getPreferredName() + " must not be null");
this.jobIds = Collections.unmodifiableList(Objects.requireNonNull(jobIds, JOB_IDS.getPreferredName() + " must not be null"));
this.description = description;
}

public String getId() {
return id;
}

public List<String> getJobIds() {
return jobIds;
}

@Nullable
public String getDescription() {
return description;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(ID.getPreferredName(), id);
builder.field(JOB_IDS.getPreferredName(), jobIds);
if (description != null) {
builder.field(DESCRIPTION.getPreferredName(), description);
}
builder.endObject();
return builder;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

Calendar other = (Calendar) obj;
return id.equals(other.id) && jobIds.equals(other.jobIds) && Objects.equals(description, other.description);
}

@Override
public int hashCode() {
return Objects.hash(id, jobIds, description);
}
}
Loading

0 comments on commit b04faa0

Please sign in to comment.