Loading config/merged/evenement.config.php +16 −0 Original line number Diff line number Diff line Loading @@ -2,7 +2,12 @@ namespace UnicaenEvenement; use Console\Command\Evenement\EvenementTraiterCommand; use Unicaen\Console\Router\Simple; use UnicaenEvenement\Command\EvenementPurgeCommand; use UnicaenEvenement\Command\EvenementPurgeCommandFactory; use UnicaenEvenement\Command\JournalPurgeCommand; use UnicaenEvenement\Command\JournalPurgeCommandFactory; use UnicaenEvenement\Controller\EvenementConsoleController; use UnicaenEvenement\Controller\EvenementConsoleControllerFactory; use UnicaenEvenement\Controller\EvenementController; Loading Loading @@ -131,6 +136,7 @@ return [ ], ], // TODO : a supprimer (remplacement par une action console 'console' => [ 'router' => [ 'routes' => [ Loading @@ -153,6 +159,9 @@ return [ EvenementService::class => EvenementServiceFactory::class, EvenementGestionService::class => EvenementGestionServiceFactory::class, EvenementCollectionService::class => EvenementCollectionServiceFactory::class, JournalPurgeCommand::class => JournalPurgeCommandFactory::class, EvenementPurgeCommand::class => EvenementPurgeCommandFactory::class, ], 'abstract_factories' => [ ] Loading Loading @@ -184,4 +193,11 @@ return [ ], ], 'laminas-cli' => [ 'commands' => [ 'evenement:purge' => EvenementPurgeCommand::class, 'journal:purge' => JournalPurgeCommand::class, ], ], ]; No newline at end of file config/unicaen-evenement.local.php.dist +3 −0 Original line number Diff line number Diff line Loading @@ -8,5 +8,8 @@ return [ // Temps d'execution maximum pour le traitement des événements en console // données en seconde 'max_time_execution' => 300, //Durée de conservations des logs //'conservation-time' => new DateInterval('P1Y'), ], ]; No newline at end of file readme.md +23 −0 Original line number Diff line number Diff line Loading @@ -130,3 +130,26 @@ return [ ], ]; ``` Purge des événements et des logs du journal --- Les commandes consoles `evenement:purge` et `evenement:purge` permettent de supprimer les instances d'événement traiter avant une date. - La durée par défaut de conservertion est de 1 an, il s'agit d'un paramétre modifiable dans la configuration sous la forme de DateInterval : ``` 'unicaen-evenement' => [ 'conservation-time' => new DateInterval('P2Y'), ], ``` 3 options possibles pour evenement:purge - `--time=P2Y` *(ou `-t P2Y`)* pour spécifier la durée de conservations des événements (on ignore alors celle définie en conf). - `--etat=XXX`, *(ou `-e XXX`)* pour ne purger que les événements dont l'état est de code `XXX` - `--type=YYY`, *(ou `-t YYY`)* pour ne purger que les événements du type dont le code est `YYY` 2 options possibles pour journal:purge - `--time=P2Y` *(ou `-t P2Y`)* pour spécifier la durée de conservations des logs (on ignore alors celle définie en conf). - `--etat=XXX`, *(ou `-e XXX`)* pour ne purger que les logs dont l'état est de code `XXX` *** dans tout les cas, on tient compte de la date de traitements des événements lors de la purge *** No newline at end of file src/UnicaenEvenement/Command/EvenementPurgeCommand.php 0 → 100644 +136 −0 Original line number Diff line number Diff line <?php namespace UnicaenEvenement\Command; use DateInterval; use DateTime; use Exception; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use UnicaenEvenement\Entity\Db\Evenement; use UnicaenEvenement\Service\Evenement\EvenementServiceAwareTrait; class EvenementPurgeCommand extends Command { protected static $defaultName = 'evenement:purge'; protected ?DateInterval $conservationTime = null; public function setConservationTime(DateInterval $conservationTime): static { $this->conservationTime = $conservationTime; return $this; } public function getDateSuppression() : DateTime { $date = new DateTime(); $conservationTime = ($this->conservationTime) ?? new DateInterval('P1Y'); $date->sub($conservationTime); return $date; } protected ?string $codeType = null; protected ?string $codeEtat = null; public function getCodeType(): ?string { return $this->codeType; } public function getCodeEtat(): ?string { return $this->codeEtat; } public function setCodeType(?string $codeType): static { $this->codeType = $codeType; return $this; } public function setCodeEtat(?string $codeCategorie): static { $this->codeEtat = $codeCategorie; return $this; } use EvenementServiceAwareTrait; protected function configure() : static { $this->setDescription("Suppression des événements traité avant une date"); $this->addOption('time', '-t', InputOption::VALUE_OPTIONAL, "Intervale de temps que l'on souhaite conserver les événements -- format : P1Y"); $this->addOption('etat', '-e', InputOption::VALUE_OPTIONAL, "Code de l'état des événements que l'on souhaite purger"); $this->addOption('type', '-type', InputOption::VALUE_OPTIONAL, "code du type d'événement que l'on souhaite purger"); return $this; } protected function initialize(InputInterface $input, OutputInterface $output): void { $io = new SymfonyStyle($input, $output); try{ $time = $input->getOption('time'); if(isset($time) && $time != ""){ $this->conservationTime = new DateInterval($time); } $codeEtat = $input->getOption('etat'); if(isset($codeEtat) && $codeEtat != ""){ $this->codeEtat = $codeEtat; } $codeType = $input->getOption('type'); if(isset($codeType) && $codeType != ""){ $this->codeType = $codeType; } } catch (Exception $e){ $io->error($e->getMessage()); exit(-1); } } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); try { $io->title("Suppression des anciens événements"); $date = $this->getDateSuppression(); $codeEtat = $this->getCodeEtat(); $codeType = $this->getCodeType(); $qb = $this->getEvenementService()->createQueryBuilder(); $qb->andWhere("evenement.dateTraitement < :date"); $qb->setParameter("date", $date); if(isset($codeEtat) && $codeEtat != ""){ $qb->andWhere($qb->expr()->eq('etat.code',':codeEtat')); $qb->setParameter("codeEtat", $codeEtat); }; if(isset($codeType) && $codeType != ""){ $qb->andWhere($qb->expr()->eq('type.code',':codeType')); $qb->setParameter("codeType", $codeType); }; $evenements = $qb->getQuery()->getResult(); if(!empty($evenements)) { $io->progressStart(sizeof($evenements)); /** @var Evenement $evenement */ foreach ($evenements as $evenement) { $this->getEvenementService()->delete($evenement); $io->progressAdvance(); } $io->progressFinish(); } else{ $io->text("Aucun événement à supprimer"); } $io->success("Suppression terminée"); } catch (Exception $e){ $io->error($e->getMessage()); return self::FAILURE; } return self::SUCCESS; } } No newline at end of file src/UnicaenEvenement/Command/EvenementPurgeCommandFactory.php 0 → 100644 +35 −0 Original line number Diff line number Diff line <?php namespace UnicaenEvenement\Command; use DateInterval; use Exception; use Psr\Container\ContainerInterface; use Symfony\Component\Console\Command\Command; use UnicaenEvenement\Service\Evenement\EvenementService; class EvenementPurgeCommandFactory extends Command { /** * @param ContainerInterface $container * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ public function __invoke(ContainerInterface $container): EvenementPurgeCommand { $command = new EvenementPurgeCommand(); $command->setEvenementService($container->get(EvenementService::class)); $config = $container->get('Configuration'); if (isset($config['unicaen-evenement']['conservation-time'])) { $conservationTime = $config['unicaen-evenement']['conservation-time']; if(!$conservationTime instanceof DateInterval) { throw new Exception("Le paramètre de coniguration 'unicaen-evenement > conservation-time' doit être un DateInterval"); } $command->setConservationTime($conservationTime); } return $command; } } No newline at end of file Loading
config/merged/evenement.config.php +16 −0 Original line number Diff line number Diff line Loading @@ -2,7 +2,12 @@ namespace UnicaenEvenement; use Console\Command\Evenement\EvenementTraiterCommand; use Unicaen\Console\Router\Simple; use UnicaenEvenement\Command\EvenementPurgeCommand; use UnicaenEvenement\Command\EvenementPurgeCommandFactory; use UnicaenEvenement\Command\JournalPurgeCommand; use UnicaenEvenement\Command\JournalPurgeCommandFactory; use UnicaenEvenement\Controller\EvenementConsoleController; use UnicaenEvenement\Controller\EvenementConsoleControllerFactory; use UnicaenEvenement\Controller\EvenementController; Loading Loading @@ -131,6 +136,7 @@ return [ ], ], // TODO : a supprimer (remplacement par une action console 'console' => [ 'router' => [ 'routes' => [ Loading @@ -153,6 +159,9 @@ return [ EvenementService::class => EvenementServiceFactory::class, EvenementGestionService::class => EvenementGestionServiceFactory::class, EvenementCollectionService::class => EvenementCollectionServiceFactory::class, JournalPurgeCommand::class => JournalPurgeCommandFactory::class, EvenementPurgeCommand::class => EvenementPurgeCommandFactory::class, ], 'abstract_factories' => [ ] Loading Loading @@ -184,4 +193,11 @@ return [ ], ], 'laminas-cli' => [ 'commands' => [ 'evenement:purge' => EvenementPurgeCommand::class, 'journal:purge' => JournalPurgeCommand::class, ], ], ]; No newline at end of file
config/unicaen-evenement.local.php.dist +3 −0 Original line number Diff line number Diff line Loading @@ -8,5 +8,8 @@ return [ // Temps d'execution maximum pour le traitement des événements en console // données en seconde 'max_time_execution' => 300, //Durée de conservations des logs //'conservation-time' => new DateInterval('P1Y'), ], ]; No newline at end of file
readme.md +23 −0 Original line number Diff line number Diff line Loading @@ -130,3 +130,26 @@ return [ ], ]; ``` Purge des événements et des logs du journal --- Les commandes consoles `evenement:purge` et `evenement:purge` permettent de supprimer les instances d'événement traiter avant une date. - La durée par défaut de conservertion est de 1 an, il s'agit d'un paramétre modifiable dans la configuration sous la forme de DateInterval : ``` 'unicaen-evenement' => [ 'conservation-time' => new DateInterval('P2Y'), ], ``` 3 options possibles pour evenement:purge - `--time=P2Y` *(ou `-t P2Y`)* pour spécifier la durée de conservations des événements (on ignore alors celle définie en conf). - `--etat=XXX`, *(ou `-e XXX`)* pour ne purger que les événements dont l'état est de code `XXX` - `--type=YYY`, *(ou `-t YYY`)* pour ne purger que les événements du type dont le code est `YYY` 2 options possibles pour journal:purge - `--time=P2Y` *(ou `-t P2Y`)* pour spécifier la durée de conservations des logs (on ignore alors celle définie en conf). - `--etat=XXX`, *(ou `-e XXX`)* pour ne purger que les logs dont l'état est de code `XXX` *** dans tout les cas, on tient compte de la date de traitements des événements lors de la purge *** No newline at end of file
src/UnicaenEvenement/Command/EvenementPurgeCommand.php 0 → 100644 +136 −0 Original line number Diff line number Diff line <?php namespace UnicaenEvenement\Command; use DateInterval; use DateTime; use Exception; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use UnicaenEvenement\Entity\Db\Evenement; use UnicaenEvenement\Service\Evenement\EvenementServiceAwareTrait; class EvenementPurgeCommand extends Command { protected static $defaultName = 'evenement:purge'; protected ?DateInterval $conservationTime = null; public function setConservationTime(DateInterval $conservationTime): static { $this->conservationTime = $conservationTime; return $this; } public function getDateSuppression() : DateTime { $date = new DateTime(); $conservationTime = ($this->conservationTime) ?? new DateInterval('P1Y'); $date->sub($conservationTime); return $date; } protected ?string $codeType = null; protected ?string $codeEtat = null; public function getCodeType(): ?string { return $this->codeType; } public function getCodeEtat(): ?string { return $this->codeEtat; } public function setCodeType(?string $codeType): static { $this->codeType = $codeType; return $this; } public function setCodeEtat(?string $codeCategorie): static { $this->codeEtat = $codeCategorie; return $this; } use EvenementServiceAwareTrait; protected function configure() : static { $this->setDescription("Suppression des événements traité avant une date"); $this->addOption('time', '-t', InputOption::VALUE_OPTIONAL, "Intervale de temps que l'on souhaite conserver les événements -- format : P1Y"); $this->addOption('etat', '-e', InputOption::VALUE_OPTIONAL, "Code de l'état des événements que l'on souhaite purger"); $this->addOption('type', '-type', InputOption::VALUE_OPTIONAL, "code du type d'événement que l'on souhaite purger"); return $this; } protected function initialize(InputInterface $input, OutputInterface $output): void { $io = new SymfonyStyle($input, $output); try{ $time = $input->getOption('time'); if(isset($time) && $time != ""){ $this->conservationTime = new DateInterval($time); } $codeEtat = $input->getOption('etat'); if(isset($codeEtat) && $codeEtat != ""){ $this->codeEtat = $codeEtat; } $codeType = $input->getOption('type'); if(isset($codeType) && $codeType != ""){ $this->codeType = $codeType; } } catch (Exception $e){ $io->error($e->getMessage()); exit(-1); } } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); try { $io->title("Suppression des anciens événements"); $date = $this->getDateSuppression(); $codeEtat = $this->getCodeEtat(); $codeType = $this->getCodeType(); $qb = $this->getEvenementService()->createQueryBuilder(); $qb->andWhere("evenement.dateTraitement < :date"); $qb->setParameter("date", $date); if(isset($codeEtat) && $codeEtat != ""){ $qb->andWhere($qb->expr()->eq('etat.code',':codeEtat')); $qb->setParameter("codeEtat", $codeEtat); }; if(isset($codeType) && $codeType != ""){ $qb->andWhere($qb->expr()->eq('type.code',':codeType')); $qb->setParameter("codeType", $codeType); }; $evenements = $qb->getQuery()->getResult(); if(!empty($evenements)) { $io->progressStart(sizeof($evenements)); /** @var Evenement $evenement */ foreach ($evenements as $evenement) { $this->getEvenementService()->delete($evenement); $io->progressAdvance(); } $io->progressFinish(); } else{ $io->text("Aucun événement à supprimer"); } $io->success("Suppression terminée"); } catch (Exception $e){ $io->error($e->getMessage()); return self::FAILURE; } return self::SUCCESS; } } No newline at end of file
src/UnicaenEvenement/Command/EvenementPurgeCommandFactory.php 0 → 100644 +35 −0 Original line number Diff line number Diff line <?php namespace UnicaenEvenement\Command; use DateInterval; use Exception; use Psr\Container\ContainerInterface; use Symfony\Component\Console\Command\Command; use UnicaenEvenement\Service\Evenement\EvenementService; class EvenementPurgeCommandFactory extends Command { /** * @param ContainerInterface $container * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ public function __invoke(ContainerInterface $container): EvenementPurgeCommand { $command = new EvenementPurgeCommand(); $command->setEvenementService($container->get(EvenementService::class)); $config = $container->get('Configuration'); if (isset($config['unicaen-evenement']['conservation-time'])) { $conservationTime = $config['unicaen-evenement']['conservation-time']; if(!$conservationTime instanceof DateInterval) { throw new Exception("Le paramètre de coniguration 'unicaen-evenement > conservation-time' doit être un DateInterval"); } $command->setConservationTime($conservationTime); } return $command; } } No newline at end of file