Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Add magento_website_count_total metric.
- add unit tests coverage.
  • Loading branch information
swnsma committed Nov 13, 2021
1 parent 44b753a commit fbcbf53
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Test/Unit/Aggregator/Store/WebsiteCountAggregatorTest.php
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();
}
}
54 changes: 54 additions & 0 deletions src/Aggregator/Store/WebsiteCountAggregator.php
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);
}
}
1 change: 1 addition & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

<!-- Store Aggregator -->
<item name="StoreCountAggregator" xsi:type="object">RunAsRoot\PrometheusExporter\Aggregator\Store\StoreCountAggregator</item>
<item name="WebsiteCountAggregator" xsi:type="object">RunAsRoot\PrometheusExporter\Aggregator\Store\WebsiteCountAggregator</item>

<!-- User Aggregator -->
<item name="AdminUserCountAggregator" xsi:type="object">RunAsRoot\PrometheusExporter\Aggregator\User\AdminUserCountAggregator</item>
Expand Down

0 comments on commit fbcbf53

Please sign in to comment.