From 38a2cd40f50ae590d8c2a6040db9b065a639ef95 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Mon, 16 Dec 2024 18:22:53 +0200 Subject: [PATCH 1/2] [Indices] Feature #3453: Add detection for closed indices --- .../Block/Adminhtml/Analysis/Analyzer.php | 1 + .../Grid/Column/Renderer/IndexStatus.php | 2 + .../Model/IndexStatsProvider.php | 9 +-- .../Model/IndexStatusProvider.php | 59 ++++++++++++++++++- 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/module-elasticsuite-indices/Block/Adminhtml/Analysis/Analyzer.php b/src/module-elasticsuite-indices/Block/Adminhtml/Analysis/Analyzer.php index 7aea42a8d..df3454785 100644 --- a/src/module-elasticsuite-indices/Block/Adminhtml/Analysis/Analyzer.php +++ b/src/module-elasticsuite-indices/Block/Adminhtml/Analysis/Analyzer.php @@ -72,6 +72,7 @@ public function getElasticSuiteIndices(): ?array IndexStatus::GHOST_STATUS, IndexStatus::EXTERNAL_STATUS, IndexStatus::UNDEFINED_STATUS, + IndexStatus::CLOSED_STATUS, ]; $indices = []; diff --git a/src/module-elasticsuite-indices/Block/Widget/Grid/Column/Renderer/IndexStatus.php b/src/module-elasticsuite-indices/Block/Widget/Grid/Column/Renderer/IndexStatus.php index 08909119e..068468a10 100644 --- a/src/module-elasticsuite-indices/Block/Widget/Grid/Column/Renderer/IndexStatus.php +++ b/src/module-elasticsuite-indices/Block/Widget/Grid/Column/Renderer/IndexStatus.php @@ -35,6 +35,7 @@ class IndexStatus extends AbstractRenderer public const REBUILDING_STATUS = 'rebuilding'; public const GHOST_STATUS = 'ghost'; public const EXTERNAL_STATUS = 'external'; + public const CLOSED_STATUS = 'closed'; public const UNDEFINED_STATUS = 'undefined'; /** @@ -45,6 +46,7 @@ class IndexStatus extends AbstractRenderer self::REBUILDING_STATUS => self::SEVERITY_MINOR, self::GHOST_STATUS => self::SEVERITY_CRITICAL, self::EXTERNAL_STATUS => self::SEVERITY_EXTERNAL, + self::CLOSED_STATUS => self::SEVERITY_MINOR, self::UNDEFINED_STATUS => self::SEVERITY_UNDEFINED, ]; diff --git a/src/module-elasticsuite-indices/Model/IndexStatsProvider.php b/src/module-elasticsuite-indices/Model/IndexStatsProvider.php index 417f38c96..1dbf83caf 100644 --- a/src/module-elasticsuite-indices/Model/IndexStatsProvider.php +++ b/src/module-elasticsuite-indices/Model/IndexStatsProvider.php @@ -120,9 +120,10 @@ public function deleteIndex($indexName): void public function indexStats($indexName, $alias): array { $data = [ - 'index_name' => $indexName, - 'index_alias' => $alias, - 'size' => 'undefined', + 'index_name' => $indexName, + 'index_alias' => $alias, + 'number_of_documents' => 'undefined', + 'size' => 'undefined', ]; try { @@ -144,7 +145,7 @@ public function indexStats($indexName, $alias): array sprintf('Error when loading/parsing statistics for index "%s"', $indexName), ['exception' => $e] ); - $data['index_status'] = IndexStatus::UNDEFINED_STATUS; + $data['index_status'] = IndexStatus::CLOSED_STATUS; } return $data; diff --git a/src/module-elasticsuite-indices/Model/IndexStatusProvider.php b/src/module-elasticsuite-indices/Model/IndexStatusProvider.php index 10a828127..0337c6531 100644 --- a/src/module-elasticsuite-indices/Model/IndexStatusProvider.php +++ b/src/module-elasticsuite-indices/Model/IndexStatusProvider.php @@ -13,7 +13,10 @@ */ namespace Smile\ElasticsuiteIndices\Model; +use Exception; +use Psr\Log\LoggerInterface; use Magento\Framework\DataObject; +use Smile\ElasticsuiteCore\Api\Client\ClientInterface; use Smile\ElasticsuiteCore\Helper\IndexSettings as IndexSettingsHelper; use Smile\ElasticsuiteIndices\Block\Widget\Grid\Column\Renderer\IndexStatus; use Smile\ElasticsuiteIndices\Model\ResourceModel\StoreIndices\CollectionFactory as StoreIndicesCollectionFactory; @@ -45,6 +48,16 @@ class IndexStatusProvider */ private const SECONDS_IN_DAY = 86400; + /** + * @var ClientInterface + */ + private $client; + + /** + * @var LoggerInterface + */ + private $logger; + /** * @var IndexSettingsHelper */ @@ -63,22 +76,28 @@ class IndexStatusProvider /** * Constructor. * + * @param ClientInterface $client ES client. * @param IndexSettingsHelper $indexSettingsHelper Index settings helper. * @param StoreIndicesCollectionFactory $storeIndicesFactory Store indices collection. * @param WorkingIndexerCollectionFactory $indexerCollectionFactory Working indexers collection. + * @param LoggerInterface $logger Logger. */ public function __construct( + ClientInterface $client, IndexSettingsHelper $indexSettingsHelper, StoreIndicesCollectionFactory $storeIndicesFactory, - WorkingIndexerCollectionFactory $indexerCollectionFactory + WorkingIndexerCollectionFactory $indexerCollectionFactory, + LoggerInterface $logger ) { + $this->client = $client; $this->indexSettingsHelper = $indexSettingsHelper; $this->storeIndices = $storeIndicesFactory->create()->getItems(); $this->workingIndexers = $indexerCollectionFactory->create()->getItems(); + $this->logger = $logger; } /** - * Get a index status. + * Get an index status. * * @param string $indexName Index name. * @param string $alias Index alias. @@ -93,6 +112,10 @@ public function getIndexStatus($indexName, $alias): string return IndexStatus::EXTERNAL_STATUS; } + if ($this->isClosed($indexName)) { + return IndexStatus::CLOSED_STATUS; + } + if ($this->isRebuilding($indexName, $indexDate)) { return IndexStatus::REBUILDING_STATUS; } @@ -151,6 +174,38 @@ private function isExternal(string $indexName): bool return true; } + /** + * Returns if the index is closed. + * + * @param string $indexName Index name. + * + * @return bool + */ + private function isClosed(string $indexName): bool + { + try { + // Ensure the index is NOT External before checking for Closed status. + if ($this->isExternal($indexName)) { + return false; + } + + // Attempt to fetch index stats or metadata to check its status. + $indexStats = $this->client->indexStats($indexName); + + // If we successfully retrieved index stats and no error occurs, the index is not closed. + return false; + } catch (Exception $e) { + // Log the error (optional for better diagnostics). + $this->logger->error( + sprintf('Error fetching index stats for "%s": %s', $indexName, $e->getMessage()) + ); + + // If an error occurs, it's safer to assume the index could be closed, or the stats are unavailable. + // Returning true here means the index is likely closed or inaccessible. + return true; + } + } + /** * Returns if index is ghost. * From 974fd381d5d45e8b8934087025e07658c7bca4d7 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Mon, 16 Dec 2024 18:30:41 +0200 Subject: [PATCH 2/2] [Indices] Feature #3453: phpmd warning --- src/module-elasticsuite-indices/Model/IndexStatusProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/module-elasticsuite-indices/Model/IndexStatusProvider.php b/src/module-elasticsuite-indices/Model/IndexStatusProvider.php index 0337c6531..b8c1aca8f 100644 --- a/src/module-elasticsuite-indices/Model/IndexStatusProvider.php +++ b/src/module-elasticsuite-indices/Model/IndexStatusProvider.php @@ -180,6 +180,7 @@ private function isExternal(string $indexName): bool * @param string $indexName Index name. * * @return bool + * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ private function isClosed(string $indexName): bool {