-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HealthChecks] Admin nav. menu decoration by number of warnings
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/module-elasticsuite-core/Block/Adminhtml/Healthcheck/MenuDecorator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/module-elasticsuite-core/view/adminhtml/templates/healthcheck/menu_decorator.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters