From b077d1ff32edef4f1352e9c5264a4bc5b5da3b9d Mon Sep 17 00:00:00 2001 From: Richard BAYET Date: Thu, 16 Jan 2025 12:19:31 +0100 Subject: [PATCH] [HealthChecks] Admin nav. menu decoration by number of warnings --- .../Adminhtml/Healthcheck/MenuDecorator.php | 95 +++++++++++++++++++ .../view/adminhtml/layout/default.xml | 3 + .../healthcheck/menu_decorator.phtml | 28 ++++++ .../adminhtml/web/css/source/_module.less | 30 ++++++ 4 files changed, 156 insertions(+) create mode 100644 src/module-elasticsuite-core/Block/Adminhtml/Healthcheck/MenuDecorator.php create mode 100644 src/module-elasticsuite-core/view/adminhtml/templates/healthcheck/menu_decorator.phtml diff --git a/src/module-elasticsuite-core/Block/Adminhtml/Healthcheck/MenuDecorator.php b/src/module-elasticsuite-core/Block/Adminhtml/Healthcheck/MenuDecorator.php new file mode 100644 index 000000000..91a9456f7 --- /dev/null +++ b/src/module-elasticsuite-core/Block/Adminhtml/Healthcheck/MenuDecorator.php @@ -0,0 +1,95 @@ + + * @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; + } +} diff --git a/src/module-elasticsuite-core/view/adminhtml/layout/default.xml b/src/module-elasticsuite-core/view/adminhtml/layout/default.xml index b87da116a..cc09a5c98 100644 --- a/src/module-elasticsuite-core/view/adminhtml/layout/default.xml +++ b/src/module-elasticsuite-core/view/adminhtml/layout/default.xml @@ -26,6 +26,9 @@ + diff --git a/src/module-elasticsuite-core/view/adminhtml/templates/healthcheck/menu_decorator.phtml b/src/module-elasticsuite-core/view/adminhtml/templates/healthcheck/menu_decorator.phtml new file mode 100644 index 000000000..5885c59d3 --- /dev/null +++ b/src/module-elasticsuite-core/view/adminhtml/templates/healthcheck/menu_decorator.phtml @@ -0,0 +1,28 @@ + + * @copyright 2025 Smile + * @license Open Software License ("OSL") v. 3.0 + */ + +/** @var \Smile\ElasticsuiteCore\Block\Adminhtml\Healthcheck\MenuDecorator $block */ +?> +isEnabled()): ?> + + diff --git a/src/module-elasticsuite-core/view/adminhtml/web/css/source/_module.less b/src/module-elasticsuite-core/view/adminhtml/web/css/source/_module.less index 620cca5bd..2fc833330 100644 --- a/src/module-elasticsuite-core/view/adminhtml/web/css/source/_module.less +++ b/src/module-elasticsuite-core/view/adminhtml/web/css/source/_module.less @@ -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; + } + } + } +} +