Skip to content

Commit

Permalink
Merge pull request #1515 from algolia/feature/MAGE-866
Browse files Browse the repository at this point in the history
Feature/mage 866
  • Loading branch information
cammonro authored May 29, 2024
2 parents 5ad5942 + 7b089a1 commit 2f2c150
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 27 deletions.
33 changes: 23 additions & 10 deletions Helper/AnalyticsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -14,9 +15,6 @@ class AnalyticsHelper
public const ANALYTICS_FILTER_PATH = '/2/filters';
public const ANALYTICS_CLICKS_PATH = '/2/clicks';

/** @var AlgoliaHelper */
private $algoliaHelper;

/** @var ConfigHelper */
private $configHelper;

Expand All @@ -26,6 +24,12 @@ class AnalyticsHelper
/** @var Logger */
private $logger;

/** @var ResolverInterface */
private $localeResolver;

public const DATE_FORMAT_PICKER = 'dd MMM yyyy';
public const DATE_FORMAT_API = 'Y-m-d';

private $searches;
private $users;
private $rateOfNoResults;
Expand Down Expand Up @@ -58,25 +62,21 @@ 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'
ResolverInterface $localeResolver
) {
$this->algoliaHelper = $algoliaHelper;
$this->configHelper = $configHelper;

$this->entityHelper = $entityHelper;

$this->logger = $logger;
$this->localeResolver = $localeResolver;
$this->region = $this->configHelper->getAnalyticsRegion();
}

Expand Down Expand Up @@ -371,4 +371,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;
}

}
77 changes: 60 additions & 17 deletions ViewModel/Adminhtml/Analytics/Overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,24 +27,30 @@ class Overview implements \Magento\Framework\View\Element\Block\ArgumentInterfac
/** @var IndexEntityDataProvider */
private $indexEntityDataProvider;

/** @var ResolverInterface */
private $localeResolver;

/** @var array */
private $analyticsParams = [];

/**
* Index constructor.
* Index constructor.
*
* @param BackendView $backendView
* @param AnalyticsHelper $analyticsHelper
* @param IndexEntityDataProvider $indexEntityDataProvider
* @param ResolverInterface $localeResolver
*/
public function __construct(
BackendView $backendView,
AnalyticsHelper $analyticsHelper,
IndexEntityDataProvider $indexEntityDataProvider
IndexEntityDataProvider $indexEntityDataProvider,
ResolverInterface $localeResolver
) {
$this->backendView = $backendView;
$this->analyticsHelper = $analyticsHelper;
$this->indexEntityDataProvider = $indexEntityDataProvider;
$this->localeResolver = $localeResolver;
}

/**
Expand All @@ -54,9 +61,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()
);
Expand All @@ -81,18 +92,16 @@ public function getIndexName()
*
* @return array
*/
public function getAnalyticsParams($additional = [])
public function getAnalyticsParams(array $additional = []): array
{
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 +111,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 @@ -306,14 +348,15 @@ public function getNoResultSearches()
*
* @return bool
*/
public function checkIsValidDateRange()
public function checkIsValidDateRange(): bool
{
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 2f2c150

Please sign in to comment.