diff --git a/assets/controllers/import_status_controller.js b/assets/controllers/import_status_controller.js index 6ffeb68..88019fd 100644 --- a/assets/controllers/import_status_controller.js +++ b/assets/controllers/import_status_controller.js @@ -56,11 +56,11 @@ export default class extends Controller { this.importBtnTarget.disabled = true; this.importBtnTarget.textContent = 'Import en cours\u2026'; - const progress = data.totalBatches > 0 - ? Math.round((data.processedBatches / data.totalBatches) * 100) + const progress = data.totalFilms > 0 + ? Math.round((data.processedFilms / data.totalFilms) * 100) : 0; - this._setStatus(`${progress}% — ${data.totalFilms} films`, 'active'); + this._setStatus(`${progress}% — ${data.processedFilms}/${data.totalFilms} films`, 'active'); } _showCompleted(data) { diff --git a/migrations/Version20260401000002.php b/migrations/Version20260401000002.php new file mode 100644 index 0000000..71e6dd4 --- /dev/null +++ b/migrations/Version20260401000002.php @@ -0,0 +1,30 @@ +addSql('ALTER TABLE import ADD processed_films INT NOT NULL DEFAULT 0'); + $this->addSql('ALTER TABLE import DROP COLUMN total_batches'); + $this->addSql('ALTER TABLE import DROP COLUMN processed_batches'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE import DROP COLUMN processed_films'); + $this->addSql('ALTER TABLE import ADD total_batches INT NOT NULL DEFAULT 0'); + $this->addSql('ALTER TABLE import ADD processed_batches INT NOT NULL DEFAULT 0'); + } +} diff --git a/src/Controller/ImportController.php b/src/Controller/ImportController.php index 0fa03bb..47779c1 100644 --- a/src/Controller/ImportController.php +++ b/src/Controller/ImportController.php @@ -37,9 +37,8 @@ class ImportController extends AbstractController 'id' => $import->getId(), 'status' => $import->getStatus(), 'totalFilms' => $import->getTotalFilms(), + 'processedFilms' => $import->getProcessedFilms(), 'failedFilms' => $import->getFailedFilms(), - 'processedBatches' => $import->getProcessedBatches(), - 'totalBatches' => $import->getTotalBatches(), ]); } diff --git a/src/Entity/Import.php b/src/Entity/Import.php index 2e6c88b..11a9a97 100644 --- a/src/Entity/Import.php +++ b/src/Entity/Import.php @@ -30,15 +30,12 @@ class Import #[ORM\Column(length: 20)] private string $status = self::STATUS_PENDING; - #[ORM\Column] - private int $totalBatches = 0; - - #[ORM\Column] - private int $processedBatches = 0; - #[ORM\Column] private int $totalFilms = 0; + #[ORM\Column] + private int $processedFilms = 0; + #[ORM\Column] private int $failedFilms = 0; @@ -91,25 +88,14 @@ class Import return $this; } - public function getTotalBatches(): int + public function getProcessedFilms(): int { - return $this->totalBatches; + return $this->processedFilms; } - public function setTotalBatches(int $totalBatches): static + public function setProcessedFilms(int $processedFilms): static { - $this->totalBatches = $totalBatches; - return $this; - } - - public function getProcessedBatches(): int - { - return $this->processedBatches; - } - - public function setProcessedBatches(int $processedBatches): static - { - $this->processedBatches = $processedBatches; + $this->processedFilms = $processedFilms; return $this; } diff --git a/src/MessageHandler/ImportFilmsBatchMessageHandler.php b/src/MessageHandler/ImportFilmsBatchMessageHandler.php index 8e6e4c4..968d662 100644 --- a/src/MessageHandler/ImportFilmsBatchMessageHandler.php +++ b/src/MessageHandler/ImportFilmsBatchMessageHandler.php @@ -54,29 +54,27 @@ readonly class ImportFilmsBatchMessageHandler $movie = $this->filmImporter->importFromLtbxdMovie($ltbxdMovie); if (!$movie) { $this->importRepository->incrementFailedFilms($import); - continue; + } else { + $this->actorSyncer->syncActorsForMovie($movie); + + foreach ($movie->getActors() as $role) { + $this->awardImporter->importForActor($role->getActor()); + } + + $user = $this->em->getReference(\App\Entity\User::class, $userId); + $existingLink = $this->em->getRepository(UserMovie::class)->findOneBy([ + 'user' => $user, + 'movie' => $movie, + ]); + if (!$existingLink) { + $userMovie = new UserMovie(); + $userMovie->setUser($user); + $userMovie->setMovie($movie); + $this->em->persist($userMovie); + } + + $this->em->flush(); } - - $this->actorSyncer->syncActorsForMovie($movie); - - // Import awards for actors of this movie - foreach ($movie->getActors() as $role) { - $this->awardImporter->importForActor($role->getActor()); - } - - $user = $this->em->getReference(\App\Entity\User::class, $userId); - $existingLink = $this->em->getRepository(UserMovie::class)->findOneBy([ - 'user' => $user, - 'movie' => $movie, - ]); - if (!$existingLink) { - $userMovie = new UserMovie(); - $userMovie->setUser($user); - $userMovie->setMovie($movie); - $this->em->persist($userMovie); - } - - $this->em->flush(); } catch (\Throwable $e) { $this->logger->warning('Failed to import film', [ 'film' => $ltbxdMovie->getName(), @@ -86,19 +84,16 @@ readonly class ImportFilmsBatchMessageHandler $this->importRepository->incrementFailedFilms($import); } + $processedFilms = $this->importRepository->incrementProcessedFilms($import); + $this->em->clear(); $import = $this->em->getRepository(Import::class)->find($importId); - } - $processedBatches = $this->importRepository->incrementProcessedBatches($import); - - if ($processedBatches >= $import->getTotalBatches()) { - // Refresh the entity to get updated failedFilms from DB - $this->em->refresh($import); - - $import->setStatus(Import::STATUS_COMPLETED); - $import->setCompletedAt(new \DateTimeImmutable()); - $this->em->flush(); + if ($processedFilms >= $import->getTotalFilms()) { + $import->setStatus(Import::STATUS_COMPLETED); + $import->setCompletedAt(new \DateTimeImmutable()); + $this->em->flush(); + } } } } diff --git a/src/MessageHandler/ProcessImportMessageHandler.php b/src/MessageHandler/ProcessImportMessageHandler.php index a311761..e0ca89d 100644 --- a/src/MessageHandler/ProcessImportMessageHandler.php +++ b/src/MessageHandler/ProcessImportMessageHandler.php @@ -49,10 +49,8 @@ readonly class ProcessImportMessageHandler } $totalFilms = count($ltbxdMovies); - $totalBatches = (int) ceil($totalFilms / self::BATCH_SIZE); $import->setTotalFilms($totalFilms); - $import->setTotalBatches($totalBatches); $import->setStatus(Import::STATUS_PROCESSING); $this->em->flush(); diff --git a/src/Repository/ImportRepository.php b/src/Repository/ImportRepository.php index fe94ecb..43e4e4c 100644 --- a/src/Repository/ImportRepository.php +++ b/src/Repository/ImportRepository.php @@ -18,10 +18,10 @@ class ImportRepository extends ServiceEntityRepository parent::__construct($registry, Import::class); } - public function incrementProcessedBatches(Import $import): int + public function incrementProcessedFilms(Import $import): int { return (int) $this->getEntityManager()->getConnection()->fetchOne( - 'UPDATE import SET processed_batches = processed_batches + 1 WHERE id = :id RETURNING processed_batches', + 'UPDATE import SET processed_films = processed_films + 1 WHERE id = :id RETURNING processed_films', ['id' => $import->getId()] ); }