Skip to content

Commit

Permalink
Merge pull request Smile-SA#3122 from rbayet/fix-invalid-tracking-ses…
Browse files Browse the repository at this point in the history
…sions-cleanup

[Tracker] Tools to check and fix invalid behavioral data
  • Loading branch information
rbayet authored Jan 9, 2024
2 parents 294045e + 5ca7d91 commit de6cafb
Show file tree
Hide file tree
Showing 13 changed files with 1,044 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/module-elasticsuite-core/Api/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,22 @@ public function mtermvectors($params);
* @return array
*/
public function reindex(array $params): array;

/**
* Run a deleteByQuery request.
*
* @param array $params Delete by query params.
*
* @return array
*/
public function deleteByQuery(array $params): array;

/**
* Run an updateByQuery request.
*
* @param array $params Delete by query params.
*
* @return array
*/
public function updateByQuery(array $params): array;
}
16 changes: 16 additions & 0 deletions src/module-elasticsuite-core/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@ public function reindex(array $params): array
return $this->getEsClient()->reindex($params);
}

/**
* {@inheritDoc}
*/
public function deleteByQuery(array $params): array
{
return $this->getEsClient()->deleteByQuery($params);
}

/**
* {@inheritDoc}
*/
public function updateByQuery(array $params): array
{
return $this->getEsClient()->updateByQuery($params);
}

/**
* @return \Elasticsearch\Client
*/
Expand Down
100 changes: 100 additions & 0 deletions src/module-elasticsuite-tracker/Console/CheckData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

declare(strict_types = 1);

namespace Smile\ElasticsuiteTracker\Console;

use Magento\Framework\Console\Cli;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteTracker\Model\Data\Checker as DataChecker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Check invalid behavioral data console command.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Richard Bayet <richard.bayet@smile.fr>
*/
class CheckData extends Command
{
/**
* @var DataChecker
*/
private $checker;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
*
* @param DataChecker $checker Data checker.
* @param StoreManagerInterface $storeManager Store manager.
* @param string|null $name The name of the command; passing null means it must be set in configure().
*
* @throws LogicException When the command name is empty
*/
public function __construct(DataChecker $checker, StoreManagerInterface $storeManager, string $name = null)
{
parent::__construct($name);
$this->checker = $checker;
$this->storeManager = $storeManager;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('elasticsuite:tracker:check-data');
$this->setDescription('Check presence of invalid data in indexed tracker data.');
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$progressIndicator = new ProgressIndicator($output, 'verbose', 100, ['', '', '', '', '', '', '', '']);
$progressIndicator->start('Processing...');

$table = new Table($output);
$table->setHeaders(['Store Name', 'Issues (if any)']);
$stores = $this->storeManager->getStores();
foreach ($stores as $store) {
$progressIndicator->advance();
if (!$store->getIsActive()) {
continue;
}
$issuesInfo = '<info>No invalid data.</info>';
$issues = $this->checker->checkData((int) $store->getId());
if (!empty($issues)) {
$issuesInfo = sprintf("<error>%s</error>", join("\n", $issues));
}
$table->addRow([$store->getName(), $issuesInfo]);
}
$progressIndicator->finish('Done');
$table->render();

return Cli::RETURN_SUCCESS;
}
}
101 changes: 101 additions & 0 deletions src/module-elasticsuite-tracker/Console/FixData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

declare(strict_types = 1);

namespace Smile\ElasticsuiteTracker\Console;

use Magento\Framework\Console\Cli;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteTracker\Model\Data\Checker as DataChecker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Fix invalid behavioral data console command.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Richard Bayet <richard.bayet@smile.fr>
*/
class FixData extends Command
{
/**
* @var DataChecker
*/
private $checker;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
*
* @param DataChecker $checker Data checker.
* @param StoreManagerInterface $storeManager Store manager.
* @param string|null $name The name of the command; passing null means it must be set in configure().
*
* @throws LogicException When the command name is empty
*/
public function __construct(DataChecker $checker, StoreManagerInterface $storeManager, string $name = null)
{
parent::__construct($name);
$this->checker = $checker;
$this->storeManager = $storeManager;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('elasticsuite:tracker:fix-data');
$this->setDescription('Check and fix invalid data in indexed tracker data.');
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.StaticAccess)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$progressIndicator = new ProgressIndicator($output, 'verbose', 100, ['', '', '', '', '', '', '', '']);
$progressIndicator->start('Processing...');

$table = new Table($output);
$table->setHeaders(['Store Name', 'Fixed issues (if any)']);
$stores = $this->storeManager->getStores();
foreach ($stores as $store) {
$progressIndicator->advance();
if (!$store->getIsActive()) {
continue;
}
$issuesInfo = '<info>Nothing to fix.</info>';
$issues = $this->checker->checkAndFixData((int) $store->getId());
if (!empty($issues)) {
$issuesInfo = sprintf("<info>%s</info>", join("\n", $issues));
}
$table->addRow([$store->getName(), $issuesInfo]);
}
$progressIndicator->finish('Done');

$table->render();

return Cli::RETURN_SUCCESS;
}
}
92 changes: 92 additions & 0 deletions src/module-elasticsuite-tracker/Model/Data/Checker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

declare(strict_types = 1);

namespace Smile\ElasticsuiteTracker\Model\Data;

use Smile\ElasticsuiteTracker\Model\Data\Checker\DataCheckerInterface;

/**
* Behavioral data checker.
* Relies on checkers to check behavioral data for a given store.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Richard Bayet <richard.bayet@smile.fr>
*/
class Checker
{
/**
* @var DataCheckerInterface[]
*/
private array $checkers;

/**
* Constructor.
*
* @param DataCheckerInterface[] $checkers Data checkers.
*/
public function __construct(array $checkers = [])
{
$this->checkers = $checkers;
}

/**
* Check behavioral data.
*
* @param int $storeId Store id.
*
* @return string[]
*/
public function checkData(int $storeId): array
{
$data = [];

foreach ($this->checkers as &$checker) {
$checkResult = $checker->check($storeId);
if ($checkResult->hasInvalidData()) {
$data[] = $checkResult->getDescription();
}
}

return $data;
}

/**
* Check and fix behavioral data when possible.
*
* @param int $storeId Store id.
*
* @return string[]
*/
public function checkAndFixData(int $storeId): array
{
$data = [];

foreach ($this->checkers as &$checker) {
$checkResult = $checker->check($storeId);
if ($checkResult->hasInvalidData()) {
$status = sprintf("Unfixed: %s", $checkResult->getDescription());
if ($checker->hasDataFixer()) {
if ($checker->getDataFixer()->fixInvalidData($storeId)) {
$status = sprintf("Fixed %s", $checkResult->getDescription());
}
}
$data[] = $status;
}
}

return $data;
}
}
Loading

0 comments on commit de6cafb

Please sign in to comment.