Move XContent rendering of ClusterIndexHealth to the class itself

This commit is contained in:
Boaz Leskes 2013-12-18 10:58:58 +01:00
parent b1626535a9
commit 5475ccf738
2 changed files with 73 additions and 39 deletions

View File

@ -28,10 +28,14 @@ import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.action.admin.cluster.health.ClusterShardHealth.readClusterShardHealth; import static org.elasticsearch.action.admin.cluster.health.ClusterShardHealth.readClusterShardHealth;
@ -39,7 +43,7 @@ import static org.elasticsearch.action.admin.cluster.health.ClusterShardHealth.r
/** /**
* *
*/ */
public class ClusterIndexHealth implements Iterable<ClusterShardHealth>, Streamable { public class ClusterIndexHealth implements Iterable<ClusterShardHealth>, Streamable, ToXContent {
private String index; private String index;
@ -239,4 +243,58 @@ public class ClusterIndexHealth implements Iterable<ClusterShardHealth>, Streama
out.writeString(failure); out.writeString(failure);
} }
} }
static final class Fields {
static final XContentBuilderString STATUS = new XContentBuilderString("status");
static final XContentBuilderString NUMBER_OF_SHARDS = new XContentBuilderString("number_of_shards");
static final XContentBuilderString NUMBER_OF_REPLICAS = new XContentBuilderString("number_of_replicas");
static final XContentBuilderString ACTIVE_PRIMARY_SHARDS = new XContentBuilderString("active_primary_shards");
static final XContentBuilderString ACTIVE_SHARDS = new XContentBuilderString("active_shards");
static final XContentBuilderString RELOCATING_SHARDS = new XContentBuilderString("relocating_shards");
static final XContentBuilderString INITIALIZING_SHARDS = new XContentBuilderString("initializing_shards");
static final XContentBuilderString UNASSIGNED_SHARDS = new XContentBuilderString("unassigned_shards");
static final XContentBuilderString VALIDATION_FAILURES = new XContentBuilderString("validation_failures");
static final XContentBuilderString SHARDS = new XContentBuilderString("shards");
static final XContentBuilderString PRIMARY_ACTIVE = new XContentBuilderString("primary_active");
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(Fields.STATUS, getStatus().name().toLowerCase(Locale.ROOT));
builder.field(Fields.NUMBER_OF_SHARDS, getNumberOfShards());
builder.field(Fields.NUMBER_OF_REPLICAS, getNumberOfReplicas());
builder.field(Fields.ACTIVE_PRIMARY_SHARDS, getActivePrimaryShards());
builder.field(Fields.ACTIVE_SHARDS, getActiveShards());
builder.field(Fields.RELOCATING_SHARDS, getRelocatingShards());
builder.field(Fields.INITIALIZING_SHARDS, getInitializingShards());
builder.field(Fields.UNASSIGNED_SHARDS, getUnassignedShards());
if (!getValidationFailures().isEmpty()) {
builder.startArray(Fields.VALIDATION_FAILURES);
for (String validationFailure : getValidationFailures()) {
builder.value(validationFailure);
}
builder.endArray();
}
if (params.paramAsBoolean("output_shards", false)) {
builder.startObject(Fields.SHARDS);
for (ClusterShardHealth shardHealth : shards.values()) {
builder.startObject(Integer.toString(shardHealth.getId()));
builder.field(Fields.STATUS, shardHealth.getStatus().name().toLowerCase(Locale.ROOT));
builder.field(Fields.PRIMARY_ACTIVE, shardHealth.isPrimaryActive());
builder.field(Fields.ACTIVE_SHARDS, shardHealth.getActiveShards());
builder.field(Fields.RELOCATING_SHARDS, shardHealth.getRelocatingShards());
builder.field(Fields.INITIALIZING_SHARDS, shardHealth.getInitializingShards());
builder.field(Fields.UNASSIGNED_SHARDS, shardHealth.getUnassignedShards());
builder.endObject();
}
builder.endObject();
}
return builder;
}
} }

View File

