-
Notifications
You must be signed in to change notification settings - Fork 276
Add commands to compare awards, scoreboard and results #2583
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29e2644
Add commands to compare awards, scoreboard and results
nickygerritsen c4d8c23
Move comparing to a service so we can reuse it (and test it)
nickygerritsen ca0665f
Add tests for compare services
nickygerritsen 2e4abad
Use ResultRow DTO now that we have it
nickygerritsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,96 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Command; | ||
|
||
use App\Service\Compare\AbstractCompareService; | ||
use App\Service\Compare\Message; | ||
use App\Service\Compare\MessageType; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
abstract class AbstractCompareCommand extends Command | ||
{ | ||
/** | ||
* @param AbstractCompareService<T> $compareService | ||
*/ | ||
public function __construct( | ||
protected readonly SerializerInterface $serializer, | ||
protected AbstractCompareService $compareService | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->addArgument('file1', InputArgument::REQUIRED, 'First file to compare') | ||
->addArgument('file2', InputArgument::REQUIRED, 'Second file to compare'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$style = new SymfonyStyle($input, $output); | ||
$messages = $this->compareService->compareFiles($input->getArgument('file1'), $input->getArgument('file2')); | ||
|
||
return $this->displayMessages($style, $messages) ?? Command::SUCCESS; | ||
} | ||
|
||
/** | ||
* @param Message[] $messages | ||
*/ | ||
protected function displayMessages(SymfonyStyle $style, array $messages): ?int | ||
{ | ||
if (empty($messages)) { | ||
$style->success('Files match fully'); | ||
return null; | ||
} | ||
|
||
$headers = ['Level', 'Message', 'Source', 'Target']; | ||
$rows = []; | ||
$counts = []; | ||
foreach ($messages as $message) { | ||
if (!isset($counts[$message->type->value])) { | ||
$counts[$message->type->value] = 0; | ||
} | ||
$counts[$message->type->value]++; | ||
$rows[] = [ | ||
$this->formatMessage($message->type, $message->type->value), | ||
$this->formatMessage($message->type, $message->message), | ||
$this->formatMessage($message->type, $message->source ?? ''), | ||
$this->formatMessage($message->type, $message->target ?? ''), | ||
]; | ||
} | ||
$style->table($headers, $rows); | ||
|
||
$style->newLine(); | ||
foreach ($counts as $type => $count) { | ||
$style->writeln($this->formatMessage(MessageType::from($type), sprintf('Found %d %s(s)', $count, $type))); | ||
} | ||
|
||
if (isset($counts['error'])) { | ||
$style->error('Files have potential critical differences'); | ||
return Command::FAILURE; | ||
} | ||
|
||
$style->success('Files have differences but probably non critical'); | ||
|
||
return null; | ||
} | ||
|
||
protected function formatMessage(MessageType $level, string $message): string | ||
{ | ||
$colors = [ | ||
MessageType::ERROR->value => 'red', | ||
MessageType::WARNING->value => 'yellow', | ||
MessageType::INFO->value => 'green', | ||
]; | ||
return sprintf('<fg=%s>%s</>', $colors[$level->value], $message); | ||
} | ||
} |
This file contains hidden or 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,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Command; | ||
|
||
use App\DataTransferObject\Award; | ||
use App\Service\Compare\AwardCompareService; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @extends AbstractCompareCommand<Award[]> | ||
*/ | ||
#[AsCommand( | ||
name: 'compare:awards', | ||
description: 'Compare awards between two files' | ||
)] | ||
class CompareAwardsCommand extends AbstractCompareCommand | ||
{ | ||
public function __construct( | ||
SerializerInterface $serializer, | ||
AwardCompareService $compareService | ||
) { | ||
parent::__construct($serializer, $compareService); | ||
} | ||
} |
This file contains hidden or 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,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Command; | ||
|
||
use App\DataTransferObject\ResultRow; | ||
use App\Service\Compare\ResultsCompareService; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @extends AbstractCompareCommand<ResultRow[]> | ||
*/ | ||
#[AsCommand( | ||
name: 'compare:results', | ||
description: 'Compare results between two files' | ||
)] | ||
class CompareResultsCommand extends AbstractCompareCommand | ||
{ | ||
public function __construct( | ||
SerializerInterface $serializer, | ||
ResultsCompareService $compareService | ||
) { | ||
parent::__construct($serializer, $compareService); | ||
} | ||
} |
This file contains hidden or 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,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Command; | ||
|
||
use App\DataTransferObject\Scoreboard\Scoreboard; | ||
use App\Service\Compare\ScoreboardCompareService; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @extends AbstractCompareCommand<Scoreboard> | ||
*/ | ||
#[AsCommand( | ||
name: 'compare:scoreboard', | ||
description: 'Compare scoreboard between two files' | ||
)] | ||
class CompareScoreboardCommand extends AbstractCompareCommand | ||
{ | ||
public function __construct( | ||
SerializerInterface $serializer, | ||
ScoreboardCompareService $compareService | ||
) { | ||
parent::__construct($serializer, $compareService); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Service\Compare; | ||
|
||
use Symfony\Component\Serializer\Exception\ExceptionInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
abstract class AbstractCompareService | ||
{ | ||
/** @var Message[] */ | ||
protected array $messages = []; | ||
|
||
public function __construct(protected readonly SerializerInterface $serializer) {} | ||
|
||
/** | ||
* @return Message[] | ||
*/ | ||
public function compareFiles(string $file1, string $file2): array | ||
{ | ||
$success = true; | ||
if (!file_exists($file1)) { | ||
$this->addMessage(MessageType::ERROR, sprintf('File "%s" does not exist', $file1)); | ||
$success = false; | ||
} | ||
if (!file_exists($file2)) { | ||
$this->addMessage(MessageType::ERROR, sprintf('File "%s" does not exist', $file2)); | ||
$success = false; | ||
} | ||
if (!$success) { | ||
return $this->messages; | ||
} | ||
|
||
try { | ||
$object1 = $this->parseFile($file1); | ||
} catch (ExceptionInterface $e) { | ||
$this->addMessage(MessageType::ERROR, sprintf('Error deserializing file "%s": %s', $file1, $e->getMessage())); | ||
} | ||
try { | ||
$object2 = $this->parseFile($file2); | ||
} catch (ExceptionInterface $e) { | ||
$this->addMessage(MessageType::ERROR, sprintf('Error deserializing file "%s": %s', $file2, $e->getMessage())); | ||
} | ||
|
||
if (!isset($object1) || !isset($object2)) { | ||
return $this->messages; | ||
} | ||
|
||
$this->compare($object1, $object2); | ||
|
||
return $this->messages; | ||
} | ||
|
||
/** | ||
* @return T|null | ||
* @throws ExceptionInterface | ||
*/ | ||
abstract protected function parseFile(string $file); | ||
|
||
/** | ||
* @param T $object1 | ||
* @param T $object2 | ||
*/ | ||
abstract public function compare($object1, $object2): void; | ||
|
||
protected function addMessage( | ||
MessageType $type, | ||
string $message, | ||
?string $source = null, | ||
?string $target = null, | ||
): void { | ||
$this->messages[] = new Message($type, $message, $source, $target); | ||
} | ||
|
||
/** | ||
* @return Message[] | ||
*/ | ||
public function getMessages(): array | ||
{ | ||
return $this->messages; | ||
} | ||
} |
This file contains hidden or 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,61 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Service\Compare; | ||
|
||
use App\DataTransferObject\Award; | ||
|
||
/** | ||
* @extends AbstractCompareService<Award[]> | ||
*/ | ||
class AwardCompareService extends AbstractCompareService | ||
{ | ||
protected function parseFile(string $file) | ||
{ | ||
return $this->serializer->deserialize(file_get_contents($file), Award::class . '[]', 'json'); | ||
} | ||
|
||
public function compare($object1, $object2): void | ||
{ | ||
$awards1Indexed = []; | ||
foreach ($object1 as $award) { | ||
$awards1Indexed[$award->id] = $award; | ||
} | ||
|
||
$awards2Indexed = []; | ||
foreach ($object2 as $award) { | ||
$awards2Indexed[$award->id] = $award; | ||
} | ||
|
||
foreach ($awards1Indexed as $awardId => $award) { | ||
if (!isset($awards2Indexed[$awardId])) { | ||
if (!$award->teamIds) { | ||
$this->addMessage(MessageType::INFO, sprintf('Award "%s" not found in second file, but has no team ID\'s in first file', $awardId)); | ||
} else { | ||
$this->addMessage(MessageType::ERROR, sprintf('Award "%s" not found in second file', $awardId)); | ||
} | ||
} else { | ||
$award2 = $awards2Indexed[$awardId]; | ||
if ($award->citation !== $award2->citation) { | ||
$this->addMessage(MessageType::WARNING, sprintf('Award "%s" has different citation', $awardId), $award->citation, $award2->citation); | ||
} | ||
$award1TeamIds = $award->teamIds; | ||
sort($award1TeamIds); | ||
$award2TeamIds = $award2->teamIds; | ||
sort($award2TeamIds); | ||
if ($award1TeamIds !== $award2TeamIds) { | ||
$this->addMessage(MessageType::ERROR, sprintf('Award "%s" has different team ID\'s', $awardId), implode(', ', $award->teamIds), implode(', ', $award2->teamIds)); | ||
} | ||
} | ||
} | ||
|
||
foreach ($awards2Indexed as $awardId => $award) { | ||
if (!isset($awards1Indexed[$awardId])) { | ||
if (!$award->teamIds) { | ||
$this->addMessage(MessageType::INFO, sprintf('Award "%s" not found in first file, but has no team ID\'s in second file', $awardId)); | ||
} else { | ||
$this->addMessage(MessageType::ERROR, sprintf('Award "%s" not found in first file', $awardId)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace App\Service\Compare; | ||
|
||
class Message | ||
{ | ||
public function __construct( | ||
public readonly MessageType $type, | ||
public readonly string $message, | ||
public readonly ?string $source = null, | ||
public readonly ?string $target = null, | ||
) {} | ||
} |
This file contains hidden or 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\Service\Compare; | ||
|
||
enum MessageType: string | ||
{ | ||
case INFO = 'info'; | ||
case WARNING = 'warning'; | ||
case ERROR = 'error'; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.