diff --git a/src/main/java/org/codelibs/fess/entity/PingResponse.java b/src/main/java/org/codelibs/fess/entity/PingResponse.java index 2b5d47797..a6d63b76d 100644 --- a/src/main/java/org/codelibs/fess/entity/PingResponse.java +++ b/src/main/java/org/codelibs/fess/entity/PingResponse.java @@ -18,6 +18,7 @@ package org.codelibs.fess.entity; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Locale; import org.apache.commons.text.StringEscapeUtils; import org.codelibs.core.lang.StringUtil; @@ -28,6 +29,22 @@ import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.xcontent.XContentType; public class PingResponse { + private static final String CLUSTER_NAME = "cluster_name"; + private static final String STATUS = "status"; + private static final String TIMED_OUT = "timed_out"; + private static final String NUMBER_OF_NODES = "number_of_nodes"; + private static final String NUMBER_OF_DATA_NODES = "number_of_data_nodes"; + private static final String NUMBER_OF_PENDING_TASKS = "number_of_pending_tasks"; + private static final String NUMBER_OF_IN_FLIGHT_FETCH = "number_of_in_flight_fetch"; + private static final String DELAYED_UNASSIGNED_SHARDS = "delayed_unassigned_shards"; + private static final String TASK_MAX_WAIT_TIME_IN_QUEUE_IN_MILLIS = "task_max_waiting_in_queue_millis"; + private static final String ACTIVE_SHARDS_PERCENT_AS_NUMBER = "active_shards_percent_as_number"; + private static final String ACTIVE_PRIMARY_SHARDS = "active_primary_shards"; + private static final String ACTIVE_SHARDS = "active_shards"; + private static final String RELOCATING_SHARDS = "relocating_shards"; + private static final String INITIALIZING_SHARDS = "initializing_shards"; + private static final String UNASSIGNED_SHARDS = "unassigned_shards"; + private final int status; private final String clusterName; @@ -40,7 +57,26 @@ public class PingResponse { status = response.getStatus() == ClusterHealthStatus.RED ? 1 : 0; clusterName = response.getClusterName(); clusterStatus = response.getStatus().toString(); - try (OutputStream out = EsUtil.getXContentOutputStream(response, XContentType.JSON)) { + try (OutputStream out = EsUtil.getXContentBuilderOutputStream((builder, params) -> { + builder.startObject(); + builder.field(CLUSTER_NAME, response.getClusterName()); + builder.field(STATUS, response.getStatus().name().toLowerCase(Locale.ROOT)); + builder.field(TIMED_OUT, response.isTimedOut()); + builder.field(NUMBER_OF_NODES, response.getNumberOfNodes()); + builder.field(NUMBER_OF_DATA_NODES, response.getNumberOfDataNodes()); + builder.field(ACTIVE_PRIMARY_SHARDS, response.getActivePrimaryShards()); + builder.field(ACTIVE_SHARDS, response.getActiveShards()); + builder.field(RELOCATING_SHARDS, response.getRelocatingShards()); + builder.field(INITIALIZING_SHARDS, response.getInitializingShards()); + builder.field(UNASSIGNED_SHARDS, response.getUnassignedShards()); + builder.field(DELAYED_UNASSIGNED_SHARDS, response.getDelayedUnassignedShards()); + builder.field(NUMBER_OF_PENDING_TASKS, response.getNumberOfPendingTasks()); + builder.field(NUMBER_OF_IN_FLIGHT_FETCH, response.getNumberOfInFlightFetch()); + builder.field(TASK_MAX_WAIT_TIME_IN_QUEUE_IN_MILLIS, response.getTaskMaxWaitingTime().getMillis()); + builder.field(ACTIVE_SHARDS_PERCENT_AS_NUMBER, response.getActiveShardsPercent()); + builder.endObject(); + return builder; + }, XContentType.JSON)) { message = ((ByteArrayOutputStream) out).toString(Constants.UTF_8); if (StringUtil.isBlank(message)) { message = "{}"; diff --git a/src/main/java/org/codelibs/fess/util/EsUtil.java b/src/main/java/org/codelibs/fess/util/EsUtil.java index bea2bf3c4..f3982918e 100644 --- a/src/main/java/org/codelibs/fess/util/EsUtil.java +++ b/src/main/java/org/codelibs/fess/util/EsUtil.java @@ -33,8 +33,8 @@ public final class EsUtil { private EsUtil() { } - public static OutputStream getXContentOutputStream(final ToXContent xContent, final XContentType xContentType) { - try (final XContentBuilder builder = xContent.toXContent(XContentFactory.contentBuilder(xContentType), ToXContent.EMPTY_PARAMS)) { + public static OutputStream getXContentBuilderOutputStream(final XContentBuilderCallback func, final XContentType xContentType) { + try (final XContentBuilder builder = func.apply(XContentFactory.contentBuilder(xContentType), ToXContent.EMPTY_PARAMS)) { builder.flush(); return builder.getOutputStream(); } catch (final IOException e) { @@ -44,4 +44,12 @@ public final class EsUtil { return new ByteArrayOutputStream(); } } + + public static OutputStream getXContentOutputStream(final ToXContent xContent, final XContentType xContentType) { + return getXContentBuilderOutputStream((builder, params) -> xContent.toXContent(builder, params), xContentType); + } + + public interface XContentBuilderCallback { + XContentBuilder apply(XContentBuilder builder, ToXContent.Params params) throws IOException; + } }