forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Smile-SA#3122 from rbayet/fix-invalid-tracking-ses…
…sions-cleanup [Tracker] Tools to check and fix invalid behavioral data
- Loading branch information
Showing
13 changed files
with
1,044 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.