Skip to content

Commit

Permalink
Merge branch '2.11.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Jan 9, 2025
2 parents 4c975a3 + ba7354d commit 67ef5e7
Show file tree
Hide file tree
Showing 24 changed files with 325 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Smile\ElasticsuiteAnalytics\Model\Report\Context as ReportContext;

/**
* Block used to display customer company selector in reports.
Expand Down Expand Up @@ -47,6 +48,11 @@ class CustomerCompanySelector extends Template
*/
protected $searchCriteriaBuilder;

/**
* @var ReportContext
*/
protected $reportContext;

/**
* @var \Magento\Company\Api\CompanyRepositoryInterface|null
*/
Expand All @@ -61,6 +67,7 @@ class CustomerCompanySelector extends Template
* @param ModuleManager $moduleManager Module manager.
* @param ScopeConfigInterface $scopeConfig Scope configuration.
* @param SearchCriteriaBuilder $searchCriteriaBuilder The search criteria builder.
* @param ReportContext $reportContext Report context.
* @param array $data Additional block data.
* @throws LocalizedException
*/
Expand All @@ -69,10 +76,12 @@ public function __construct(
ModuleManager $moduleManager,
ScopeConfigInterface $scopeConfig,
SearchCriteriaBuilder $searchCriteriaBuilder,
ReportContext $reportContext,
array $data = []
) {
$this->scopeConfig = $scopeConfig;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->reportContext = $reportContext;

// Check if Magento_Company module is enabled before attempting to load the repository.
if ($moduleManager->isEnabled('Magento_Company')) {
Expand Down Expand Up @@ -117,4 +126,14 @@ public function getCompaniesList()

return [];
}

/**
* Get customer company ID.
*
* @return mixed
*/
public function getCustomerCompanyId()
{
return $this->reportContext->getCustomerCompanyId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
use Smile\ElasticsuiteAnalytics\Model\Report\Context as ReportContext;

/**
* Block used to display customer group selector in reports.
Expand All @@ -32,19 +33,27 @@ class CustomerGroupSelector extends Template
*/
protected $customerGroupCollectionFactory;

/**
* @var ReportContext
*/
protected $reportContext;

/**
* CustomerGroupSelector constructor.
*
* @param Template\Context $context The context of the template.
* @param CollectionFactory $customerGroupCollectionFactory Factory for creating customer group collection.
* @param ReportContext $reportContext Report context.
* @param array $data Additional block data.
*/
public function __construct(
Template\Context $context,
CollectionFactory $customerGroupCollectionFactory,
ReportContext $reportContext,
array $data = []
) {
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
$this->reportContext = $reportContext;
parent::__construct($context, $data);
}

Expand All @@ -57,4 +66,14 @@ public function getCustomerGroups()
{
return $this->customerGroupCollectionFactory->create()->toOptionArray();
}

/**
* Get customer group ID.
*
* @return mixed
*/
public function getCurrentCustomerGroupId()
{
return $this->reportContext->getCustomerGroupId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;
use Smile\ElasticsuiteCore\Search\Request\MetricInterface;
use Smile\ElasticsuiteAnalytics\Model\Report\AggregationProviderInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Default AggregationProvider
Expand Down Expand Up @@ -84,6 +85,7 @@ public function getAggregation()
'name' => 'search_terms',
'metrics' => $this->getMetrics(),
'pipelines' => $this->getPipelines(),
'childBuckets' => [$this->getFilteredResultCountMetric()],
'sortOrder' => ['unique_sessions' => 'desc'],
'size' => $this->helper->getMaxSearchTerms(),
];
Expand All @@ -105,9 +107,7 @@ protected function getMetrics()
$this->metricFactory->create(
['name' => 'unique_visitors', 'field' => 'session.vid', 'type' => MetricInterface::TYPE_CARDINALITY]
),
$this->metricFactory->create(
['name' => 'result_count', 'field' => 'page.product_list.product_count', 'type' => MetricInterface::TYPE_AVG]
),
// Metrics result_count moved to a sub-aggregation to ignore filtered search pages.
];

return $metrics;
Expand All @@ -124,4 +124,38 @@ protected function getPipelines()

return $pipelines;
}

