Skip to content

Commit c2fb8d3

Browse files
authored
Merge pull request #1642 from algolia/feat/MAGE-1104-granular-profiling
Feat/mage 1104 granular profiling
2 parents 4e65891 + 8d8ca0c commit c2fb8d3

24 files changed

+468
-182
lines changed

Helper/AnalyticsHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Algolia\AlgoliaSearch\Api\AnalyticsClient;
66
use Algolia\AlgoliaSearch\Configuration\AnalyticsConfig;
77
use Algolia\AlgoliaSearch\DataProvider\Analytics\IndexEntityDataProvider;
8+
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
89
use Magento\Framework\Locale\ResolverInterface;
910

1011
class AnalyticsHelper
@@ -54,13 +55,13 @@ class AnalyticsHelper
5455
/**
5556
* @param ConfigHelper $configHelper
5657
* @param IndexEntityDataProvider $entityHelper
57-
* @param Logger $logger
58+
* @param DiagnosticsLogger $logger
5859
* @param ResolverInterface $localeResolver
5960
*/
6061
public function __construct(
6162
private ConfigHelper $configHelper,
6263
private IndexEntityDataProvider $entityHelper,
63-
private Logger $logger,
64+
private DiagnosticsLogger $logger,
6465
private ResolverInterface $localeResolver
6566
)
6667
{

Helper/ConfigHelper.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class ConfigHelper
109109
public const READ_TIMEOUT = 'algoliasearch_advanced/advanced/read_timeout';
110110
public const WRITE_TIMEOUT = 'algoliasearch_advanced/advanced/write_timeout';
111111

112+
public const PROFILER_ENABLED = 'algoliasearch_advanced/advanced/enable_profiler';
113+
112114
public const SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock';
113115

114116
public const USE_SECURE_IN_FRONTEND = 'web/secure/use_in_frontend';
@@ -334,11 +336,20 @@ public function makeSeoRequest($storeId = null)
334336
* @param $storeId
335337
* @return bool
336338
*/
337-
public function isLoggingEnabled($storeId = null)
339+
public function isLoggingEnabled($storeId = null): bool
338340
{
339341
return $this->configInterface->isSetFlag(self::LOGGING_ENABLED, ScopeInterface::SCOPE_STORE, $storeId);
340342
}
341343

344+
/**
345+
* @param $storeId
346+
* @return bool
347+
*/
348+
public function isProfilerEnabled($storeId = null): bool
349+
{
350+
return $this->configInterface->isSetFlag(self::PROFILER_ENABLED, ScopeInterface::SCOPE_STORE, $storeId);
351+
}
352+
342353
/**
343354
* @param $storeId
344355
* @return bool

Helper/Data.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Algolia\AlgoliaSearch\Helper\Entity\PageHelper;
1212
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
1313
use Algolia\AlgoliaSearch\Helper\Entity\SuggestionHelper;
14+
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
1415
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
1516
use Magento\Catalog\Model\Category;
1617
use Magento\Catalog\Model\Product;
@@ -44,7 +45,7 @@ public function __construct(
4445
protected SuggestionHelper $suggestionHelper,
4546
protected AdditionalSectionHelper $additionalSectionHelper,
4647
protected Emulation $emulation,
47-
protected Logger $logger,
48+
protected DiagnosticsLogger $logger,
4849
protected ResourceConnection $resource,
4950
protected ManagerInterface $eventManager,
5051
protected ScopeCodeResolver $scopeCodeResolver,
@@ -384,10 +385,13 @@ public function rebuildStoreProductIndex(int $storeId, array $productIds): void
384385
$this->startEmulation($storeId);
385386
$this->logger->start('Indexing');
386387
try {
387-
$this->logger->start('ok');
388388
$onlyVisible = !$this->configHelper->includeNonVisibleProductsInIndex($storeId);
389389
$collection = $this->productHelper->getProductCollectionQuery($storeId, $productIds, $onlyVisible);
390+
$timerName = __METHOD__ . ' (Get product collection size)';
391+
$this->logger->startProfiling($timerName);
390392
$size = $collection->getSize();
393+
$this->logger->stopProfiling($timerName);
394+
391395
if (!empty($productIds)) {
392396
$size = max(count($productIds), $size);
393397
}
@@ -554,7 +558,8 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted
554558
);
555559
}
556560

557-
$this->logger->start('CREATE RECORDS ' . $this->logger->getStoreName($storeId));
561+
$logEventName = 'CREATE RECORDS ' . $this->logger->getStoreName($storeId);
562+
$this->logger->start($logEventName, true);
558563
$this->logger->log(count($collection) . ' product records to create');
559564
$salesData = $this->getSalesData($storeId, $collection);
560565
$transport = new ProductDataArray();
@@ -607,7 +612,7 @@ protected function getProductsRecords($storeId, $collection, $potentiallyDeleted
607612
$productsToRemove = array_merge($productsToRemove, $potentiallyDeletedProductsIds);
608613
}
609614

610-
$this->logger->stop('CREATE RECORDS ' . $this->logger->getStoreName($storeId));
615+
$this->logger->stop($logEventName, true);
611616
return [
612617
'toIndex' => $productsToIndex,
613618
'toRemove' => array_unique($productsToRemove),
@@ -700,7 +705,7 @@ public function rebuildStoreProductIndexPage(
700705
$wrapperLogMessage = 'rebuildStoreProductIndexPage: ' . $this->logger->getStoreName($storeId) . ',
701706
page ' . $page . ',
702707
pageSize ' . $pageSize;
703-
$this->logger->start($wrapperLogMessage);
708+
$this->logger->start($wrapperLogMessage, true);
704709
if ($emulationInfo === null) {
705710
$this->startEmulation($storeId);
706711
}
@@ -761,7 +766,7 @@ public function rebuildStoreProductIndexPage(
761766

762767
$this->algoliaHelper->setStoreId(AlgoliaHelper::ALGOLIA_DEFAULT_SCOPE);
763768

764-
$this->logger->stop($wrapperLogMessage);
769+
$this->logger->stop($wrapperLogMessage, true);
765770
}
766771

767772
/**
@@ -838,6 +843,7 @@ protected function getIdsToRealRemove($indexName, $idsToRemove)
838843
*/
839844
protected function getSalesData($storeId, Collection $collection)
840845
{
846+
$this->logger->startProfiling(__METHOD__);
841847
$additionalAttributes = $this->configHelper->getProductAdditionalAttributes($storeId);
842848
if ($this->productHelper->isAttributeEnabled($additionalAttributes, 'ordered_qty') === false
843849
&& $this->productHelper->isAttributeEnabled($additionalAttributes, 'total_ordered') === false) {
@@ -862,6 +868,7 @@ protected function getSalesData($storeId, Collection $collection)
862868
->group('product_id');
863869
$salesData = $salesConnection->fetchAll($select, [], \PDO::FETCH_GROUP | \PDO::FETCH_ASSOC | \PDO::FETCH_UNIQUE);
864870
}
871+
$this->logger->stopProfiling(__METHOD__);
865872
return $salesData;
866873
}
867874

@@ -926,7 +933,6 @@ public function getBaseIndexName(int $storeId = null): string
926933
*/
927934
public function getIndexDataByStoreIds(): array
928935
{
929-
930936
$indexNames = [];
931937
$indexNames[AlgoliaHelper::ALGOLIA_DEFAULT_SCOPE] = $this->buildIndexData();
932938
foreach ($this->storeManager->getStores() as $store) {

Helper/Entity/Product/PriceManager/ProductWithoutChildren.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace Algolia\AlgoliaSearch\Helper\Entity\Product\PriceManager;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6-
use Algolia\AlgoliaSearch\Helper\Logger;
6+
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
77
use DateTime;
8+
use Magento\Catalog\Api\ScopedProductTierPriceManagementInterface;
89
use Magento\Catalog\Helper\Data as CatalogHelper;
910
use Magento\Catalog\Model\Product;
1011
use Magento\Catalog\Model\ProductFactory;
1112
use Magento\CatalogRule\Model\ResourceModel\Rule;
12-
use Magento\Customer\Model\Group;
1313
use Magento\Customer\Api\Data\GroupInterface;
14-
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
1514
use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface;
15+
use Magento\Customer\Model\Group;
16+
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
1617
use Magento\Framework\Pricing\PriceCurrencyInterface;
1718
use Magento\Tax\Helper\Data as TaxHelper;
1819
use Magento\Tax\Model\Config as TaxConfig;
19-
use Magento\Catalog\Api\ScopedProductTierPriceManagementInterface;
2020

2121
abstract class ProductWithoutChildren
2222
{
@@ -60,7 +60,7 @@ abstract class ProductWithoutChildren
6060
private $productTierPrice;
6161

6262
/**
63-
* @var Logger
63+
* @var DiagnosticsLogger
6464
*/
6565
protected $logger;
6666

@@ -80,7 +80,7 @@ abstract class ProductWithoutChildren
8080
* @param Rule $rule
8181
* @param ProductFactory $productloader
8282
* @param ScopedProductTierPriceManagementInterface $productTierPrice
83-
* @param Logger $logger
83+
* @param DiagnosticsLogger $logger
8484
*/
8585
public function __construct(
8686
ConfigHelper $configHelper,
@@ -92,7 +92,7 @@ public function __construct(
9292
Rule $rule,
9393
ProductFactory $productloader,
9494
ScopedProductTierPriceManagementInterface $productTierPrice,
95-
Logger $logger
95+
DiagnosticsLogger $logger
9696
) {
9797
$this->configHelper = $configHelper;
9898
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
@@ -115,6 +115,7 @@ public function __construct(
115115
*/
116116
public function addPriceData($customData, Product $product, $subProducts): array
117117
{
118+
$this->logger->startProfiling(__METHOD__);
118119
$this->customData = $customData;
119120
$this->store = $product->getStore();
120121
$this->areCustomersGroupsEnabled = $this->configHelper->isCustomerGroupsEnabled($product->getStoreId());
@@ -165,6 +166,7 @@ public function addPriceData($customData, Product $product, $subProducts): array
165166
}
166167
}
167168

169+
$this->logger->stopProfiling(__METHOD__);
168170
return $this->customData;
169171
}
170172

@@ -281,6 +283,7 @@ protected function getSpecialPrice(Product $product, $currencyCode, $withTax, $s
281283
*/
282284
protected function getTierPrice(Product $product, $currencyCode, $withTax)
283285
{
286+
$this->logger->startProfiling(__METHOD__);
284287
$tierPrice = [];
285288
$tierPrices = [];
286289

@@ -347,6 +350,7 @@ protected function getTierPrice(Product $product, $currencyCode, $withTax)
347350
$tierPrice[$groupId] = $this->getTaxPrice($product, $currentTierPrice, $withTax);
348351
}
349352

353+
$this->logger->stopProfiling(__METHOD__);
350354
return $tierPrice;
351355
}
352356

Helper/Entity/ProductHelper.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
1515
use Algolia\AlgoliaSearch\Helper\Entity\Product\PriceManager;
1616
use Algolia\AlgoliaSearch\Helper\Image as ImageHelper;
17-
use Algolia\AlgoliaSearch\Helper\Logger;
17+
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
1818
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
1919
use Magento\Bundle\Model\Product\Type as BundleProductType;
2020
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
@@ -94,7 +94,7 @@ public function __construct(
9494
protected Config $eavConfig,
9595
protected ConfigHelper $configHelper,
9696
protected AlgoliaHelper $algoliaHelper,
97-
protected Logger $logger,
97+
protected DiagnosticsLogger $logger,
9898
protected StoreManagerInterface $storeManager,
9999
protected ManagerInterface $eventManager,
100100
protected Visibility $visibility,
@@ -203,6 +203,7 @@ public function getProductCollectionQuery(
203203
bool $includeNotVisibleIndividually = false
204204
): ProductCollection
205205
{
206+
$this->logger->startProfiling(__METHOD__);
206207
$productCollection = $this->productCollectionFactory->create();
207208
$products = $productCollection
208209
->setStoreId($storeId)
@@ -252,6 +253,7 @@ public function getProductCollectionQuery(
252253
]
253254
);
254255

256+
$this->logger->stopProfiling(__METHOD__);
255257
return $products;
256258
}
257259

@@ -428,7 +430,8 @@ public function getObject(Product $product)
428430
{
429431
$storeId = $product->getStoreId();
430432

431-
$this->logger->start('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($storeId));
433+
$logEventName = 'CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($storeId);
434+
$this->logger->start($logEventName, true);
432435
$defaultData = [];
433436

434437
$transport = new DataObject($defaultData);
@@ -497,7 +500,7 @@ public function getObject(Product $product)
497500
);
498501
$customData = $transport->getData();
499502

500-
$this->logger->stop('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($storeId));
503+
$this->logger->stop($logEventName, true);
501504

502505
return $customData;
503506
}
@@ -508,6 +511,7 @@ public function getObject(Product $product)
508511
*/
509512
protected function getSubProducts(Product $product): array
510513
{
514+
$this->logger->startProfiling(__METHOD__);
511515
$type = $product->getTypeId();
512516

513517
if (!in_array($type, ['bundle', 'grouped', 'configurable'], true)) {
@@ -537,6 +541,7 @@ protected function getSubProducts(Product $product): array
537541
}
538542
}
539543

544+
$this->logger->stopProfiling(__METHOD__);
540545
return $subProducts;
541546
}
542547

@@ -549,11 +554,13 @@ protected function getSubProducts(Product $product): array
549554
*/
550555
public function getParentProductIds(array $productIds): array
551556
{
557+
$this->logger->startProfiling(__METHOD__);
552558
$parentIds = [];
553559
foreach ($this->getCompositeTypes() as $typeInstance) {
554560
$parentIds = array_merge($parentIds, $typeInstance->getParentIdsByChild($productIds));
555561
}
556562

563+
$this->logger->stopProfiling(__METHOD__);
557564
return $parentIds;
558565
}
559566

@@ -753,6 +760,7 @@ protected function autoAnchorParentCategories(array $paths): array {
753760
*/
754761
protected function addCategoryData(array $algoliaData, Product $product): array
755762
{
763+
$this->logger->startProfiling(__METHOD__);
756764
$storeId = $product->getStoreId();
757765

758766
$categoryData = $this->buildCategoryData($product);
@@ -766,6 +774,7 @@ protected function addCategoryData(array $algoliaData, Product $product): array
766774
$algoliaData[$this->configHelper->getCategoryPageIdAttributeName($storeId)] = $this->flattenCategoryPaths($autoAnchorPaths, $storeId);
767775
}
768776

777+
$this->logger->stopProfiling(__METHOD__);
769778
return $algoliaData;
770779
}
771780

@@ -889,6 +898,7 @@ protected function addStockQty($defaultData, $customData, $additionalAttributes,
889898
*/
890899
protected function addAdditionalAttributes($customData, $additionalAttributes, Product $product, $subProducts)
891900
{
901+
$this->logger->startProfiling(__METHOD__);
892902
foreach ($additionalAttributes as $attribute) {
893903
$attributeName = $attribute['attribute'];
894904

@@ -924,6 +934,7 @@ protected function addAdditionalAttributes($customData, $additionalAttributes, P
924934

925935
$customData = $this->addNullValue($customData, $subProducts, $attribute, $attributeResource);
926936
}
937+
$this->logger->stopProfiling(__METHOD__);
927938

928939
return $customData;
929940
}
@@ -1301,6 +1312,7 @@ protected function clearFacetsQueryRules($indexName): void
13011312
*/
13021313
public function canProductBeReindexed($product, $storeId, $isChildProduct = false)
13031314
{
1315+
$this->logger->startProfiling(__METHOD__);
13041316
if ($product->isDeleted() === true) {
13051317
throw (new ProductDeletedException())
13061318
->withProduct($product)
@@ -1334,6 +1346,7 @@ public function canProductBeReindexed($product, $storeId, $isChildProduct = fals
13341346
->withStoreId($storeId);
13351347
}
13361348

1349+
$this->logger->stopProfiling(__METHOD__);
13371350
return true;
13381351
}
13391352

Helper/Image.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Helper;
44

5+
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
56
use Magento\Catalog\Model\Product\ImageFactory;
67
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ProductTypeConfigurable;
78
use Magento\Framework\App\Helper\Context;
@@ -23,16 +24,16 @@ class Image extends \Magento\Catalog\Helper\Image
2324
* @param ImageFactory $productImageFactory
2425
* @param Repository $assetRepo
2526
* @param ConfigInterface $viewConfig
26-
* @param Logger $logger
27+
* @param DiagnosticsLogger $logger
2728
* @param ConfigHelper $configHelper
2829
*/
2930
public function __construct(
30-
Context $context,
31-
ImageFactory $productImageFactory,
32-
Repository $assetRepo,
33-
ConfigInterface $viewConfig,
34-
Logger $logger,
35-
ConfigHelper $configHelper
31+
Context $context,
32+
ImageFactory $productImageFactory,
33+
Repository $assetRepo,
34+
ConfigInterface $viewConfig,
35+
DiagnosticsLogger $logger,
36+
ConfigHelper $configHelper
3637
) {
3738
parent::__construct($context, $productImageFactory, $assetRepo, $viewConfig);
3839
$this->logger = $logger;

0 commit comments

Comments
 (0)