/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.ml.action.stats; import java.io.IOException; import java.util.List; import org.opensearch.action.FailedNodeException; import org.opensearch.action.support.nodes.BaseNodesResponse; import org.opensearch.cluster.ClusterName; import org.opensearch.cluster.node.DiscoveryNode; 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; public class MLStatsNodesResponse extends BaseNodesResponse implements ToXContentObject { private static final String NODES_KEY = "nodes"; /** * Constructor * * @param in StreamInput * @throws IOException thrown when unable to read from stream */ public MLStatsNodesResponse(StreamInput in) throws IOException { super(new ClusterName(in), in.readList(MLStatsNodeResponse::readStats), in.readList(FailedNodeException::new)); } /** * Constructor * * @param clusterName name of cluster * @param nodes List of MLStatsNodeResponses from nodes * @param failures List of failures from nodes */ public MLStatsNodesResponse(ClusterName clusterName, List nodes, List failures) { super(clusterName, nodes, failures); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); } @Override public void writeNodesTo(StreamOutput out, List nodes) throws IOException { out.writeList(nodes); } @Override public List readNodesFrom(StreamInput in) throws IOException { return in.readList(MLStatsNodeResponse::readStats); } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { String nodeId; DiscoveryNode node; builder.startObject("nodes"); for (MLStatsNodeResponse mlStats : getNodes()) { node = mlStats.getNode(); nodeId = node.getId(); builder.startObject(nodeId); mlStats.toXContent(builder, params); builder.endObject(); } builder.endObject(); return builder; } }