8
8
use Magento \Framework \Exception \NoSuchEntityException ;
9
9
use Magento \Sales \Api \OrderRepositoryInterface ;
10
10
use Magento \Store \Api \StoreRepositoryInterface ;
11
+ use RunAsRoot \PrometheusExporter \Api \Data \MetricInterface ;
11
12
use RunAsRoot \PrometheusExporter \Api \MetricAggregatorInterface ;
12
- use RunAsRoot \PrometheusExporter \Service \ UpdateMetricService ;
13
+ use RunAsRoot \PrometheusExporter \Repository \ MetricRepository ;
13
14
use RunAsRoot \PrometheusExporter \Service \UpdateMetricServiceInterface ;
14
15
use function array_key_exists ;
15
16
16
17
class OrderAmountAggregator implements MetricAggregatorInterface
17
18
{
18
19
private const METRIC_CODE = 'magento_orders_amount_total ' ;
19
20
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 ;
24
26
25
27
public function __construct (
26
- UpdateMetricServiceInterface $ updateMetricService ,
28
+ MetricRepository $ metricRepository ,
27
29
OrderRepositoryInterface $ orderRepository ,
30
+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
28
31
StoreRepositoryInterface $ storeRepository ,
29
- SearchCriteriaBuilder $ searchCriteriaBuilder
32
+ UpdateMetricServiceInterface $ updateMetricService
30
33
) {
31
- $ this ->updateMetricService = $ updateMetricService ;
34
+ $ this ->metricRepository = $ metricRepository ;
32
35
$ this ->orderRepository = $ orderRepository ;
33
36
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
34
37
$ this ->storeRepository = $ storeRepository ;
38
+ $ this ->updateMetricService = $ updateMetricService ;
35
39
}
36
40
37
41
public function getCode (): string
@@ -51,8 +55,9 @@ public function getType(): string
51
55
52
56
public function aggregate (): bool
53
57
{
54
- $ searchCriteria = $ this ->searchCriteriaBuilder -> create ();
58
+ $ this ->resetMetrics ();
55
59
60
+ $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
56
61
$ orderSearchResult = $ this ->orderRepository ->getList ($ searchCriteria );
57
62
58
63
if ($ orderSearchResult ->getTotalCount () === 0 ) {
@@ -86,12 +91,24 @@ public function aggregate(): bool
86
91
87
92
foreach ($ grandTotalsByStore as $ storeCode => $ grandTotals ) {
88
93
foreach ($ grandTotals as $ state => $ grandTotal ) {
89
- $ labels = [ 'state ' => $ state , 'store_code ' => $ storeCode ];
94
+ $ labels = ['state ' => $ state , 'store_code ' => $ storeCode ];
90
95
91
96
$ this ->updateMetricService ->update (self ::METRIC_CODE , (string )$ grandTotal , $ labels );
92
97
}
93
98
}
94
99
95
100
return true ;
96
101
}
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
+ }
97
114
}
0 commit comments