diff --git a/Command/ArchiveUserStatsCommand.php b/Command/ArchiveUserStatsCommand.php new file mode 100644 index 0000000..5a8f439 --- /dev/null +++ b/Command/ArchiveUserStatsCommand.php @@ -0,0 +1,102 @@ +setName('make:user:stats:archive'); + $this->setDescription('Archive all user data in another table'); + } + + public function __construct(private EntityManagerInterface $em, private ContainerBagInterface $containerBag) + { + parent::__construct(); + } + + /** + * Send reminder email to user when their gift entry is coming to an end + * Launch this command once a day with cron on production + * @param InputInterface $input + * @param OutputInterface $output + * @return int + * @throws TransportExceptionInterface + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->output = $output; + $this->archiveData(); + + return 0; + } + + /** + * @return void + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + */ + private function archiveData() :void + { + $this->output->writeln("Begin get data"); + + $dateToArchive = (new DateTime())->modify("-" . $this->containerBag->get('fluffy_user_stats')['max_month_before_archive'] . " months"); + + $userStatsLinesToArchives = $this->em->getRepository(UserStatsLines::class)->findToArchive($dateToArchive); + + $progressBar = new ProgressBar($this->output, count($userStatsLinesToArchives)); + $progressBar->setFormat('debug'); + $progressBar->setProgressCharacter('>'); + $progressBar->setBarCharacter('-'); + + $this->output->writeln( "Begin transfert data"); + /** @var UserStatsLines $userStatsLinesToArchive */ + foreach ($userStatsLinesToArchives as $userStatsLine) { + $progressBar->advance(); + + $userStatsLinesArchive = new UserStatsLinesArchives(); + + $userStatsLinesArchive + ->setUser($userStatsLine->getUser()) + ->setBrowser($userStatsLine->getBrowser()) + ->setCreatedAt($userStatsLine->getCreatedAt()) + ->setRoute($userStatsLine->getRoute()) + ->setSessionId($userStatsLine->getSessionId()) + ->setUrl($userStatsLine->getUrl()); + + $this->em->persist($userStatsLinesArchive); + $this->em->remove($userStatsLine); + } + $progressBar->finish(); + $this->output->writeln( "\nEnd transfert data"); + + + $this->output->writeln( "Begin flush data"); + $this->em->flush(); + $this->output->writeln( "End flush data"); + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 21d7e35..e513068 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -22,9 +22,6 @@ public function getConfigTreeBuilder() ->integerNode('max_month_before_archive') ->defaultValue(6)->min(0)->max(240) ->end() - ->booleanNode('archive_enabled') - ->defaultFalse() - ->end() ->end() ; diff --git a/Entity/UserStatsLines.php b/Entity/UserStatsLines.php index 243145a..0b241c7 100644 --- a/Entity/UserStatsLines.php +++ b/Entity/UserStatsLines.php @@ -9,7 +9,7 @@ use Symfony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: "FluffyFactory\Bundle\UserStatsBundle\Repository\UserStatsLinesRepository")] -#[ORM\Index(columns: ["created_at", "user_id"], name: "user_archive")] +#[ORM\Index(columns: ["created_at"], name: "date_archive")] class UserStatsLines { #[ORM\Id] diff --git a/EventSubscriber/UserStatsSubscriber.php b/EventSubscriber/UserStatsSubscriber.php index 6dba598..07c3471 100644 --- a/EventSubscriber/UserStatsSubscriber.php +++ b/EventSubscriber/UserStatsSubscriber.php @@ -60,10 +60,6 @@ public function onKernelRequest(RequestEvent $event) $user->setLastVisited(new DateTime()); $user->setNbPageViews($user->getNbPageViews() + 1); - if ($this->containerBag->get('fluffy_user_stats')['archive_enabled']) { - $this->archiveData($user); - } - $userStatsLines = new UserStatsLines(); $userStatsLines->setUser($user); $userStatsLines->setUrl($event->getRequest()->getRequestUri()); @@ -80,33 +76,4 @@ public function onKernelRequest(RequestEvent $event) } } } - - /** - * @param User $user - * @return void - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface - */ - private function archiveData(User $user) :void - { - $dateToArchive = (new DateTime())->modify("-" . $this->containerBag->get('fluffy_user_stats')['max_month_before_archive'] . " months"); - $userStatsLinesToArchives = $this->em->getRepository(UserStatsLines::class)->findToArchive($user, $dateToArchive); - - /** @var UserStatsLines $userStatsLinesToArchive */ - foreach ($userStatsLinesToArchives as $userStatsLine) { - $userStatsLinesArchive = new UserStatsLinesArchives(); - - $userStatsLinesArchive - ->setUser($userStatsLine->getUser()) - ->setBrowser($userStatsLine->getBrowser()) - ->setCreatedAt($userStatsLine->getCreatedAt()) - ->setRoute($userStatsLine->getRoute()) - ->setSessionId($userStatsLine->getSessionId()) - ->setUrl($userStatsLine->getUrl()); - - $this->em->persist($userStatsLinesArchive); - $this->em->remove($userStatsLine); - } - $this->em->flush(); - } } diff --git a/Repository/UserStatsLinesRepository.php b/Repository/UserStatsLinesRepository.php index 25c2460..47815f3 100644 --- a/Repository/UserStatsLinesRepository.php +++ b/Repository/UserStatsLinesRepository.php @@ -89,4 +89,18 @@ public function findBySession(User $user): array ->getResult() ; } + + /** + * @param DateTime $dateToArchive + * @return array + */ + public function findToArchive(DateTime $dateToArchive): array + { + return $this->createQueryBuilder('usl') + ->andWhere('usl.createdAt < :dateToArchive') + ->setParameter('dateToArchive', $dateToArchive) + ->getQuery() + ->getResult() + ; + } } diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 080e9f4..f2a292a 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -9,4 +9,8 @@ services: FluffyFactory\Bundle\UserStatsBundle\EventListener\LoginListener: tags: - - { name: 'kernel.event_listener', event: 'security.interactive_login' } \ No newline at end of file + - { name: 'kernel.event_listener', event: 'security.interactive_login' } + + FluffyFactory\Bundle\UserStatsBundle\Command\ArchiveUserStatsCommand: + tags: + - { name: 'console.command', command: 'make:user:stats:archive' }