-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[2.2] - Add command to view mview state and queue #12122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-team
merged 18 commits into
magento:2.2-develop
from
convenient:2-2-0-mview-list
Dec 7, 2017
Merged
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9021561
Add command to view mview state and queue
convenient 1e34fde
Make indexer status mview 5.5 compatible
convenient 6d3bffa
Use factories/interfaces correctly
convenient dede2d1
Update code style
convenient fa1b310
Remove the copyright year from file headers
convenient f4857ec
Fix extends phpunit class
convenient c80710a
Add mview getListSize command
convenient 63ec3f7
Remove indexer:status:mview command
convenient d3d300b
Update indexer:status to display schedule backlog
convenient c63330f
Remove status-mview from di.xml
convenient 709f88a
Update method visibility
convenient 831000b
Correctly assert table output
convenient 62e67e1
Code style fixes
convenient 4af04b1
Add ChangelogCounterInterface
convenient 700fe22
Add command to view mview state and queue
ihor-sviziev ca50c9d
Remove backwards breaking ChangelogCounterInterface
convenient 10df2ce
Replace getListSize with getList
convenient 3d34bc8
Remove changes to Changelog
convenient File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php
This file contains hidden or 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,98 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Indexer\Console\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Magento\Framework\Mview\View; | ||
use Magento\Framework\Mview\View\CollectionFactory; | ||
use Magento\Framework\Console\Cli; | ||
|
||
/** | ||
* Command for displaying status of mview indexers. | ||
*/ | ||
class IndexerStatusMviewCommand extends Command | ||
{ | ||
/** @var \Magento\Framework\Mview\View\CollectionInterface $mviewCollection */ | ||
private $mviewCollection; | ||
|
||
public function __construct( | ||
CollectionFactory $collectionFactory | ||
) { | ||
$this->mviewCollection = $collectionFactory->create(); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('indexer:status:mview') | ||
->setDescription('Shows status of Mview Indexers and their queue status'); | ||
|
||
parent::configure(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
try { | ||
$table = $this->getHelperSet()->get('table'); | ||
$table->setHeaders(['ID', 'Mode', 'Status', 'Updated', 'Version ID', 'Backlog']); | ||
|
||
$rows = []; | ||
|
||
/** @var \Magento\Framework\Mview\View $view */ | ||
foreach ($this->mviewCollection as $view) { | ||
$state = $view->getState(); | ||
$changelog = $view->getChangelog(); | ||
|
||
try { | ||
$currentVersionId = $changelog->getVersion(); | ||
} catch (View\ChangelogTableNotExistsException $e) { | ||
continue; | ||
} | ||
|
||
$pendingCount = count($changelog->getList($state->getVersionId(), $currentVersionId)); | ||
|
||
$pendingString = "<error>$pendingCount</error>"; | ||
if ($pendingCount <= 0) { | ||
$pendingString = "<info>$pendingCount</info>"; | ||
} | ||
|
||
$rows[] = [ | ||
$view->getId(), | ||
$state->getMode(), | ||
$state->getStatus(), | ||
$state->getUpdated(), | ||
$state->getVersionId(), | ||
$pendingString, | ||
]; | ||
} | ||
|
||
usort($rows, function ($comp1, $comp2) { | ||
return strcmp($comp1[0], $comp2[0]); | ||
}); | ||
|
||
$table->addRows($rows); | ||
$table->render($output); | ||
|
||
return Cli::RETURN_SUCCESS; | ||
} catch (\Exception $e) { | ||
$output->writeln('<error>' . $e->getMessage() . '</error>'); | ||
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { | ||
$output->writeln($e->getTraceAsString()); | ||
} | ||
|
||
return Cli::RETURN_FAILURE; | ||
} | ||
} | ||
} |
265 changes: 265 additions & 0 deletions
265
app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php
This file contains hidden or 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,265 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Indexer\Test\Unit\Console\Command; | ||
|
||
use \Magento\Framework\Mview; | ||
use Magento\Indexer\Console\Command\IndexerStatusMviewCommand; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\Console\Helper\HelperSet; | ||
use Symfony\Component\Console\Helper\TableHelper; | ||
use Magento\Store\Model\Website; | ||
use Magento\Framework\Console\Cli; | ||
use Magento\Framework\Mview\View\CollectionFactory; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
*/ | ||
class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var IndexerStatusMviewCommand | ||
*/ | ||
private $command; | ||
|
||
/** | ||
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var \Magento\Framework\Mview\View\Collection | ||
*/ | ||
private $collection; | ||
|
||
protected function setUp() | ||
{ | ||
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); | ||
|
||
/** @var \Magento\Framework\Mview\View\Collection $collection */ | ||
$this->collection = $this->objectManager->getObject(Mview\View\Collection::class); | ||
|
||
$reflectedCollection = new \ReflectionObject($this->collection); | ||
$isLoadedProperty = $reflectedCollection->getProperty('_isCollectionLoaded'); | ||
$isLoadedProperty->setAccessible(true); | ||
$isLoadedProperty->setValue($this->collection, true); | ||
|
||
$collectionFactory = $this->getMockBuilder(CollectionFactory::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$collectionFactory->method('create') | ||
->willReturn($this->collection); | ||
|
||
$this->command = $this->objectManager->getObject( | ||
IndexerStatusMviewCommand::class, | ||
['collectionFactory' => $collectionFactory] | ||
); | ||
|
||
/** @var HelperSet $helperSet */ | ||
$helperSet = $this->objectManager->getObject( | ||
HelperSet::class, | ||
['helpers' => [$this->objectManager->getObject(TableHelper::class)]] | ||
); | ||
|
||
//Inject table helper for output | ||
$this->command->setHelperSet($helperSet); | ||
} | ||
|
||
public function testExecute() | ||
{ | ||
$mviews = [ | ||
[ | ||
'view' => [ | ||
'view_id' => 'catalog_category_product', | ||
], | ||
'state' => [ | ||
'mode' => 'enabled', | ||
'status' => 'idle', | ||
'updated' => '2017-01-01 11:11:11', | ||
'version_id' => 100, | ||
], | ||
'changelog' => [ | ||
'version_id' => 110 | ||
], | ||
], | ||
[ | ||
'view' => [ | ||
'view_id' => 'catalog_product_category', | ||
], | ||
'state' => [ | ||
'mode' => 'disabled', | ||
'status' => 'idle', | ||
'updated' => '2017-01-01 11:11:11', | ||
'version_id' => 100, | ||
], | ||
'changelog' => [ | ||
'version_id' => 200 | ||
], | ||
], | ||
[ | ||
'view' => [ | ||
'view_id' => 'catalog_product_attribute', | ||
], | ||
'state' => [ | ||
'mode' => 'enabled', | ||
'status' => 'idle', | ||
'updated' => '2017-01-01 11:11:11', | ||
'version_id' => 100, | ||
], | ||
'changelog' => [ | ||
'version_id' => 100 | ||
], | ||
], | ||
]; | ||
|
||
foreach ($mviews as $data) { | ||
$this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'], $data['state'])); | ||
} | ||
$this->collection->addItem($this->getNeverEnabledMviewIndexerWithNoTable()); | ||
|
||
$tester = new CommandTester($this->command); | ||
$this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([])); | ||
|
||
$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay())); | ||
$this->assertCount(7, $linesOutput, 'There should be 7 lines output. 3 Spacers, 1 header, 3 content.'); | ||
$this->assertEquals($linesOutput[0], $linesOutput[2], "Lines 0, 2, 7 should be spacer lines"); | ||
$this->assertEquals($linesOutput[2], $linesOutput[6], "Lines 0, 2, 6 should be spacer lines"); | ||
|
||
$headerValues = array_values(array_filter(explode('|', $linesOutput[1]))); | ||
$this->assertEquals('ID', trim($headerValues[0])); | ||
$this->assertEquals('Mode', trim($headerValues[1])); | ||
$this->assertEquals('Status', trim($headerValues[2])); | ||
$this->assertEquals('Updated', trim($headerValues[3])); | ||
$this->assertEquals('Version ID', trim($headerValues[4])); | ||
$this->assertEquals('Backlog', trim($headerValues[5])); | ||
|
||
$categoryProduct = array_values(array_filter(explode('|', $linesOutput[3]))); | ||
$this->assertEquals('catalog_category_product', trim($categoryProduct[0])); | ||
$this->assertEquals('enabled', trim($categoryProduct[1])); | ||
$this->assertEquals('idle', trim($categoryProduct[2])); | ||
$this->assertEquals('2017-01-01 11:11:11', trim($categoryProduct[3])); | ||
$this->assertEquals('100', trim($categoryProduct[4])); | ||
$this->assertEquals('10', trim($categoryProduct[5])); | ||
unset($categoryProduct); | ||
|
||
$productAttribute = array_values(array_filter(explode('|', $linesOutput[4]))); | ||
$this->assertEquals('catalog_product_attribute', trim($productAttribute[0])); | ||
$this->assertEquals('enabled', trim($productAttribute[1])); | ||
$this->assertEquals('idle', trim($productAttribute[2])); | ||
$this->assertEquals('2017-01-01 11:11:11', trim($productAttribute[3])); | ||
$this->assertEquals('100', trim($productAttribute[4])); | ||
$this->assertEquals('0', trim($productAttribute[5])); | ||
unset($productAttribute); | ||
|
||
$productCategory = array_values(array_filter(explode('|', $linesOutput[5]))); | ||
$this->assertEquals('catalog_product_category', trim($productCategory[0])); | ||
$this->assertEquals('disabled', trim($productCategory[1])); | ||
$this->assertEquals('idle', trim($productCategory[2])); | ||
$this->assertEquals('2017-01-01 11:11:11', trim($productCategory[3])); | ||
$this->assertEquals('100', trim($productCategory[4])); | ||
$this->assertEquals('100', trim($productCategory[5])); | ||
unset($productCategory); | ||
} | ||
|
||
/** | ||
* @param array $viewData | ||
* @param array $changelogData | ||
* @param array $stateData | ||
* @return Mview\View|Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected function generateMviewStub(array $viewData, array $changelogData, array $stateData) | ||
{ | ||
/** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ | ||
$changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$list = []; | ||
if ($changelogData['version_id'] !== $stateData['version_id']) { | ||
$list = range($stateData['version_id']+1, $changelogData['version_id']); | ||
} | ||
|
||
$changelog->expects($this->any()) | ||
->method('getList') | ||
->willReturn($list); | ||
|
||
$changelog->expects($this->any()) | ||
->method('getVersion') | ||
->willReturn($changelogData['version_id']); | ||
|
||
/** @var \Magento\Indexer\Model\Mview\View\State|\PHPUnit_Framework_MockObject_MockObject $stub */ | ||
$state = $this->getMockBuilder(\Magento\Indexer\Model\Mview\View\State::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['loadByView']) | ||
->getMock(); | ||
|
||
$state->setData($stateData); | ||
|
||
/** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ | ||
$stub = $this->getMockBuilder(\Magento\Framework\Mview\View::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getChangelog', 'getState']) | ||
->getMock(); | ||
|
||
$stub->expects($this->any()) | ||
->method('getChangelog') | ||
->willReturn($changelog); | ||
|
||
$stub->expects($this->any()) | ||
->method('getState') | ||
->willReturn($state); | ||
|
||
$stub->setData($viewData); | ||
|
||
return $stub; | ||
} | ||
|
||
/** | ||
* @return Mview\View|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected function getNeverEnabledMviewIndexerWithNoTable() | ||
{ | ||
/** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ | ||
$changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$changelog->expects($this->any()) | ||
->method('getVersion') | ||
->willThrowException( | ||
new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render")) | ||
); | ||
|
||
/** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */ | ||
$notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$notInitiatedMview->expects($this->any()) | ||
->method('getChangelog') | ||
->willReturn($changelog); | ||
|
||
return $notInitiatedMview; | ||
} | ||
|
||
public function testExecuteExceptionNoVerbosity() | ||
{ | ||
/** @var \Magento\Framework\Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ | ||
$stub = $this->getMockBuilder(Mview\View::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$stub->expects($this->any()) | ||
->method('getChangelog') | ||
->willThrowException(new \Exception("Dummy test exception")); | ||
|
||
$this->collection->addItem($stub); | ||
|
||
$tester = new CommandTester($this->command); | ||
$this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([])); | ||
$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay())); | ||
$this->assertEquals('Dummy test exception', $linesOutput[0]); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that this code will have memory usage issue in case of huge number of item in log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, it's defined as
I'll address.