Skip to content

Commit 586009b

Browse files
authored
Merge pull request #3492 from rbayet/feat-healthchecks-menu-decorator
[HealthChecks] Admin nav. menu decoration by number of warnings
2 parents 0ea1027 + 8e84bdd commit 586009b

File tree

22 files changed

+394
-187
lines changed

22 files changed

+394
-187
lines changed

src/module-elasticsuite-core/Block/Adminhtml/Healthcheck/Healthcheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ public function __construct(Context $context, HealthcheckList $healthcheckList,
5656
*/
5757
public function getHealthchecks(): array
5858
{
59-
return $this->healthcheckList->getChecks();
59+
return array_values($this->healthcheckList->getCheckResults());
6060
}
6161
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* DISCLAIMER
4+
*
5+
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
6+
*
7+
* @category Smile
8+
* @package Smile\ElasticsuiteCore
9+
* @author Richard BAYET <[email protected]>
10+
* @copyright 2025 Smile
11+
* @license Open Software License ("OSL") v. 3.0
12+
*/
13+
14+
namespace Smile\ElasticsuiteCore\Block\Adminhtml\Healthcheck;
15+
16+
use Magento\Backend\Block\Template;
17+
use Magento\Directory\Helper\Data as DirectoryHelper;
18+
use Magento\Framework\Json\Helper\Data as JsonHelper;
19+
use Smile\ElasticsuiteCore\Api\Healthcheck\CheckInterface;
20+
use Smile\ElasticsuiteCore\Model\Healthcheck\HealthcheckList;
21+
22+
/**
23+
* Elasticsuite menu decorator block.
24+
* Adds a failed/warning healthchecks counter next to Elasticsuite elements in the menu.
25+
*
26+
* @category Smile
27+
* @package Smile\ElasticsuiteCore
28+
*/
29+
class MenuDecorator extends Template
30+
{
31+
/** @var HealthcheckList */
32+
private $healthcheckList;
33+
34+
/** @var integer */
35+
private $issuesCount;
36+
37+
/**
38+
* Constructor.
39+
*
40+
* @param HealthcheckList $healthcheckList Healthchecks list.
41+
* @param Template\Context $context Template context.
42+
* @param array $data Data.
43+
* @param JsonHelper|null $jsonHelper Json helper.
44+
* @param DirectoryHelper|null $directoryHelper Directory helper.
45+
*/
46+
public function __construct(
47+
HealthcheckList $healthcheckList,
48+
Template\Context $context,
49+
array $data = [],
50+
?JsonHelper $jsonHelper = null,
51+
?DirectoryHelper $directoryHelper = null
52+
) {
53+
parent::__construct($context, $data, $jsonHelper, $directoryHelper);
54+
$this->healthcheckList = $healthcheckList;
55+
}
56+
57+
/**
58+
* Returns true if the menu decoration should happen.
59+
*
60+
* @return bool
61+
*/
62+
public function isEnabled()
63+
{
64+
return $this->getIssuesCount() > 0;
65+
}
66+
67+
/**
68+
* Returns the number of failed tests.
69+
*
70+
* @return int
71+
*/
72+
public function getIssuesCount()
73+
{
74+
if (null === $this->issuesCount) {
75+
$this->issuesCount = 0;
76+
77+
foreach ($this->healthcheckList->getCheckResults() as $check) {
78+
if ($check->getStatus() === CheckInterface::STATUS_FAILED) {
79+
$this->issuesCount++;
80+
}
81+
}
82+
}
83+
84+
return $this->issuesCount;
85+
}
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
protected function getCacheLifetime()
91+
{
92+
// Very short cache TTL until a proper cache mechanism is set up at the healthcheck list level.
93+
return 60;
94+
}
95+
96+
/**
97+
* {@inheritDoc}
98+
*/
99+
protected function getCacheTags()
100+
{
101+
return array_merge(parent::getCacheTags(), [HealthcheckList::CACHE_TAG]);
102+
}
103+
}

src/module-elasticsuite-core/Controller/Adminhtml/Healthcheck/Index.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,54 @@
1414

1515
namespace Smile\ElasticsuiteCore\Controller\Adminhtml\Healthcheck;
1616

17+
use Magento\Backend\App\Action;
18+
use Magento\Backend\App\Action\Context;
1719
use Magento\Framework\App\Action\HttpGetActionInterface;
1820
use Magento\Framework\Controller\ResultFactory;
1921
use Magento\Framework\View\Result\Page;
20-
use Smile\ElasticsuiteIndices\Controller\Adminhtml\AbstractAction;
22+
use Smile\ElasticsuiteCore\Model\Healthcheck\HealthcheckList;
23+
use Magento\Framework\App\CacheInterface;
2124

2225
/**
2326
* Class Index.
2427
*/
25-
class Index extends AbstractAction implements HttpGetActionInterface
28+
class Index extends Action implements HttpGetActionInterface
2629
{
30+
/**
31+
* Authorization level of a basic admin session.
32+
*
33+
* @see _isAllowed()
34+
*/
35+
public const ADMIN_RESOURCE = 'Smile_ElasticsuiteCore::healthcheck';
36+
37+
/** @var CacheInterface */
38+
private $cache;
39+
40+
/**
41+
* Constructor.
42+
*
43+
* @param CacheInterface $cache App cache.
44+
* @param Context $context Context.
45+
*
46+
*/
47+
public function __construct(
48+
CacheInterface $cache,
49+
Context $context
50+
) {
51+
parent::__construct($context);
52+
$this->cache = $cache;
53+
}
54+
2755
/**
2856
* @inheritdoc
2957
*
3058
* @return Page
3159
*/
3260
public function execute(): Page
3361
{
62+
// Refresh the cache so the menu decorator is refreshed if need be.
63+
$this->cache->clean(HealthcheckList::CACHE_TAG);
64+
3465
$breadMain = __('Healthcheck');
3566

3667
/** @var Page $resultPage */
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* DISCLAIMER
4+
*
5+
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
6+
*
7+
* @category Smile
8+
* @package Smile\ElasticsuiteCore
9+
* @author Richard BAYET <[email protected]>
10+
* @copyright 2025 Smile
11+
* @license Open Software License ("OSL") v. 3.0
12+
*/
13+
14+
namespace Smile\ElasticsuiteCore\Model\Healthcheck;
15+
16+
use Smile\ElasticsuiteCore\Api\Healthcheck\CheckInterface;
17+
18+
/**
19+
* Cached healthcheck.
20+
*
21+
* @category Smile
22+
* @package Smile\ElasticsuiteCore
23+
*/
24+
class CachedHealthcheck implements CheckInterface
25+
{
26+
/** @var string */
27+
private $identifier;
28+
29+
/** @var string */
30+
private $status;
31+
32+
/** @var string */
33+
private $description;
34+
35+
/**
36+
* Constructor.
37+
*
38+
* @param string $identifier Check identifier.
39+
* @param string $status Check status.
40+
* @param string $description Check description.
41+
*/
42+
public function __construct($identifier, $status, $description)
43+
{
44+
$this->identifier = $identifier;
45+
$this->status = $status;
46+
$this->description = $description;
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function getIdentifier(): string
53+
{
54+
return $this->identifier;
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function getStatus(): string
61+
{
62+
return $this->status;
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
public function getDescription(): string
69+
{
70+
return $this->description;
71+
}
72+
}

src/module-elasticsuite-core/Model/Healthcheck/HealthcheckList.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Smile\ElasticsuiteCore\Model\Healthcheck;
1616

1717
use Smile\ElasticsuiteCore\Api\Healthcheck\CheckInterface;
18+
use Smile\ElasticsuiteCore\Model\Healthcheck\CachedHealthcheckFactory;
1819

1920
/**
2021
* Class HealthcheckList.
@@ -23,20 +24,37 @@
2324
*/
2425
class HealthcheckList
2526
{
27+
/** @var string */
28+
const CACHE_TAG = 'healthcheck_list';
29+
30+
/** @var CachedHealthcheckFactory */
31+
private $cachedChecksFactory;
32+
2633
/**
2734
* Array of health checks implementing the CheckInterface.
2835
*
2936
* @var CheckInterface[]
3037
*/
3138
private $checks;
3239

40+
/**
41+
* Array of executed healthchecks.
42+
*
43+
* @var CheckInterface[]
44+
*/
45+
private $checkResults;
46+
3347
/**
3448
* Constructor.
3549
*
36-
* @param CheckInterface[] $checks Array of health checks to be managed by this list.
50+
* @param CachedHealthcheckFactory $cachedChecksFactory Cached healthcheck factory.
51+
* @param CheckInterface[] $checks Array of health checks to be managed by this list.
3752
*/
38-
public function __construct(array $checks = [])
39-
{
53+
public function __construct(
54+
CachedHealthcheckFactory $cachedChecksFactory,
55+
array $checks = []
56+
) {
57+
$this->cachedChecksFactory = $cachedChecksFactory;
4058
$this->checks = $checks;
4159
}
4260

@@ -56,4 +74,24 @@ public function getChecks(): array
5674

5775
return $this->checks;
5876
}
77+
78+
/**
79+
* Retrieve all executed health checks, sorted by their sort order.
80+
*
81+
* @return CheckInterface[]
82+
*/
83+
public function getCheckResults(): array
84+
{
85+
if (null === $this->checkResults) {
86+
foreach ($this->getChecks() as $check) {
87+
$this->checkResults[$check->getIdentifier()] = $this->cachedChecksFactory->create([
88+
'identifier' => $check->getIdentifier(),
89+
'status' => $check->getStatus(),
90+
'description' => $check->getDescription(),
91+
]);
92+
}
93+
}
94+
95+
return $this->checkResults;
96+
}
5997
}

0 commit comments

Comments
 (0)