Stats.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Elastica\Node;
  3. use Elastica\Node as BaseNode;
  4. /**
  5. * Elastica cluster node object.
  6. *
  7. * @author Nicolas Ruflin <spam@ruflin.com>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-status.html
  10. */
  11. class Stats
  12. {
  13. /**
  14. * Response.
  15. *
  16. * @var \Elastica\Response Response object
  17. */
  18. protected $_response;
  19. /**
  20. * Stats data.
  21. *
  22. * @var array stats data
  23. */
  24. protected $_data = [];
  25. /**
  26. * Node.
  27. *
  28. * @var \Elastica\Node Node object
  29. */
  30. protected $_node;
  31. /**
  32. * Create new stats for node.
  33. *
  34. * @param \Elastica\Node $node Elastica node object
  35. */
  36. public function __construct(BaseNode $node)
  37. {
  38. $this->_node = $node;
  39. $this->refresh();
  40. }
  41. /**
  42. * Returns all node stats as array based on the arguments.
  43. *
  44. * Several arguments can be use
  45. * get('index', 'test', 'example')
  46. *
  47. * @return array Node stats for the given field or null if not found
  48. */
  49. public function get()
  50. {
  51. $data = $this->getData();
  52. foreach (func_get_args() as $arg) {
  53. if (isset($data[$arg])) {
  54. $data = $data[$arg];
  55. } else {
  56. return;
  57. }
  58. }
  59. return $data;
  60. }
  61. /**
  62. * Returns all stats data.
  63. *
  64. * @return array Data array
  65. */
  66. public function getData()
  67. {
  68. return $this->_data;
  69. }
  70. /**
  71. * Returns node object.
  72. *
  73. * @return \Elastica\Node Node object
  74. */
  75. public function getNode()
  76. {
  77. return $this->_node;
  78. }
  79. /**
  80. * Returns response object.
  81. *
  82. * @return \Elastica\Response Response object
  83. */
  84. public function getResponse()
  85. {
  86. return $this->_response;
  87. }
  88. /**
  89. * Reloads all nodes information. Has to be called if informations changed.
  90. *
  91. * @return \Elastica\Response Response object
  92. */
  93. public function refresh()
  94. {
  95. $endpoint = new \Elasticsearch\Endpoints\Cluster\Nodes\Stats();
  96. $endpoint->setNodeID($this->getNode()->getName());
  97. $this->_response = $this->getNode()->getClient()->requestEndpoint($endpoint);
  98. $data = $this->getResponse()->getData();
  99. $this->_data = reset($data['nodes']);
  100. }
  101. }