added a ToXContent implementation to NodeStats

This commit is contained in:
Boaz Leskes 2013-05-22 11:10:56 +02:00
parent a18f47e62e
commit 2f0271062a
2 changed files with 55 additions and 45 deletions

View File

@ -24,6 +24,8 @@ import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.http.HttpStats;
import org.elasticsearch.indices.NodeIndicesStats;
import org.elasticsearch.monitor.fs.FsStats;
@ -35,11 +37,12 @@ import org.elasticsearch.threadpool.ThreadPoolStats;
import org.elasticsearch.transport.TransportStats;
import java.io.IOException;
import java.util.Map;
/**
* Node statistics (dynamic, changes depending on when created).
*/
public class NodeStats extends NodeOperationResponse {
public class NodeStats extends NodeOperationResponse implements ToXContent {
private long timestamp;
@ -275,4 +278,54 @@ public class NodeStats extends NodeOperationResponse {
http.writeTo(out);
}
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("timestamp", getTimestamp());
builder.field("name", getNode().name(), XContentBuilder.FieldCaseConversion.NONE);
builder.field("transport_address", getNode().address().toString());
if (getHostname() != null) {
builder.field("hostname", getHostname(), XContentBuilder.FieldCaseConversion.NONE);
}
if (!getNode().attributes().isEmpty()) {
builder.startObject("attributes");
for (Map.Entry<String, String> attr : getNode().attributes().entrySet()) {
builder.field(attr.getKey(), attr.getValue());
}
builder.endObject();
}
if (getIndices() != null) {
getIndices().toXContent(builder, params);
}
if (getOs() != null) {
getOs().toXContent(builder, params);
}
if (getProcess() != null) {
getProcess().toXContent(builder, params);
}
if (getJvm() != null) {
getJvm().toXContent(builder, params);
}
if (getThreadPool() != null) {
getThreadPool().toXContent(builder, params);
}
if (getNetwork() != null) {
getNetwork().toXContent(builder, params);
}
if (getFs() != null) {
getFs().toXContent(builder, params);
}
if (getTransport() != null) {
getTransport().toXContent(builder, params);
}
if (getHttp() != null) {
getHttp().toXContent(builder, params);
}
return builder;
}
}

View File

@ -68,50 +68,7 @@ public class NodesStatsResponse extends NodesOperationResponse<NodeStats> implem
for (NodeStats nodeStats : this) {
builder.startObject(nodeStats.getNode().id(), XContentBuilder.FieldCaseConversion.NONE);
builder.field("timestamp", nodeStats.getTimestamp());
builder.field("name", nodeStats.getNode().name(), XContentBuilder.FieldCaseConversion.NONE);
builder.field("transport_address", nodeStats.getNode().address().toString());
if (nodeStats.getHostname() != null) {
builder.field("hostname", nodeStats.getHostname(), XContentBuilder.FieldCaseConversion.NONE);
}
if (!nodeStats.getNode().attributes().isEmpty()) {
builder.startObject("attributes");
for (Map.Entry<String, String> attr : nodeStats.getNode().attributes().entrySet()) {
builder.field(attr.getKey(), attr.getValue());
}
builder.endObject();
}
if (nodeStats.getIndices() != null) {
nodeStats.getIndices().toXContent(builder, params);
}
if (nodeStats.getOs() != null) {
nodeStats.getOs().toXContent(builder, params);
}
if (nodeStats.getProcess() != null) {
nodeStats.getProcess().toXContent(builder, params);
}
if (nodeStats.getJvm() != null) {
nodeStats.getJvm().toXContent(builder, params);
}
if (nodeStats.getThreadPool() != null) {
nodeStats.getThreadPool().toXContent(builder, params);
}
if (nodeStats.getNetwork() != null) {
nodeStats.getNetwork().toXContent(builder, params);
}
if (nodeStats.getFs() != null) {
nodeStats.getFs().toXContent(builder, params);
}
if (nodeStats.getTransport() != null) {
nodeStats.getTransport().toXContent(builder, params);
}
if (nodeStats.getHttp() != null) {
nodeStats.getHttp().toXContent(builder, params);
}
nodeStats.toXContent(builder, params);
builder.endObject();
}