forked from run-as-root/magento2-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add magento_website_count_total metric. - add unit tests coverage.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RunAsRoot\PrometheusExporter\Test\Unit\Aggregator\Store; | ||
|
||
use Magento\Store\Api\WebsiteRepositoryInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use RunAsRoot\PrometheusExporter\Aggregator\Store\WebsiteCountAggregator; | ||
use RunAsRoot\PrometheusExporter\Service\UpdateMetricService; | ||
|
||
final class WebsiteCountAggregatorTest extends TestCase | ||
{ | ||
private const METRIC_CODE = 'magento_website_count_total'; | ||
|
||
private WebsiteCountAggregator $sut; | ||
|
||
/** @var MockObject|UpdateMetricService */ | ||
private $updateMetricService; | ||
|
||
/** @var MockObject|WebsiteRepositoryInterface */ | ||
private $websiteRepository; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->updateMetricService = $this->createMock(UpdateMetricService::class); | ||
$this->websiteRepository = $this->createMock(WebsiteRepositoryInterface::class); | ||
|
||
$this->sut = new WebsiteCountAggregator( | ||
$this->updateMetricService, | ||
$this->websiteRepository | ||
); | ||
} | ||
|
||
public function testAggregate(): void | ||
{ | ||
$this->websiteRepository | ||
->expects($this->once()) | ||
->method('getList') | ||
->willReturn(['a', 'b', 'c', 'd', '3']); | ||
|
||
$this->updateMetricService | ||
->expects($this->once()) | ||
->method('update') | ||
->with(self::METRIC_CODE, '5'); | ||
|
||
$this->sut->aggregate(); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RunAsRoot\PrometheusExporter\Aggregator\Store; | ||
|
||
use Magento\Store\Api\StoreRepositoryInterface; | ||
use Magento\Store\Api\WebsiteRepositoryInterface; | ||
use RunAsRoot\PrometheusExporter\Api\MetricAggregatorInterface; | ||
use RunAsRoot\PrometheusExporter\Service\UpdateMetricService; | ||
|
||
class WebsiteCountAggregator implements MetricAggregatorInterface | ||
{ | ||
private const METRIC_CODE = 'magento_website_count_total'; | ||
|
||
private UpdateMetricService $updateMetricService; | ||
|
||
private WebsiteRepositoryInterface $websiteRepository; | ||
|
||
/** | ||
* @param UpdateMetricService $updateMetricService | ||
* @param WebsiteRepositoryInterface $websiteRepository | ||
*/ | ||
public function __construct( | ||
UpdateMetricService $updateMetricService, | ||
WebsiteRepositoryInterface $websiteRepository | ||
) { | ||
$this->updateMetricService = $updateMetricService; | ||
$this->websiteRepository = $websiteRepository; | ||
} | ||
|
||
public function getCode(): string | ||
{ | ||
return self::METRIC_CODE; | ||
} | ||
|
||
public function getHelp(): string | ||
{ | ||
return 'Magento 2 Website Count.'; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return 'gauge'; | ||
} | ||
|
||
public function aggregate(): bool | ||
{ | ||
$websiteList = $this->websiteRepository->getList(); | ||
$websiteCount = (string)count($websiteList); | ||
|
||
return $this->updateMetricService->update(self::METRIC_CODE, $websiteCount); | ||
} | ||
} |
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