From 975b4730b6bd4200cd8f81c7d360a8bfa9fde9ad Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Tue, 16 Jul 2024 11:28:13 +0700 Subject: [PATCH] restore metrics --- .../src/Batch/BatchCommand.php | 1 + .../src/Batch/{ => Internal}/BatchTimer.php | 5 +++- .../CommandBatchProcessorDecorator.php | 27 +++++++++++++------ 3 files changed, 24 insertions(+), 9 deletions(-) rename packages/rekapager-symfony-bridge/src/Batch/{ => Internal}/BatchTimer.php (96%) rename packages/rekapager-symfony-bridge/src/Batch/{ => Internal}/CommandBatchProcessorDecorator.php (85%) diff --git a/packages/rekapager-symfony-bridge/src/Batch/BatchCommand.php b/packages/rekapager-symfony-bridge/src/Batch/BatchCommand.php index 02c720b..ade55c0 100644 --- a/packages/rekapager-symfony-bridge/src/Batch/BatchCommand.php +++ b/packages/rekapager-symfony-bridge/src/Batch/BatchCommand.php @@ -20,6 +20,7 @@ use Rekalogika\Rekapager\Batch\BatchProcess; use Rekalogika\Rekapager\Batch\BatchProcessFactoryInterface; use Rekalogika\Rekapager\Batch\BatchProcessorInterface; +use Rekalogika\Rekapager\Symfony\Batch\Internal\CommandBatchProcessorDecorator; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\SignalableCommandInterface; use Symfony\Component\Console\Input\InputInterface; diff --git a/packages/rekapager-symfony-bridge/src/Batch/BatchTimer.php b/packages/rekapager-symfony-bridge/src/Batch/Internal/BatchTimer.php similarity index 96% rename from packages/rekapager-symfony-bridge/src/Batch/BatchTimer.php rename to packages/rekapager-symfony-bridge/src/Batch/Internal/BatchTimer.php index d7532cc..7da1d63 100644 --- a/packages/rekapager-symfony-bridge/src/Batch/BatchTimer.php +++ b/packages/rekapager-symfony-bridge/src/Batch/Internal/BatchTimer.php @@ -11,10 +11,13 @@ * that was distributed with this source code. */ -namespace Rekalogika\Rekapager\Symfony\Batch; +namespace Rekalogika\Rekapager\Symfony\Batch\Internal; use Rekalogika\Contracts\Rekapager\Exception\LogicException; +/** + * @internal + */ class BatchTimer { public const TIMER_PROCESS = 'process'; diff --git a/packages/rekapager-symfony-bridge/src/Batch/CommandBatchProcessorDecorator.php b/packages/rekapager-symfony-bridge/src/Batch/Internal/CommandBatchProcessorDecorator.php similarity index 85% rename from packages/rekapager-symfony-bridge/src/Batch/CommandBatchProcessorDecorator.php rename to packages/rekapager-symfony-bridge/src/Batch/Internal/CommandBatchProcessorDecorator.php index b64a412..980e573 100644 --- a/packages/rekapager-symfony-bridge/src/Batch/CommandBatchProcessorDecorator.php +++ b/packages/rekapager-symfony-bridge/src/Batch/Internal/CommandBatchProcessorDecorator.php @@ -11,7 +11,7 @@ * that was distributed with this source code. */ -namespace Rekalogika\Rekapager\Symfony\Batch; +namespace Rekalogika\Rekapager\Symfony\Batch\Internal; use Rekalogika\Rekapager\Batch\BatchProcessorDecorator; use Rekalogika\Rekapager\Batch\BatchProcessorInterface; @@ -20,6 +20,7 @@ use Rekalogika\Rekapager\Batch\Event\BeforePageEvent; use Rekalogika\Rekapager\Batch\Event\BeforeProcessEvent; use Rekalogika\Rekapager\Batch\Event\InterruptEvent; +use Rekalogika\Rekapager\Batch\Event\ItemEvent; use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Style\SymfonyStyle; @@ -27,10 +28,13 @@ * @template TKey of array-key * @template T * @extends BatchProcessorDecorator + * @internal */ class CommandBatchProcessorDecorator extends BatchProcessorDecorator { private readonly BatchTimer $timer; + private int $pageNumber = 0; + private int $itemNumber = 0; /** * @param BatchProcessorInterface $decorated @@ -71,14 +75,13 @@ public function afterProcess(AfterProcessEvent $event): void unlink($this->progressFile); } - $batchDuration = $this->timer->stop(BatchTimer::TIMER_PROCESS); - $this->io->success('Batch process completed'); $this->showStats($event); } public function beforePage(BeforePageEvent $event): void { + $this->pageNumber++; $this->timer->start(BatchTimer::TIMER_PAGE); if ($this->progressFile !== null) { @@ -108,6 +111,13 @@ public function afterPage(AfterPageEvent $event): void } } + public function processItem(ItemEvent $itemEvent): void + { + $this->itemNumber++; + + $this->decorated->processItem($itemEvent); + } + public function onInterrupt(InterruptEvent $event): void { $this->decorated->onInterrupt($event); @@ -138,13 +148,14 @@ public function onInterrupt(InterruptEvent $event): void private function showStats(AfterPageEvent|AfterProcessEvent|InterruptEvent $event): void { $this->io->writeln(''); + $processDuration = $this->timer->getDuration(BatchTimer::TIMER_PROCESS); $this->io->definitionList( - // ['Time elapsed' => Helper::formatTime($event->getProcessDuration())], + ['Time elapsed' => Helper::formatTime($processDuration ?? 0)], + ['Page processed' => $this->pageNumber], + ['Item processed' => $this->itemNumber], ['Memory usage' => Helper::formatMemory(memory_get_usage(true))], - // ['Pages processed' => $event->getPagesProcessed()], - // ['Items processed' => $event->getItemsProcessed()], - // ['Pages/minute' => round($event->getPagesProcessed() / $event->getProcessDuration() * 60, 2)], - // ['Items/minute' => round($event->getItemsProcessed() / $event->getProcessDuration() * 60, 2)], + ['Pages/minute' => round($this->pageNumber / $processDuration * 60, 2)], + ['Items/minute' => round($this->itemNumber / $processDuration * 60, 2)], ); } }