Commit 92b1b4a8 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Merge code

parents 4d0b82b4 60161053
Loading
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
<?php


use UnicaenEvenement\Command\EvenementPurgeCommand;
use UnicaenEvenement\Command\EvenementPurgeCommandFactory;
use UnicaenEvenement\Command\JournalPurgeCommand;
use UnicaenEvenement\Command\JournalPurgeCommandFactory;
use UnicaenEvenement\Command\TraiterEvenementsCommand;
use UnicaenEvenement\Command\TraiterEvenementsCommandFactory;

@@ -13,6 +17,8 @@ return [
    'laminas-cli' => [
        'commands' => [
            'evenement:traiter-evenements' => TraiterEvenementsCommand::class,
            'evenement:purger-journal' => JournalPurgeCommand::class,
            'evenement:purger-evenement' => EvenementPurgeCommand::class,
        ],
    ],

@@ -23,6 +29,8 @@ return [
    'service_manager' => [
        'factories' => [
            TraiterEvenementsCommand::class => TraiterEvenementsCommandFactory::class,
            JournalPurgeCommand::class => JournalPurgeCommandFactory::class,
            EvenementPurgeCommand::class => EvenementPurgeCommandFactory::class,
        ],
    ],

+3 −0
Original line number Diff line number Diff line
@@ -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
+23 −0
Original line number Diff line number Diff line
@@ -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
+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:purger-evenement';

    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és 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
+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