Skip to content
This repository has been archived by the owner on May 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3287 from magento-performance/pr-2.3-develop
Browse files Browse the repository at this point in the history
[performance] performance improvements
### Tasks:
* MAGETWO-91388: Enable caching of AJAX requests on Varnish/Fastly
* MAGETWO-94671: Product Export fails
* MAGETWO-95412: [2.3] Add missed dependencies for catalogsearch_fulltext indexer
  • Loading branch information
duhon authored Oct 12, 2018
2 parents 4f94998 + 6059152 commit 708017a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 25 deletions.
52 changes: 39 additions & 13 deletions app/code/Magento/CatalogImportExport/Model/Export/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @since 100.0.2
*/
class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
Expand Down Expand Up @@ -349,12 +350,13 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
private $productEntityLinkField;

/**
* Product constructor.
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Eav\Model\Config $config
* @param \Magento\Framework\App\ResourceConnection $resource
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
* @param \Magento\ImportExport\Model\Export\ConfigInterface $exportConfig
* @param \Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFactory
Expand All @@ -363,10 +365,10 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
* @param \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $optionColFactory
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory
* @param Product\Type\Factory $_typeFactory
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
* @param \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
* @param ProductEntity\LinkTypeProvider $linkTypeProvider
* @param RowCustomizerInterface $rowCustomizer
* @param array $dateAttrCodes
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function __construct(
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
Expand Down Expand Up @@ -520,10 +522,13 @@ protected function getMediaGallery(array $productIds)
if (empty($productIds)) {
return [];
}

$productEntityJoinField = $this->getProductEntityLinkField();

$select = $this->_connection->select()->from(
['mgvte' => $this->_resourceModel->getTableName('catalog_product_entity_media_gallery_value_to_entity')],
[
"mgvte.{$this->getProductEntityLinkField()}",
"mgvte.$productEntityJoinField",
'mgvte.value_id'
]
)->joinLeft(
Expand All @@ -535,22 +540,22 @@ protected function getMediaGallery(array $productIds)
]
)->joinLeft(
['mgv' => $this->_resourceModel->getTableName('catalog_product_entity_media_gallery_value')],
'(mg.value_id = mgv.value_id)',
"(mg.value_id = mgv.value_id) and (mgvte.$productEntityJoinField = mgv.$productEntityJoinField)",
[
'mgv.label',
'mgv.position',
'mgv.disabled',
'mgv.store_id',
]
)->where(
"mgvte.{$this->getProductEntityLinkField()} IN (?)",
"mgvte.$productEntityJoinField IN (?)",
$productIds
);

$rowMediaGallery = [];
$stmt = $this->_connection->query($select);
while ($mediaRow = $stmt->fetch()) {
$rowMediaGallery[$mediaRow[$this->getProductEntityLinkField()]][] = [
$rowMediaGallery[$mediaRow[$productEntityJoinField]][] = [
'_media_attribute_id' => $mediaRow['attribute_id'],
'_media_image' => $mediaRow['filename'],
'_media_label' => $mediaRow['label'],
Expand Down Expand Up @@ -692,7 +697,9 @@ protected function updateDataWithCategoryColumns(&$dataRow, &$rowCategories, $pr
}

/**
* {@inheritdoc}
* Get header columns
*
* @return string[]
*/
public function _getHeaderColumns()
{
Expand Down Expand Up @@ -751,7 +758,10 @@ protected function _getExportMainAttrCodes()
}

/**
* {@inheritdoc}
* Get entity collection
*
* @param bool $resetCollection
* @return \Magento\Framework\Data\Collection\AbstractDb
*/
protected function _getEntityCollection($resetCollection = false)
{
Expand Down Expand Up @@ -858,7 +868,10 @@ public function export()
}

/**
* {@inheritdoc}
* Apply filter to collection and add not skipped attributes to select.
*
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
* @since 100.2.0
*/
protected function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection\AbstractCollection $collection)
Expand Down Expand Up @@ -920,8 +933,7 @@ protected function getExportData()
}

/**
* Load products' data from the collection
* and filter it (if needed).
* Load products' data from the collection and filter it (if needed).
*
* @return array Keys are product IDs, values arrays with keys as store IDs
* and values as store-specific versions of Product entity.
Expand Down Expand Up @@ -1063,6 +1075,8 @@ private function wrapValue($value)
}

/**
* Collect multi raw data from
*
* @return array
*/
protected function collectMultirawData()
Expand Down Expand Up @@ -1104,6 +1118,8 @@ protected function collectMultirawData()
}

