Skip to content

Commit

Permalink
[Indices] Feature Smile-SA#3452, code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vahonc committed Jan 13, 2025
1 parent fd49441 commit 46025cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/module-elasticsuite-indices/Model/IndexStatsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -45,9 +45,9 @@ class IndexStatsProvider
const CACHE_LIFETIME = 7200;

/**
* @var CacheInterface
* @var CacheHelper
*/
private $cache;
private $cacheHelper;

/**
* @var SerializerInterface
Expand Down Expand Up @@ -92,22 +92,22 @@ 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.
* @param IndexStatusProvider $indexStatusProvider Index Status Provider.
* @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;
Expand Down Expand Up @@ -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']);
Expand All @@ -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),
Expand All @@ -205,15 +209,15 @@ 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);
} else {
$settingsData = $this->client->getSettings($indexName);

// Save to cache with a tag.
$this->cache->save(
$this->cacheHelper->saveCache(
$this->serializer->serialize($settingsData),
$cacheKey,
$this->getCacheTags(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 46025cf

Please sign in to comment.