Skip to content

Commit

Permalink
Ajoute une option pour afficher des infos de debug dans le rapport Li…
Browse files Browse the repository at this point in the history
…tteralis
  • Loading branch information
florimondmanca committed Jan 8, 2025
1 parent c0534af commit 246756b
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/Infrastructure/IntegrationReport/CommonRecordEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum CommonRecordEnum: string
{
case ATTR_REGULATION_ID = 'regulationId';
case ATTR_URL = 'url';
case ATTR_DEBUG = 'debug';

case FACT_INTEGRATION_NAME = 'common.integration_name';
case FACT_ORGANIZATION = 'common.organization';
Expand Down
14 changes: 12 additions & 2 deletions src/Infrastructure/IntegrationReport/ReportFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function arrayEncode(array $array): string
return implode(' ; ', $items);
}

public function format(array $records): string
public function format(array $records, bool $isDebugEnabled = false): string
{
$lines = [];

Expand Down Expand Up @@ -138,12 +138,17 @@ public function format(array $records): string

if (!\in_array($regulationId, $info['regulations'])) {
$info['regulations'][] = $regulationId;
$info['debug'][$regulationId] = [];

if (\array_key_exists(CommonRecordEnum::ATTR_URL->value, $context)) {
$info['urls'][$regulationId] = $context[CommonRecordEnum::ATTR_URL->value];
}
}

if (\array_key_exists(CommonRecordEnum::ATTR_DEBUG->value, $context)) {
$info['debug'][$regulationId][] = $context[CommonRecordEnum::ATTR_DEBUG->value];
}

$caseLists[$type][$name] = $info;
}

Expand Down Expand Up @@ -173,11 +178,16 @@ public function format(array $records): string
foreach ($info['regulations'] as $id) {
$line = \sprintf(' %s', $id);

if (\array_key_exists($id, $info['urls'])) {
if (!empty($info['urls']) && \array_key_exists($id, $info['urls'])) {
$url = $info['urls'][$id];
$line = \sprintf('%s (%s)', $line, $url);
}

if ($isDebugEnabled && \array_key_exists($id, $info['debug']) && !empty($info['debug'][$id])) {
$debugItems = $info['debug'][$id];
$line = \sprintf('%s (debug: %s)', $line, json_encode($debugItems));
}

$lines[] = $line;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/Litteralis/Fougeres/FougeresExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function __construct(
$this->executor->configure($fougeresCredentials);
}

public function execute(\DateTimeInterface $laterThan, Reporter $reporter): string
public function execute(\DateTimeInterface $laterThan, Reporter $reporter, bool $isDebugEnabled = false): string
{
return $this->executor->execute(self::INTEGRATION_NAME, $this->fougeresOrgId, $laterThan, $reporter);
return $this->executor->execute(self::INTEGRATION_NAME, $this->fougeresOrgId, $laterThan, $reporter, $isDebugEnabled);
}
}
14 changes: 10 additions & 4 deletions src/Infrastructure/Litteralis/LitteralisExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function configure(string $credentials): void
$this->extractor->configure($credentials);
}

