-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0527db
commit 65fc39f
Showing
13 changed files
with
178 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Metabase Migrate | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'src/Infrastructure/Persistence/Doctrine/MetabaseMigrations/**' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v1 | ||
|
||
- name: Setup PHP with PECL extension | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.2' | ||
|
||
- name: Get Composer Cache Directory | ||
id: composer-cache | ||
run: | | ||
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT | ||
- uses: actions/cache@v3 | ||
with: | ||
path: ${{ steps.composer-cache.outputs.dir }} | ||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-composer- | ||
- name: Init environment variables | ||
run: | | ||
echo "METABASE_DATABASE_URL=${{ secrets.METABASE_MIGRATIONS_METABASE_DATABASE_URL }}" >> .env | ||
- name: CI | ||
run: make ci_metabase_migrate BIN_COMPOSER="composer" BIN_CONSOLE="php bin/console" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
migrations_paths: | ||
App\Infrastructure\Persistence\Doctrine\MetabaseMigrations: 'src/Infrastructure/Persistence/Doctrine/MetabaseMigrations' | ||
em: metabase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Domain/Statistics/Repository/StatisticsRepositoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Domain\Statistics\Repository; | ||
|
||
interface StatisticsRepositoryInterface | ||
{ | ||
public function addUserActiveStatistics(\DateTimeImmutable $now): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Infrastructure/Persistence/Doctrine/MetabaseMigrations/Version20241216132451.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Persistence\Doctrine\MetabaseMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Version20241216132451 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql( | ||
'CREATE TABLE IF NOT EXISTS analytics_user_active ( | ||
id UUID NOT NULL, | ||
uploaded_at TIMESTAMP(0), | ||
last_active_at TIMESTAMP(0), | ||
PRIMARY KEY(id) | ||
);', | ||
); | ||
|
||
$this->addSql( | ||
'CREATE INDEX IF NOT EXISTS idx_analytics_user_active_uploaded_at | ||
ON analytics_user_active (uploaded_at);', | ||
); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('DROP TABLE IF EXISTS analytics_user_active'); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Infrastructure/Persistence/Doctrine/Repository/Statistics/StatisticsRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Persistence\Doctrine\Repository\Statistics; | ||
|
||
use App\Domain\Statistics\Repository\StatisticsRepositoryInterface; | ||
use App\Domain\User\Repository\UserRepositoryInterface; | ||
use Doctrine\DBAL\Connection; | ||
|
||
final class StatisticsRepository implements StatisticsRepositoryInterface | ||
{ | ||
public function __construct( | ||
private UserRepositoryInterface $userRepository, | ||
private Connection $metabaseConnection, | ||
) { | ||
} | ||
|
||
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. | ||
// Dans Metabase cela permet de calculer le nombre d'utilisateurs actifs au moment de chaque exécution. | ||
// (Par exemple avec un filtre : "[last_active_at] >= [uploaded_at] - 7 jours", puis en groupant sur le uploaded_at.) | ||
$userRows = $this->userRepository->findAllForStatistics(); | ||
$this->bulkInsertUserActiveStatistics($now, $userRows); | ||
} | ||
|
||
private function bulkInsertUserActiveStatistics(\DateTimeInterface $now, array $userRows): void | ||
{ | ||
$stmt = $this->metabaseConnection->prepare( | ||
'INSERT INTO analytics_user_active(id, uploaded_at, last_active_at) | ||
VALUES (:id, (:uploadedAt)::timestamp(0), (:lastActiveAt)::timestamp(0))', | ||
); | ||
|
||
foreach ($userRows as $row) { | ||
$stmt->bindValue('id', $row['uuid']); | ||
$stmt->bindValue('uploadedAt', $now->format(\DateTimeInterface::ATOM)); | ||
$stmt->bindValue('lastActiveAt', $row['lastActiveAt']?->format(\DateTimeInterface::ATOM)); | ||
$stmt->execute(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters