Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intègre les indicateurs existants dans Metabase #1120

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

interface StatisticsRepositoryInterface
{
public function addCountStatistics(\DateTimeImmutable $now): void;

public function addUserActiveStatistics(\DateTimeImmutable $now): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Persistence\Doctrine\MetabaseMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20241216142341 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,48 @@

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;

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]));
Expand Down
Loading