Skip to content

Commit

Permalink
[Indices] Feature Smile-SA#3452, add number of primary shards and rep…
Browse files Browse the repository at this point in the history
…licas in the Indices grid
  • Loading branch information
vahonc committed Jan 6, 2025
1 parent 21805db commit 0baf18f
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
129 changes: 129 additions & 0 deletions src/module-elasticsuite-indices/Model/IndexStatsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Smile\ElasticsuiteCore\Api\Client\ClientInterface;
use Smile\ElasticsuiteIndices\Block\Widget\Grid\Column\Renderer\IndexStatus;

Expand All @@ -27,6 +29,31 @@
*/
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 CacheInterface
*/
private $cache;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var ClientInterface
*/
Expand Down Expand Up @@ -57,20 +84,31 @@ class IndexStatsProvider
*/
private $indicesStats = null;

/**
* @var array
*/
private $cachedIndexSettings = [];

/**
* Constructor.
*
* @param CacheInterface $cache Cache.
* @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,
SerializerInterface $serializer,
ClientInterface $client,
IndicesList $indicesList,
IndexStatusProvider $indexStatusProvider,
LoggerInterface $logger
) {
$this->cache = $cache;
$this->serializer = $serializer;
$this->client = $client;
$this->indicesList = $indicesList;
$this->indexStatusProvider = $indexStatusProvider;
Expand Down Expand Up @@ -140,6 +178,8 @@ 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 @@ -151,6 +191,82 @@ public function indexStats($indexName, $alias): array
return $data;
}

/**
* Get index settings with caching.
*
* @param string $indexName
* @return array
*/
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->cache->load($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->serializer->serialize($settingsData),
$cacheKey,
$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.
*
Expand Down Expand Up @@ -206,4 +322,17 @@ private function initStats()
}
}
}

/**
* Get cache tags.
*
* @return array
*/
private function getCacheTags()
{
return [
\Smile\ElasticsuiteCore\Cache\Type\Elasticsuite::CACHE_TAG,
self::CACHE_TAG,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
</arguments>
</block>


<block class="Magento\Backend\Block\Widget\Grid\Column" as="size">
<arguments>
<argument name="header" xsi:type="string" translate="true">Size</argument>
Expand All @@ -82,6 +81,28 @@
</arguments>
</block>

<block class="Magento\Backend\Block\Widget\Grid\Column" as="number_of_shards">
<arguments>
<argument name="header" xsi:type="string" translate="true">Shards</argument>
<argument name="index" xsi:type="string">number_of_shards</argument>
<argument name="filter" xsi:type="string">0</argument>
<argument name="type" xsi:type="string">text</argument>
<argument name="align" xsi:type="string">right</argument>
<argument name="sortable" xsi:type="boolean">0</argument>
</arguments>
</block>

<block class="Magento\Backend\Block\Widget\Grid\Column" as="number_of_replicas">
<arguments>
<argument name="header" xsi:type="string" translate="true">Replicas</argument>
<argument name="index" xsi:type="string">number_of_replicas</argument>
<argument name="filter" xsi:type="string">0</argument>
<argument name="type" xsi:type="string">text</argument>
<argument name="align" xsi:type="string">right</argument>
<argument name="sortable" xsi:type="boolean">0</argument>
</arguments>
</block>

<block class="Magento\Backend\Block\Widget\Grid\Column" as="index_status">
<arguments>
<argument name="header" xsi:type="string" translate="true">Index Status</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 0baf18f

Please sign in to comment.