1<?php
2
3namespace Elastica\Cluster;
4
5use Elastica\Client;
6use Elastica\Cluster\Health\Index;
7use Elastica\Exception\ClientException;
8use Elastica\Exception\ConnectionException;
9use Elastica\Exception\ResponseException;
10
11/**
12 * Elastic cluster health.
13 *
14 * @author Ray Ward <ray.ward@bigcommerce.com>
15 *
16 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
17 */
18class Health
19{
20    /**
21     * @var Client client object
22     */
23    protected $_client;
24
25    /**
26     * @var array the cluster health data
27     */
28    protected $_data;
29
30    public function __construct(Client $client)
31    {
32        $this->_client = $client;
33        $this->refresh();
34    }
35
36    /**
37     * Gets the health data.
38     */
39    public function getData(): array
40    {
41        return $this->_data;
42    }
43
44    /**
45     * Refreshes the health data for the cluster.
46     */
47    public function refresh(): self
48    {
49        $this->_data = $this->_retrieveHealthData();
50
51        return $this;
52    }
53
54    /**
55     * Gets the name of the cluster.
56     */
57    public function getClusterName(): string
58    {
59        return $this->_data['cluster_name'];
60    }
61
62    /**
63     * Gets the status of the cluster.
64     *
65     * @return string green, yellow or red
66     */
67    public function getStatus(): string
68    {
69        return $this->_data['status'];
70    }
71
72    /**
73     * TODO determine the purpose of this.
74     */
75    public function getTimedOut(): bool
76    {
77        return $this->_data['timed_out'];
78    }
79
80    /**
81     * Gets the number of nodes in the cluster.
82     */
83    public function getNumberOfNodes(): int
84    {
85        return $this->_data['number_of_nodes'];
86    }
87
88    /**
89     * Gets the number of data nodes in the cluster.
90     */
91    public function getNumberOfDataNodes(): int
92    {
93        return $this->_data['number_of_data_nodes'];
94    }
95
96    /**
97     * Gets the number of active primary shards.
98     */
99    public function getActivePrimaryShards(): int
100    {
101        return $this->_data['active_primary_shards'];
102    }
103
104    /**
105     * Gets the number of active shards.
106     */
107    public function getActiveShards(): int
108    {
109        return $this->_data['active_shards'];
110    }
111
112    /**
113     * Gets the number of relocating shards.
114     */
115    public function getRelocatingShards(): int
116    {
117        return $this->_data['relocating_shards'];
118    }
119
120    /**
121     * Gets the number of initializing shards.
122     */
123    public function getInitializingShards(): int
124    {
125        return $this->_data['initializing_shards'];
126    }
127
128    /**
129     * Gets the number of unassigned shards.
130     */
131    public function getUnassignedShards(): int
132    {
133        return $this->_data['unassigned_shards'];
134    }
135
136    /**
137     * get the number of delayed unassined shards.
138     */
139    public function getDelayedUnassignedShards(): int
140    {
141        return $this->_data['delayed_unassigned_shards'];
142    }
143
144    public function getNumberOfPendingTasks(): int
145    {
146        return $this->_data['number_of_pending_tasks'];
147    }
148
149    public function getNumberOfInFlightFetch(): int
150    {
151        return $this->_data['number_of_in_flight_fetch'];
152    }
153
154    public function getTaskMaxWaitingInQueueMillis(): int
155    {
156        return $this->_data['task_max_waiting_in_queue_millis'];
157    }
158
159    public function getActiveShardsPercentAsNumber(): int
160    {
161        return $this->_data['active_shards_percent_as_number'];
162    }
163
164    /**
165     * Gets the status of the indices.
166     *
167     * @return Index[]
168     */
169    public function getIndices(): array
170    {
171        $indices = [];
172        foreach ($this->_data['indices'] as $indexName => $index) {
173            $indices[$indexName] = new Index($indexName, $index);
174        }
175
176        return $indices;
177    }
178
179    /**
180     * Retrieves the health data from the cluster.
181     *
182     * @throws ClientException
183     * @throws ConnectionException
184     * @throws ResponseException
185     */
186    protected function _retrieveHealthData(): array
187    {
188        $endpoint = new \Elasticsearch\Endpoints\Cluster\Health();
189        $endpoint->setParams(['level' => 'shards']);
190
191        $response = $this->_client->requestEndpoint($endpoint);
192
193        return $response->getData();
194    }
195}
196