Skip to content

Commit

Permalink
added adminhtml configuration dropdown, update RecrawlService with dy…
Browse files Browse the repository at this point in the history
…namic values
  • Loading branch information
makso8makso committed Oct 22, 2024
1 parent 0d90835 commit c3c8bf9
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 13 deletions.
32 changes: 32 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ class Data extends AbstractHelper
*/
public const XML_PATH_INDEXER_MEMORY = 'nosto/flags/indexer_memory';

/**
* Product per request
*/
public const XML_PATH_PRODUCT_PER_REQUEST = 'nosto/flags/product_per_request';

/**
* Request timeout
*/
public const XML_PATH_REQUEST_TIMEOUT = 'nosto/flags/request_timeout';

/**
* Path to the configuration object that stores the preference for indexing disabled products
*/
Expand Down Expand Up @@ -441,6 +451,28 @@ public function getIndexerMemory(StoreInterface $store = null)
return $this->getStoreConfig(self::XML_PATH_INDEXER_MEMORY, $store);
}

/**
* Products per request
*
* @param StoreInterface|null $store the store model or null.
* @return int the configuration value
*/
public function getProductsPerRequest(StoreInterface $store = null)
{
return $this->getStoreConfig(self::XML_PATH_PRODUCT_PER_REQUEST, $store);
}

/**
* Request timeout
*
* @param StoreInterface|null $store the store model or null.
* @return int the configuration value
*/
public function getRequestTimeout(StoreInterface $store = null)
{
return $this->getStoreConfig(self::XML_PATH_REQUEST_TIMEOUT, $store);
}

/**
* Returns maximum percentage of PHP available memory that indexer should use
*
Expand Down
59 changes: 59 additions & 0 deletions Model/Config/Source/ProductsPerRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\Phrase;

class ProductsPerRequest implements OptionSourceInterface
{
/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
return [
['value' => 100, 'label' => new Phrase('100')],
['value' => 200, 'label' => new Phrase('200')],
['value' => 300, 'label' => new Phrase('300')],
['value' => 400, 'label' => new Phrase('400')],
['value' => 500, 'label' => new Phrase('500')],
];
}
}
59 changes: 59 additions & 0 deletions Model/Config/Source/TimeBetweenBatchOfRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\Phrase;

class TimeBetweenBatchOfRequests implements OptionSourceInterface
{
/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
return [
['value' => 60, 'label' => new Phrase('60')],
['value' => 120, 'label' => new Phrase('120')],
['value' => 180, 'label' => new Phrase('180')],
['value' => 240, 'label' => new Phrase('240')],
['value' => 300, 'label' => new Phrase('300')],
];
}
}
21 changes: 9 additions & 12 deletions Model/Service/Sync/Recrawl/RecrawlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class RecrawlService extends AbstractService

public const BENCHMARK_RECRAWL_NAME = 'nosto_product_recrawl';
public const BENCHMARK_RECRAWL_BREAKPOINT = 1;
public const PRODUCT_RECRAWL_BATCH_SIZE = 100;

/** @var CacheService */
private CacheService $cacheService;
Expand All @@ -78,9 +77,6 @@ class RecrawlService extends AbstractService
/** @var ProductRepository */
private ProductRepository $productRepository;

/** @var int */
private int $recrawlBatchSize;

