Skip to content

Commit

Permalink
[HealthChecks] Admin nav. menu decoration by number of warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Jan 16, 2025
1 parent 2efd9cf commit b077d1f
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <[email protected]>
* @copyright 2025 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Block\Adminhtml\Healthcheck;

use Magento\Backend\Block\Template;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Smile\ElasticsuiteCore\Api\Healthcheck\CheckInterface;
use Smile\ElasticsuiteCore\Model\Healthcheck\HealthcheckList;

/**
* Elasticsuite menu decorator block.
* Adds a failed/warning healthchecks counter next to Elasticsuite elements in the menu.
*
* @category Smile
* @package Smile\ElasticsuiteCore
*/
class MenuDecorator extends Template
{
/** @var HealthcheckList */
private $healthcheckList;

/** @var integer */
private $issuesCount;

/**
* Constructor.
*
* @param HealthcheckList $healthcheckList Healthchecks list.
* @param Template\Context $context Template context.
* @param array $data Data.
* @param JsonHelper|null $jsonHelper Json helper.
* @param DirectoryHelper|null $directoryHelper Directory helper.
*/
public function __construct(
HealthcheckList $healthcheckList,
Template\Context $context,
array $data = [],
?JsonHelper $jsonHelper = null,
?DirectoryHelper $directoryHelper = null
) {
parent::__construct($context, $data, $jsonHelper, $directoryHelper);
$this->healthcheckList = $healthcheckList;
}

/**
* Returns true if the menu decoration should happen.
*
* @return bool
*/
public function isEnabled()
{
return $this->getIssuesCount() > 0;
}

/**
* Returns the number of failed tests.
*
* @return int
*/
public function getIssuesCount()
{
if (null === $this->issuesCount) {
$this->issuesCount = 0;

foreach ($this->healthcheckList->getChecks() as $check) {
if ($check->getStatus() === CheckInterface::WARNING_STATUS) {
$this->issuesCount++;
}
}
}

return $this->issuesCount;
}

/**
* {@inheritDoc}
*/
protected function getCacheLifetime()
{
// Very short cache TTL until a proper cache mechanism is set up at the healthcheck list level.
return 60;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<block class="Smile\ElasticsuiteCore\Block\Adminhtml\System\Version" after="version" name="es-version" as="es-version" />
</referenceContainer>
</referenceContainer>
<block class="Smile\ElasticsuiteCore\Block\Adminhtml\Healthcheck\MenuDecorator"
name="healthcheck.menu_decorator"
template="Smile_ElasticsuiteCore::healthcheck/menu_decorator.phtml" />
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Healthcheck menu decorator block
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <[email protected]>
* @copyright 2025 Smile
* @license Open Software License ("OSL") v. 3.0
*/

/** @var \Smile\ElasticsuiteCore\Block\Adminhtml\Healthcheck\MenuDecorator $block */
?>
<?php if ($block->isEnabled()): ?>
<script type="text/javascript">
require(["jquery"], function ($) {
$('nav').on('globalnavigationcreate', function () {
let counter = '<span class="healthchecks-warning-counter">' + <?= (int) $block->getIssuesCount() ?> + '</span>';
$('#menu-smile-elasticsuitecore-elasticsuite-menu a').first().append(counter);
$('#menu-smile-elasticsuitecore-elasticsuite-menu .item-healthcheck a').first().append(counter);
});
});
</script>
<?php endif; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,33 @@ fieldset.radioset-tooltip {
}
}

#nav {
#menu-smile-elasticsuitecore-elasticsuite-menu {
span.healthchecks-warning-counter {
background-color: @color-tomato-brick;;
border-radius: 1em;
color: @color-white;;
display: inline-block;
font-size: round(@font-size__s - .1rem, 1);
font-weight: @font-weight__bold;
height: 20px;
left: 50%;
line-height: 20px;
margin-left: .3em;
margin-top: -1.1em;
min-width: 20px;
position: absolute;
text-align: center;
top: 50%;
}

.item-healthcheck {
span.healthchecks-warning-counter {
position: static;
top: inherit;
margin-left: 5px;
}
}
}
}

0 comments on commit b077d1f

Please sign in to comment.