public function execute(string $name, string $orgId, \DateTimeInterface $laterThan, Reporter $reporter): string
public function execute(string $name, string $orgId, \DateTimeInterface $laterThan, Reporter $reporter, bool $isDebugEnabled = false): string
{
try {
/** @var Organization */
Expand Down Expand Up @@ -69,7 +69,9 @@ public function execute(string $name, string $orgId, \DateTimeInterface $laterTh
$reporter->addError(LitteralisRecordEnum::ERROR_IMPORT_COMMAND_FAILED->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $regulationFeatures[0]['properties']['arretesrcid'],
CommonRecordEnum::ATTR_URL->value => $regulationFeatures[0]['properties']['shorturl'],
'message' => $exc->getMessage(),
CommonRecordEnum::ATTR_DEBUG->value => [
'message' => $exc->getMessage(),
],
'violations' => $exc instanceof ValidationFailedException ? iterator_to_array($exc->getViolations()) : null,
'command' => $command,
]);
Expand All @@ -78,10 +80,14 @@ public function execute(string $name, string $orgId, \DateTimeInterface $laterTh
$reporter->acknowledgeNewErrors();
}

$reporter->addCount(LitteralisRecordEnum::COUNT_IMPORTED_FEATURES->value, $numImportedFeatures, ['regulationsCount' => $numImportedRegulations]);
$reporter->addCount(LitteralisRecordEnum::COUNT_IMPORTED_FEATURES->value, $numImportedFeatures, [
CommonRecordEnum::ATTR_DEBUG->value => [
'regulationsCount' => $numImportedRegulations,
],
]);

$reporter->end(endTime: $this->dateUtils->getNow());
$report = $this->reportFormatter->format($reporter->getRecords());
$report = $this->reportFormatter->format($reporter->getRecords(), $isDebugEnabled);
$reporter->onReport($report);

return $report;
Expand Down
10 changes: 8 additions & 2 deletions src/Infrastructure/Litteralis/LitteralisExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function extractFeaturesByRegulation(\DateTimeInterface $laterThan, Repor
$reporter->addWarning(LitteralisRecordEnum::WARNING_MISSING_GEOMETRY->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $identifier,
CommonRecordEnum::ATTR_URL->value => $feature['properties']['shorturl'],
'idemprise' => $feature['properties']['idemprise'],
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => $feature['properties']['idemprise'],
],
]);
continue;
}
Expand All @@ -67,7 +69,11 @@ public function extractFeaturesByRegulation(\DateTimeInterface $laterThan, Repor
++$numExtractedFeatures;
}

$reporter->addCount(LitteralisRecordEnum::COUNT_EXTRACTED_FEATURES->value, $numExtractedFeatures, ['regulationsCount' => \count($featuresByRegulation)]);
$reporter->addCount(LitteralisRecordEnum::COUNT_EXTRACTED_FEATURES->value, $numExtractedFeatures, [
CommonRecordEnum::ATTR_DEBUG->value => [
'regulationsCount' => \count($featuresByRegulation),
],
]);
$reporter->onExtract(json_encode($featuresByRegulation, JSON_UNESCAPED_UNICODE & JSON_UNESCAPED_SLASHES));

return $featuresByRegulation;
Expand Down
22 changes: 14 additions & 8 deletions src/Infrastructure/Litteralis/LitteralisPeriodParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ private function setPeriodDates(SavePeriodCommand $periodCommand, array $propert
[
CommonRecordEnum::ATTR_REGULATION_ID->value => $properties['arretesrcid'],
CommonRecordEnum::ATTR_URL->value => $properties['shorturl'],
'idemprise' => $properties['idemprise'],
$startDateProperty => $properties[$startDateProperty],
'format' => $dateFormat,
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => $properties['idemprise'],
$startDateProperty => $properties[$startDateProperty],
'format' => $dateFormat,
],
],
);
}
Expand All @@ -82,9 +84,11 @@ private function setPeriodDates(SavePeriodCommand $periodCommand, array $propert
[
CommonRecordEnum::ATTR_REGULATION_ID->value => $properties['arretesrcid'],
CommonRecordEnum::ATTR_URL->value => $properties['shorturl'],
'idemprise' => $properties['idemprise'],
$endDateProperty => $properties[$endDateProperty],
'format' => $dateFormat,
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => $properties['idemprise'],
$endDateProperty => $properties[$endDateProperty],
'format' => $dateFormat,
],
],
);
}
Expand Down Expand Up @@ -168,8 +172,10 @@ public function parseTimeSlots(string $value, array $properties, Reporter $repor
$reporter->addError(LitteralisRecordEnum::ERROR_PERIOD_UNPARSABLE->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $properties['arretesrcid'],
CommonRecordEnum::ATTR_URL->value => $properties['shorturl'],
'idemprise' => $properties['idemprise'],
'jours et horaires' => $value,
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => $properties['idemprise'],
'jours et horaires' => $value,
],
]);

