/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.knn.plugin.transport; import org.opensearch.action.support.nodes.BaseNodeResponse; 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.ToXContentFragment; import org.opensearch.core.xcontent.XContentBuilder; import java.io.IOException; import java.util.Map; /** * KNNStatsNodeResponse represents the responses generated by an individual node */ public class KNNStatsNodeResponse extends BaseNodeResponse implements ToXContentFragment { private Map statsMap; /** * Constructor * * @param in stream * @throws IOException in case of I/O errors */ public KNNStatsNodeResponse(StreamInput in) throws IOException { super(in); this.statsMap = in.readMap(StreamInput::readString, StreamInput::readGenericValue); } /** * Constructor * * @param node node * @param statsToValues mapping of stat name to value */ public KNNStatsNodeResponse(DiscoveryNode node, Map statsToValues) { super(node); this.statsMap = statsToValues; } /** * Creates a new KNNStatsNodeResponse object and reads in the stats from an input stream * * @param in StreamInput to read from * @return KNNStatsNodeResponse object corresponding to the input stream * @throws IOException throws an IO exception if the StreamInput cannot be read from */ public static KNNStatsNodeResponse readStats(StreamInput in) throws IOException { KNNStatsNodeResponse knnStats = new KNNStatsNodeResponse(in); return knnStats; } /** * Get the map of stats * * @return map of stats */ public Map getStatsMap() { return statsMap; } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeMap(statsMap, StreamOutput::writeString, StreamOutput::writeGenericValue); } /** * Converts statsMap to xContent * * @param builder XContentBuilder * @param params Params * @return XContentBuilder * @throws IOException thrown by builder for invalid field */ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { for (String stat : statsMap.keySet()) { builder.field(stat, statsMap.get(stat)); } return builder; } }