/**
* RecrawlService constructor.
* @param CacheService $cacheService
Expand All @@ -90,7 +86,6 @@ class RecrawlService extends AbstractService
* @param NostoHelperUrl $nostoHelperUrl
* @param ProductRepository $productRepository
* @param NostoLogger $logger
* @param int $recrawlBatchSize
*/
public function __construct(
CacheService $cacheService,
Expand All @@ -99,15 +94,13 @@ public function __construct(
NostoHelperData $nostoHelperData,
NostoHelperUrl $nostoHelperUrl,
ProductRepository $productRepository,
NostoLogger $logger,
int $recrawlBatchSize = self::PRODUCT_RECRAWL_BATCH_SIZE
NostoLogger $logger
) {
$this->cacheService = $cacheService;
$this->productService = $productService;
$this->nostoHelperAccount = $nostoHelperAccount;
$this->nostoHelperUrl = $nostoHelperUrl;
$this->productRepository = $productRepository;
$this->recrawlBatchSize = $recrawlBatchSize;
parent::__construct($nostoHelperData, $nostoHelperAccount, $logger);
}

Expand All @@ -132,8 +125,11 @@ public function recrawl(ProductCollection $collection, Store $store)
$account = $this->nostoHelperAccount->findAccount($store);
$this->startBenchmark(self::BENCHMARK_RECRAWL_NAME, self::BENCHMARK_RECRAWL_BREAKPOINT);

$timeBetweenBatchOfRequests = $this->getDataHelper()->getRequestTimeout();
$productsPerRequest = $this->getDataHelper()->getProductsPerRequest();

$index = 0;
$collection->setPageSize($this->recrawlBatchSize);
$collection->setPageSize($productsPerRequest);
$iterator = new PagingIterator($collection);

/** @var ProductCollection $page */
Expand All @@ -144,8 +140,8 @@ public function recrawl(ProductCollection $collection, Store $store)
$op->setResponseTimeout(60);

Check failure on line 140 in Model/Service/Sync/Recrawl/RecrawlService.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to method setResponseTimeout from undeclared class \Nosto\Operation\RecrawlProduct
$products = $this->productRepository->getByIds(
$page->getAllIds(
$this->recrawlBatchSize,
($iterator->getCurrentPageNumber() - 1) * $this->recrawlBatchSize
$productsPerRequest,
($iterator->getCurrentPageNumber() - 1) * $productsPerRequest
)
);
/** @var Product $product */
Expand All @@ -167,13 +163,14 @@ public function recrawl(ProductCollection $collection, Store $store)
$this->logDebugWithStore(
sprintf(
'Upserting batch of %d (%s) - API timeout is set to %d seconds',
$this->recrawlBatchSize,
$productsPerRequest,
implode(',', $productIdsInBatch),
60
),
$store
);
$op->requestRecrawl();

Check failure on line 172 in Model/Service/Sync/Recrawl/RecrawlService.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to method requestRecrawl from undeclared class \Nosto\Operation\RecrawlProduct
sleep($timeBetweenBatchOfRequests);

Check warning on line 173 in Model/Service/Sync/Recrawl/RecrawlService.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

The use of function sleep() is discouraged

Check failure on line 173 in Model/Service/Sync/Recrawl/RecrawlService.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

The use of function sleep() is forbidden
}
$this->logBenchmarkSummary(self::BENCHMARK_RECRAWL_NAME, $store, $this);
}
Expand Down
16 changes: 16 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@
</comment>
<source_model>Nosto\Tagging\Model\Config\Source\Memory</source_model>
</field>
<field id="product_per_request" translate="label comment" type="select" sortOrder="110"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>Light Recrawl Indexer - Products per Request</label>
<comment>
<![CDATA[Set the number of product IDs to be sent to Nosto in each request. A lower number can prevent overwhelming Nosto during updates.]]>
</comment>
<source_model>Nosto\Tagging\Model\Config\Source\ProductsPerRequest</source_model>
</field>
<field id="time_between_batch_of_requests" translate="label comment" type="select" sortOrder="110"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>Light Recrawl Indexer - Time between batch of requests (seconds)</label>
<comment>
<![CDATA[Define the time between batch of requests to Nosto. This helps manage traffic and prevents potential spikes that could affect performance.]]>
</comment>
<source_model>Nosto\Tagging\Model\Config\Source\TimeBetweenBatchOfRequests</source_model>
</field>
<field id="indexer_disabled_products" translate="label comment" type="select" sortOrder="120"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>Index disabled products</label>
Expand Down
2 changes: 2 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<low_stock_indication>0</low_stock_indication>
<send_customer_data>1</send_customer_data>
<indexer_memory>50</indexer_memory>
<product_per_request>100</product_per_request>
<time_between_batch_of_requests>60</time_between_batch_of_requests>
<indexer_disabled_products>0</indexer_disabled_products>
<tag_date_published>0</tag_date_published>
<track_multi_channel_orders>1</track_multi_channel_orders>
Expand Down
2 changes: 1 addition & 1 deletion etc/indexer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</indexer>

<indexer id="nosto_index_light_product" view_id="nosto_index_light_product" class="Nosto\Tagging\Model\Indexer\LightProductIndexer">
<title>Nosto Product Indexer</title>
<title>Nosto Product Light Indexer</title>
<description>Populates message queue with product ids to be sent to Nosto</description>
</indexer>
</config>

0 comments on commit c3c8bf9

Please sign in to comment.