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

API Nightly : Support for database #216

Merged
merged 1 commit into from
Jun 13, 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
4 changes: 3 additions & 1 deletion src/Command/ImportMochaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ protected function configure(): void
->addArgument('filename', InputArgument::REQUIRED)
->addOption('platform', 'p', InputOption::VALUE_REQUIRED, '', ReportMochaImporter::FILTER_PLATFORMS[0], ReportMochaImporter::FILTER_PLATFORMS)
->addOption('campaign', 'c', InputOption::VALUE_REQUIRED, '', ReportMochaImporter::FILTER_CAMPAIGNS[0], ReportMochaImporter::FILTER_CAMPAIGNS)
->addOption('database', 'd', InputOption::VALUE_REQUIRED, '', ReportMochaImporter::FILTER_DATABASES[0], ReportMochaImporter::FILTER_DATABASES)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}-(.*)?\.json/', $input->getArgument('filename'), $matchesVersion);
preg_match(ReportMochaImporter::REGEX_FILE, $input->getArgument('filename'), $matchesVersion);
if (!isset($matchesVersion[1]) || strlen($matchesVersion[1]) < 1) {
$output->writeln(sprintf(
'<error>Version found not correct (%s) from filename %s</error>',
Expand Down Expand Up @@ -69,6 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->reportImporter->import(
$input->getArgument('filename'),
$input->getOption('platform'),
$input->getOption('database'),
$input->getOption('campaign'),
$matchesVersion[1],
$startDate,
Expand Down
4 changes: 3 additions & 1 deletion src/Command/ImportPlaywrightCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ protected function configure(): void
->addArgument('filename', InputArgument::REQUIRED)
->addOption('platform', 'p', InputOption::VALUE_REQUIRED, '', ReportPlaywrightImporter::FILTER_PLATFORMS[0], ReportPlaywrightImporter::FILTER_PLATFORMS)
->addOption('campaign', 'c', InputOption::VALUE_REQUIRED, '', ReportPlaywrightImporter::FILTER_CAMPAIGNS[0], ReportPlaywrightImporter::FILTER_CAMPAIGNS)
->addOption('database', 'd', InputOption::VALUE_REQUIRED, '', ReportPlaywrightImporter::FILTER_DATABASES[0], ReportPlaywrightImporter::FILTER_DATABASES)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}-(.*)?\.json/', $input->getArgument('filename'), $matchesVersion);
preg_match(ReportPlaywrightImporter::REGEX_FILE, $input->getArgument('filename'), $matchesVersion);
if (!isset($matchesVersion[1]) || strlen($matchesVersion[1]) < 1) {
$output->writeln(sprintf(
'<error>Version found not correct (%s) from filename %s</error>',
Expand Down Expand Up @@ -69,6 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->reportImporter->import(
$input->getArgument('filename'),
$input->getOption('platform'),
$input->getOption('database'),
$input->getOption('campaign'),
$matchesVersion[1],
$startDate,
Expand Down
21 changes: 18 additions & 3 deletions src/Controller/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Controller;

use App\Repository\ExecutionRepository;
use App\Service\AbstractReportImporter;
use App\Service\ReportMochaImporter;
use App\Service\ReportPlaywrightImporter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand Down Expand Up @@ -31,6 +32,8 @@ class ImportController extends AbstractController

private ?string $platform;

private ?string $database;

private ?string $campaign;

private ?\DateTime $startDate;
Expand Down Expand Up @@ -60,6 +63,7 @@ public function importReportMocha(Request $request): JsonResponse
$execution = $this->reportMochaImporter->import(
$this->filename,
$this->platform,
$this->database,
$this->campaign,
$this->version,
$this->startDate,
Expand All @@ -83,6 +87,7 @@ public function importReportPlaywright(Request $request): JsonResponse
$execution = $this->reportPlaywrightImporter->import(
$this->filename,
$this->platform,
$this->database,
$this->campaign,
$this->version,
$this->startDate,
Expand Down Expand Up @@ -114,7 +119,7 @@ private function checkAuth(Request $request, array $allowedCampaigns, bool $forc
], Response::HTTP_UNAUTHORIZED);
}

preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}-(.*)?\.json/', $this->filename, $matchesVersion);
preg_match(AbstractReportImporter::REGEX_FILE, $this->filename, $matchesVersion);
if (!isset($matchesVersion[1])) {
return new JsonResponse([
'message' => 'Could not retrieve version from filename',
Expand Down Expand Up @@ -154,6 +159,9 @@ private function checkAuth(Request $request, array $allowedCampaigns, bool $forc
);
$this->platform = in_array($this->platform, ReportMochaImporter::FILTER_PLATFORMS) ? $this->platform : ReportMochaImporter::FILTER_PLATFORMS[0];

$this->database = $request->query->has('database') ? $request->query->get('database') : null;
$this->database = in_array($this->database, ReportMochaImporter::FILTER_DATABASES) ? $this->database : ReportMochaImporter::FILTER_DATABASES[0];

$this->campaign = $request->query->has('campaign') ? $request->query->get('campaign') : null;
if (!in_array($this->campaign, $allowedCampaigns)) {
if ($forceCampaign) {
Expand All @@ -172,13 +180,20 @@ private function checkAuth(Request $request, array $allowedCampaigns, bool $forc
$this->startDate = \DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $this->jsonContent->stats->start ?? $this->jsonContent->stats->startTime);

// Check if there is no similar entry
if (!$force && $this->executionRepository->findOneByNightly($this->version, $this->platform, $this->campaign, $this->startDate->format('Y-m-d'))) {
if (!$force && $this->executionRepository->findOneByNightly(
$this->version,
$this->platform,
$this->campaign,
$this->database,
$this->startDate->format('Y-m-d')
)) {
return new JsonResponse([
'message' => sprintf(
'A similar entry was found (criteria: version %s, platform %s, campaign %s, date %s).',
'A similar entry was found (criteria: version %s, platform %s, campaign %s, database %s, date %s).',
$this->version,
$this->platform,
$this->campaign,
$this->database,
$this->startDate->format('Y-m-d')
),
], Response::HTTP_FORBIDDEN);
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function reports(Request $request): JsonResponse
'campaign' => $execution->getCampaign(),
'browser' => $execution->getPlatform(), // retro-compatibility
'platform' => $execution->getPlatform(),
'database' => $execution->getDatabase(),
'start_date' => $execution->getStartDate()->format('Y-m-d H:i:s'),
'end_date' => $execution->getEndDate()->format('Y-m-d H:i:s'),
'duration' => $execution->getDuration(),
Expand Down Expand Up @@ -134,6 +135,7 @@ public function report(int $idReport, Request $request): JsonResponse
'campaign' => $execution->getCampaign(),
'browser' => $execution->getPlatform(), // retro-compatibility
'platform' => $execution->getPlatform(),
'database' => $execution->getDatabase(),
'start_date' => $execution->getStartDate()->setTimezone(new \DateTimeZone('-01:00'))->format('Y-m-d H:i:s'),
'end_date' => $execution->getEndDate()->setTimezone(new \DateTimeZone('-01:00'))->format('Y-m-d H:i:s'),
'duration' => $execution->getDuration(),
Expand Down
15 changes: 15 additions & 0 deletions src/Entity/Execution.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Execution
#[ORM\Column(length: 50, nullable: false)]
private string $platform = 'chromium';

#[ORM\Column(name: '`database`', length: 50, nullable: false)]
private string $database = 'mysql';

#[ORM\Column(nullable: true)]
private ?int $suites = null;

Expand Down Expand Up @@ -82,11 +85,23 @@ public function __construct()
$this->suitesCollection = new ArrayCollection();
}

public function getDatabase(): ?string
{
return $this->database;
}

public function getId(): ?int
{
return $this->id;
}

public function setDatabase(string $database): static
{
$this->database = $database;

return $this;
}

public function setId(int $id): static
{
$this->id = $id;
Expand Down
6 changes: 6 additions & 0 deletions src/Repository/ExecutionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ public function findOneByNightly(
string $version,
string $platform,
string $campaign,
string $database,
string $date
): ?Execution {
$qb = $this->createQueryBuilder('e')
->andWhere('e.version = :version')
->andWhere('e.platform = :platform')
->andWhere('e.campaign = :campaign')
->andWhere('e.database = :database')
->andWhere('DATE(e.start_date) = :date')
->setParameter('version', $version)
->setParameter('platform', $platform)
->setParameter('campaign', $campaign)
->setParameter('database', $database)
->setParameter('date', $date)
->orderBy('e.start_date', 'DESC');

Expand All @@ -47,16 +50,19 @@ public function findOneByNightlyBefore(
string $version,
string $platform,
string $campaign,
string $database,
\DateTimeInterface $dateUntil
): ?Execution {
$qb = $this->createQueryBuilder('e')
->andWhere('e.version = :version')
->andWhere('e.platform = :platform')
->andWhere('e.campaign = :campaign')
->andWhere('e.database = :database')
->andWhere('e.start_date < :dateUntil')
->setParameter('version', $version)
->setParameter('platform', $platform)
->setParameter('campaign', $campaign)
->setParameter('database', $database)
->setParameter('dateUntil', $dateUntil)
->orderBy('e.start_date', 'DESC');

Expand Down
6 changes: 6 additions & 0 deletions src/Service/AbstractReportImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class AbstractReportImporter
abstract public function import(
string $filename,
string $platform,
string $database,
string $campaign,
string $version,
\DateTime $startDate,
Expand All @@ -19,6 +20,10 @@ abstract public function import(

public const FILTER_PLATFORMS = ['chromium', 'firefox', 'webkit', 'cli'];

public const FILTER_DATABASES = ['mysql', 'mariadb'];

public const REGEX_FILE = '/[0-9]{4}-[0-9]{2}-[0-9]{2}-([^-]*)[-]?(.*)]?\.json/';

protected ExecutionRepository $executionRepository;

protected TestRepository $testRepository;
Expand All @@ -39,6 +44,7 @@ protected function compareReportData(Execution $execution): Execution
$execution->getVersion(),
$execution->getPlatform(),
$execution->getCampaign(),
$execution->getDatabase(),
$execution->getStartDate()
);
if (!$executionPrevious) {
Expand Down
2 changes: 2 additions & 0 deletions src/Service/ReportMochaImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
public function import(
string $filename,
string $platform,
string $database,
string $campaign,
string $version,
\DateTime $startDate,
Expand All @@ -37,6 +38,7 @@ public function import(
->setRef(date('YmdHis'))
->setFilename($filename)
->setPlatform($platform)
->setDatabase($database)
->setCampaign($campaign)
->setStartDate($startDate)
->setEndDate(\DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $jsonContent->stats->end))
Expand Down
2 changes: 2 additions & 0 deletions src/Service/ReportPlaywrightImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
public function import(
string $filename,
string $platform,
string $database,
string $campaign,
string $version,
\DateTime $startDate,
Expand All @@ -43,6 +44,7 @@ public function import(
->setRef(date('YmdHis'))
->setFilename($filename)
->setPlatform($platform)
->setDatabase($database)
->setCampaign($campaign)
->setStartDate($startDate)
->setEndDate($endDate)
Expand Down
2 changes: 2 additions & 0 deletions tests/Command/ImportMochaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testImportAutoupgrade(): void
$commandTester->execute([
'--platform' => 'cli',
'--campaign' => 'autoupgrade',
'--database' => 'mysql',
'filename' => 'autoupgrade_2024-01-25-develop.json',
]);

Expand All @@ -37,6 +38,7 @@ public function testImportCore(): void
$commandTester->execute([
'--platform' => 'chromium',
'--campaign' => 'functional',
'--database' => 'mysql',
'filename' => '2024-01-25-develop.json',
]);

Expand Down
1 change: 1 addition & 0 deletions tests/Command/ImportPlaywrightCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testImportBlockwislist(): void
$commandTester->execute([
'--platform' => 'chromium',
'--campaign' => 'blockwishlist',
'--database' => 'mysql',
'filename' => 'blockwishlist_2024-01-25-develop.json',
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/ImportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function testReportMochaAlreadyExisting(): void
$content = $response->getContent();
$content = json_decode($content, true);
$this->assertArrayHasKey('message', $content);
$this->assertEquals('A similar entry was found (criteria: version develop, platform chromium, campaign functional, date ' . self::DATE_RESOURCE . ').', $content['message']);
$this->assertEquals('A similar entry was found (criteria: version develop, platform chromium, campaign functional, database mysql, date ' . self::DATE_RESOURCE . ').', $content['message']);
}

public function testReportPlaywrightWithNoParameters(): void
Expand Down Expand Up @@ -332,6 +332,6 @@ public function testReportPlaywrightAlreadyExisting(): void
$content = $response->getContent();
$content = json_decode($content, true);
$this->assertArrayHasKey('message', $content);
$this->assertEquals('A similar entry was found (criteria: version develop, platform chromium, campaign blockwishlist, date ' . self::DATE_RESOURCE . ').', $content['message']);
$this->assertEquals('A similar entry was found (criteria: version develop, platform chromium, campaign blockwishlist, database mysql, date ' . self::DATE_RESOURCE . ').', $content['message']);
}
}
4 changes: 4 additions & 0 deletions tests/Controller/ReportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public function testReportsFilters(array $query, int $count): void
$this->assertArrayHasKey('platform', $item);
$this->assertContains($item['platform'], ReportMochaImporter::FILTER_PLATFORMS);
$this->assertEquals($item['browser'], $item['platform']);
$this->assertArrayHasKey('database', $item);
$this->assertContains($item['database'], ReportMochaImporter::FILTER_DATABASES);
if (isset($query['filter_platform'])) {
$this->assertEquals($item['platform'], $query['filter_platform']);
}
Expand Down Expand Up @@ -177,6 +179,8 @@ public function testReportID(int $reportId, array $campaigns): void
$this->assertArrayHasKey('platform', $content);
$this->assertContains($content['platform'], ReportMochaImporter::FILTER_PLATFORMS);
$this->assertEquals($content['browser'], $content['platform']);
$this->assertArrayHasKey('database', $content);
$this->assertContains($content['database'], ReportMochaImporter::FILTER_DATABASES);
$this->assertArrayHasKey('start_date', $content);
$this->assertArrayHasKey('end_date', $content);
$this->assertArrayHasKey('duration', $content);
Expand Down
Loading