Skip to content

Commit b49c751

Browse files
Merge pull request #34 from run-as-root/bugfix/fix-magento_indexer_backlog_count_total-values
Fix magento_indexer_backlog_count_total
2 parents 667b452 + b4c6e7e commit b49c751

File tree

4 files changed

+126
-316
lines changed

4 files changed

+126
-316
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ in the table and renders the correct response for prometheus.
4848

4949
The following metrics will be collected:
5050

51-
| Metric | Labels | TYPE | Help |
52-
|:------------------------------------|:--------------------------------|:------|:-----------------------------------------------------------------------------|
53-
| magento_orders_count_total | status, store_code | gauge | All Magento Orders |
54-
| magento_orders_amount_total | status, store_code | gauge | Total amount of all Magento Orders |
55-
| magento_order_items_count_total | status, store_code | gauge | Total count of orderitems |
56-
| magento_cms_block_count_total | store_code | gauge | Total count of available cms blocks |
57-
| magento_cms_page_count_total | store_code | gauge | Total count of available cms pages |
58-
| magento_customer_count_total | store_code | gauge | Total count of available customer |
59-
| magento_cronjob_broken_count_total | | gauge | Broken CronJobs occur when when status is pending but execution_time is set. |
60-
| magento_cronjob_count_total | status, job_code | gauge | Total count of available CronJob Count. |
61-
| magento_indexer_backlog_count_total | isValid, title, status | gauge | Total count of backlog item in indexer. |
62-
| magento_shipments_count_total | source, store_code | counter| Count of Shipments created by store and source. |
63-
| magento_catalog_category_count_total| status, menu_status, store_code | gauge | Count of Categories by store, status and menu status. |
64-
| magento_store_count_total | status | gauge | Total count of Stores by status. |
65-
| magento_website_count_total | | gauge | Total count websites. |
51+
| Metric | Labels | TYPE | Help |
52+
|:-------------------------------------|:--------------------------------|:--------|:--------------------------------------------------------------------------------|
53+
| magento_orders_count_total | status, store_code | gauge | All Magento Orders |
54+
| magento_orders_amount_total | status, store_code | gauge | Total amount of all Magento Orders |
55+
| magento_order_items_count_total | status, store_code | gauge | Total count of orderitems |
56+
| magento_cms_block_count_total | store_code | gauge | Total count of available cms blocks |
57+
| magento_cms_page_count_total | store_code | gauge | Total count of available cms pages |
58+
| magento_customer_count_total | store_code | gauge | Total count of available customer |
59+
| magento_cronjob_broken_count_total | | gauge | Broken CronJobs occur when when status is pending but execution_time is set. |
60+
| magento_cronjob_count_total | status, job_code | gauge | Total count of available CronJob Count. |
61+
| magento_indexer_backlog_count_total | title | gauge | Total count of backlog item in indexer (the data from `indexer:status` command) |
62+
| magento_shipments_count_total | source, store_code | counter | Count of Shipments created by store and source. |
63+
| magento_catalog_category_count_total | status, menu_status, store_code | gauge | Count of Categories by store, status and menu status. |
64+
| magento_store_count_total | status | gauge | Total count of Stores by status. |
65+
| magento_website_count_total | | gauge | Total count websites. |
6666

6767
## Add you own Metric
6868

Test/Unit/Aggregator/Index/IndexerBacklogCountAggregatorTest.php

Lines changed: 51 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -4,260 +4,94 @@
44

55
namespace RunAsRoot\PrometheusExporter\Test\Unit\Aggregator\Index;
66

7-
use Magento\Framework\App\ResourceConnection;
8-
use Magento\Framework\DB\Adapter\AdapterInterface;
9-
use Magento\Framework\DB\Select;
107
use Magento\Framework\Indexer\IndexerInterface;
118
use Magento\Framework\Mview\View\ChangelogInterface;
129
use Magento\Framework\Mview\ViewInterface;
1310
use Magento\Indexer\Model\Indexer\Collection;
1411
use Magento\Indexer\Model\Indexer\CollectionFactory;
12+
use Magento\Indexer\Model\Mview\View\State;
1513
use PHPUnit\Framework\MockObject\MockObject;
1614
use PHPUnit\Framework\TestCase;
17-
use Psr\Log\LoggerInterface;
1815
use RunAsRoot\PrometheusExporter\Aggregator\Index\IndexerBacklogCountAggregator;
1916
use RunAsRoot\PrometheusExporter\Service\UpdateMetricService;
2017
use RunAsRoot\PrometheusExporter\Service\UpdateMetricServiceInterface;
2118

