View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.entity;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.util.Locale;
22  import java.util.Set;
23  
24  import org.codelibs.core.lang.StringUtil;
25  import org.codelibs.fess.Constants;
26  import org.codelibs.fess.util.ComponentUtil;
27  import org.codelibs.fess.util.SearchEngineUtil;
28  import org.lastaflute.di.exception.IORuntimeException;
29  import org.opensearch.action.admin.cluster.health.ClusterHealthResponse;
30  import org.opensearch.cluster.health.ClusterHealthStatus;
31  import org.opensearch.common.xcontent.XContentType;
32  
33  /**
34   * Response entity for ping operations.
35   */
36  public class PingResponse {
37      private static final String CLUSTER_NAME = "cluster_name";
38      private static final String STATUS = "status";
39      private static final String TIMED_OUT = "timed_out";
40      private static final String NUMBER_OF_NODES = "number_of_nodes";
41      private static final String NUMBER_OF_DATA_NODES = "number_of_data_nodes";
42      private static final String NUMBER_OF_PENDING_TASKS = "number_of_pending_tasks";
43      private static final String NUMBER_OF_IN_FLIGHT_FETCH = "number_of_in_flight_fetch";
44      private static final String DELAYED_UNASSIGNED_SHARDS = "delayed_unassigned_shards";
45      private static final String TASK_MAX_WAIT_TIME_IN_QUEUE_IN_MILLIS = "task_max_waiting_in_queue_millis";
46      private static final String ACTIVE_SHARDS_PERCENT_AS_NUMBER = "active_shards_percent_as_number";
47      private static final String ACTIVE_PRIMARY_SHARDS = "active_primary_shards";
48      private static final String ACTIVE_SHARDS = "active_shards";
49      private static final String RELOCATING_SHARDS = "relocating_shards";
50      private static final String INITIALIZING_SHARDS = "initializing_shards";
51      private static final String UNASSIGNED_SHARDS = "unassigned_shards";
52  
53      private final int status;
54  
55      private final String clusterName;
56  
57      private final String clusterStatus;
58  
59      private String message = StringUtil.EMPTY;
60  
61      /**
62       * Creates a ping response from cluster health response.
63       *
64       * @param response the cluster health response
65       */
66      public PingResponse(final ClusterHealthResponse response) {
67          status = response.getStatus() == ClusterHealthStatus.RED ? 1 : 0;
68          clusterName = response.getClusterName();
69          clusterStatus = response.getStatus().toString();
70          final Set<String> fieldSet = ComponentUtil.getFessConfig().getApiPingEsFieldSet();
71          try (OutputStream out = SearchEngineUtil.getXContentBuilderOutputStream((builder, params) -> {
72              builder.startObject();
73              if (fieldSet.contains(CLUSTER_NAME)) {
74                  builder.field(CLUSTER_NAME, response.getClusterName());
75              }
76              if (fieldSet.contains(STATUS)) {
77                  builder.field(STATUS, response.getStatus().name().toLowerCase(Locale.ROOT));
78              }
79              if (fieldSet.contains(TIMED_OUT)) {
80                  builder.field(TIMED_OUT, response.isTimedOut());
81              }
82              if (fieldSet.contains(NUMBER_OF_NODES)) {
83                  builder.field(NUMBER_OF_NODES, response.getNumberOfNodes());
84              }
85              if (fieldSet.contains(NUMBER_OF_DATA_NODES)) {
86                  builder.field(NUMBER_OF_DATA_NODES, response.getNumberOfDataNodes());
87              }
88              if (fieldSet.contains(ACTIVE_PRIMARY_SHARDS)) {
89                  builder.field(ACTIVE_PRIMARY_SHARDS, response.getActivePrimaryShards());
90              }
91              if (fieldSet.contains(ACTIVE_SHARDS)) {
92                  builder.field(ACTIVE_SHARDS, response.getActiveShards());
93              }
94              if (fieldSet.contains(RELOCATING_SHARDS)) {
95                  builder.field(RELOCATING_SHARDS, response.getRelocatingShards());
96              }
97              if (fieldSet.contains(INITIALIZING_SHARDS)) {
98                  builder.field(INITIALIZING_SHARDS, response.getInitializingShards());
99              }
100             if (fieldSet.contains(UNASSIGNED_SHARDS)) {
101                 builder.field(UNASSIGNED_SHARDS, response.getUnassignedShards());
102             }
103             if (fieldSet.contains(DELAYED_UNASSIGNED_SHARDS)) {
104                 builder.field(DELAYED_UNASSIGNED_SHARDS, response.getDelayedUnassignedShards());
105             }
106             if (fieldSet.contains(NUMBER_OF_PENDING_TASKS)) {
107                 builder.field(NUMBER_OF_PENDING_TASKS, response.getNumberOfPendingTasks());
108             }
109             if (fieldSet.contains(NUMBER_OF_IN_FLIGHT_FETCH)) {
110                 builder.field(NUMBER_OF_IN_FLIGHT_FETCH, response.getNumberOfInFlightFetch());
111             }
112             if (fieldSet.contains(TASK_MAX_WAIT_TIME_IN_QUEUE_IN_MILLIS)) {
113                 builder.field(TASK_MAX_WAIT_TIME_IN_QUEUE_IN_MILLIS, response.getTaskMaxWaitingTime().getMillis());
114             }
115             if (fieldSet.contains(ACTIVE_SHARDS_PERCENT_AS_NUMBER)) {
116                 builder.field(ACTIVE_SHARDS_PERCENT_AS_NUMBER, response.getActiveShardsPercent());
117             }
118             builder.endObject();
119             return builder;
120         }, XContentType.JSON)) {
121             message = ((ByteArrayOutputStream) out).toString(Constants.UTF_8);
122             if (StringUtil.isBlank(message)) {
123                 message = "{}";
124             }
125         } catch (final IOException e) {
126             throw new IORuntimeException(e);
127         }
128     }
129 
130     /**
131      * Gets the status.
132      *
133      * @return the status
134      */
135     public int getStatus() {
136         return status;
137     }
138 
139     /**
140      * Gets the cluster name.
141      *
142      * @return the cluster name
143      */
144     public String getClusterName() {
145         return clusterName;
146     }
147 
148     /**
149      * Gets the cluster status.
150      *
151      * @return the cluster status
152      */
153     public String getClusterStatus() {
154         return clusterStatus;
155     }
156 
157     /**
158      * Gets the message.
159      *
160      * @return the message
161      */
162     public String getMessage() {
163         return message;
164     }
165 
166 }