Skip to content

Commit

Permalink
Merge pull request #17 from fluffy-factory/archiveData
Browse files Browse the repository at this point in the history
change the logic from a subscriber into a command
  • Loading branch information
DeusExM authored Oct 28, 2022
2 parents 001e809 + 7c0aad5 commit 3be237d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 38 deletions.
102 changes: 102 additions & 0 deletions Command/ArchiveUserStatsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace FluffyFactory\Bundle\UserStatsBundle\Command;

use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use FluffyFactory\Bundle\UserStatsBundle\Entity\UserStatsLines;
use FluffyFactory\Bundle\UserStatsBundle\Entity\UserStatsLinesArchives;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

#[AsCommand(
name: 'make:user:stats:archive',
description: 'Archive all user data in another table',
)]
class ArchiveUserStatsCommand extends Command
{
private OutputInterface $output;

protected function configure()
{
$this->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('<fg=blue>></>');
$progressBar->setBarCharacter('<fg=green>-</>');

$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");
}
}
3 changes: 0 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;

Expand Down
2 changes: 1 addition & 1 deletion Entity/UserStatsLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
33 changes: 0 additions & 33 deletions EventSubscriber/UserStatsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
}
}
14 changes: 14 additions & 0 deletions Repository/UserStatsLinesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
}
}
6 changes: 5 additions & 1 deletion Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ services:

FluffyFactory\Bundle\UserStatsBundle\EventListener\LoginListener:
tags:
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
- { name: 'kernel.event_listener', event: 'security.interactive_login' }

FluffyFactory\Bundle\UserStatsBundle\Command\ArchiveUserStatsCommand:
tags:
- { name: 'console.command', command: 'make:user:stats:archive' }

0 comments on commit 3be237d

Please sign in to comment.