diff --git a/src/module-elasticsuite-indices/Model/IndexStatsProvider.php b/src/module-elasticsuite-indices/Model/IndexStatsProvider.php index 1dbf83caf..88875e867 100644 --- a/src/module-elasticsuite-indices/Model/IndexStatsProvider.php +++ b/src/module-elasticsuite-indices/Model/IndexStatsProvider.php @@ -16,6 +16,7 @@ use Exception; use Psr\Log\LoggerInterface; use Smile\ElasticsuiteCore\Api\Client\ClientInterface; +use Smile\ElasticsuiteCore\Helper\Cache as CacheHelper; use Smile\ElasticsuiteIndices\Block\Widget\Grid\Column\Renderer\IndexStatus; /** @@ -27,6 +28,26 @@ */ class IndexStatsProvider { + /** + * Cache Key Prefix. + */ + const CACHE_KEY_PREFIX = 'es_index_settings_'; + + /** + * Cache Tag. + */ + const CACHE_TAG = 'index_settings'; + + /** + * @var int Cache lifetime. + */ + const CACHE_LIFETIME = 7200; + + /** + * @var CacheHelper + */ + private $cacheHelper; + /** * @var ClientInterface */ @@ -57,20 +78,28 @@ class IndexStatsProvider */ private $indicesStats = null; + /** + * @var array + */ + private $cachedIndexSettings = []; + /** * Constructor. * + * @param CacheHelper $cacheHelper ES cache helper. * @param ClientInterface $client ES client. * @param IndicesList $indicesList Index list. * @param IndexStatusProvider $indexStatusProvider Index Status Provider. * @param LoggerInterface $logger Logger. */ public function __construct( + CacheHelper $cacheHelper, ClientInterface $client, IndicesList $indicesList, IndexStatusProvider $indexStatusProvider, LoggerInterface $logger ) { + $this->cacheHelper = $cacheHelper; $this->client = $client; $this->indicesList = $indicesList; $this->indexStatusProvider = $indexStatusProvider; @@ -122,11 +151,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']); @@ -151,6 +186,84 @@ public function indexStats($indexName, $alias): array return $data; } + /** + * Get index settings with caching. + * + * @param string $indexName Index name. + * @return array + * + * @SuppressWarnings(PHPMD.ElseExpression) + */ + public function getIndexSettings(string $indexName): array + { + $cacheKey = self::CACHE_KEY_PREFIX . $indexName; + + // Check if the settings are already in memory. + if (!isset($this->cachedIndexSettings[$cacheKey])) { + $cachedData = $this->cacheHelper->loadCache($cacheKey); + + if ($cachedData) { + $this->cachedIndexSettings[$cacheKey] = $cachedData; + } else { + $settingsData = $this->client->getSettings($indexName); + + // Save to cache with a tag. + $this->cacheHelper->saveCache( + $cacheKey, + $settingsData, + $this->getCacheTags(), + self::CACHE_LIFETIME + ); + + $this->cachedIndexSettings[$cacheKey] = $settingsData; + } + } + + return $this->cachedIndexSettings[$cacheKey]; + } + + /** + * Retrieve number of shards from index settings. + * + * @param string $indexName Index name. + * + * @return int + */ + public function getShardsConfiguration($indexName) + { + // Retrieve the index settings. + $indexSettings = $this->getIndexSettings($indexName); + + // Check if settings for the given index exist and retrieve number_of_shards. + if (isset($indexSettings[$indexName]['settings']['index']['number_of_shards'])) { + return (int) $indexSettings[$indexName]['settings']['index']['number_of_shards']; + } + + // Return null or throw an exception if the value doesn't exist. + throw new \RuntimeException("number_of_shards setting not found for index: $indexName"); + } + + /** + * Retrieve number of replicas from index settings. + * + * @param string $indexName Index name. + * + * @return int + */ + public function getReplicasConfiguration($indexName) + { + // Retrieve the index settings. + $indexSettings = $this->getIndexSettings($indexName); + + // Check if settings for the given index exist and retrieve number_of_replicas. + if (isset($indexSettings[$indexName]['settings']['index']['number_of_replicas'])) { + return (int) $indexSettings[$indexName]['settings']['index']['number_of_replicas']; + } + + // Return null or throw an exception if the value doesn't exist. + throw new \RuntimeException("number_of_replicas setting not found for index: $indexName"); + } + /** * Returns if index is elastic suite index. * @@ -206,4 +319,17 @@ private function initStats() } } } + + /** + * Get cache tags. + * + * @return array + */ + private function getCacheTags() + { + return [ + \Smile\ElasticsuiteCore\Cache\Type\Elasticsuite::CACHE_TAG, + self::CACHE_TAG, + ]; + } } 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 diff --git a/src/module-elasticsuite-indices/view/adminhtml/layout/smile_elasticsuite_indices_index_index.xml b/src/module-elasticsuite-indices/view/adminhtml/layout/smile_elasticsuite_indices_index_index.xml index c378ae375..1cbf02665 100644 --- a/src/module-elasticsuite-indices/view/adminhtml/layout/smile_elasticsuite_indices_index_index.xml +++ b/src/module-elasticsuite-indices/view/adminhtml/layout/smile_elasticsuite_indices_index_index.xml @@ -70,7 +70,6 @@ - Size @@ -82,6 +81,28 @@ + + + Shards + number_of_shards + 0 + text + right + 0 + + + + + + Replicas + number_of_replicas + 0 + text + right + 0 + + + Index Status diff --git a/src/module-elasticsuite-indices/view/adminhtml/web/css/source/_module.less b/src/module-elasticsuite-indices/view/adminhtml/web/css/source/_module.less index c8dddf9c0..8e3ddf71a 100644 --- a/src/module-elasticsuite-indices/view/adminhtml/web/css/source/_module.less +++ b/src/module-elasticsuite-indices/view/adminhtml/web/css/source/_module.less @@ -35,8 +35,14 @@ .col-index_status { max-width: 13rem; } - .col-number_of_documents, .col-size { + .col-number_of_documents, .col-size, .col-number_of_shards, .col-number_of_replicas { text-align: right; } + .col-number_of_shards, .col-number_of_replicas { + width: 70px; + } + .col-actions { + width: 170px; + } } }