Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: data sync performance optimisation #634

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Model/Connector/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Dotdigitalgroup\Email\Model\Connector;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
use Magento\Catalog\Model\ResourceModel\Category\Collection;

class Category
{
/**
* @var array
*/
private $categoryNames = [];

/**
* @var StoreManagerInterface
*/
protected StoreManagerInterface $storeManager;

/**
* @var Collection
*/
protected Collection $categoryCollection;

/**
* @var CategoryResource
*/
protected CategoryResource $categoryResource;

/**
* @param StoreManagerInterface $storeManager
* @param Collection $categoryCollection
* @param CategoryResource $categoryResource
*/
public function __construct(
StoreManagerInterface $storeManager,
Collection $categoryCollection,
CategoryResource $categoryResource
) {
$this->storeManager = $storeManager;
$this->categoryCollection = $categoryCollection;
$this->categoryResource = $categoryResource;
$this->init();
}

/**
* @return Category
*/
public function init()
{
foreach ($this->categoryCollection->setPageSize(10000)->load() as $category) {
/** @var Category $category */
foreach ($this->storeManager->getStores() as $store) {
$category->setStoreId($store->getId());
$this->categoryResource->load($category, $category->getId());
if (!array_key_exists($category->getId(), $this->categoryNames)) {
$this->categoryNames[$category->getId()] = [];
}
if (!array_key_exists($store->getId(), $this->categoryNames[$category->getId()])) {
$this->categoryNames[$category->getId()][$store->getId()] = null;
}
$this->categoryNames[$category->getId()][$store->getId()] = $category->getName();
}
}

return $this;
}

/**
*
* Get category names.
*
* @param array $categoryIds
* @return string
*/
public function getCategoryNames($categoryIds, $storeId): string
{
$names = [];
foreach ($categoryIds as $id) {
if (array_key_exists($id, $this->categoryNames)
&& array_key_exists($storeId, $this->categoryNames[$id])) {
$names[$id] = $this->categoryNames[$id][$storeId];
}
}

//comma separated category names
if (count($names)) {
return implode(',', $names);
}

return '';
}
}
24 changes: 13 additions & 11 deletions Model/Connector/ContactData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Dotdigitalgroup\Email\Model\Connector;

use Dotdigitalgroup\Email\Logger\Logger;
use Dotdigitalgroup\Email\Model\Connector\Category as ConnectorCategoryModel;
use Dotdigitalgroup\Email\Model\Customer\DataField\Date;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Model\AbstractModel;

/**
Expand Down Expand Up @@ -58,6 +60,11 @@ class ContactData
*/
private $categoryFactory;

/**
* @var ConnectorCategoryModel
*/
protected ConnectorCategoryModel $connectorCategoryModel;

