Skip to content

Commit

Permalink
MAGE-866: commit cherry-picked from 864
Browse files Browse the repository at this point in the history
  • Loading branch information
cammonro authored and mrahman3177 committed May 21, 2024
1 parent 5ad5942 commit 8aa0274
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 46 deletions.
46 changes: 21 additions & 25 deletions Helper/AnalyticsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@
use Algolia\AlgoliaSearch\Config\AnalyticsConfig;
use Algolia\AlgoliaSearch\DataProvider\Analytics\IndexEntityDataProvider;
use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory;
use Magento\Framework\Locale\ResolverInterface;

class AnalyticsHelper
{
public const ANALYTICS_SEARCH_PATH = '/2/searches';
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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}

}
69 changes: 48 additions & 21 deletions ViewModel/Adminhtml/Analytics/Overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']);
}
}

Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8aa0274

Please sign in to comment.