Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- adjust 'magento_store_count_total' metric;
- add status label to stores count;
- adjust unit tests coverage;
  • Loading branch information
swnsma committed Nov 13, 2021
1 parent fc06fbe commit 44b753a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
22 changes: 18 additions & 4 deletions Test/Unit/Aggregator/Store/StoreCountAggregatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RunAsRoot\PrometheusExporter\Test\Unit\Aggregator\Module;

use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -12,6 +13,8 @@

final class StoreCountAggregatorTest extends TestCase
{
private const METRIC_CODE = 'magento_store_count_total';

/** @var StoreCountAggregator */
private $sut;

Expand All @@ -21,7 +24,7 @@ final class StoreCountAggregatorTest extends TestCase
/** @var MockObject|StoreRepositoryInterface */
private $storeRepository;

protected function setUp()
protected function setUp(): void
{
$this->updateMetricService = $this->createMock(UpdateMetricService::class);
$this->storeRepository = $this->createMock(StoreRepositoryInterface::class);
Expand All @@ -34,15 +37,26 @@ protected function setUp()

public function testAggregate(): void
{
$store1 = $this->createMock(StoreInterface::class);
$store2 = $this->createMock(StoreInterface::class);
$store3 = $this->createMock(StoreInterface::class);

$store1->expects($this->once())->method('getIsActive')->willReturn(1);
$store2->expects($this->once())->method('getIsActive')->willReturn(0);
$store3->expects($this->once())->method('getIsActive')->willReturn(1);

$this->storeRepository
->expects($this->once())
->method('getList')
->willReturn(['a', 'b', 'c']);
->willReturn([$store1, $store2, $store3]);

$this->updateMetricService
->expects($this->once())
->expects($this->exactly(2))
->method('update')
->with(...['magento_store_count_total', '3']);
->withConsecutive(
[self::METRIC_CODE, '2', ['status' => 'enabled']],
[self::METRIC_CODE, '1', ['status' => 'disabled']],
);

$this->sut->aggregate();
}
Expand Down
19 changes: 14 additions & 5 deletions src/Aggregator/Store/StoreCountAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class StoreCountAggregator implements MetricAggregatorInterface
{
private const METRIC_CODE = 'magento_store_count_total';

private $updateMetricService;
private UpdateMetricService $updateMetricService;

private $storeRepository;
private StoreRepositoryInterface $storeRepository;

public function __construct(
UpdateMetricService $updateMetricService,
Expand All @@ -31,7 +31,7 @@ public function getCode(): string

public function getHelp(): string
{
return 'Magento 2 Store Count';
return 'Magento 2 Store Count by status.';
}

public function getType(): string
Expand All @@ -42,8 +42,17 @@ public function getType(): string
public function aggregate(): bool
{
$storeList = $this->storeRepository->getList();
$storeCount = (string)count($storeList);

return $this->updateMetricService->update(self::METRIC_CODE, $storeCount);
$active = 0;
$disabled = 0;

foreach ($storeList as $store) {
$store->getIsActive() ? $active++ : $disabled++;
}

$this->updateMetricService->update(self::METRIC_CODE, (string)$active, ['status' => 'enabled']);
$this->updateMetricService->update(self::METRIC_CODE, (string)$disabled, ['status' => 'disabled']);

return true;
}
}

0 comments on commit 44b753a

Please sign in to comment.