View Javadoc
1   /*
2    * Copyright 2012-2021 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  
23  import org.apache.commons.text.StringEscapeUtils;
24  import org.codelibs.core.lang.StringUtil;
25  import org.codelibs.fesen.action.admin.cluster.health.ClusterHealthResponse;
26  import org.codelibs.fesen.cluster.health.ClusterHealthStatus;
27  import org.codelibs.fesen.common.xcontent.XContentType;
28  import org.codelibs.fess.Constants;
29  import org.codelibs.fess.util.SearchEngineUtil;
30  
31  public class PingResponse {
32      private static final String CLUSTER_NAME = "cluster_name";
33      private static final String STATUS = "status";
34      private static final String TIMED_OUT = "timed_out";
35      private static final String NUMBER_OF_NODES = "number_of_nodes";
36      private static final String NUMBER_OF_DATA_NODES = "number_of_data_nodes";
37      private static final String NUMBER_OF_PENDING_TASKS = "number_of_pending_tasks";
38      private static final String NUMBER_OF_IN_FLIGHT_FETCH = "number_of_in_flight_fetch";
39      private static final String DELAYED_UNASSIGNED_SHARDS = "delayed_unassigned_shards";
40      private static final String TASK_MAX_WAIT_TIME_IN_QUEUE_IN_MILLIS = "task_max_waiting_in_queue_millis";
41      private static final String ACTIVE_SHARDS_PERCENT_AS_NUMBER = "active_shards_percent_as_number";
42      private static final String ACTIVE_PRIMARY_SHARDS = "active_primary_shards";
43      private static final String ACTIVE_SHARDS = "active_shards";
44      private static final String RELOCATING_SHARDS = "relocating_shards";
45      private static final String INITIALIZING_SHARDS = "initializing_shards";
46      private static final String UNASSIGNED_SHARDS = "unassigned_shards";
47  
48      private final int status;
49  
50      private final String clusterName;
51  
52      private final String clusterStatus;
53  
54      private String message = StringUtil.EMPTY;
55  
56      public PingResponse(final ClusterHealthResponse response) {
57          status = response.getStatus() == ClusterHealthStatus.RED ? 1 : 0;
58          clusterName = response.getClusterName();
59          clusterStatus = response.getStatus().toString();
60          try (OutputStream out = SearchEngineUtil.getXContentBuilderOutputStream((builder, params) -> {
61              builder.startObject();
62              builder.field(CLUSTER_NAME, response.getClusterName());
63              builder.field(STATUS, response.getStatus().name().toLowerCase(Locale.ROOT));
64              builder.field(TIMED_OUT, response.isTimedOut());
65              builder.field(NUMBER_OF_NODES, response.getNumberOfNodes());
66              builder.field(NUMBER_OF_DATA_NODES, response.getNumberOfDataNodes());
67              builder.field(ACTIVE_PRIMARY_SHARDS, response.getActivePrimaryShards());
68              builder.field(ACTIVE_SHARDS, response.getActiveShards());
69              builder.field(RELOCATING_SHARDS, response.getRelocatingShards());
70              builder.field(INITIALIZING_SHARDS, response.getInitializingShards());
71              builder.field(UNASSIGNED_SHARDS, response.getUnassignedShards());
72              builder.field(DELAYED_UNASSIGNED_SHARDS, response.getDelayedUnassignedShards());
73              builder.field(NUMBER_OF_PENDING_TASKS, response.getNumberOfPendingTasks());
74              builder.field(NUMBER_OF_IN_FLIGHT_FETCH, response.getNumberOfInFlightFetch());
75              builder.field(TASK_MAX_WAIT_TIME_IN_QUEUE_IN_MILLIS, response.getTaskMaxWaitingTime().getMillis());
76              builder.field(ACTIVE_SHARDS_PERCENT_AS_NUMBER, response.getActiveShardsPercent());
77              builder.endObject();
78              return builder;
79          }, XContentType.JSON)) {
80              message = ((ByteArrayOutputStream) out).toString(Constants.UTF_8);
81              if (StringUtil.isBlank(message)) {
82                  message = "{}";
83              }
84          } catch (final IOException e) {
85              message = "{ \"error\" : \"" + StringEscapeUtils.escapeJson(e.getMessage()) + "\"}";
86          }
87      }
88  
89      public int getStatus() {
90          return status;
91      }
92  
93      public String getClusterName() {
94          return clusterName;
95      }
96  
97      public String getClusterStatus() {
98          return clusterStatus;
99      }
100 
101     public String getMessage() {
102         return message;
103     }
104 
105 }