/**
* Check the current data has multiselect value
*
* @param \Magento\Catalog\Model\Product $item
* @param int $storeId
* @return bool
Expand All @@ -1116,6 +1132,8 @@ protected function hasMultiselectData($item, $storeId)
}

/**
* Collect multiselect values based on value
*
* @param \Magento\Catalog\Model\Product $item
* @param string $attrCode
* @param int $storeId
Expand All @@ -1140,6 +1158,8 @@ protected function collectMultiselectValues($item, $attrCode, $storeId)
}

/**
* Check attribute is valid
*
* @param string $code
* @param mixed $value
* @return bool
Expand All @@ -1159,6 +1179,8 @@ protected function isValidAttributeValue($code, $value)
}

/**
* Append multi row data
*
* @param array $dataRow
* @param array $multiRawData
* @return array
Expand Down Expand Up @@ -1288,6 +1310,8 @@ private function appendMultirowData(&$dataRow, $multiRawData)
}

/**
* Add multi row data to export
*
* @deprecated 100.1.0
* @param array $dataRow
* @param array $multiRawData
Expand Down Expand Up @@ -1335,6 +1359,8 @@ protected function _customHeadersMapping($rowData)
}

/**
* Convert option row to cell string
*
* @param array $option
* @return string
*/
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Elasticsearch/etc/indexer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
<indexer id="catalogsearch_fulltext">
<dependencies>
<indexer id="catalog_category_product" />
<indexer id="cataloginventory_stock" />
<indexer id="catalog_product_price" />
</dependencies>
Expand Down
27 changes: 22 additions & 5 deletions app/code/Magento/Swatches/Controller/Ajax/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Class Media
*/
class Media extends \Magento\Framework\App\Action\Action
class Media extends \Magento\Framework\App\Action\Action implements \Magento\Framework\App\Action\HttpGetActionInterface
{
/**
* @var \Magento\Catalog\Model\Product Factory
Expand All @@ -24,18 +24,26 @@ class Media extends \Magento\Framework\App\Action\Action
*/
private $swatchHelper;

/**
* @var \Magento\PageCache\Model\Config
*/
protected $config;

/**
* @param Context $context
* @param \Magento\Catalog\Model\ProductFactory $productModelFactory
* @param \Magento\Swatches\Helper\Data $swatchHelper
* @param \Magento\PageCache\Model\Config $config
*/
public function __construct(
Context $context,
\Magento\Catalog\Model\ProductFactory $productModelFactory,
\Magento\Swatches\Helper\Data $swatchHelper
\Magento\Swatches\Helper\Data $swatchHelper,
\Magento\PageCache\Model\Config $config
) {
$this->productModelFactory = $productModelFactory;
$this->swatchHelper = $swatchHelper;
$this->config = $config;

parent::__construct($context);
}
Expand All @@ -48,14 +56,23 @@ public function __construct(
public function execute()
{
$productMedia = [];

/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);

/** @var \Magento\Framework\App\ResponseInterface $response */
$response = $this->getResponse();

if ($productId = (int)$this->getRequest()->getParam('product_id')) {
$product = $this->productModelFactory->create()->load($productId);
$productMedia = $this->swatchHelper->getProductMediaGallery(
$this->productModelFactory->create()->load($productId)
$product
);
$resultJson->setHeader('X-Magento-Tags', implode(',', $product->getIdentities()));

$response->setPublicHeaders($this->config->getTtl());
}

/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($productMedia);
return $resultJson;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class MediaTest extends \PHPUnit\Framework\TestCase
/** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
private $productModelFactoryMock;

/** @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
private $config;

/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
private $productMock;

Expand All @@ -29,6 +32,9 @@ class MediaTest extends \PHPUnit\Framework\TestCase
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
private $requestMock;

/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
private $responseMock;

/** @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject */
private $resultFactory;

Expand Down Expand Up @@ -57,11 +63,20 @@ protected function setUp()
\Magento\Catalog\Model\ProductFactory::class,
['create']
);
$this->config = $this->createMock(\Magento\PageCache\Model\Config::class);
$this->config->method('getTtl')->willReturn(1);

$this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
$this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);

$this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
$this->contextMock->method('getRequest')->willReturn($this->requestMock);
$this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
->disableOriginalConstructor()
->setMethods(['setPublicHeaders'])
->getMockForAbstractClass();
$this->responseMock->method('setPublicHeaders')->willReturnSelf();
$this->contextMock->method('getResponse')->willReturn($this->responseMock);
$this->resultFactory = $this->createPartialMock(\Magento\Framework\Controller\ResultFactory::class, ['create']);
$this->contextMock->method('getResultFactory')->willReturn($this->resultFactory);

Expand All @@ -73,7 +88,8 @@ protected function setUp()
[
'context' => $this->contextMock,
'swatchHelper' => $this->swatchHelperMock,
'productModelFactory' => $this->productModelFactoryMock
'productModelFactory' => $this->productModelFactoryMock,
'config' => $this->config
]
);
}
Expand All @@ -86,6 +102,10 @@ public function testExecute()
->method('load')
->with(59)
->willReturn($this->productMock);
$this->productMock
->expects($this->once())
->method('getIdentities')
->willReturn(['tags']);

$this->productModelFactoryMock
->expects($this->once())
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Swatches/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"magento/module-configurable-product": "*",
"magento/module-customer": "*",
"magento/module-eav": "*",
"magento/module-page-cache": "*",
"magento/module-media-storage": "*",
"magento/module-store": "*",
"magento/module-theme": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,12 +1029,14 @@ define([
mediaCallData.isAjax = true;
$widget._XhrKiller();
$widget._EnableProductMediaLoader($this);
$widget.xhr = $.get(
$widget.options.mediaCallback,
mediaCallData,
mediaSuccessCallback,
'json'
).done(function () {
$widget.xhr = $.ajax({
url: $widget.options.mediaCallback,
cache: true,
type: 'GET',
dataType: 'json',
data: mediaCallData,
success: mediaSuccessCallback
}).done(function () {
$widget._XhrKiller();
});
}
Expand Down

0 comments on commit 708017a

Please sign in to comment.