|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.plugin.insights.rules.resthandler.health_stats; |
| 10 | + |
| 11 | +import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.QUERY_INSIGHTS_HEALTH_STATS_URI; |
| 12 | +import static org.opensearch.rest.RestRequest.Method.GET; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.Set; |
| 16 | +import org.opensearch.client.node.NodeClient; |
| 17 | +import org.opensearch.common.settings.Settings; |
| 18 | +import org.opensearch.core.common.Strings; |
| 19 | +import org.opensearch.core.rest.RestStatus; |
| 20 | +import org.opensearch.core.xcontent.ToXContent; |
| 21 | +import org.opensearch.plugin.insights.rules.action.health_stats.HealthStatsAction; |
| 22 | +import org.opensearch.plugin.insights.rules.action.health_stats.HealthStatsRequest; |
| 23 | +import org.opensearch.plugin.insights.rules.action.health_stats.HealthStatsResponse; |
| 24 | +import org.opensearch.rest.BaseRestHandler; |
| 25 | +import org.opensearch.rest.BytesRestResponse; |
| 26 | +import org.opensearch.rest.RestChannel; |
| 27 | +import org.opensearch.rest.RestRequest; |
| 28 | +import org.opensearch.rest.RestResponse; |
| 29 | +import org.opensearch.rest.action.RestResponseListener; |
| 30 | + |
| 31 | +/** |
| 32 | + * Rest action to get operational health stats of the query insights plugin |
| 33 | + */ |
| 34 | +public class RestHealthStatsAction extends BaseRestHandler { |
| 35 | + /** |
| 36 | + * Constructor for RestHealthStatsAction |
| 37 | + */ |
| 38 | + public RestHealthStatsAction() {} |
| 39 | + |
| 40 | + @Override |
| 41 | + public List<Route> routes() { |
| 42 | + return List.of(new Route(GET, QUERY_INSIGHTS_HEALTH_STATS_URI)); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getName() { |
| 47 | + return "query_insights_health_stats_action"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) { |
| 52 | + final HealthStatsRequest healthStatsRequest = prepareRequest(request); |
| 53 | + healthStatsRequest.timeout(request.param("timeout")); |
| 54 | + |
| 55 | + return channel -> client.execute(HealthStatsAction.INSTANCE, healthStatsRequest, healthStatsResponse(channel)); |
| 56 | + } |
| 57 | + |
| 58 | + static HealthStatsRequest prepareRequest(final RestRequest request) { |
| 59 | + final String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodeId")); |
| 60 | + return new HealthStatsRequest(nodesIds); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected Set<String> responseParams() { |
| 65 | + return Settings.FORMAT_PARAMS; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean canTripCircuitBreaker() { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + RestResponseListener<HealthStatsResponse> healthStatsResponse(final RestChannel channel) { |
| 74 | + return new RestResponseListener<>(channel) { |
| 75 | + @Override |
| 76 | + public RestResponse buildResponse(final HealthStatsResponse response) throws Exception { |
| 77 | + return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS)); |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | +} |
0 commit comments