Skip to content

Commit

Permalink
Affiche plus de détails dans le rapport Litteralis
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Jan 13, 2025
1 parent 8f35323 commit f2d9a6f
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 43 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_DETAILS = 'details';

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

private function addDetails(string $line, array $data): string
{
$detailItems = [];

foreach ($data as $key => $item) {
$detailItem = [];

if (\is_string($key)) {
// Details is a single ['key' => 'val'] array
$detailItem[] = \sprintf('%s = %s', $key, var_export($item, true));
} else {
// Details is a list of ['key' => 'val'] arrays
foreach ($item as $key => $value) {
$detailItem[] = \sprintf('%s = %s', $key, var_export($value, true));
}
}

$detailItems[] = implode(', ', $detailItem);
}

return \sprintf('%s (%s)', $line, implode(' ; ', $detailItems));
}

public function format(array $records): string
{
$lines = [];
Expand All @@ -77,7 +100,13 @@ public function format(array $records): string
$value = $this->arrayEncode($value);
}

$lines[] = \sprintf('%s : %s', $verboseName, $value);
$line = \sprintf('%s : %s', $verboseName, $value);

if (!empty($context[CommonRecordEnum::ATTR_DETAILS->value])) {
$line = $this->addDetails($line, $context[CommonRecordEnum::ATTR_DETAILS->value]);
}

$lines[] = $line;
}

$lines[] = '';
Expand All @@ -104,6 +133,10 @@ public function format(array $records): string
);
}

if (!empty($context[CommonRecordEnum::ATTR_DETAILS->value])) {
$line = $this->addDetails($line, $context[CommonRecordEnum::ATTR_DETAILS->value]);
}

$lines[] = $line;
}

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

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

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

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

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

Expand Down Expand Up @@ -173,11 +211,15 @@ 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 (\array_key_exists($id, $info['details']) && !empty($info['details'][$id])) {
$line = $this->addDetails($line, data: $info['details'][$id]);
}

$lines[] = $line;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Infrastructure/Litteralis/LitteralisExecutor.php
Original file line number Diff line number Diff line change
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_DETAILS->value => [
'message' => $exc->getMessage(),
],
'violations' => $exc instanceof ValidationFailedException ? iterator_to_array($exc->getViolations()) : null,
'command' => $command,
]);
Expand Down
4 changes: 3 additions & 1 deletion 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_DETAILS->value => [
'idemprise' => $feature['properties']['idemprise'],
],
]);
continue;
}
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_DETAILS->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_DETAILS->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_DETAILS->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_DETAILS->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_DETAILS->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_DETAILS->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_DETAILS->value => [
'idemprise' => $properties['idemprise'],
'limite de vitesse' => $value,
],
]);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,104 @@ public function testFormat(): void
Arrêtés :
arrete3
";

$this->assertSame($expectedResult, $reportFormatter->format($records));
}

public function testDetails(): void
{
$reportFormatter = new ReportFormatter($this->translator);

$records = [
[
RecordTypeEnum::FACT->value,
[
RecordTypeEnum::FACT->value => 'example.organization',
'value' => 'Ville exemple',
CommonRecordEnum::ATTR_REGULATION_ID->value => 'regulation1',
CommonRecordEnum::ATTR_DETAILS->value => ['orgId' => 'abc123'],
],
],
[
RecordTypeEnum::COUNT->value,
[
RecordTypeEnum::COUNT->value => 'example.numFeatures',
'value' => 42,
CommonRecordEnum::ATTR_REGULATION_ID->value => 'regulation1',
CommonRecordEnum::ATTR_DETAILS->value => ['numRegulations' => 104],
],
],
[
RecordTypeEnum::ERROR->value,
[
RecordTypeEnum::ERROR->value => 'example.importCommandFailed',
CommonRecordEnum::ATTR_REGULATION_ID->value => 'regulation1',
CommonRecordEnum::ATTR_DETAILS->value => ['message' => 'oops'],
],
],
[
RecordTypeEnum::WARNING->value,
[
RecordTypeEnum::WARNING->value => 'example.missingGeometry',
CommonRecordEnum::ATTR_REGULATION_ID->value => 'regulation1',
CommonRecordEnum::ATTR_DETAILS->value => ['idemprise' => 'def456'],
],
],
[
RecordTypeEnum::NOTICE->value,
[
RecordTypeEnum::NOTICE->value => 'example.unsupportedMeasure',
CommonRecordEnum::ATTR_REGULATION_ID->value => 'regulation1',
CommonRecordEnum::ATTR_DETAILS->value => ['name' => 'Stationnement interdit', 'idemprise' => 123],
],
],
[
RecordTypeEnum::NOTICE->value,
[
RecordTypeEnum::NOTICE->value => 'example.unsupportedMeasure',
CommonRecordEnum::ATTR_REGULATION_ID->value => 'regulation1',
CommonRecordEnum::ATTR_DETAILS->value => ['name' => 'Interdiction de dépasser', 'idemprise' => 456],
],
],
];

$expectedResult = "Rapport d'intégration
======================
Informations d'exécution
-------------------------
integration.report.fact.example.organization : Ville exemple (orgId = 'abc123')
Décomptes
----------
integration.report.count.example.numFeatures : 42 (numRegulations = 104)
Erreurs
-------
integration.report.error.example.importCommandFailed : 1 (dans 1 arrêtés)
Arrêtés :
regulation1 (message = 'oops')
Avertissements
--------------
integration.report.warning.example.missingGeometry : 1 (dans 1 arrêtés)
Arrêtés :
regulation1 (idemprise = 'def456')
Remarques
---------
integration.report.notice.example.unsupportedMeasure : 2 (dans 1 arrêtés)
Arrêtés :
regulation1 (name = 'Stationnement interdit', idemprise = 123 ; name = 'Interdiction de dépasser', idemprise = 456)
";

$this->assertSame($expectedResult, $reportFormatter->format($records));
Expand Down
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_DETAILS->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_DETAILS->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_DETAILS->value => [
'idemprise' => 'id1',
'jours et horaires' => $value,
],
],
);

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

0 comments on commit f2d9a6f

Please sign in to comment.