Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Jan 9, 2022
1 parent 25b35d0 commit 8a364ce
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 133 deletions.
8 changes: 4 additions & 4 deletions src/CliCommandMultiProc.php
Expand Up @@ -18,9 +18,9 @@
namespace JBZoo\Cli;

use BluePsyduck\SymfonyProcessManager\ProcessManager;
use JBZoo\Cli\ProgressBars\ProgressBarProcessManager;
use JBZoo\Utils\Cli;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;

Expand All @@ -41,7 +41,7 @@ abstract class CliCommandMultiProc extends CliCommand
private $procPool = [];

/**
* @var ProgressBar|null
* @var ProgressBarProcessManager|null
*/
private $progressBar;

Expand Down Expand Up @@ -90,7 +90,7 @@ protected function configure(): void
'pm-no-progress',
null,
InputOption::VALUE_NONE,
'Process Manager. Show progress bar to additinal information.'
'Process Manager. Show progress bar to additional information.'
);

parent::configure();
Expand Down Expand Up @@ -152,7 +152,7 @@ protected function executeMultiProcessAction(): int
$procListIds = $this->getListOfChildIds();

if (!$this->getOptBool('pm-no-progress')) {
$this->progressBar = new ProgressBar($this->output, \count($procListIds));
$this->progressBar = new ProgressBarProcessManager($this->output, \count($procListIds));
$this->progressBar->start();
}

Expand Down
155 changes: 155 additions & 0 deletions src/ProgressBars/AbstractProgressBar.php
@@ -0,0 +1,155 @@
<?php

/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/

declare(strict_types=1);

namespace JBZoo\Cli\ProgressBars;

use JBZoo\Utils\Arr;
use JBZoo\Utils\Dates;
use JBZoo\Utils\FS;
use JBZoo\Utils\Stats;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Helper\Helper as SymfonyHelper;
use Symfony\Component\Console\Helper\ProgressBar as SymfonyProgressBar;

/**
* Class AbstractProgressBar
* @package JBZoo\Cli\ProgressBars
*/
abstract class AbstractProgressBar
{
public const BREAK = '<yellow>Progress stopped</yellow>';

public const MAX_LINE_LENGTH = 80;

/**
* @var int[]
*/
protected $stepMemoryDiff = [];

/**
* @var float[]
*/
protected $stepTimers = [];

/**
* @return string
*/
abstract protected function buildTemplate(): string;

/**
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function configureProgressBar(): void
{
static $inited;

if ($inited) {
return;
}

$inited = true;

// Memory
self::setPlaceholder('jbzoo_memory_current', static function (): string {
return SymfonyHelper::formatMemory(\memory_get_usage(false));
});

self::setPlaceholder('jbzoo_memory_peak', static function (): string {
return SymfonyHelper::formatMemory(\memory_get_peak_usage(false));
});

self::setPlaceholder('jbzoo_memory_limit', static function (): string {
return Sys::iniGet('memory_limit');
});

self::setPlaceholder('jbzoo_memory_step_avg', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) {
return 'n/a';
}

return FS::format((int)Stats::mean($this->stepMemoryDiff));
});

self::setPlaceholder('jbzoo_memory_step_last', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) {
return 'n/a';
}

return FS::format(Arr::last($this->stepMemoryDiff));
});

// Timers
self::setPlaceholder('jbzoo_time_elapsed', static function (SymfonyProgressBar $bar): string {
return Dates::formatTime(\time() - $bar->getStartTime());
});

self::setPlaceholder('jbzoo_time_remaining', static function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps()) {
return 'n/a';
}

if (!$bar->getProgress()) {
$remaining = 0;
} else {
$remaining = \round(
(\time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress())
);
}

return Dates::formatTime($remaining);
});

self::setPlaceholder('jbzoo_time_estimated', static function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps()) {
return 'n/a';
}

if (!$bar->getProgress()) {
$estimated = 0;
} else {
$estimated = \round((\time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
}

return Dates::formatTime($estimated);
});

self::setPlaceholder('jbzoo_time_step_avg', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) {
return 'n/a';
}

return \str_replace('±', '<blue>±</blue>', Stats::renderAverage($this->stepTimers)) . ' sec';
});

self::setPlaceholder('jbzoo_time_step_last', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) {
return 'n/a';
}

return Dates::formatTime(Arr::last($this->stepTimers));
});
}

/**
* @param string $name
* @param callable $callable
*/
public static function setPlaceholder(string $name, callable $callable): void
{
SymfonyProgressBar::setPlaceholderFormatterDefinition($name, $callable);
}
}
26 changes: 26 additions & 0 deletions src/ProgressBars/Exception.php
@@ -0,0 +1,26 @@
<?php

