Skip to content

Commit

Permalink
Merge pull request #3477 from vahonc/3452-elasticsuite-indices-add-nu…
Browse files Browse the repository at this point in the history
…mber-of-shards-and-replicas-in-the-indices-grid-2.10-feature

[Indices] Feature #3452, add number of primary shards and replicas in the Indices grid
  • Loading branch information
rbayet authored Jan 13, 2025
2 parents 1806a3c + 70798c0 commit ef91cfe
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 5 deletions.
130 changes: 128 additions & 2 deletions src/module-elasticsuite-indices/Model/IndexStatsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand All @@ -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.
*
Expand Down Expand Up @@ -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,
];
}
}
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
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 ef91cfe

Please sign in to comment.