From 46025cf9a423bde4d12979c8d462730891cb03e5 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Mon, 13 Jan 2025 16:30:04 +0200 Subject: [PATCH] [Indices] Feature #3452, code refactoring --- .../Model/IndexStatsProvider.php | 28 +++++++++++-------- .../WarningAboutClusterShardsMisconfig.php | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/module-elasticsuite-indices/Model/IndexStatsProvider.php b/src/module-elasticsuite-indices/Model/IndexStatsProvider.php index 05314fb9e..c573a43bf 100644 --- a/src/module-elasticsuite-indices/Model/IndexStatsProvider.php +++ b/src/module-elasticsuite-indices/Model/IndexStatsProvider.php @@ -15,9 +15,9 @@ use Exception; use Psr\Log\LoggerInterface; -use Magento\Framework\App\CacheInterface; use Magento\Framework\Serialize\SerializerInterface; use Smile\ElasticsuiteCore\Api\Client\ClientInterface; +use Smile\ElasticsuiteCore\Helper\Cache as CacheHelper; use Smile\ElasticsuiteIndices\Block\Widget\Grid\Column\Renderer\IndexStatus; /** @@ -45,9 +45,9 @@ class IndexStatsProvider const CACHE_LIFETIME = 7200; /** - * @var CacheInterface + * @var CacheHelper */ - private $cache; + private $cacheHelper; /** * @var SerializerInterface @@ -92,7 +92,7 @@ class IndexStatsProvider /** * Constructor. * - * @param CacheInterface $cache Cache. + * @param CacheHelper $cacheHelper ES cache helper. * @param SerializerInterface $serializer Serializer. * @param ClientInterface $client ES client. * @param IndicesList $indicesList Index list. @@ -100,14 +100,14 @@ class IndexStatsProvider * @param LoggerInterface $logger Logger. */ public function __construct( - CacheInterface $cache, + CacheHelper $cacheHelper, SerializerInterface $serializer, ClientInterface $client, IndicesList $indicesList, IndexStatusProvider $indexStatusProvider, LoggerInterface $logger ) { - $this->cache = $cache; + $this->cacheHelper = $cacheHelper; $this->serializer = $serializer; $this->client = $client; $this->indicesList = $indicesList; @@ -160,11 +160,17 @@ public function indexStats($indexName, $alias): array $data = [ 'index_name' => $indexName, 'index_alias' => $alias, - 'number_of_documents' => 'undefined', - 'size' => 'undefined', + 'number_of_documents' => 'N/A', + 'size' => 'N/A', + 'number_of_shards' => 'N/A', + 'number_of_replicas' => 'N/A', ]; try { + // Retrieve number of shards and replicas configuration. + $data['number_of_shards'] = $this->getShardsConfiguration($indexName); + $data['number_of_replicas'] = $this->getReplicasConfiguration($indexName); + if (!isset($this->indicesStats[$indexName])) { $indexStatsResponse = $this->client->indexStats($indexName); $this->indicesStats[$indexName] = current($indexStatsResponse['indices']); @@ -178,8 +184,6 @@ public function indexStats($indexName, $alias): array $data['size'] = $this->sizeFormatted((int) $indexStats['total']['store']['size_in_bytes']); $data['size_in_bytes'] = $indexStats['total']['store']['size_in_bytes']; } - $data['number_of_shards'] = $this->getShardsConfiguration($indexName); - $data['number_of_replicas'] = $this->getReplicasConfiguration($indexName); } catch (Exception $e) { $this->logger->error( sprintf('Error when loading/parsing statistics for index "%s"', $indexName), @@ -205,7 +209,7 @@ public function getIndexSettings(string $indexName): array // Check if the settings are already in memory. if (!isset($this->cachedIndexSettings[$cacheKey])) { - $cachedData = $this->cache->load($cacheKey); + $cachedData = $this->cacheHelper->loadCache($cacheKey); if ($cachedData) { $this->cachedIndexSettings[$cacheKey] = $this->serializer->unserialize($cachedData); @@ -213,7 +217,7 @@ public function getIndexSettings(string $indexName): array $settingsData = $this->client->getSettings($indexName); // Save to cache with a tag. - $this->cache->save( + $this->cacheHelper->saveCache( $this->serializer->serialize($settingsData), $cacheKey, $this->getCacheTags(), diff --git a/src/module-elasticsuite-indices/Model/System/Message/WarningAboutClusterShardsMisconfig.php b/src/module-elasticsuite-indices/Model/System/Message/WarningAboutClusterShardsMisconfig.php index 6ddefbc4b..438dc6191 100644 --- a/src/module-elasticsuite-indices/Model/System/Message/WarningAboutClusterShardsMisconfig.php +++ b/src/module-elasticsuite-indices/Model/System/Message/WarningAboutClusterShardsMisconfig.php @@ -44,7 +44,7 @@ class WarningAboutClusterShardsMisconfig implements MessageInterface */ private const ES_INDICES_SETTINGS_WIKI_PAGE = 'https://github.com/Smile-SA/elasticsuite/wiki/ModuleInstall#indices-settings'; - public const UNDEFINED_SIZE = 'undefined'; + public const UNDEFINED_SIZE = 'N/A'; /** * @var IndexSettingsHelper