1<?php
2
3namespace Elastica\Cluster\Health;
4
5/**
6 * Wraps status information for an index.
7 *
8 * @author Ray Ward <ray.ward@bigcommerce.com>
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
11 */
12class Index
13{
14    /**
15     * @var string the name of the index
16     */
17    protected $_name;
18
19    /**
20     * @var array the index health data
21     */
22    protected $_data;
23
24    /**
25     * @param string $name the name of the index
26     * @param array  $data the index health data
27     */
28    public function __construct(string $name, array $data)
29    {
30        $this->_name = $name;
31        $this->_data = $data;
32    }
33
34    /**
35     * Gets the name of the index.
36     *
37     * @return string
38     */
39    public function getName(): string
40    {
41        return $this->_name;
42    }
43
44    /**
45     * Gets the status of the index.
46     *
47     * @return string green, yellow or red
48     */
49    public function getStatus(): string
50    {
51        return $this->_data['status'];
52    }
53
54    /**
55     * Gets the number of nodes in the index.
56     *
57     * @return int
58     */
59    public function getNumberOfShards(): int
60    {
61        return $this->_data['number_of_shards'];
62    }
63
64    /**
65     * Gets the number of data nodes in the index.
66     *
67     * @return int
68     */
69    public function getNumberOfReplicas(): int
70    {
71        return $this->_data['number_of_replicas'];
72    }
73
74    /**
75     * Gets the number of active primary shards.
76     *
77     * @return int
78     */
79    public function getActivePrimaryShards(): int
80    {
81        return $this->_data['active_primary_shards'];
82    }
83
84    /**
85     * Gets the number of active shards.
86     *
87     * @return int
88     */
89    public function getActiveShards(): int
90    {
91        return $this->_data['active_shards'];
92    }
93
94    /**
95     * Gets the number of relocating shards.
96     *
97     * @return int
98     */
99    public function getRelocatingShards(): int
100    {
101        return $this->_data['relocating_shards'];
102    }
103
104    /**
105     * Gets the number of initializing shards.
106     *
107     * @return int
108     */
109    public function getInitializingShards(): int
110    {
111        return $this->_data['initializing_shards'];
112    }
113
114    /**
115     * Gets the number of unassigned shards.
116     *
117     * @return int
118     */
119    public function getUnassignedShards(): int
120    {
121        return $this->_data['unassigned_shards'];
122    }
123
124    /**
125     * Gets the health of the shards in this index.
126     *
127     * @return Shard[]
128     */
129    public function getShards(): array
130    {
131        $shards = [];
132        foreach ($this->_data['shards'] as $shardNumber => $shard) {
133            $shards[] = new Shard($shardNumber, $shard);
134        }
135
136        return $shards;
137    }
138}
139