From 8aa0274ab770ab312988df0d67df786b44c000a4 Mon Sep 17 00:00:00 2001 From: Eric Wright Date: Wed, 10 Apr 2024 22:49:40 -0400 Subject: [PATCH 1/3] MAGE-866: commit cherry-picked from 864 --- Helper/AnalyticsHelper.php | 46 +++++++-------- ViewModel/Adminhtml/Analytics/Overview.php | 69 +++++++++++++++------- 2 files changed, 69 insertions(+), 46 deletions(-) diff --git a/Helper/AnalyticsHelper.php b/Helper/AnalyticsHelper.php index a08c96ef6..459cf022a 100644 --- a/Helper/AnalyticsHelper.php +++ b/Helper/AnalyticsHelper.php @@ -6,6 +6,7 @@ use Algolia\AlgoliaSearch\Config\AnalyticsConfig; use Algolia\AlgoliaSearch\DataProvider\Analytics\IndexEntityDataProvider; use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory; +use Magento\Framework\Locale\ResolverInterface; class AnalyticsHelper { @@ -13,18 +14,8 @@ class AnalyticsHelper public const ANALYTICS_HITS_PATH = '/2/hits'; public const ANALYTICS_FILTER_PATH = '/2/filters'; public const ANALYTICS_CLICKS_PATH = '/2/clicks'; - - /** @var AlgoliaHelper */ - private $algoliaHelper; - - /** @var ConfigHelper */ - private $configHelper; - - /** @var IndexEntityDataProvider */ - private $entityHelper; - - /** @var Logger */ - private $logger; + public const DATE_FORMAT_PICKER = 'dd MMM yyyy'; + public const DATE_FORMAT_API = 'Y-m-d'; private $searches; private $users; @@ -58,25 +49,17 @@ class AnalyticsHelper protected $region; /** - * @param AlgoliaHelper $algoliaHelper * @param ConfigHelper $configHelper * @param IndexEntityDataProvider $entityHelper * @param Logger $logger - * @param string $region + * @param ResolverInterface $localeResolver */ public function __construct( - AlgoliaHelper $algoliaHelper, - ConfigHelper $configHelper, - IndexEntityDataProvider $entityHelper, - Logger $logger, - string $region = 'us' + private ConfigHelper $configHelper, + private IndexEntityDataProvider $entityHelper, + private Logger $logger, + private ResolverInterface $localeResolver ) { - $this->algoliaHelper = $algoliaHelper; - $this->configHelper = $configHelper; - - $this->entityHelper = $entityHelper; - - $this->logger = $logger; $this->region = $this->configHelper->getAnalyticsRegion(); } @@ -371,4 +354,17 @@ public function getErrors() { return $this->errors; } + + /** + * @param string $timezone + * @return \IntlDateFormatter + */ + public function getAnalyticsDatePickerFormatter(string $timezone): \IntlDateFormatter + { + $locale = $this->localeResolver->getLocale(); + $dateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, \IntlDateFormatter::NONE, $timezone); + $dateFormatter->setPattern(self::DATE_FORMAT_PICKER); + return $dateFormatter; + } + } diff --git a/ViewModel/Adminhtml/Analytics/Overview.php b/ViewModel/Adminhtml/Analytics/Overview.php index 103b40e55..0865b4bb1 100644 --- a/ViewModel/Adminhtml/Analytics/Overview.php +++ b/ViewModel/Adminhtml/Analytics/Overview.php @@ -30,21 +30,16 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac private $analyticsParams = []; /** - * Index constructor. - * * @param BackendView $backendView * @param AnalyticsHelper $analyticsHelper * @param IndexEntityDataProvider $indexEntityDataProvider */ public function __construct( - BackendView $backendView, - AnalyticsHelper $analyticsHelper, - IndexEntityDataProvider $indexEntityDataProvider - ) { - $this->backendView = $backendView; - $this->analyticsHelper = $analyticsHelper; - $this->indexEntityDataProvider = $indexEntityDataProvider; - } + protected BackendView $backendView, + protected AnalyticsHelper $analyticsHelper, + protected IndexEntityDataProvider $indexEntityDataProvider, + protected ResolverInterface $localeResolver + ) { } /** * @return BackendView @@ -86,13 +81,11 @@ public function getAnalyticsParams($additional = []) if (empty($this->analyticsParams)) { $params = ['index' => $this->getIndexName()]; if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) { - $dateTime = $this->getBackendView()->getDateTime(); - $timeZone = $this->getTimeZone(); - if (isset($formData['from']) && $formData['from'] !== '') { - $params['startDate'] = $dateTime->date($formData['from'], $timeZone, true, false)->format('Y-m-d'); + if (!empty($formData['from'])) { + $params['startDate'] = $this->formatFormSubmittedDate($formData['from']); } - if (isset($formData['to']) && $formData['to'] !== '') { - $params['endDate'] = $dateTime->date($formData['to'], $timeZone, true, false)->format('Y-m-d'); + if (!empty($formData['to'])) { + $params['endDate'] = $this->formatFormSubmittedDate($formData['to']); } } @@ -102,6 +95,39 @@ public function getAnalyticsParams($additional = []) return array_merge($this->analyticsParams, $additional); } + /** + * @param string $dateString + * @return string + * @throws NoSuchEntityException + */ + protected function formatFormSubmittedDate(string $dateString): string + { + $timezone = $this->getTimeZone(); + $dateTime = $this->parseFormSubmittedDate($dateString, $timezone); + return $dateTime->format(AnalyticsHelper::DATE_FORMAT_API); + } + + /** + * @param string|null $dateString + * @param string|null $timezone + * @return \DateTime + * @throws NoSuchEntityException + */ + protected function parseFormSubmittedDate(string $dateString = null, string $timezone = null): \DateTime + { + if (empty($timezone)) { + $timezone = $this->getTimeZone(); + } + + if (empty($dateString)) { + return new \DateTime('now', new \DateTimeZone($timezone)); + } + + $dateFormatter = $this->analyticsHelper->getAnalyticsDatePickerFormatter($timezone); + $parsedDate = $dateFormatter->parse($dateString); + return (new \DateTime('now', new \DateTimeZone($timezone)))->setTimestamp($parsedDate); + } + public function getTotalCountOfSearches() { return $this->analyticsHelper->getTotalCountOfSearches($this->getAnalyticsParams()); @@ -309,11 +335,12 @@ public function getNoResultSearches() public function checkIsValidDateRange() { if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) { - if (isset($formData['from']) && !empty($formData['from'])) { - $dateTime = $this->getBackendView()->getDateTime(); - $timeZone = $this->getTimeZone(); - $startDate = $dateTime->date($formData['from'], $timeZone, true, false); - $diff = date_diff($startDate, $dateTime->date(null, $timeZone, true, null)); + if (!empty($formData['from'])) { + $timezone = $this->getTimeZone(); + + $startDate = $this->parseFormSubmittedDate($formData['from'], $timezone); + $now = $this->parseFormSubmittedDate(null, $timezone); + $diff = date_diff($startDate, $now); if ($diff->days > $this->getAnalyticRetentionDays()) { return false; From 76ae6f6a2eb05a4e095385e625b7f498f199e6cd Mon Sep 17 00:00:00 2001 From: Eric Wright Date: Wed, 10 Apr 2024 11:03:25 -0400 Subject: [PATCH 2/3] MAGE-866: commit cherry-picked from 864 --- ViewModel/Adminhtml/Analytics/Overview.php | 23 ++++++++++------------ composer.json | 1 + 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/ViewModel/Adminhtml/Analytics/Overview.php b/ViewModel/Adminhtml/Analytics/Overview.php index 0865b4bb1..7ccc91a39 100644 --- a/ViewModel/Adminhtml/Analytics/Overview.php +++ b/ViewModel/Adminhtml/Analytics/Overview.php @@ -7,6 +7,7 @@ use Algolia\AlgoliaSearch\ViewModel\Adminhtml\BackendView; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Locale\ResolverInterface; use Magento\Store\Api\Data\StoreInterface; class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterface @@ -17,15 +18,6 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac public const DEFAULT_RETENTION_DAYS = 90; - /** @var BackendView */ - private $backendView; - - /** @var AnalyticsHelper */ - private $analyticsHelper; - - /** @var IndexEntityDataProvider */ - private $indexEntityDataProvider; - /** @var array */ private $analyticsParams = []; @@ -33,6 +25,7 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac * @param BackendView $backendView * @param AnalyticsHelper $analyticsHelper * @param IndexEntityDataProvider $indexEntityDataProvider + * @param ResolverInterface $localeResolver */ public function __construct( protected BackendView $backendView, @@ -49,9 +42,13 @@ public function getBackendView() return $this->backendView; } - public function getTimeZone() + /** + * @return string + * @throws NoSuchEntityException + */ + public function getTimeZone(): string { - return $this->backendView->getDateTime()->getConfigTimezone( + return (string) $this->backendView->getDateTime()->getConfigTimezone( \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->getStore()->getId() ); @@ -76,7 +73,7 @@ public function getIndexName() * * @return array */ - public function getAnalyticsParams($additional = []) + public function getAnalyticsParams(array $additional = []): array { if (empty($this->analyticsParams)) { $params = ['index' => $this->getIndexName()]; @@ -332,7 +329,7 @@ public function getNoResultSearches() * * @return bool */ - public function checkIsValidDateRange() + public function checkIsValidDateRange(): bool { if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) { if (!empty($formData['from'])) { diff --git a/composer.json b/composer.json index bc18202ab..7d77e5827 100755 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "license": ["MIT"], "version": "3.13.2", "require": { + "php": "~8.1|~8.2", "magento/framework": "~102.0|~103.0", "algolia/algoliasearch-client-php": "3.3.2", "guzzlehttp/guzzle": "^6.3.3|^7.3.0", From 7b089a1b3d002414b1e50ba71b177b582456d05f Mon Sep 17 00:00:00 2001 From: Mohammad Rahman Date: Wed, 29 May 2024 08:49:49 -0400 Subject: [PATCH 3/3] MAGE-866: code updated as per code review --- Helper/AnalyticsHelper.php | 25 ++++++++++++++++--- ViewModel/Adminhtml/Analytics/Overview.php | 29 ++++++++++++++++++---- composer.json | 1 - 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Helper/AnalyticsHelper.php b/Helper/AnalyticsHelper.php index 459cf022a..5987da55e 100644 --- a/Helper/AnalyticsHelper.php +++ b/Helper/AnalyticsHelper.php @@ -14,6 +14,19 @@ class AnalyticsHelper public const ANALYTICS_HITS_PATH = '/2/hits'; public const ANALYTICS_FILTER_PATH = '/2/filters'; public const ANALYTICS_CLICKS_PATH = '/2/clicks'; + + /** @var ConfigHelper */ + private $configHelper; + + /** @var IndexEntityDataProvider */ + private $entityHelper; + + /** @var Logger */ + private $logger; + + /** @var ResolverInterface */ + private $localeResolver; + public const DATE_FORMAT_PICKER = 'dd MMM yyyy'; public const DATE_FORMAT_API = 'Y-m-d'; @@ -55,11 +68,15 @@ class AnalyticsHelper * @param ResolverInterface $localeResolver */ public function __construct( - private ConfigHelper $configHelper, - private IndexEntityDataProvider $entityHelper, - private Logger $logger, - private ResolverInterface $localeResolver + ConfigHelper $configHelper, + IndexEntityDataProvider $entityHelper, + Logger $logger, + ResolverInterface $localeResolver ) { + $this->configHelper = $configHelper; + $this->entityHelper = $entityHelper; + $this->logger = $logger; + $this->localeResolver = $localeResolver; $this->region = $this->configHelper->getAnalyticsRegion(); } diff --git a/ViewModel/Adminhtml/Analytics/Overview.php b/ViewModel/Adminhtml/Analytics/Overview.php index 7ccc91a39..f03b0168e 100644 --- a/ViewModel/Adminhtml/Analytics/Overview.php +++ b/ViewModel/Adminhtml/Analytics/Overview.php @@ -18,21 +18,40 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac public const DEFAULT_RETENTION_DAYS = 90; + /** @var BackendView */ + private $backendView; + + /** @var AnalyticsHelper */ + private $analyticsHelper; + + /** @var IndexEntityDataProvider */ + private $indexEntityDataProvider; + + /** @var ResolverInterface */ + private $localeResolver; + /** @var array */ private $analyticsParams = []; /** + * Index constructor. + * * @param BackendView $backendView * @param AnalyticsHelper $analyticsHelper * @param IndexEntityDataProvider $indexEntityDataProvider * @param ResolverInterface $localeResolver */ public function __construct( - protected BackendView $backendView, - protected AnalyticsHelper $analyticsHelper, - protected IndexEntityDataProvider $indexEntityDataProvider, - protected ResolverInterface $localeResolver - ) { } + BackendView $backendView, + AnalyticsHelper $analyticsHelper, + IndexEntityDataProvider $indexEntityDataProvider, + ResolverInterface $localeResolver + ) { + $this->backendView = $backendView; + $this->analyticsHelper = $analyticsHelper; + $this->indexEntityDataProvider = $indexEntityDataProvider; + $this->localeResolver = $localeResolver; + } /** * @return BackendView diff --git a/composer.json b/composer.json index 7d77e5827..bc18202ab 100755 --- a/composer.json +++ b/composer.json @@ -5,7 +5,6 @@ "license": ["MIT"], "version": "3.13.2", "require": { - "php": "~8.1|~8.2", "magento/framework": "~102.0|~103.0", "algolia/algoliasearch-client-php": "3.3.2", "guzzlehttp/guzzle": "^6.3.3|^7.3.0",