/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.ad.transport; import static org.opensearch.action.ValidateActions.addValidationError; import java.io.IOException; import java.util.Locale; import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.action.ActionRequest; import org.opensearch.action.ActionRequestValidationException; import org.opensearch.ad.constant.ADCommonMessages; import org.opensearch.ad.constant.ADCommonName; import org.opensearch.core.common.Strings; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.xcontent.ToXContentObject; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.timeseries.constant.CommonMessages; import org.opensearch.timeseries.constant.CommonName; import org.opensearch.timeseries.model.Entity; public class EntityResultRequest extends ActionRequest implements ToXContentObject { private static final Logger LOG = LogManager.getLogger(EntityResultRequest.class); private String detectorId; // changed from Map to Map private Map entities; private long start; private long end; public EntityResultRequest(StreamInput in) throws IOException { super(in); this.detectorId = in.readString(); // guarded with version check. Just in case we receive requests from older node where we use String // to represent an entity this.entities = in.readMap(Entity::new, StreamInput::readDoubleArray); this.start = in.readLong(); this.end = in.readLong(); } public EntityResultRequest(String detectorId, Map entities, long start, long end) { super(); this.detectorId = detectorId; this.entities = entities; this.start = start; this.end = end; } public String getId() { return this.detectorId; } public Map getEntities() { return this.entities; } public long getStart() { return this.start; } public long getEnd() { return this.end; } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(this.detectorId); // guarded with version check. Just in case we send requests to older node where we use String // to represent an entity out.writeMap(entities, (s, e) -> e.writeTo(s), StreamOutput::writeDoubleArray); out.writeLong(this.start); out.writeLong(this.end); } @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (Strings.isEmpty(detectorId)) { validationException = addValidationError(ADCommonMessages.AD_ID_MISSING_MSG, validationException); } if (start <= 0 || end <= 0 || start > end) { validationException = addValidationError( String.format(Locale.ROOT, "%s: start %d, end %d", CommonMessages.INVALID_TIMESTAMP_ERR_MSG, start, end), validationException ); } return validationException; } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(ADCommonName.ID_JSON_KEY, detectorId); builder.field(CommonName.START_JSON_KEY, start); builder.field(CommonName.END_JSON_KEY, end); builder.startArray(CommonName.ENTITIES_JSON_KEY); for (final Map.Entry entry : entities.entrySet()) { if (entry.getKey() != null) { builder.startObject(); builder.field(CommonName.ENTITY_KEY, entry.getKey()); builder.field(CommonName.VALUE_JSON_KEY, entry.getValue()); builder.endObject(); } } builder.endArray(); builder.endObject(); return builder; } }