/**
* Return aggregation providing the filtered (no filtered search pages) result/product count metrics.
*
* @return BucketInterface
*/
protected function getFilteredResultCountMetric()
{
return $this->aggregationFactory->create(
BucketInterface::TYPE_METRIC,
[
'name' => 'result_count',
'metricType' => MetricInterface::TYPE_AVG,
'field' => 'page.product_list.product_count',
'filter' => $this->queryFactory->create(
QueryInterface::TYPE_BOOL,
[
'mustNot' => [
$this->queryFactory->create(
QueryInterface::TYPE_NESTED,
[
'path' => 'page.product_list.filters',
'query' => $this->queryFactory->create(
QueryInterface::TYPE_EXISTS,
['field' => 'page.product_list.filters']
),
]
),
],
]
),
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct(

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function processResponse(\Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Response\QueryResponse $response)
{
Expand All @@ -63,7 +64,14 @@ protected function processResponse(\Smile\ElasticsuiteCore\Search\Adapter\Elasti
'conversion_rate' => number_format(0, 2),
];
if (array_key_exists('result_count', $value->getMetrics())) {
$data[$searchTerm]['result_count'] = round((float) $value->getMetrics()['result_count'] ?: 0);
$resultCountMetrics = $value->getMetrics()['result_count'];
if (is_array($resultCountMetrics)
&& array_key_exists('result_count', $resultCountMetrics)
&& array_key_exists('value', $resultCountMetrics['result_count'])
) {
$resultCountMetrics = $resultCountMetrics['result_count']['value'] ?: 0;
}
$data[$searchTerm]['result_count'] = round((float) $resultCountMetrics ?: 0);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function getPipelines()
PipelineInterface::TYPE_BUCKET_SELECTOR,
[
'name' => 'result_count_filter',
'bucketsPath' => ['avg_result_count' => 'result_count'],
'bucketsPath' => ['avg_result_count' => 'result_count.result_count'],
'script' => 'params.avg_result_count > 0',
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerCompanySelector $block
*/
$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'company_id' => '__company_id__']);
$companyId = $block->getCustomerCompanyId();
?>
<?php if ($block->isCompanyEnabled()): ?>
<?php $companies = $block->getCompaniesList(); ?>
Expand All @@ -25,15 +27,17 @@
<select id="company_id" name="company_id" class="admin__control-select">
<option value="all"><?= __('All Companies');?></option>
<?php foreach ($companies as $company): ?>
<option value="<?= $company->getId(); ?>"><?= $company->getCompanyName(); ?></option>
<option value="<?= $block->escapeHtmlAttr($company->getId()); ?>" <?php if ($companyId == $company->getId()): ?> selected="selected"<?php endif; ?>><?= $block->escapeHtml($company->getCompanyName()); ?></option>
<?php endforeach; ?>
</select>
</div>

<script type="text/x-magento-init">
{
"*": {
"customerCompanySelector": {}
"customerCompanySelector": {
"baseUrl": "<?= $block->escapeJs($block->escapeUrl($baseUrl)) ?>"
}
}
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@
* @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerGroupSelector $block
*/
$customerGroups = $block->getCustomerGroups();
$customerGroupId = $block->getCurrentCustomerGroupId();
$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'customer_group' => '__customer_group__']);
?>
<div class="customer-group-selector">
<label for="customer_group"><?= __('Customer Group:');?></label>
<select id="customer_group" name="customer_group" class="admin__control-select">
<option value="all"><?= __('All Customer Groups');?></option>
<?php foreach ($customerGroups as $group): ?>
<option value="<?php echo $group['value']; ?>"><?php echo $group['label']; ?></option>
<option value="<?= $block->escapeHtmlAttr($group['value']); ?>" <?php if ($customerGroupId == $group['value']): ?> selected="selected"<?php endif; ?>><?= $block->escapeHtml($group['label']); ?></option>
<?php endforeach; ?>
</select>
</div>

<script type="text/x-magento-init">
{
"*": {
"customerGroupSelector": {}
"customerGroupSelector": {
"baseUrl": "<?= $block->escapeJs($block->escapeUrl($baseUrl)) ?>"
}
}
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
require(['jquery', 'mage/calendar', 'mage/adminhtml/tools'], function ($) {
$('#<?= $block->getJsId('date-range-picker'); ?>').dateRange(<?= $block->getJsConfig(); ?>);
$('#<?= $block->getJsId('date-range-picker', 'apply'); ?>').on('click', function() {
var url = "<?= $block->getUrl('*/*/*', ['from' => '__from__', 'to' => '__to__']); ?>"
let url = "<?= $block->getUrl('*/*/*', ['_current' => true, 'from' => '__from__', 'to' => '__to__']); ?>"
.replace('__from__', Base64.encode($("#<?= $block->getJsId('date-range-picker', 'from'); ?>")[0].value))
.replace('__to__', Base64.encode($("#<?= $block->getJsId('date-range-picker', 'to'); ?>")[0].value));
window.location = url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@
*/

define('Smile_ElasticsuiteAnalytics/js/report/customer-company-selector', [
'jquery',
'mage/url'
], function($, urlBuilder) {
'jquery'
], function($) {
'use strict';

return function() {
// On document ready, set the selected value in the company dropdown.
$(document).ready(function() {
var urlParams = new URLSearchParams(window.location.search);
var selectedCompany = urlParams.get('company_id');

if (selectedCompany) {
$('#company_id').val(selectedCompany);
}
});

return function(config) {
// Handle the company dropdown value change.
$('#company_id').on('change', function() {
var selectedCompany = $(this).val();
var newUrl = new URL(window.location.href);

newUrl.searchParams.set('company_id', selectedCompany);
let selectedCompany = $(this).val();

// Redirect to the new URL with the company filter.
window.location.href = newUrl.href;
window.location = config.baseUrl.replace('__company_id__', selectedCompany);
});
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@
*/

define('Smile_ElasticsuiteAnalytics/js/report/customer-group-selector', [
'jquery',
'mage/url'
], function($, urlBuilder) {
'jquery'
], function($) {
'use strict';

return function() {
// !On document ready, set the selected value in the customer group dropdown.
$(document).ready(function() {
var urlParams = new URLSearchParams(window.location.search);
var selectedGroup = urlParams.get('customer_group');

if (selectedGroup) {
$('#customer_group').val(selectedGroup);
}
});

return function(config) {
// Handle the customer group dropdown value change.
$('#customer_group').on('change', function() {
var selectedGroup = $(this).val();
var newUrl = new URL(window.location.href);

newUrl.searchParams.set('customer_group', selectedGroup);
let selectedGroup = $(this).val();

// Redirect to the new URL with the customer group filter.
window.location.href = newUrl.href;
window.location = config.baseUrl.replace('__customer_group__', selectedGroup);
});
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function getElasticSuiteIndices(): ?array
IndexStatus::GHOST_STATUS,
IndexStatus::EXTERNAL_STATUS,
IndexStatus::UNDEFINED_STATUS,
IndexStatus::CLOSED_STATUS,
];
$indices = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class IndexStatus extends AbstractRenderer
public const REBUILDING_STATUS = 'rebuilding';
public const GHOST_STATUS = 'ghost';
public const EXTERNAL_STATUS = 'external';
public const CLOSED_STATUS = 'closed';
public const UNDEFINED_STATUS = 'undefined';

/**
Expand All @@ -45,6 +46,7 @@ class IndexStatus extends AbstractRenderer
self::REBUILDING_STATUS => self::SEVERITY_MINOR,
self::GHOST_STATUS => self::SEVERITY_CRITICAL,
self::EXTERNAL_STATUS => self::SEVERITY_EXTERNAL,
self::CLOSED_STATUS => self::SEVERITY_MINOR,
self::UNDEFINED_STATUS => self::SEVERITY_UNDEFINED,
];

Expand Down
9 changes: 5 additions & 4 deletions src/module-elasticsuite-indices/Model/IndexStatsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ public function deleteIndex($indexName): void
public function indexStats($indexName, $alias): array
{
$data = [
'index_name' => $indexName,
'index_alias' => $alias,
'size' => 'undefined',
'index_name' => $indexName,
'index_alias' => $alias,
'number_of_documents' => 'undefined',
'size' => 'undefined',
];

try {
Expand All @@ -144,7 +145,7 @@ public function indexStats($indexName, $alias): array
sprintf('Error when loading/parsing statistics for index "%s"', $indexName),
['exception' => $e]
);
$data['index_status'] = IndexStatus::UNDEFINED_STATUS;
$data['index_status'] = IndexStatus::CLOSED_STATUS;
}

return $data;
Expand Down
Loading

0 comments on commit 67ef5e7

Please sign in to comment.