return [];
Expand Down
28 changes: 18 additions & 10 deletions src/Infrastructure/Litteralis/LitteralisTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,10 @@ private function gatherMeasureParameters(array $properties, Reporter $reporter):
if (!\array_key_exists($name, self::MEASURE_MAP)) {
$reporter->addNotice(LitteralisRecordEnum::NOTICE_UNSUPPORTED_MEASURE->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $properties['arretesrcid'],
'name' => $name,
'idemprise' => $properties['idemprise'],
CommonRecordEnum::ATTR_DEBUG->value => [
'name' => $name,
'idemprise' => $properties['idemprise'],
],
]);
continue;
}
Expand Down Expand Up @@ -294,10 +296,12 @@ private function gatherMeasureParameters(array $properties, Reporter $reporter):
$reporter->addError(LitteralisRecordEnum::ERROR_MEASURE_PARAMETER_INCONSISTENT_NUMBER->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $properties['arretesrcid'],
CommonRecordEnum::ATTR_URL->value => $properties['shorturl'],
'idemprise' => $properties['idemprise'],
'measureName' => $name,
'expected' => $index + 1,
'actual' => $number,
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => $properties['idemprise'],
'measureName' => $name,
'expected' => $index + 1,
'actual' => $number,
],
]);

continue;
Expand Down Expand Up @@ -393,8 +397,10 @@ private function parseMaxSpeed(array $properties, array $parameters, Reporter $r
$reporter->addError(LitteralisRecordEnum::ERROR_MAX_SPEED_VALUE_MISSING->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $properties['arretesrcid'],
CommonRecordEnum::ATTR_URL->value => $properties['shorturl'],
'idemprise' => $properties['idemprise'],
'mesures' => $properties['mesures'],
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => $properties['idemprise'],
'mesures' => $properties['mesures'],
],
]);

return null;
Expand All @@ -404,8 +410,10 @@ private function parseMaxSpeed(array $properties, array $parameters, Reporter $r
$reporter->addError(LitteralisRecordEnum::ERROR_MAX_SPEED_VALUE_INVALID->value, [
CommonRecordEnum::ATTR_REGULATION_ID->value => $properties['arretesrcid'],
CommonRecordEnum::ATTR_URL->value => $properties['shorturl'],
'idemprise' => $properties['idemprise'],
'limite de vitesse' => $value,
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => $properties['idemprise'],
'limite de vitesse' => $value,
],
]);

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/Litteralis/MEL/MELExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function __construct(
$this->executor->configure($melCredentials);
}

public function execute(\DateTimeInterface $laterThan, Reporter $reporter): string
public function execute(\DateTimeInterface $laterThan, Reporter $reporter, bool $isDebugEnabled = false): string
{
return $this->executor->execute(self::INTEGRATION_NAME, $this->melOrgId, $laterThan, $reporter);
return $this->executor->execute(self::INTEGRATION_NAME, $this->melOrgId, $laterThan, $reporter, $isDebugEnabled);
}
}
9 changes: 8 additions & 1 deletion src/Infrastructure/Symfony/Command/FougeresImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Application\DateUtilsInterface;
use App\Infrastructure\IntegrationReport\Reporter;
use App\Infrastructure\Litteralis\Fougeres\FougeresExecutor;
use App\Infrastructure\Symfony\Option\Litteralis\DebugOption;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -28,13 +29,19 @@ public function __construct(
parent::__construct();
}

protected function configure(): void
{
DebugOption::configureCommand($this);
}

