Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve GetAnomalyDetectorTransportAction to accept generic ActionRequest #1150

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

package org.opensearch.ad.transport;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.InputStreamStreamInput;
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.timeseries.model.Entity;
Expand Down Expand Up @@ -119,4 +123,19 @@ public void writeTo(StreamOutput out) throws IOException {
public ActionRequestValidationException validate() {
return null;
}

public static GetAnomalyDetectorRequest fromActionRequest(final ActionRequest actionRequest) {
if (actionRequest instanceof GetAnomalyDetectorRequest) {
return (GetAnomalyDetectorRequest) actionRequest;
}

try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
actionRequest.writeTo(osso);
try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
return new GetAnomalyDetectorRequest(input);
}
} catch (IOException e) {
throw new IllegalArgumentException("failed to parse ActionRequest into GetAnomalyDetectorRequest", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchStatusException;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.get.MultiGetItemResponse;
import org.opensearch.action.get.MultiGetRequest;
import org.opensearch.action.get.MultiGetResponse;
Expand Down Expand Up @@ -75,7 +76,7 @@

import com.google.common.collect.Sets;

public class GetAnomalyDetectorTransportAction extends HandledTransportAction<GetAnomalyDetectorRequest, GetAnomalyDetectorResponse> {
public class GetAnomalyDetectorTransportAction extends HandledTransportAction<ActionRequest, GetAnomalyDetectorResponse> {

private static final Logger LOG = LogManager.getLogger(GetAnomalyDetectorTransportAction.class);

Expand Down Expand Up @@ -131,7 +132,9 @@ public GetAnomalyDetectorTransportAction(
}

@Override
protected void doExecute(Task task, GetAnomalyDetectorRequest request, ActionListener<GetAnomalyDetectorResponse> actionListener) {
protected void doExecute(Task task, ActionRequest actionRequest, ActionListener<GetAnomalyDetectorResponse> actionListener) {
GetAnomalyDetectorRequest request = GetAnomalyDetectorRequest.fromActionRequest(actionRequest);

String detectorID = request.getDetectorID();
User user = getUserContext(client);
ActionListener<GetAnomalyDetectorResponse> listener = wrapRestActionListener(actionListener, FAIL_TO_GET_DETECTOR);
Expand Down
Loading