diff --git a/src/Domain/Statistics/Repository/StatisticsRepositoryInterface.php b/src/Domain/Statistics/Repository/StatisticsRepositoryInterface.php index eece58d35..869c0aed4 100644 --- a/src/Domain/Statistics/Repository/StatisticsRepositoryInterface.php +++ b/src/Domain/Statistics/Repository/StatisticsRepositoryInterface.php @@ -6,5 +6,7 @@ interface StatisticsRepositoryInterface { + public function addCountStatistics(\DateTimeImmutable $now): void; + public function addUserActiveStatistics(\DateTimeImmutable $now): void; } diff --git a/src/Infrastructure/Persistence/Doctrine/MetabaseMigrations/Version20241216142341.php b/src/Infrastructure/Persistence/Doctrine/MetabaseMigrations/Version20241216142341.php new file mode 100644 index 000000000..bcd0a683c --- /dev/null +++ b/src/Infrastructure/Persistence/Doctrine/MetabaseMigrations/Version20241216142341.php @@ -0,0 +1,44 @@ +addSql( + 'CREATE TABLE IF NOT EXISTS analytics_count ( + id UUID NOT NULL, + uploaded_at TIMESTAMP(0), + name VARCHAR(32), + value INTEGER, + PRIMARY KEY(id) + );', + ); + + $this->addSql( + 'CREATE INDEX IF NOT EXISTS idx_analytics_count_uploaded_at + ON analytics_count (uploaded_at);', + ); + + $this->addSql( + 'CREATE INDEX IF NOT EXISTS idx_analytics_count_name + ON analytics_count (name);', + ); + } + + public function down(Schema $schema): void + { + $this->addSql('DROP TABLE analytics_count'); + } +} diff --git a/src/Infrastructure/Persistence/Doctrine/Repository/Statistics/StatisticsRepository.php b/src/Infrastructure/Persistence/Doctrine/Repository/Statistics/StatisticsRepository.php index f2ab74dc7..052404bb7 100644 --- a/src/Infrastructure/Persistence/Doctrine/Repository/Statistics/StatisticsRepository.php +++ b/src/Infrastructure/Persistence/Doctrine/Repository/Statistics/StatisticsRepository.php @@ -4,7 +4,9 @@ namespace App\Infrastructure\Persistence\Doctrine\Repository\Statistics; +use App\Domain\Regulation\Repository\RegulationOrderRecordRepositoryInterface; use App\Domain\Statistics\Repository\StatisticsRepositoryInterface; +use App\Domain\User\Repository\OrganizationRepositoryInterface; use App\Domain\User\Repository\UserRepositoryInterface; use Doctrine\DBAL\Connection; @@ -12,10 +14,38 @@ final class StatisticsRepository implements StatisticsRepositoryInterface { public function __construct( private UserRepositoryInterface $userRepository, + private OrganizationRepositoryInterface $organizationRepository, + private RegulationOrderRecordRepositoryInterface $regulationOrderRecordRepository, private Connection $metabaseConnection, ) { } + public function addCountStatistics(\DateTimeInterface $now): void + { + // On peut tracer le graphique d'évolution de chaque count en groupant par 'name' et + // en utilisant 'uploadedAt' (la date d'exécution) comme abscisse. + $counts = [ + 'users' => $this->userRepository->countUsers(), + 'organizations' => $this->organizationRepository->countOrganizations(), + 'regulationOrderRecords' => $this->regulationOrderRecordRepository->countTotalRegulationOrderRecords(), + 'regulationOrderRecords.published' => $this->regulationOrderRecordRepository->countPublishedRegulationOrderRecords(), + 'regulationOrderRecords.permanent' => $this->regulationOrderRecordRepository->countPermanentRegulationOrderRecords(), + 'regulationOrderRecords.temporary' => $this->regulationOrderRecordRepository->countTemporaryRegulationOrderRecords(), + ]; + + $stmt = $this->metabaseConnection->prepare( + 'INSERT INTO analytics_count(id, uploaded_at, name, value) + VALUES (uuid_generate_v4(), :uploadedAt, :name, :value)', + ); + + foreach ($counts as $name => $value) { + $stmt->bindValue('uploadedAt', $now->format(\DateTimeInterface::ATOM)); + $stmt->bindValue('name', $name); + $stmt->bindValue('value', $value); + $stmt->execute(); + } + } + public function addUserActiveStatistics(\DateTimeInterface $now): void { // À chaque export des statistiques, on ajoute la liste des dates de dernière activité pour chaque utilisateur, et la date d'exécution. diff --git a/src/Infrastructure/Symfony/Command/RunMetabaseExportCommand.php b/src/Infrastructure/Symfony/Command/RunMetabaseExportCommand.php index 548560b7e..26f1f425a 100644 --- a/src/Infrastructure/Symfony/Command/RunMetabaseExportCommand.php +++ b/src/Infrastructure/Symfony/Command/RunMetabaseExportCommand.php @@ -29,6 +29,7 @@ public function execute(InputInterface $input, OutputInterface $output): int { $now = $this->dateUtils->getNow(); + $this->statisticsRepository->addCountStatistics($now); $this->statisticsRepository->addUserActiveStatistics($now); return Command::SUCCESS; diff --git a/tests/Integration/Infrastructure/Symfony/Command/RunMetabaseExportCommandTest.php b/tests/Integration/Infrastructure/Symfony/Command/RunMetabaseExportCommandTest.php index 8c3102161..a5c151e7e 100644 --- a/tests/Integration/Infrastructure/Symfony/Command/RunMetabaseExportCommandTest.php +++ b/tests/Integration/Infrastructure/Symfony/Command/RunMetabaseExportCommandTest.php @@ -14,6 +14,8 @@ public function testExecute(): void { self::bootKernel(); + $executionDate = '2023-06-09 00:00:00'; // Defined in DateUtilsMock + $container = static::getContainer(); $command = $container->get(RunMetabaseExportCommand::class); $commandTester = new CommandTester($command); @@ -22,6 +24,30 @@ public function testExecute(): void /** @var \Doctrine\DBAL\Connection */ $metabaseConnection = $container->get('doctrine.dbal.metabase_connection'); + + // Check count statistics + $rows = $metabaseConnection->fetchAllAssociative('SELECT * FROM analytics_count'); + $this->assertCount(6, $rows); + $this->assertEquals(['id', 'uploaded_at', 'name', 'value'], array_keys($rows[0])); + + $counts = []; + + foreach ($rows as $row) { + $this->assertSame($executionDate, $row['uploaded_at']); + $counts[$row['name']] = $row['value']; + } + + $this->assertEquals([ + 'users' => 3, + 'organizations' => 2, + // Only counts regulations outside of DiaLog org + 'regulationOrderRecords' => 1, + 'regulationOrderRecords.published' => 1, + 'regulationOrderRecords.permanent' => 0, + 'regulationOrderRecords.temporary' => 1, + ], $counts); + + // Check user active statistics $rows = $metabaseConnection->fetchAllAssociative('SELECT * FROM analytics_user_active'); $this->assertCount(3, $rows); $this->assertEquals(['id', 'uploaded_at', 'last_active_at'], array_keys($rows[0]));