Skip to content

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 4 commits into from
Aug 3, 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
96 changes: 96 additions & 0 deletions webapp/src/Command/AbstractCompareCommand.php
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);
}
}
25 changes: 25 additions & 0 deletions webapp/src/Command/CompareAwardsCommand.php
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);
}
}
25 changes: 25 additions & 0 deletions webapp/src/Command/CompareResultsCommand.php
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);
}
}
25 changes: 25 additions & 0 deletions webapp/src/Command/CompareScoreboardCommand.php
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);
}
}
2 changes: 1 addition & 1 deletion webapp/src/DataTransferObject/Award.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Award
*/
public function __construct(
public readonly string $id,
public readonly string $citation,
public readonly ?string $citation,
#[Serializer\Type('array<string>')]
public readonly array $teamIds,
) {}
Expand Down
12 changes: 6 additions & 6 deletions webapp/src/DataTransferObject/ContestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
class ContestState
{
public function __construct(
public readonly ?string $started,
public readonly ?string $ended,
public readonly ?string $frozen,
public readonly ?string $thawed,
public readonly ?string $finalized,
public readonly ?string $endOfUpdates,
public readonly ?string $started = null,
public readonly ?string $ended = null,
public readonly ?string $frozen = null,
public readonly ?string $thawed = null,
public readonly ?string $finalized = null,
public readonly ?string $endOfUpdates = null,
) {}
}
2 changes: 1 addition & 1 deletion webapp/src/DataTransferObject/Scoreboard/Problem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Problem
{
public function __construct(
#[Serializer\Groups([ARC::GROUP_NONSTRICT])]
public readonly string $label,
public readonly ?string $label,
public readonly string $problemId,
public readonly int $numJudged,
public readonly int $numPending,
Expand Down
84 changes: 84 additions & 0 deletions webapp/src/Service/Compare/AbstractCompareService.php
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;
}
}
61 changes: 61 additions & 0 deletions webapp/src/Service/Compare/AwardCompareService.php
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));
}
}
}
}
}
13 changes: 13 additions & 0 deletions webapp/src/Service/Compare/Message.php
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,
) {}
}
10 changes: 10 additions & 0 deletions webapp/src/Service/Compare/MessageType.php
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';
}
Loading
Loading