Skip to content

Commit da72054

Browse files
committed
MC-14937: Complete Page Builder Analytics data collection
- Refactor implementation to use batching system
1 parent 328a339 commit da72054

File tree

8 files changed

+388
-103
lines changed

8 files changed

+388
-103
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilderAnalytics\Model;
10+
11+
use Magento\Analytics\ReportXml\QueryFactory;
12+
13+
/**
14+
* Collect analytics data for Catalog Categories
15+
*/
16+
class CatalogCategoryProvider
17+
{
18+
/**
19+
* @var QueryFactory
20+
*/
21+
private $queryFactory;
22+
23+
/**
24+
* @var ContentTypeBatchReportBuilder
25+
*/
26+
private $reportBuilder;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $queryName;
32+
33+
/**
34+
* @param QueryFactory $queryFactory
35+
* @param ContentTypeBatchReportBuilder $reportBuilder
36+
* @param string $queryName
37+
*/
38+
public function __construct(
39+
QueryFactory $queryFactory,
40+
ContentTypeBatchReportBuilder $reportBuilder,
41+
$queryName
42+
) {
43+
$this->queryFactory = $queryFactory;
44+
$this->reportBuilder = $reportBuilder;
45+
$this->queryName = $queryName;
46+
}
47+
48+
/**
49+
* Build the report
50+
*
51+
* @return \IteratorIterator
52+
* @throws \Zend_Db_Statement_Exception
53+
*/
54+
public function getReport() : \IteratorIterator
55+
{
56+
return $this->reportBuilder->create(
57+
$this->queryFactory->create($this->queryName)
58+
);
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilderAnalytics\Model;
10+
11+
use Magento\Analytics\ReportXml\QueryFactory;
12+
13+
/**
14+
* Collect analytics data for Catalog Products
15+
*/
16+
class CatalogProductProvider
17+
{
18+
/**
19+
* @var QueryFactory
20+
*/
21+
private $queryFactory;
22+
23+
/**
24+
* @var ContentTypeBatchReportBuilder
25+
*/
26+
private $reportBuilder;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $queryName;
32+
33+
/**
34+
* @param QueryFactory $queryFactory
35+
* @param ContentTypeBatchReportBuilder $reportBuilder
36+
* @param string $queryName
37+
*/
38+
public function __construct(
39+
QueryFactory $queryFactory,
40+
ContentTypeBatchReportBuilder $reportBuilder,
41+
$queryName
42+
) {
43+
$this->queryFactory = $queryFactory;
44+
$this->reportBuilder = $reportBuilder;
45+
$this->queryName = $queryName;
46+
}
47+
48+
/**
49+
* Build the report
50+
*
51+
* @return \IteratorIterator
52+
* @throws \Zend_Db_Statement_Exception
53+
*/
54+
public function getReport() : \IteratorIterator
55+
{
56+
return $this->reportBuilder->create(
57+
$this->queryFactory->create($this->queryName)
58+
);
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilderAnalytics\Model;
10+
11+
use Magento\Analytics\ReportXml\QueryFactory;
12+
13+
/**
14+
* Collect analytics data for CMS Blocks
15+
*/
16+
class CmsBlockProvider
17+
{
18+
/**
19+
* @var QueryFactory
20+
*/
21+
private $queryFactory;
22+
23+
/**
24+
* @var ContentTypeBatchReportBuilder
25+
*/
26+
private $reportBuilder;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $queryName;
32+
33+
/**
34+
* @param QueryFactory $queryFactory
35+
* @param ContentTypeBatchReportBuilder $reportBuilder
36+
* @param string $queryName
37+
*/
38+
public function __construct(
39+
QueryFactory $queryFactory,
40+
ContentTypeBatchReportBuilder $reportBuilder,
41+
$queryName
42+
) {
43+
$this->queryFactory = $queryFactory;
44+
$this->reportBuilder = $reportBuilder;
45+
$this->queryName = $queryName;
46+
}
47+
48+
/**
49+
* Build the report
50+
*
51+
* @return \IteratorIterator
52+
* @throws \Zend_Db_Statement_Exception
53+
*/
54+
public function getReport() : \IteratorIterator
55+
{
56+
return $this->reportBuilder->create(
57+
$this->queryFactory->create($this->queryName)
58+
);
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilderAnalytics\Model;
10+
11+
use Magento\Analytics\ReportXml\QueryFactory;
12+
13+
/**
14+
* Collect analytics data for CMS Pages
15+
*/
16+
class CmsPageProvider
17+
{
18+
/**
19+
* @var QueryFactory
20+
*/
21+
private $queryFactory;
22+
23+
/**
24+
* @var ContentTypeBatchReportBuilder
25+
*/
26+
private $reportBuilder;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $queryName;
32+
33+
/**
34+
* @param QueryFactory $queryFactory
35+
* @param ContentTypeBatchReportBuilder $reportBuilder
36+
* @param string $queryName
37+
*/
38+
public function __construct(
39+
QueryFactory $queryFactory,
40+
ContentTypeBatchReportBuilder $reportBuilder,
41+
$queryName
42+
) {
43+
$this->queryFactory = $queryFactory;
44+
$this->reportBuilder = $reportBuilder;
45+
$this->queryName = $queryName;
46+
}
47+
48+
/**
49+
* Build the report
50+
*
51+
* @return \IteratorIterator
52+
* @throws \Zend_Db_Statement_Exception
53+
*/
54+
public function getReport() : \IteratorIterator
55+
{
56+
return $this->reportBuilder->create(
57+
$this->queryFactory->create($this->queryName)
58+
);
59+
}
60+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilderAnalytics\Model;
10+
11+
use Magento\Analytics\ReportXml\ConnectionFactory;
12+
use Magento\PageBuilder\Model\Config;
13+
use Magento\Analytics\ReportXml\Query;
14+
use Magento\Framework\DB\Adapter\AdapterInterface;
15+
16+
/**
17+
* Provides content type data report
18+
*/
19+
class ContentTypeBatchReportBuilder
20+
{
21+
/**
22+
* @var Config
23+
*/
24+
private $config;
25+
26+
/**
27+
* @var ConnectionFactory
28+
*/
29+
private $connectionFactory;
30+
31+
/**
32+
* @var int
33+
*/
34+
private $batchSize;
35+
36+
/**
37+
* @param Config $config
38+
* @param ConnectionFactory $connectionFactory
39+
* @param int $batchSize
40+
*/
41+
public function __construct(
42+
Config $config,
43+
ConnectionFactory $connectionFactory,
44+
$batchSize = 1000
45+
) {
46+
$this->config = $config;
47+
$this->connectionFactory = $connectionFactory;
48+
$this->batchSize = $batchSize;
49+
}
50+
51+
/**
52+
* Create the report based on the supplied query
53+
*
54+
* @param Query $query
55+
*
56+
* @return \IteratorIterator
57+
* @throws \Zend_Db_Statement_Exception
58+
*/
59+
public function create(Query $query) : \IteratorIterator
60+
{
61+
// Prepare our type count data
62+
$typeCounts = [];
63+
$contentTypes = $this->config->getContentTypes();
64+
foreach ($contentTypes as $type) {
65+
$typeCounts[$type['name']] = 0;
66+
}
67+
68+
$connection = $this->connectionFactory->getConnection($query->getConnectionName());
69+
70+
// Determine the total row count and then calculate the batch size
71+
$rowCount = $this->getRowCount($connection, $query);
72+
$batches = ceil($rowCount / $this->batchSize);
73+
74+
if ($batches > 0) {
75+
for ($batch = 0; $batch <= $batches; $batch++) {
76+
$batchQuery = $connection->query(
77+
$query->getSelect()->limit($this->batchSize, $batch * $this->batchSize)
78+
);
79+
foreach ($batchQuery->fetchAll() as $row) {
80+
foreach ($contentTypes as $type) {
81+
// Count the amount of content types within the content
82+
$typeCounts[$type['name']] += substr_count(
83+
$row['content'],
84+
'data-content-type="' . $type['name'] . '"'
85+
);
86+
}
87+
}
88+
}
89+
}
90+
91+
$reportData[] = ['Content Type', 'Count'];
92+
foreach ($contentTypes as $type) {
93+
$reportData[] = [$type['name'], $typeCounts[$type['name']]];
94+
}
95+
96+
return new \IteratorIterator(
97+
new \ArrayIterator($reportData)
98+
);
99+
}
100+
101+
/**
102+
* Determine the row count for the current entity
103+
*
104+
* @param AdapterInterface $connection
105+
* @param Query $query
106+
*
107+
* @return string
108+
*/
109+
private function getRowCount(AdapterInterface $connection, Query $query) : string
110+
{
111+
$countSelect = clone $query->getSelect();
112+
$countSelect->reset(\Magento\Framework\DB\Select::COLUMNS)
113+
->columns(['row_count' => new \Zend_Db_Expr('COUNT(*)')]);
114+
115+
return $connection->fetchOne(
116+
$countSelect
117+
);
118+
}
119+
}

0 commit comments

Comments
 (0)