Skip to content

Commit 6b3dd69

Browse files
committed
5050: Add progressbar to import jobs
1 parent f1e1762 commit 6b3dd69

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/Command/AbstractImportCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Service\BaseImporter;
77
use Doctrine\ORM\EntityManagerInterface;
88
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Helper\ProgressBar;
910
use Symfony\Component\Console\Output\OutputInterface;
1011

1112
abstract class AbstractImportCommand extends Command
@@ -22,13 +23,15 @@ public function __construct(
2223
*
2324
* @throws \Exception
2425
*/
25-
protected function import(string $type, string $src, OutputInterface $output): void
26+
protected function import(string $type, string $src, OutputInterface $output, bool $progress = false): void
2627
{
2728
$success = true;
2829
$errorMessage = null;
2930

31+
$progressBar = $progress ? new ProgressBar($output) : null;
32+
3033
try {
31-
$this->importer->import($src);
34+
$this->importer->import($src, $progressBar);
3235
} catch (\Exception $e) {
3336
$success = false;
3437
$errorMessage = $e->getMessage();

src/Command/ReportImportCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ORM\EntityManagerInterface;
88
use Symfony\Component\Console\Input\InputArgument;
99
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
1011
use Symfony\Component\Console\Output\OutputInterface;
1112

1213
class ReportImportCommand extends AbstractImportCommand
@@ -22,6 +23,7 @@ protected function configure(): void
2223
->setName('itstyr:import:report')
2324
->setDescription('Import reports from feed.')
2425
->addArgument('src', InputArgument::REQUIRED, 'The src of the feed.')
26+
->addOption('progress', 'p', InputOption::VALUE_NONE, 'The src of the feed.')
2527
;
2628
}
2729

@@ -30,7 +32,7 @@ protected function configure(): void
3032
*/
3133
protected function execute(InputInterface $input, OutputInterface $output): int
3234
{
33-
$this->import(Report::class, $input->getArgument('src'), $output);
35+
$this->import(Report::class, $input->getArgument('src'), $output, $input->getOption('progress'));
3436

3537
return 0;
3638
}

src/Service/ImportInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Service;
44

5+
use Symfony\Component\Console\Helper\ProgressBar;
6+
57
interface ImportInterface
68
{
79
/**
@@ -10,5 +12,5 @@ interface ImportInterface
1012
* @param string $src
1113
* Path to the source
1214
*/
13-
public function import(string $src): void;
15+
public function import(string $src, ProgressBar $progressBar = null): void;
1416
}

src/Service/ReportImporter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace App\Service;
44

55
use App\Entity\Report;
6+
use Symfony\Component\Console\Helper\ProgressBar;
67

78
class ReportImporter extends BaseImporter
89
{
9-
public function import(string $src): void
10+
public function import(string $src, ProgressBar $progressBar = null): void
1011
{
1112
$systemURL = getenv('SYSTEM_URL');
1213

@@ -18,6 +19,8 @@ public function import(string $src): void
1819
return;
1920
}
2021

22+
$progressBar?->setMaxSteps(\count($entries));
23+
2124
// List of ids from Anmeldelsesportalen.
2225
$sysInternalIds = [];
2326

@@ -99,8 +102,12 @@ public function import(string $src): void
99102
$report->setSysOwnerSub($subGroupName);
100103
}
101104
}
105+
106+
$progressBar?->advance();
102107
}
103108

109+
$progressBar?->setMessage('Starting archiving ...');
110+
104111
// Archive reports that no longer exist in anmeldelsesportalen.
105112
$this->reportRepository->createQueryBuilder('e')
106113
->update()
@@ -112,6 +119,10 @@ public function import(string $src): void
112119
->execute()
113120
;
114121

122+
$progressBar?->setMessage('Flushing ...');
123+
115124
$this->entityManager->flush();
125+
126+
$progressBar?->finish();
116127
}
117128
}

src/Service/SystemImporter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Repository\SelfServiceAvailableFromItemRepository;
99
use App\Repository\SystemRepository;
1010
use Doctrine\ORM\EntityManagerInterface;
11+
use Symfony\Component\Console\Helper\ProgressBar;
1112

1213
class SystemImporter extends BaseImporter
1314
{
@@ -21,7 +22,7 @@ public function __construct(
2122
parent::__construct($reportRepository, $systemRepository, $groupRepository, $entityManager);
2223
}
2324

24-
public function import(string $src): void
25+
public function import(string $src, ProgressBar $progressBar = null): void
2526
{
2627
$systemURL = getenv('SYSTEM_URL');
2728

@@ -33,6 +34,8 @@ public function import(string $src): void
3334
return;
3435
}
3536

37+
$progressBar?->setMaxSteps(\count($entries));
38+
3639
// List of ids from Systemoversigten.
3740
$sysInternalIds = [];
3841

@@ -137,10 +140,14 @@ public function import(string $src): void
137140
$system->setSysOwnerSub($subGroupName);
138141
}
139142
}
143+
144+
$progressBar?->advance();
140145
}
141146

142147
// Archive systems that no longer exist in Systemoversigten.
143148

149+
$progressBar?->setMessage('Starting archiving ...');
150+
144151
$this->systemRepository->createQueryBuilder('e')
145152
->update()
146153
->set('e.archivedAt', ':now')
@@ -152,6 +159,10 @@ public function import(string $src): void
152159
->execute()
153160
;
154161

162+
$progressBar?->setMessage('Flushing ...');
163+
155164
$this->entityManager->flush();
165+
166+
$progressBar?->finish();
156167
}
157168
}

0 commit comments

Comments
 (0)