Skip to content

Commit

Permalink
Merge pull request Smile-SA#3382 from rbayet/feat-indices-deal-with-i…
Browse files Browse the repository at this point in the history
…ndexstats-issues

[Indices] Status 'unknown' when stats fails + logging failures
  • Loading branch information
rbayet authored Sep 13, 2024
2 parents 06fe67d + a559198 commit 586eb5f
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/module-elasticsuite-indices/Model/IndexStatsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Smile\ElasticsuiteIndices\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Smile\ElasticsuiteCore\Api\Client\ClientInterface;
use Smile\ElasticsuiteIndices\Block\Widget\Grid\Column\Renderer\IndexStatus;

Expand Down Expand Up @@ -41,6 +42,11 @@ class IndexStatsProvider
*/
protected $indexStatusProvider;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var null
*/
Expand All @@ -57,15 +63,18 @@ class IndexStatsProvider
* @param ClientInterface $client ES client.
* @param IndicesList $indicesList Index list.
* @param IndexStatusProvider $indexStatusProvider Index Status Provider.
* @param LoggerInterface $logger Logger.
*/
public function __construct(
ClientInterface $client,
IndicesList $indicesList,
IndexStatusProvider $indexStatusProvider
IndexStatusProvider $indexStatusProvider,
LoggerInterface $logger
) {
$this->client = $client;
$this->indicesList = $indicesList;
$this->indexStatusProvider = $indexStatusProvider;
$this->logger = $logger;
$this->initStats();
}

Expand Down Expand Up @@ -131,7 +140,11 @@ public function indexStats($indexName, $alias): array
$data['size_in_bytes'] = $indexStats['total']['store']['size_in_bytes'];
}
} catch (Exception $e) {
$data['index_status'] = IndexStatus::REBUILDING_STATUS;
$this->logger->error(
sprintf('Error when loading/parsing statistics for index "%s"', $indexName),
['exception' => $e]
);
$data['index_status'] = IndexStatus::UNDEFINED_STATUS;
}

return $data;
Expand Down Expand Up @@ -183,8 +196,13 @@ private function sizeFormatted(int $bytes): string
private function initStats()
{
if ($this->indicesStats === null) {
$indexStatsResponse = $this->client->indexStats('_all');
$this->indicesStats = $indexStatsResponse['indices'] ?? [];
try {
$indexStatsResponse = $this->client->indexStats('_all');
$this->indicesStats = $indexStatsResponse['indices'] ?? [];
} catch (Exception $e) {
$this->logger->error('Error when loading all indices statistics', ['exception' => $e]);
$this->indicesStats = [];
}
}
}
}

0 comments on commit 586eb5f

Please sign in to comment.