Skip to content

Commit 2a39e2a

Browse files
Merge pull request #23 from marvincaspar/master
Reset order count and order amount metrics
2 parents 58fc142 + 25ed820 commit 2a39e2a

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
66
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.0.2] - 2021-06-11
9+
10+
### Fixed
11+
12+
- Reset order count/amount metric before setting them
13+
814
## [2.0.1] - 2021-05-31
915

1016
### Fixed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "run_as_root/magento2-prometheus-exporter",
33
"description": "Magento2 Prometheus Exporter",
4-
"version": "2.0.1",
4+
"version": "2.0.2",
55
"type": "magento2-module",
66
"license": "MIT",
77
"homepage": "https://github.com/run-as-root/magento2-prometheus-exporter",
@@ -45,4 +45,4 @@
4545
"RunAsRoot\\PrometheusExporter\\Test\\": "Test/"
4646
}
4747
}
48-
}
48+
}

src/Aggregator/Order/OrderAmountAggregator.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,34 @@
88
use Magento\Framework\Exception\NoSuchEntityException;
99
use Magento\Sales\Api\OrderRepositoryInterface;
1010
use Magento\Store\Api\StoreRepositoryInterface;
11+
use RunAsRoot\PrometheusExporter\Api\Data\MetricInterface;
1112
use RunAsRoot\PrometheusExporter\Api\MetricAggregatorInterface;
12-
use RunAsRoot\PrometheusExporter\Service\UpdateMetricService;
13+
use RunAsRoot\PrometheusExporter\Repository\MetricRepository;
1314
use RunAsRoot\PrometheusExporter\Service\UpdateMetricServiceInterface;
1415
use function array_key_exists;
1516

