From ef2671dd36e69d7343091ec6a2a8a102b875fac4 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Wed, 11 Mar 2015 15:11:42 +1100 Subject: [PATCH] Moved the progress helper closure building to a dedicated class --- Command/PopulateCommand.php | 66 +++------------------ Command/ProgressClosureBuilder.php | 94 ++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 59 deletions(-) create mode 100644 Command/ProgressClosureBuilder.php diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php index 3bb6290..e4ed4c9 100644 --- a/Command/PopulateCommand.php +++ b/Command/PopulateCommand.php @@ -23,6 +23,11 @@ class PopulateCommand extends ContainerAwareCommand */ private $indexManager; + /** + * @var ProgressClosureBuilder + */ + private $progressClosureBuilder; + /** * @var ProviderRegistry */ @@ -60,6 +65,7 @@ class PopulateCommand extends ContainerAwareCommand $this->indexManager = $this->getContainer()->get('fos_elastica.index_manager'); $this->providerRegistry = $this->getContainer()->get('fos_elastica.provider_registry'); $this->resetter = $this->getContainer()->get('fos_elastica.resetter'); + $this->progressClosureBuilder = new ProgressClosureBuilder(); if (!$input->getOption('no-overwrite-format')) { ProgressBar::setFormatDefinition('normal', " %current%/%max% [%bar%] %percent:3s%%\n%message%"); @@ -116,69 +122,11 @@ class PopulateCommand extends ContainerAwareCommand */ private function doPopulateType(ProviderInterface $provider, OutputInterface $output, $input, $type, $options) { - $loggerClosure = $this->getLoggerClosure($output, $input, $type); + $loggerClosure = $this->progressClosureBuilder->build($output, 'Populating', $input, $type); $provider->populate($loggerClosure, $options); } - /** - * Builds a loggerClosure to be called from inside the Provider to update the command - * line. - * - * @param OutputInterface $output - * @param string $index - * @param string $type - * @return callable - */ - private function getLoggerClosure(OutputInterface $output, $index, $type) - { - if (!class_exists('Symfony\Component\Console\Helper\ProgressBar')) { - $lastStep = null; - $current = 0; - - return function ($increment, $totalObjects) use ($output, $index, $type, &$lastStep, &$current) { - if ($current + $increment > $totalObjects) { - $increment = $totalObjects - $current; - } - - $currentTime = microtime(true); - $timeDifference = $currentTime - $lastStep; - $objectsPerSecond = $lastStep ? ($increment / $timeDifference) : $increment; - $lastStep = $currentTime; - $current += $increment; - $percent = 100 * $current / $totalObjects; - - $output->writeln(sprintf( - 'Populating %s/%s %0.1f%% (%d/%d), %d objects/s (RAM: current=%uMo peak=%uMo)', - $index, - $type, - $percent, - $current, - $totalObjects, - $objectsPerSecond, - round(memory_get_usage() / (1024 * 1024)), - round(memory_get_peak_usage() / (1024 * 1024)) - )); - }; - } - - $progress = null; - - return function ($increment, $totalObjects) use (&$progress, $output, $index, $type) { - if (null === $progress) { - $progress = new ProgressBar($output, $totalObjects); - $progress->start(); - } - - $progress->setMessage(sprintf('Populating %s/%s', $index, $type)); - $progress->advance($increment); - - if ($progress->getProgressPercent() >= 1.0) { - $progress->finish(); - } - }; - } - /** * Recreates an index, populates its types, and refreshes the index. * diff --git a/Command/ProgressClosureBuilder.php b/Command/ProgressClosureBuilder.php new file mode 100644 index 0000000..c312899 --- /dev/null +++ b/Command/ProgressClosureBuilder.php @@ -0,0 +1,94 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Command; + +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Output\OutputInterface; + +class ProgressClosureBuilder +{ + /** + * Builds a loggerClosure to be called from inside the Provider to update the command + * line. + * + * @param OutputInterface $output + * @param string $action + * @param string $index + * @param string $type + * @return callable + */ + public function build(OutputInterface $output, $action, $index, $type) + { + if (!class_exists('Symfony\Component\Console\Helper\ProgressBar')) { + return $this->buildLegacy($output, $action, $index, $type); + } + + $progress = null; + + return function ($increment, $totalObjects) use (&$progress, $output, $action, $index, $type) { + if (null === $progress) { + $progress = new ProgressBar($output, $totalObjects); + $progress->start(); + } + + $progress->setMessage(sprintf('%s %s/%s', $action, $index, $type)); + $progress->advance($increment); + + if ($progress->getProgressPercent() >= 1.0) { + $progress->finish(); + } + }; + } + + /** + * Builds a legacy closure that outputs lines for each step. Used in cases + * where the ProgressBar component doesnt exist or does not have the correct + * methods to support what we need. + * + * @param OutputInterface $output + * @param string $action + * @param string $index + * @param string $type + * @return callable + */ + private function buildLegacy(OutputInterface $output, $action, $index, $type) + { + $lastStep = null; + $current = 0; + + return function ($increment, $totalObjects) use ($output, $action, $index, $type, &$lastStep, &$current) { + if ($current + $increment > $totalObjects) { + $increment = $totalObjects - $current; + } + + $currentTime = microtime(true); + $timeDifference = $currentTime - $lastStep; + $objectsPerSecond = $lastStep ? ($increment / $timeDifference) : $increment; + $lastStep = $currentTime; + $current += $increment; + $percent = 100 * $current / $totalObjects; + + $output->writeln(sprintf( + '%s %s/%s %0.1f%% (%d/%d), %d objects/s (RAM: current=%uMo peak=%uMo)', + $action, + $index, + $type, + $percent, + $current, + $totalObjects, + $objectsPerSecond, + round(memory_get_usage() / (1024 * 1024)), + round(memory_get_peak_usage() / (1024 * 1024)) + )); + }; + } +}