/**
* @var \Magento\Catalog\Model\ResourceModel\Category
*/
Expand Down Expand Up @@ -118,6 +125,7 @@ class ContactData
* @param \Magento\Sales\Model\ResourceModel\Order $orderResource
* @param \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory
* @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
* @param Category $connectorCategoryModel
* @param \Dotdigitalgroup\Email\Helper\Config $configHelper
* @param Logger $logger
* @param Date $dateField
Expand All @@ -131,6 +139,7 @@ public function __construct(
\Magento\Sales\Model\ResourceModel\Order $orderResource,
\Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory,
\Magento\Catalog\Model\ResourceModel\Category $categoryResource,
ConnectorCategoryModel $connectorCategoryModel,
\Dotdigitalgroup\Email\Helper\Config $configHelper,
Logger $logger,
Date $dateField,
Expand All @@ -142,6 +151,7 @@ public function __construct(
$this->orderResource = $orderResource;
$this->productFactory = $productFactory;
$this->categoryFactory = $categoryFactory;
$this->connectorCategoryModel = $connectorCategoryModel;
$this->productResource = $productResource;
$this->categoryResource = $categoryResource;
$this->logger = $logger;
Expand Down Expand Up @@ -441,19 +451,11 @@ private function getCategoryValue($categoryId)
*/
public function getCategoryNames($categoryIds)
{
$names = [];
foreach ($categoryIds as $id) {
if (! isset($this->categoryNames[$id])) {
$this->categoryNames[$id] = $this->getCategoryValue($id);
}
$names[$this->categoryNames[$id]] = $this->categoryNames[$id];
}
//comma separated category names
if (count($names)) {
return implode(',', $names);
if (!$this->connectorCategoryModel) {
$this->connectorCategoryModel = ObjectManager::getInstance()->get(ConnectorCategoryModel::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why this is necessary if the class was injected in the constructor in the usual way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sta1r

That snippet was maintained there since the original solution was applied as a patch. You are right, DI should suffice and the use of objectManager to be deleted/discouraged.

}

return '';
return $this->connectorCategoryModel->getCategoryNames($categoryIds, $this->model->getStoreId());
}

/**
Expand Down
16 changes: 16 additions & 0 deletions Model/ResourceModel/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@
namespace Dotdigitalgroup\Email\Model\ResourceModel;

use Dotdigitalgroup\Email\Setup\SchemaInterface as Schema;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Psr\Log\LoggerInterface;

class Order extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* @param Context $context
* @param LoggerInterface $_logger
* @param $connectionName
*/
public function __construct(
Context $context,
LoggerInterface $_logger,
$connectionName = null
) {
$this->_logger = $_logger;
parent::__construct($context, $connectionName);
}

/**
* Initialize resource.
*
Expand Down
2 changes: 1 addition & 1 deletion Model/Sync/Consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function sync(\DateTime $from = null)
'Error in consent sync for website id: %d',
$websiteId
),
[(string)$e]
['message' => $e->getMessage()]
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Setup/Install/Type/AbstractDataMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function getTableName()
*/
public function isEnabled(): bool
{
return true;
//change applied to ensure no setup:upgrade induced data migration and sync
global $argv;
return (isset($argv) && is_array($argv) && in_array('dotdigital:migrate', $argv));
}
}
4 changes: 3 additions & 1 deletion Setup/Install/Type/InsertEmailContactTableCustomerSales.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ protected function insertData(Select $selectStatement)
*/
public function isEnabled(): bool
{
return $this->config->isAccountSharingGlobal();
global $argv;
$status = (isset($argv) && is_array($argv) && in_array('dotdigital:migrate', $argv));
return $this->config->isAccountSharingGlobal() && $status;
}
}
4 changes: 3 additions & 1 deletion Setup/Install/Type/UpdateEmailContactTableCustomerSales.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public function getUpdateWhereClause($row)
*/
public function isEnabled(): bool
{
return $this->config->isAccountSharingGlobal();
global $argv;
$status = (isset($argv) && is_array($argv) && in_array('dotdigital:migrate', $argv));
return $this->config->isAccountSharingGlobal() && $status;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Setup/Patch/Data/ProcessPendingAutomations.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
*/
public function apply()
{
$this->automationFactory->create()->sync();
//$this->automationFactory->create()->sync();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption is that this model is not used by CLI/cron job and this change won't prevent automation processing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch was added when we introduced queues for automation processing. It was possible at point of upgrade that a merchant still has some residual automations in the table (up to 15 mins' worth) which would not be queued and therefore left to become stale. Without this merchants would need to remember / be reminded to do this to process remaining old-style automations.

}

/**
Expand Down
11 changes: 11 additions & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<index referenceId="EMAIL_CONTACT_UPDATED_AT" indexType="btree">
<column name="updated_at"/>
</index>
<index referenceId="EMAIL_CONTACT_CUST_IMPORT_WEBSITE" indexType="btree">
<column name="customer_id"/>
<column name="email_imported"/>
<column name="website_id"/>
</index>
</table>
<table name="email_order" resource="default" engine="innodb" comment="Transactional Order Data">
<column xsi:type="int" name="email_order_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Primary Key"/>
Expand Down Expand Up @@ -443,4 +448,10 @@
<table name="salesrule_coupon" resource="default">
<column xsi:type="smallint" name="generated_by_dotmailer" padding="6" unsigned="false" nullable="true" identity="false" comment="1 = Generated by dotmailer"/>
</table>

<table name="quote">
<index referenceId="SALES_QUOTE_CUSTOMER_EMAIL" indexType="btree">
<column name="customer_email"/>
</index>
</table>
</schema>