1617
class OrderAmountAggregator implements MetricAggregatorInterface
1718
{
1819
private const METRIC_CODE = 'magento_orders_amount_total';
1920

20-
private $updateMetricService;
21-
private $orderRepository;
22-
private $searchCriteriaBuilder;
23-
private $storeRepository;
21+
private MetricRepository $metricRepository;
22+
private OrderRepositoryInterface $orderRepository;
23+
private SearchCriteriaBuilder $searchCriteriaBuilder;
24+
private StoreRepositoryInterface $storeRepository;
25+
private UpdateMetricServiceInterface $updateMetricService;
2426

2527
public function __construct(
26-
UpdateMetricServiceInterface $updateMetricService,
28+
MetricRepository $metricRepository,
2729
OrderRepositoryInterface $orderRepository,
30+
SearchCriteriaBuilder $searchCriteriaBuilder,
2831
StoreRepositoryInterface $storeRepository,
29-
SearchCriteriaBuilder $searchCriteriaBuilder
32+
UpdateMetricServiceInterface $updateMetricService
3033
) {
31-
$this->updateMetricService = $updateMetricService;
34+
$this->metricRepository = $metricRepository;
3235
$this->orderRepository = $orderRepository;
3336
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
3437
$this->storeRepository = $storeRepository;
38+
$this->updateMetricService = $updateMetricService;
3539
}
3640

3741
public function getCode(): string
@@ -51,8 +55,9 @@ public function getType(): string
5155

5256
public function aggregate(): bool
5357
{
54-
$searchCriteria = $this->searchCriteriaBuilder->create();
58+
$this->resetMetrics();
5559

60+
$searchCriteria = $this->searchCriteriaBuilder->create();
5661
$orderSearchResult = $this->orderRepository->getList($searchCriteria);
5762

5863
if ($orderSearchResult->getTotalCount() === 0) {
@@ -86,12 +91,24 @@ public function aggregate(): bool
8691

8792
foreach ($grandTotalsByStore as $storeCode => $grandTotals) {
8893
foreach ($grandTotals as $state => $grandTotal) {
89-
$labels = [ 'state' => $state, 'store_code' => $storeCode ];
94+
$labels = ['state' => $state, 'store_code' => $storeCode];
9095

9196
$this->updateMetricService->update(self::METRIC_CODE, (string)$grandTotal, $labels);
9297
}
9398
}
9499

95100
return true;
96101
}
102+
103+
protected function resetMetrics(): void
104+
{
105+
$searchCriteriaMetrics = $this->searchCriteriaBuilder->addFilter('code', self::METRIC_CODE)->create();
106+
$metricsSearchResult = $this->metricRepository->getList($searchCriteriaMetrics);
107+
$metrics = $metricsSearchResult->getItems();
108+
/** @var MetricInterface $metric */
109+
foreach ($metrics as $metric) {
110+
$metric->setValue("0");
111+
$this->metricRepository->save($metric);
112+
}
113+
}
97114
}

src/Aggregator/Order/OrderCountAggregator.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,34 @@
88
use Magento\Framework\Exception\NoSuchEntityException;
99
use Magento\Sales\Api\OrderRepositoryInterface;
1010
use Magento\Store\Api\StoreRepositoryInterface;
11+
use RunAsRoot\PrometheusExporter\Api\Data\MetricInterface;
1112
use RunAsRoot\PrometheusExporter\Api\MetricAggregatorInterface;
13+
use RunAsRoot\PrometheusExporter\Repository\MetricRepository;
1214
use RunAsRoot\PrometheusExporter\Service\UpdateMetricService;
1315
use function array_key_exists;
1416

1517
class OrderCountAggregator implements MetricAggregatorInterface
1618
{
1719
private const METRIC_CODE = 'magento_orders_count_total';
1820

19-
private $updateMetricService;
20-
private $orderRepository;
21-
private $searchCriteriaBuilder;
22-
private $storeRepository;
21+
private MetricRepository $metricRepository;
22+
private OrderRepositoryInterface $orderRepository;
23+
private SearchCriteriaBuilder $searchCriteriaBuilder;
24+
private StoreRepositoryInterface $storeRepository;
25+
private UpdateMetricService $updateMetricService;
2326

2427
public function __construct(
25-
UpdateMetricService $updateMetricService,
28+
MetricRepository $metricRepository,
2629
OrderRepositoryInterface $orderRepository,
30+
SearchCriteriaBuilder $searchCriteriaBuilder,
2731
StoreRepositoryInterface $storeRepository,
28-
SearchCriteriaBuilder $searchCriteriaBuilder
32+
UpdateMetricService $updateMetricService
2933
) {
30-
$this->updateMetricService = $updateMetricService;
34+
$this->metricRepository = $metricRepository;
3135
$this->orderRepository = $orderRepository;
3236
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
3337
$this->storeRepository = $storeRepository;
38+
$this->updateMetricService = $updateMetricService;
3439
}
3540

3641
public function getCode(): string
@@ -50,8 +55,9 @@ public function getType(): string
5055

5156
public function aggregate(): bool
5257
{
53-
$searchCriteria = $this->searchCriteriaBuilder->create();
58+
$this->resetMetrics();
5459

60+
$searchCriteria = $this->searchCriteriaBuilder->create();
5561
$orderSearchResult = $this->orderRepository->getList($searchCriteria);
5662

5763
if ($orderSearchResult->getTotalCount() === 0) {
@@ -85,12 +91,24 @@ public function aggregate(): bool
8591

8692
foreach ($countByStore as $storeCode => $countByState) {
8793
foreach ($countByState as $state => $count) {
88-
$labels = [ 'state' => $state, 'store_code' => $storeCode ];
94+
$labels = ['state' => $state, 'store_code' => $storeCode];
8995

9096
$this->updateMetricService->update(self::METRIC_CODE, (string)$count, $labels);
9197
}
9298
}
9399

94100
return true;
95101
}
102+
103+
protected function resetMetrics(): void
104+
{
105+
$searchCriteriaMetrics = $this->searchCriteriaBuilder->addFilter('code', self::METRIC_CODE)->create();
106+
$metricsSearchResult = $this->metricRepository->getList($searchCriteriaMetrics);
107+
$metrics = $metricsSearchResult->getItems();
108+
/** @var MetricInterface $metric */
109+
foreach ($metrics as $metric) {
110+
$metric->setValue("0");
111+
$this->metricRepository->save($metric);
112+
}
113+
}
96114
}

0 commit comments

Comments
 (0)