@ -19,12 +19,17 @@
package org.elasticsearch.rest.action.admin.cluster.health; package org.elasticsearch.rest.action.admin.cluster.health;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.health.*; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.cluster.health.ClusterIndexHealth;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
@ -32,6 +37,7 @@ import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import static org.elasticsearch.client.Requests.clusterHealthRequest; import static org.elasticsearch.client.Requests.clusterHealthRequest;
import static org.elasticsearch.rest.RestStatus.PRECONDITION_FAILED; import static org.elasticsearch.rest.RestStatus.PRECONDITION_FAILED;
@ -41,6 +47,8 @@ import static org.elasticsearch.rest.RestStatus.PRECONDITION_FAILED;
*/ */
public class RestClusterHealthAction extends BaseRestHandler { public class RestClusterHealthAction extends BaseRestHandler {
private static final Map<String, String> SHARD_LEVEL_PARAMS = ImmutableMap.of("output_shards", "true");
@Inject @Inject
public RestClusterHealthAction(Settings settings, Client client, RestController controller) { public RestClusterHealthAction(Settings settings, Client client, RestController controller) {
super(settings, client); super(settings, client);
@ -139,41 +147,13 @@ public class RestClusterHealthAction extends BaseRestHandler {
for (ClusterIndexHealth indexHealth : response) { for (ClusterIndexHealth indexHealth : response) {
builder.startObject(indexHealth.getIndex(), XContentBuilder.FieldCaseConversion.NONE); builder.startObject(indexHealth.getIndex(), XContentBuilder.FieldCaseConversion.NONE);
builder.field(Fields.STATUS, indexHealth.getStatus().name().toLowerCase(Locale.ROOT)); ToXContent.Params params;
builder.field(Fields.NUMBER_OF_SHARDS, indexHealth.getNumberOfShards());
builder.field(Fields.NUMBER_OF_REPLICAS, indexHealth.getNumberOfReplicas());
builder.field(Fields.ACTIVE_PRIMARY_SHARDS, indexHealth.getActivePrimaryShards());
builder.field(Fields.ACTIVE_SHARDS, indexHealth.getActiveShards());
builder.field(Fields.RELOCATING_SHARDS, indexHealth.getRelocatingShards());
builder.field(Fields.INITIALIZING_SHARDS, indexHealth.getInitializingShards());
builder.field(Fields.UNASSIGNED_SHARDS, indexHealth.getUnassignedShards());
if (!indexHealth.getValidationFailures().isEmpty()) {
builder.startArray(Fields.VALIDATION_FAILURES);
for (String validationFailure : indexHealth.getValidationFailures()) {
builder.value(validationFailure);
}
builder.endArray();
}
if (fLevel > 1) { if (fLevel > 1) {
builder.startObject(Fields.SHARDS); params = new ToXContent.DelegatingMapParams(SHARD_LEVEL_PARAMS, request);
} else {
for (ClusterShardHealth shardHealth : indexHealth) { params = request;
builder.startObject(Integer.toString(shardHealth.getId()));
builder.field(Fields.STATUS, shardHealth.getStatus().name().toLowerCase(Locale.ROOT));
builder.field(Fields.PRIMARY_ACTIVE, shardHealth.isPrimaryActive());
builder.field(Fields.ACTIVE_SHARDS, shardHealth.getActiveShards());
builder.field(Fields.RELOCATING_SHARDS, shardHealth.getRelocatingShards());
builder.field(Fields.INITIALIZING_SHARDS, shardHealth.getInitializingShards());
builder.field(Fields.UNASSIGNED_SHARDS, shardHealth.getUnassignedShards());
builder.endObject();
}
builder.endObject();
} }
indexHealth.toXContent(builder, params);
builder.endObject(); builder.endObject();
} }
@ -203,8 +183,6 @@ public class RestClusterHealthAction extends BaseRestHandler {
static final XContentBuilderString CLUSTER_NAME = new XContentBuilderString("cluster_name"); static final XContentBuilderString CLUSTER_NAME = new XContentBuilderString("cluster_name");
static final XContentBuilderString STATUS = new XContentBuilderString("status"); static final XContentBuilderString STATUS = new XContentBuilderString("status");
static final XContentBuilderString TIMED_OUT = new XContentBuilderString("timed_out"); static final XContentBuilderString TIMED_OUT = new XContentBuilderString("timed_out");
static final XContentBuilderString NUMBER_OF_SHARDS = new XContentBuilderString("number_of_shards");
static final XContentBuilderString NUMBER_OF_REPLICAS = new XContentBuilderString("number_of_replicas");
static final XContentBuilderString NUMBER_OF_NODES = new XContentBuilderString("number_of_nodes"); static final XContentBuilderString NUMBER_OF_NODES = new XContentBuilderString("number_of_nodes");
static final XContentBuilderString NUMBER_OF_DATA_NODES = new XContentBuilderString("number_of_data_nodes"); static final XContentBuilderString NUMBER_OF_DATA_NODES = new XContentBuilderString("number_of_data_nodes");
static final XContentBuilderString ACTIVE_PRIMARY_SHARDS = new XContentBuilderString("active_primary_shards"); static final XContentBuilderString ACTIVE_PRIMARY_SHARDS = new XContentBuilderString("active_primary_shards");
@ -214,7 +192,5 @@ public class RestClusterHealthAction extends BaseRestHandler {
static final XContentBuilderString UNASSIGNED_SHARDS = new XContentBuilderString("unassigned_shards"); static final XContentBuilderString UNASSIGNED_SHARDS = new XContentBuilderString("unassigned_shards");
static final XContentBuilderString VALIDATION_FAILURES = new XContentBuilderString("validation_failures"); static final XContentBuilderString VALIDATION_FAILURES = new XContentBuilderString("validation_failures");
static final XContentBuilderString INDICES = new XContentBuilderString("indices"); static final XContentBuilderString INDICES = new XContentBuilderString("indices");
static final XContentBuilderString SHARDS = new XContentBuilderString("shards");
static final XContentBuilderString PRIMARY_ACTIVE = new XContentBuilderString("primary_active");
} }
} }