Skip to content

Commit

Permalink
Merge pull request #30 from Nosto/feature/add-logs-and-remove-nosto-id
Browse files Browse the repository at this point in the history
Refactor logs and debugging
  • Loading branch information
olsi-qose authored Mar 4, 2020
2 parents 106d569 + d08b949 commit a9d2128
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 78 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

### 1.0.3
* Function `getCmpResult` return only value of `CategoryMerchandisingResult`
* Refactor logging and exception throwing

### 1.0.2
* Use `Product\Collection` instead of `Fulltext\Collection`
* Add exceptions for easier debug
Expand Down
16 changes: 7 additions & 9 deletions Model/Service/Recommendation/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
use Magento\Framework\Stdlib\CookieManagerInterface;
use Nosto\Result\Graphql\Recommendation\CategoryMerchandisingResult;
use Nosto\Cmp\Model\Filter\FilterBuilder;
use Nosto\Exception\TokenException\MissingAppsTokenException;
use Nosto\NostoException;

class Category
{
Expand Down Expand Up @@ -74,7 +76,9 @@ public function __construct(
* @param $category
* @param int $pageNumber
* @param int $limit
* @return CategoryMerchandisingResult|null
* @return CategoryMerchandisingResult
* @throws MissingAppsTokenException
* @throws NostoException
*/
public function getPersonalisationResult(
NostoAccount $nostoAccount,
Expand All @@ -85,10 +89,9 @@ public function getPersonalisationResult(
$limit = self::MAX_PRODUCT_AMOUNT
) {
$limit = self::MAX_PRODUCT_AMOUNT < $limit ? self::MAX_PRODUCT_AMOUNT : $limit;
$result = null;
$featureAccess = new FeatureAccess($nostoAccount);
if (!$featureAccess->canUseGraphql()) {
return null;
throw new MissingAppsTokenException();
}
$previewMode = (bool)$this->cookieManager->getCookie(self::NOSTO_PREVIEW_COOKIE);
$categoryMerchandising = new CategoryMerchandising(
Expand All @@ -104,11 +107,6 @@ public function getPersonalisationResult(
$limit
);

try {
$result = $categoryMerchandising->execute();
} catch (Exception $e) {
$this->logger->exception($e);
}
return $result;
return $categoryMerchandising->execute();
}
}
37 changes: 22 additions & 15 deletions Plugin/Catalog/Block/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Nosto\Cmp\Utils\Debug\ServerTiming;
use Nosto\Cmp\Model\Service\Recommendation\Category as CategoryRecommendation;
use Nosto\Cmp\Plugin\Catalog\Model\Product as NostoProductPlugin;
use Nosto\Exception\TokenException\MissingAppsTokenException;
use Nosto\Helper\ArrayHelper as NostoHelperArray;
use Nosto\NostoException;
use Nosto\Result\Graphql\Recommendation\CategoryMerchandisingResult;
Expand Down Expand Up @@ -150,11 +151,6 @@ public function afterSetCollection(
}
$this->setLimit($subjectCollection->getPageSize());
$result = $this->getCmpResult($store);
if (!$result instanceof CategoryMerchandisingResult) {
throw new NostoException(
"Cmp result is not instanceof CategoryMerchandisingResult"
);
}
//Get ids of products to order
$nostoProductIds = $this->parseProductIds($result);
if (!empty($nostoProductIds)
Expand All @@ -166,6 +162,11 @@ public function afterSetCollection(
$this->sortByProductIds($subjectCollection, $nostoProductIds);
$this->whereInProductIds($subjectCollection, $nostoProductIds);
$this->addTrackParamToProduct($subjectCollection, $result->getTrackingCode(), $nostoProductIds);
} else {
$this->logger->info(sprintf(
"CMP result is empty for category: %s",
$this->getCurrentCategory($store)
));
}
} catch (Exception $e) {
$this->logger->exception($e);
Expand All @@ -177,8 +178,9 @@ public function afterSetCollection(

/**
* @param Store $store
* @return CategoryMerchandisingResult|null
* @return CategoryMerchandisingResult
* @throws NostoException
* @throws MissingAppsTokenException
* @throws LocalizedException
*/
private function getCmpResult(Store $store)
Expand All @@ -187,31 +189,36 @@ private function getCmpResult(Store $store)
if ($nostoAccount === null) {
throw new NostoException('Account cannot be null');
}
$personalizationResult = null;

// Build filters
$this->nostoFilterBuilder->init($store);
$this->nostoFilterBuilder->buildFromSelectedFilters(
$this->state->getActiveFilters()
);

ServerTiming::getInstance()->instrument(
function () use ($nostoAccount, $store, &$personalizationResult) {
/** @noinspection PhpDeprecationInspection */
$category = $this->registry->registry('current_category');
$categoryString = $this->categoryBuilder->getCategory($category, $store);
$personalizationResult = $this->categoryRecommendation->getPersonalisationResult(
return ServerTiming::getInstance()->instrument(
function () use ($nostoAccount, $store) {
return $this->categoryRecommendation->getPersonalisationResult(
$nostoAccount,
$this->nostoFilterBuilder,
$this->cookieManager->getCookie(NostoCustomer::COOKIE_NAME),
$categoryString,
$this->getCurrentCategory($store),
$this->getCurrentPageNumber() - 1,
$this->getLimit()
);
},
self::TIME_PROF_GRAPHQL_QUERY
);
return $personalizationResult;
}

/**
* Get the current category
* @return null|string
*/
private function getCurrentCategory(Store $store) {
/** @noinspection PhpDeprecationInspection */
$category = $this->registry->registry('current_category');
return $this->categoryBuilder->getCategory($category, $store);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions Plugin/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public function afterGetProductUrl(MagentoProduct $product, $url)
$existingParams = $this->parseExistingQueryParams($url);
$nostoParam = [
self::NOSTO_TRACKING_PARAMETER_NAME => $product->getData(self::NOSTO_TRACKING_PARAMETER_NAME),
self::NOSTO_SOURCE_PARAMETER_NAME => self::NOSTO_SOURCE_PARAMETER_VALUE,
self::NOSTO_PRODUCT_ID_PARAMETER_VALUE => $product->getId()
self::NOSTO_SOURCE_PARAMETER_NAME => self::NOSTO_SOURCE_PARAMETER_VALUE
];
$params = array_merge($existingParams, $nostoParam);
$url = $product->getUrlModel()->getUrl($product, ['_query' => $params]);
Expand Down
4 changes: 3 additions & 1 deletion Utils/Debug/ServerTiming.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ private function __construct()
* Call user function, measure how long it takes and add time to array
* @param callable $fn
* @param string $name
* @return mixed;
*/
public function instrument(callable $fn, $name)
{
$start = microtime(true);
$fn();
$value = $fn();
$stop = microtime(true);
$this->times[$name] = round(($stop - $start) * 1000);
return $value;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nosto/module-nostocmp",
"description": "Nosto Category Merchandising extension for Magento 2",
"type": "magento2-module",
"version": "1.0.2",
"version": "1.0.3",
"require-dev": {
"php": ">=7.1.0"
},
Expand All @@ -12,6 +12,7 @@
"minimum-stability": "dev",
"require": {
"nosto/module-nostotagging": ">=4.0.0",
"nosto/php-sdk": "4.0.11",
"php": ">=7.0.0"
},
"repositories": [
Expand Down
Loading

0 comments on commit a9d2128

Please sign in to comment.