2219
final class IndexerBacklogCountAggregatorTest extends TestCase
2320
{
24-
private const TABLE_CHANGELOG_1 = 'table_name_cl';
25-
private const TABLE_CHANGELOG_2 = 'table_name_2_cl';
2621
private const VIEW_ID_1 = 'some_name';
2722
private const VIEW_ID_2 = 'some_name_another_name';
28-
private const TABLE_MVIEW_STATE = 'mview_state';
2923
private const METRIC_CODE = 'magento_indexer_backlog_count_total';
3024

3125
private IndexerBacklogCountAggregator $sut;
32-
33-
/** @var MockObject|UpdateMetricServiceInterface */
34-
private $updateMetricService;
35-
36-
/** @var MockObject|CollectionFactory */
37-
private $indexerCollectionFactory;
38-
39-
/** @var MockObject|LoggerInterface */
40-
private $logger;
41-
42-
/** @var MockObject|Collection */
43-
private $indexerCollection;
44-
45-
/** @var MockObject|ResourceConnection */
46-
private $resourceConnection;
26+
private MockObject|UpdateMetricServiceInterface $updateMetricService;
27+
private MockObject|Collection $indexerCollection;
4728

4829
protected function setUp(): void
4930
{
5031
$this->updateMetricService = $this->createMock(UpdateMetricService::class);
51-
$this->indexerCollectionFactory = $this->createMock(CollectionFactory::class);
32+
$indexerCollectionFactory = $this->createMock(CollectionFactory::class);
5233
$this->indexerCollection = $this->createMock(Collection::class);
53-
$this->logger = $this->createMock(LoggerInterface::class);
54-
$this->resourceConnection = $this->createMock(ResourceConnection::class);
5534

56-
$this->indexerCollectionFactory->method('create')->willReturn($this->indexerCollection);
35+
$indexerCollectionFactory->method('create')->willReturn($this->indexerCollection);
5736

58-
$this->sut = new IndexerBacklogCountAggregator(
59-
$this->updateMetricService,
60-
$this->indexerCollectionFactory,
61-
$this->resourceConnection,
62-
$this->logger
63-
);
37+
$this->sut = new IndexerBacklogCountAggregator($this->updateMetricService, $indexerCollectionFactory);
6438
}
6539

66-
public function testAggregate(): void
40+
public function testItAggregatesIndexersChangelogCount(): void
6741
{
68-
$this->setUpIndexCollection();
69-
$this->setUpSelects();
70-
71-
$lables1 = [
72-
'isValid' => true,
73-
'title' => 'name',
74-
'status' => 'success',
75-
];
76-
77-
$lables2 = [
78-
'isValid' => true,
79-
'title' => 'other_name',
80-
'status' => 'failed',
81-
];
42+
$indexer1 = $this->createMock(IndexerInterface::class);
43+
$indexer2 = $this->createMock(IndexerInterface::class);
44+
$view1 = $this->createMock(ViewInterface::class);
45+
$view2 = $this->createMock(ViewInterface::class);
46+
$changelog1 = $this->createMock(ChangelogInterface::class);
47+
$changelog2 = $this->createMock(ChangelogInterface::class);
48+
$stateMock1 = $this->createMock(State::class);
49+
$stateMock2 = $this->createMock(State::class);
50+
51+
$testCurrentVersionId1 = '1111';
52+
$testCurrentVersionId2 = '2222';
53+
$testStateVersion1 = '1100';
54+
$testStateVersion2 = '2200';
55+
$testPendingCount1 = 11;
56+
$testPendingCount2 = 22;
57+
58+
$indexer1->method('getTitle')->willReturn(self::VIEW_ID_1);
59+
$indexer2->method('getTitle')->willReturn(self::VIEW_ID_2);
60+
61+
$indexer1->expects($this->once())->method('getView')->willReturn($view1);
62+
$indexer2->expects($this->once())->method('getView')->willReturn($view2);
63+
$view1->expects($this->once())->method('getChangelog')->willReturn($changelog1);
64+
$view2->expects($this->once())->method('getChangelog')->willReturn($changelog2);
65+
$view1->expects($this->once())->method('getState')->willReturn($stateMock1);
66+
$view2->expects($this->once())->method('getState')->willReturn($stateMock2);
67+
68+
$changelog1->method('getVersion')->willReturn($testCurrentVersionId1);
69+
$changelog2->method('getVersion')->willReturn($testCurrentVersionId2);
70+
$stateMock1->method('getVersionId')->willReturn($testStateVersion1);
71+
$stateMock2->method('getVersionId')->willReturn($testStateVersion2);
72+
73+
$changelog1->method('getList')
74+
->with($testStateVersion1, $testCurrentVersionId1)
75+
->willReturn(range(1, $testPendingCount1));
76+
$changelog2->method('getList')
77+
->with($testStateVersion2, $testCurrentVersionId2)
78+
->willReturn(range(1, $testPendingCount2));
79+
80+
$this->indexerCollection->expects($this->once())->method('getItems')->willReturn([ $indexer1, $indexer2 ]);
81+
82+
$lables1 = [ 'title' => self::VIEW_ID_1 ];
83+
$lables2 = [ 'title' => self::VIEW_ID_2 ];
8284

8385
$this->updateMetricService
8486
->expects($this->at(0))
8587
->method('update')
86-
->with(self::METRIC_CODE, '320', $lables1);
88+
->with(self::METRIC_CODE, '11', $lables1);
8789

8890
$this->updateMetricService
8991
->expects($this->at(1))
9092
->method('update')
91-
->with(self::METRIC_CODE, '33', $lables2);
93+
->with(self::METRIC_CODE, '22', $lables2);
9294

9395
$this->sut->aggregate();
9496
}
95-
96-
public function testExceptionBehaviour(): void
97-
{
98-
$this->setUpIndexCollection();
99-
$this->setUpSelects(true);
100-
101-
$this->logger
102-
->expects($this->at(0))
103-
->method('critical')
104-
->with(...['ERROR CTNEE']);
105-
106-
$this->logger
107-
->expects($this->at(1))
108-
->method('critical')
109-
->with(...['ERROR RE']);
110-
111-
$this->updateMetricService
112-
->expects($this->never())
113-
->method('update');
114-
115-
$this->sut->aggregate();
116-
}
117-
118-
private function setUpSelects(bool $throwException = false): void
119-
{
120-
$select1 = $this->createMock(Select::class);
121-
$select2 = $this->createMock(Select::class);
122-
$select3 = $this->createMock(Select::class);
123-
$select4 = $this->createMock(Select::class);
124-
125-
$connection = $this->createMock(AdapterInterface::class);
126-
$this->resourceConnection->expects($this->once())->method('getConnection')
127-
->willReturn($connection);
128-
$connection->expects($this->exactly(4))
129-
->method('select')
130-
->will($this->onConsecutiveCalls($select1, $select2, $select3, $select4));
131-
$connection->expects($this->exactly(4))
132-
->method('getTableName')
133-
->willReturnMap(
134-
[
135-
[self::TABLE_CHANGELOG_1, self::TABLE_CHANGELOG_1],
136-
[self::TABLE_CHANGELOG_2, self::TABLE_CHANGELOG_2],
137-
[self::TABLE_MVIEW_STATE, self::TABLE_MVIEW_STATE]
138-
]
139-
);
140-
$select1 = $this->setUpSelectChangelog($select1, self::TABLE_CHANGELOG_1);
141-
$select2 = $this->setUpSelectMviewState($select2, self::VIEW_ID_1);
142-
$select3 = $this->setUpSelectChangelog($select3, self::TABLE_CHANGELOG_2);
143-
$select4 = $this->setUpSelectMviewState($select4, self::VIEW_ID_2);
144-
145-
if (!$throwException) {
146-
$connection->expects($this->exactly(4))
147-
->method('fetchOne')
148-
->willReturnMap(
149-
[
150-
[$select1, [], 550],
151-
[$select2, [], 230],
152-
[$select3, [], 99],
153-
[$select4, [], 66]
154-
]
155-
);
156-
} else {
157-
$connection->expects($this->exactly(4))
158-
->method('fetchOne')
159-
->will(
160-
$this->returnCallback(function ($arg) use ($select2, $select4) {
161-
if (spl_object_id($select2) === spl_object_id($arg)) {
162-
throw new \Zend_Db_Exception('ERROR CTNEE');
163-
} elseif (spl_object_id($select4) === spl_object_id($arg)) {
164-
throw new \Zend_Db_Exception('ERROR RE');
165-
} else {
166-
return 20;
167-
}
168-
})
169-
);
170-
}
171-
}
172-
173-
private function setUpSelectMviewState(Select $select, string $viewId): Select
174-
{
175-
$select->expects($this->once())->method('from')->with(self::TABLE_MVIEW_STATE)
176-
->willReturn($select);
177-
$select->expects($this->once())->method('reset')->with(Select::COLUMNS)
178-
->willReturn($select);
179-
$select->expects($this->once())->method('where')->with('view_id = ?', $viewId)
180-
->willReturn($select);
181-
$select->expects($this->once())->method('columns')->with(['version_id'])
182-
->willReturn($select);
183-
184-
return $select;
185-
}
186-
187-
private function setUpSelectChangelog(Select $select, string $changelogTableName): Select
188-
{
189-
$select->expects($this->once())->method('from')->with($changelogTableName)
190-
->willReturn($select);
191-
$select->expects($this->once())->method('reset')->with(Select::COLUMNS)
192-
->willReturn($select);
193-
$select->expects($this->once())->method('order')->with('version_id DESC')
194-
->willReturn($select);
195-
$select->expects($this->once())->method('columns')->with(['version_id'])
196-
->willReturn($select);
197-
198-
return $select;
199-
}
200-
201-
private function setUpIndexCollection(): void
202-
{
203-
$indexer1 = $this->createMock(IndexerInterface::class);
204-
$indexer2 = $this->createMock(IndexerInterface::class);
205-
$view1 = $this->createMock(ViewInterface::class);
206-
$view2 = $this->createMock(ViewInterface::class);
207-
$changelog1 = $this->createMock(ChangelogInterface::class);
208-
$changelog2 = $this->createMock(ChangelogInterface::class);
209-
210-
$indexer1->method('isValid')->willReturn(true);
211-
$indexer1->method('getTitle')->willReturn('name');
212-
$indexer1->method('getStatus')->willReturn('success');
213-
214-
$indexer2->method('isValid')->willReturn(true);
215-
$indexer2->method('getTitle')->willReturn('other_name');
216-
$indexer2->method('getStatus')->willReturn('failed');
217-
218-
$indexer1
219-
->expects($this->once())
220-
->method('getView')
221-
->willReturn($view1);
222-
223-
$indexer2
224-
->expects($this->once())
225-
->method('getView')
226-
->willReturn($view2);
227-
228-
$view1
229-
->expects($this->once())
230-
->method('getChangelog')
231-
->willReturn($changelog1);
232-
233-
$view2
234-
->expects($this->once())
235-
->method('getChangelog')
236-
->willReturn($changelog2);
237-
238-
$view1
239-
->expects($this->once())
240-
->method('getId')
241-
->willReturn(self::VIEW_ID_1);
242-
243-
$view2
244-
->expects($this->once())
245-
->method('getId')
246-
->willReturn(self::VIEW_ID_2);
247-
248-
$changelog1
249-
->expects($this->once())
250-
->method('getName')
251-
->willReturn(self::TABLE_CHANGELOG_1);
252-
253-
$changelog2
254-
->expects($this->once())
255-
->method('getName')
256-
->willReturn(self::TABLE_CHANGELOG_2);
257-
258-
$this->indexerCollection
259-
->expects($this->once())
260-
->method('getItems')
261-
->willReturn([$indexer1, $indexer2]);
262-
}
26397
}

0 commit comments

Comments
 (0)