/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/

declare(strict_types=1);

namespace JBZoo\Cli\ProgressBars;

/**
* Class Exception
* @package JBZoo\Cli\ProgressBars
*/
class Exception extends \JBZoo\Cli\Exception
{
}
133 changes: 6 additions & 127 deletions src/ProgressBar.php → src/ProgressBars/ProgressBar.php
Expand Up @@ -15,29 +15,21 @@

declare(strict_types=1);

namespace JBZoo\Cli;
namespace JBZoo\Cli\ProgressBars;

use Countable;
use JBZoo\Utils\Arr;
use JBZoo\Utils\Dates;
use JBZoo\Utils\FS;
use JBZoo\Utils\Stats;
use JBZoo\Cli\Helper;
use JBZoo\Cli\Icons;
use JBZoo\Utils\Str;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Helper\Helper as SymfonyHelper;
use Symfony\Component\Console\Helper\ProgressBar as SymfonyProgressBar;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class ProgressBar
* @package JBZoo\Cli
* @package JBZoo\Cli\ProgressBars
*/
class ProgressBar
class ProgressBar extends AbstractProgressBar
{
public const BREAK = '<yellow>Progress stopped</yellow>';

private const MAX_LINE_LENGTH = 80;

/**
* @var OutputInterface
*/
Expand Down Expand Up @@ -68,16 +60,6 @@ class ProgressBar
*/
private $max = 0;

/**
* @var float[]
*/
private $stepTimers = [];

/**
* @var int[]
*/
private $stepMemoryDiff = [];

/**
* @var \Closure|null
*/
Expand Down Expand Up @@ -382,7 +364,7 @@ private function createProgressBar(): ?SymfonyProgressBar
/**
* @return string
*/
private function buildTemplate(): string
protected function buildTemplate(): string
{
$progressBarLines = [];
$footerLine = [];
Expand Down Expand Up @@ -446,109 +428,6 @@ private function buildTemplate(): string
return \implode("\n", $progressBarLines) . "\n" . Helper::renderList($footerLine) . "\n";
}

/**
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
private function configureProgressBar(): void
{
static $inited;

if ($inited) {
return;
}

$inited = true;

// Memory
self::setPlaceholder('jbzoo_memory_current', static function (): string {
return SymfonyHelper::formatMemory(\memory_get_usage(false));
});

self::setPlaceholder('jbzoo_memory_peak', static function (): string {
return SymfonyHelper::formatMemory(\memory_get_peak_usage(false));
});

self::setPlaceholder('jbzoo_memory_limit', static function (): string {
return Sys::iniGet('memory_limit');
});

self::setPlaceholder('jbzoo_memory_step_avg', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) {
return 'n/a';
}

return FS::format((int)Stats::mean($this->stepMemoryDiff));
});

self::setPlaceholder('jbzoo_memory_step_last', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) {
return 'n/a';
}

return FS::format(Arr::last($this->stepMemoryDiff));
});

// Timers
self::setPlaceholder('jbzoo_time_elapsed', static function (SymfonyProgressBar $bar): string {
return Dates::formatTime(\time() - $bar->getStartTime());
});

self::setPlaceholder('jbzoo_time_remaining', static function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps()) {
return 'n/a';
}

if (!$bar->getProgress()) {
$remaining = 0;
} else {
$remaining = \round(
(\time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress())
);
}

return Dates::formatTime($remaining);
});

self::setPlaceholder('jbzoo_time_estimated', static function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps()) {
return 'n/a';
}

if (!$bar->getProgress()) {
$estimated = 0;
} else {
$estimated = \round((\time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
}

return Dates::formatTime($estimated);
});

self::setPlaceholder('jbzoo_time_step_avg', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) {
return 'n/a';
}

return \str_replace('±', '<blue>±</blue>', Stats::renderAverage($this->stepTimers)) . ' sec';
});

self::setPlaceholder('jbzoo_time_step_last', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) {
return 'n/a';
}

return Dates::formatTime(Arr::last($this->stepTimers));
});
}

/**
* @param string $name
* @param callable $callable
*/
public static function setPlaceholder(string $name, callable $callable): void
{
SymfonyProgressBar::setPlaceholderFormatterDefinition($name, $callable);
}

/**
* @param array $exceptionMessages
*/
Expand Down

0 comments on commit 8a364ce

Please sign in to comment.