public function execute(InputInterface $input, OutputInterface $output): int
{
$reporter = new Reporter($this->logger);
$now = $this->dateUtils->getNow();
$isDebugEnabled = DebugOption::getFromInput($input);

try {
$report = $this->executor->execute($now, $reporter);
$report = $this->executor->execute($now, $reporter, $isDebugEnabled);

$output->write($report);
} catch (\RuntimeException $exc) {
Expand Down
9 changes: 8 additions & 1 deletion src/Infrastructure/Symfony/Command/MELImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Application\DateUtilsInterface;
use App\Infrastructure\IntegrationReport\Reporter;
use App\Infrastructure\Litteralis\MEL\MELExecutor;
use App\Infrastructure\Symfony\Option\Litteralis\DebugOption;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -28,13 +29,19 @@ public function __construct(
parent::__construct();
}

protected function configure(): void
{
DebugOption::configureCommand($this);
}

public function execute(InputInterface $input, OutputInterface $output): int
{
$reporter = new Reporter($this->logger);
$now = $this->dateUtils->getNow();
$isDebugEnabled = DebugOption::getFromInput($input);

try {
$report = $this->executor->execute(laterThan: $now, reporter: $reporter);
$report = $this->executor->execute($now, $reporter, $isDebugEnabled);

$output->write($report);
} catch (\RuntimeException $exc) {
Expand Down
21 changes: 21 additions & 0 deletions src/Infrastructure/Symfony/Option/Litteralis/DebugOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Symfony\Option\Litteralis;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

class DebugOption
{
public static function configureCommand(Command $command): void
{
$command->addOption('debug', description: 'Include debug information in report');
}

public static function getFromInput(InputInterface $input): bool
{
return $input->getOption('debug');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ public function testExecute(): void
->expects(self::once())
->method('addError')
->with(LitteralisRecordEnum::ERROR_IMPORT_COMMAND_FAILED->value, [
'message' => 'oops',
CommonRecordEnum::ATTR_REGULATION_ID->value => '1234',
CommonRecordEnum::ATTR_URL->value => 'https://dl.sogelink.fr/?n3omzTyS',
CommonRecordEnum::ATTR_DEBUG->value => [
'message' => 'oops',
],
'violations' => null,
'command' => $command3,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ public function testExtractFeaturesByRegulation(): void
->expects(self::once())
->method('addWarning')
->with(LitteralisRecordEnum::WARNING_MISSING_GEOMETRY->value, [
'idemprise' => 'emprise4',
CommonRecordEnum::ATTR_REGULATION_ID->value => 'arrete3',
CommonRecordEnum::ATTR_URL->value => 'https://dl.sogelink.fr/?n3omzTyS',
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => 'emprise4',
],
]);

$extractor = new LitteralisExtractor($this->client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Application\Regulation\Command\Period\SavePeriodCommand;
use App\Application\Regulation\Command\Period\SaveTimeSlotCommand;
use App\Domain\Condition\Period\Enum\PeriodRecurrenceTypeEnum;
use App\Infrastructure\IntegrationReport\CommonRecordEnum;
use App\Infrastructure\IntegrationReport\Reporter;
use App\Infrastructure\Litteralis\LitteralisPeriodParser;
use App\Infrastructure\Litteralis\LitteralisRecordEnum;
Expand Down Expand Up @@ -104,7 +105,17 @@ public function testUnparsable(string $value): void
$reporter
->expects(self::once())
->method('addError')
->with(LitteralisRecordEnum::ERROR_PERIOD_UNPARSABLE->value);
->with(
LitteralisRecordEnum::ERROR_PERIOD_UNPARSABLE->value,
[
CommonRecordEnum::ATTR_REGULATION_ID->value => 'id2',
CommonRecordEnum::ATTR_URL->value => 'url',
CommonRecordEnum::ATTR_DEBUG->value => [
'idemprise' => 'id1',
'jours et horaires' => $value,
],
],
);

$this->assertEquals([], $this->parser->parseTimeSlots($value, $properties, $reporter));
}
Expand Down
Loading

0 comments on commit 246756b

Please sign in to comment.