-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathContestController.php
990 lines (906 loc) · 39.5 KB
/
ContestController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
<?php declare(strict_types=1);
namespace App\Controller\API;
use App\DataTransferObject\ContestState;
use App\DataTransferObject\ContestStatus;
use App\DataTransferObject\PatchContest;
use App\Entity\Contest;
use App\Entity\ContestProblem;
use App\Entity\Event;
use App\Service\AssetUpdateService;
use App\Service\ConfigurationService;
use App\Service\DOMJudgeService;
use App\Service\EventLogService;
use App\Service\ImportExportService;
use App\Utils\EventFeedFormat;
use App\Utils\Utils;
use BadMethodCallException;
use Doctrine\Inflector\InflectorFactory;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\QueryBuilder;
use FOS\RestBundle\Controller\Annotations as Rest;
use JMS\Serializer\Metadata\PropertyMetadata;
use Metadata\MetadataFactoryInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Attributes as OA;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Yaml\Yaml;
use TypeError;
/**
* @extends AbstractRestController<Contest, Contest>
*/
#[Rest\Route('/contests')]
#[OA\Tag(name: 'Contests')]
#[OA\Parameter(ref: '#/components/parameters/strict')]
#[OA\Response(ref: '#/components/responses/InvalidResponse', response: 400)]
#[OA\Response(ref: '#/components/responses/Unauthenticated', response: 401)]
#[OA\Response(ref: '#/components/responses/Unauthorized', response: 403)]
#[OA\Response(ref: '#/components/responses/NotFound', response: 404)]
class ContestController extends AbstractRestController
{
public function __construct(
EntityManagerInterface $entityManager,
DOMJudgeService $dj,
ConfigurationService $config,
EventLogService $eventLogService,
protected readonly ImportExportService $importExportService,
protected readonly LoggerInterface $logger,
protected readonly AssetUpdateService $assetUpdater
) {
parent::__construct($entityManager, $dj, $config, $eventLogService);
}
/**
* Add a new contest.
* @throws BadRequestHttpException
*/
#[IsGranted('ROLE_API_CONTEST_EDITOR')]
#[Rest\Post('')]
#[OA\RequestBody(
required: true,
content: new OA\MediaType(
mediaType: 'multipart/form-data',
schema: new OA\Schema(
properties: [
new OA\Property(
property: 'yaml',
description: 'The contest.yaml file to import.',
type: 'string',
format: 'binary'
),
new OA\Property(
property: 'json',
description: 'The contest.json file to import.',
type: 'string',
format: 'binary'
),
]))
)]
#[OA\Response(response: 200, description: 'Returns the API ID of the added contest.')]
public function addContestAction(Request $request): string
{
/** @var UploadedFile|null $yamlFile */
$yamlFile = $request->files->get('yaml');
/** @var UploadedFile|null $jsonFile */
$jsonFile = $request->files->get('json');
if ((!$yamlFile && !$jsonFile) || ($yamlFile && $jsonFile)) {
throw new BadRequestHttpException('Supply exactly one of \'json\' or \'yaml\'');
}
$message = null;
if ($yamlFile) {
$data = Yaml::parseFile($yamlFile->getRealPath(), Yaml::PARSE_DATETIME);
if ($this->importExportService->importContestData($data, $message, $cid)) {
return $cid;
}
} elseif ($jsonFile) {
$data = $this->dj->jsonDecode(file_get_contents($jsonFile->getRealPath()));
if ($this->importExportService->importContestData($data, $message, $cid)) {
return $cid;
}
}
throw new BadRequestHttpException("Error while adding contest: $message");
}
/**
* Get all the contests.
* @throws NonUniqueResultException
*/
#[Rest\Get('')]
#[OA\Response(
response: 200,
description: 'Returns all contests visible to the user (all contests for privileged users, active contests otherwise)',
content: new OA\JsonContent(
type: 'array',
items: new OA\Items(ref: new Model(type: Contest::class))
)
)]
#[OA\Parameter(ref: '#/components/parameters/idlist')]
#[OA\Parameter(
name: 'onlyActive',
description: 'Whether to only return data pertaining to contests that are active',
in: 'query',
schema: new OA\Schema(type: 'boolean', default: false)
)]
public function listAction(Request $request): Response
{
return parent::performListAction($request);
}
/**
* Get the given contest.
* @throws NonUniqueResultException
*/
#[Rest\Get('/{cid}')]
#[OA\Response(
response: 200,
description: 'Returns the given contest',
content: new OA\JsonContent(ref: new Model(type: Contest::class))
)]
#[OA\Parameter(ref: '#/components/parameters/cid')]
public function singleAction(Request $request, string $cid): Response
{
return parent::performSingleAction($request, $cid);
}
/**
* Get the banner for the given contest.
*/
#[Rest\Get('/{cid}/banner', name: 'contest_banner')]
#[OA\Response(
response: 200,
description: 'Returns the given contest banner in PNG, JPG or SVG format',
content: [
new OA\MediaType(mediaType: 'image/png'),
new OA\MediaType(mediaType: 'image/jpeg'),
new OA\MediaType(mediaType: 'image/svg+xml'),
]
)]
#[OA\Parameter(ref: '#/components/parameters/cid')]
public function bannerAction(Request $request, string $cid): Response
{
/** @var Contest|null $contest */
$contest = $this->getQueryBuilder($request)
->andWhere(sprintf('%s = :id', $this->getIdField()))
->setParameter('id', $cid)
->getQuery()
->getOneOrNullResult();
if ($contest === null) {
throw new NotFoundHttpException(sprintf('Object with ID \'%s\' not found', $cid));
}
$banner = $this->dj->assetPath($cid, 'contest', true);
if ($banner && file_exists($banner)) {
return static::sendBinaryFileResponse($request, $banner);
}
throw new NotFoundHttpException('Contest banner not found');
}
/**
* Delete the banner for the given contest.
*/
#[IsGranted('ROLE_API_CONTEST_EDITOR')]
#[Rest\Delete('/{cid}/banner', name: 'delete_contest_banner')]
#[OA\Response(response: 204, description: 'Deleting banner succeeded')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
public function deleteBannerAction(Request $request, string $cid): Response
{
/** @var Contest $contest */
$contest = $this->getContestAndCheckIfLocked($request, $cid);
$contest->setClearBanner(true);
$this->assetUpdater->updateAssets($contest);
$this->eventLogService->log('contests', $contest->getCid(), EventLogService::ACTION_UPDATE,
$contest->getCid());
return new Response('', Response::HTTP_NO_CONTENT);
}
/**
* Set the banner for the given contest.
*/
#[IsGranted('ROLE_API_CONTEST_EDITOR')]
#[Rest\Post("/{cid}/banner", name: 'post_contest_banner')]
#[Rest\Put("/{cid}/banner", name: 'put_contest_banner')]
#[OA\RequestBody(
required: true,
content: new OA\MediaType(
mediaType: 'multipart/form-data',
schema: new OA\Schema(
required: ['banner'],
properties: [
new OA\Property(
property: 'banner',
description: 'The banner to use.',
type: 'string',
format: 'binary'
),
]
)
)
)]
#[OA\Response(response: 204, description: 'Setting banner succeeded')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
public function setBannerAction(Request $request, string $cid, ValidatorInterface $validator): Response
{
$contest = $this->getContestAndCheckIfLocked($request, $cid);
/** @var UploadedFile|null $banner */
$banner = $request->files->get('banner');
if (!$banner) {
return new JsonResponse(['title' => 'Validation failed', 'errors' => ['Please supply a banner']], Response::HTTP_BAD_REQUEST);
}
$contest->setBannerFile($banner);
if ($errorResponse = $this->responseForErrors($validator->validate($contest), true)) {
return $errorResponse;
}
$this->assetUpdater->updateAssets($contest);
$this->eventLogService->log('contests', $contest->getCid(), EventLogService::ACTION_UPDATE,
$contest->getCid());
return new Response('', Response::HTTP_NO_CONTENT);
}
/**
* Delete the problemset document for the given contest.
*/
#[IsGranted('ROLE_API_CONTEST_EDITOR')]
#[Rest\Delete('/{cid}/problemset', name: 'delete_contest_problemset')]
#[OA\Response(response: 204, description: 'Deleting problemset document succeeded')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
public function deleteProblemsetAction(Request $request, string $cid): Response
{
$contest = $this->getContestAndCheckIfLocked($request, $cid);
$contest->setClearContestProblemset(true);
$contest->processContestProblemset();
$this->em->flush();
$this->eventLogService->log('contests', $contest->getCid(), EventLogService::ACTION_UPDATE,
$contest->getCid());
return new Response('', Response::HTTP_NO_CONTENT);
}
/**
* Set the problemset document for the given contest.
*/
#[IsGranted('ROLE_API_CONTEST_EDITOR')]
#[Rest\Post("/{cid}/problemset", name: 'post_contest_problemset')]
#[Rest\Put("/{cid}/problemset", name: 'put_contest_problemset')]
#[OA\RequestBody(
required: true,
content: new OA\MediaType(
mediaType: 'multipart/form-data',
schema: new OA\Schema(
required: ['problemset'],
properties: [
new OA\Property(
property: 'problemset',
description: 'The problemset document to use, as either text/html, text/plain or application/pdf.',
type: 'string',
format: 'binary'
),
]
)
)
)]
#[OA\Response(response: 204, description: 'Setting problemset document succeeded')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
public function setProblemsetAction(Request $request, string $cid, ValidatorInterface $validator): Response
{
$contest = $this->getContestAndCheckIfLocked($request, $cid);
/** @var UploadedFile|null $problemset */
$problemset = $request->files->get('problemset');
if (!$problemset) {
return new JsonResponse(['title' => 'Validation failed', 'errors' => ['Please supply a problemset document']], Response::HTTP_BAD_REQUEST);
}
if (!in_array($problemset->getMimeType(), ['text/html', 'text/plain', 'application/pdf'])) {
return new JsonResponse(['title' => 'Validation failed', 'errors' => ['Invalid problemset document type']], Response::HTTP_BAD_REQUEST);
}
$contest->setContestProblemsetFile($problemset);
if ($errorResponse = $this->responseForErrors($validator->validate($contest), true)) {
return $errorResponse;
}
$contest->processContestProblemset();
$this->em->flush();
$this->eventLogService->log('contests', $contest->getCid(), EventLogService::ACTION_UPDATE,
$contest->getCid());
return new Response('', Response::HTTP_NO_CONTENT);
}
/**
* Get the problemset document for the given contest.
*/
#[Rest\Get('/{cid}/problemset', name: 'contest_problemset')]
#[OA\Response(
response: 200,
description: 'Returns the given contest problemset document in PDF, HTML or TXT format',
content: [
new OA\MediaType(mediaType: 'application/pdf'),
new OA\MediaType(mediaType: 'text/plain'),
new OA\MediaType(mediaType: 'text/html'),
]
)]
#[OA\Parameter(ref: '#/components/parameters/cid')]
public function problemsetAction(Request $request, string $cid): Response
{
/** @var Contest|null $contest */
$contest = $this->getQueryBuilder($request)
->andWhere(sprintf('%s = :id', $this->getIdField()))
->setParameter('id', $cid)
->getQuery()
->getOneOrNullResult();
$hasAccess = $this->dj->checkrole('jury') ||
$this->dj->checkrole('api_reader') ||
$contest->getFreezeData()->started();
if (!$hasAccess) {
throw new AccessDeniedHttpException();
}
if ($contest === null) {
throw new NotFoundHttpException(sprintf('Object with ID \'%s\' not found', $cid));
}
if (!$contest->getContestProblemsetType()) {
throw new NotFoundHttpException(sprintf('Contest with ID \'%s\' has no problemset text', $cid));
}
return $contest->getContestProblemsetStreamedResponse();
}
/**
* Change the start time or unfreeze (thaw) time of the given contest.
* @throws NonUniqueResultException
*/
#[IsGranted(new Expression("is_granted('ROLE_API_WRITER') or is_granted('ROLE_API_CONTEST_EDITOR')"))]
#[Rest\Patch('/{cid}')]
#[OA\RequestBody(
required: true,
content: new OA\MediaType(
mediaType: 'application/x-www-form-urlencoded',
schema: new OA\Schema(ref: new Model(type: PatchContest::class))
)
)]
#[OA\Response(
response: 204,
description: 'The change was successful'
)]
#[OA\Response(
response: 200,
description: 'Contest start time changed successfully',
content: new OA\JsonContent(ref: new Model(type: Contest::class))
)]
public function changeStartTimeAction(
#[MapRequestPayload(validationFailedStatusCode: Response::HTTP_BAD_REQUEST)]
PatchContest $patchContest,
Request $request,
#[OA\PathParameter(description: 'The ID of the contest to change the start time for')]
string $cid
): Response {
$response = new Response('', Response::HTTP_NO_CONTENT);
$contest = $this->getContestWithId($request, $cid);
$now = (int)Utils::now();
$changed = false;
// We still need these checks explicit check since they can be null.
if (!$request->request->has('start_time') && !$request->request->has('scoreboard_thaw_time')) {
throw new BadRequestHttpException('Missing "start_time" or "scoreboard_thaw_time" in request.');
}
if ($request->request->get('id') != $contest->getExternalid()) {
throw new BadRequestHttpException('Invalid "id" in request.');
}
if ($request->request->has('start_time') && $request->request->has('scoreboard_thaw_time')) {
throw new BadRequestHttpException('Setting both "start_time" and "scoreboard_thaw_time" at the same time is not allowed.');
}
if ($request->request->has('start_time')) {
// By default, it is not allowed to change the start time in the last 30 seconds before contest start.
// We allow the "force" parameter to override this.
if (!$patchContest->force &&
$contest->getStarttime() != null &&
$contest->getStarttime() < $now + 30) {
throw new AccessDeniedHttpException('Current contest already started or about to start.');
}
if ($patchContest->startTime === null) {
$contest->setStarttimeEnabled(false);
$this->em->flush();
$changed = true;
} else {
$date = date_create($patchContest->startTime);
if ($date === false) {
throw new BadRequestHttpException('Invalid "start_time" in request.');
}
$new_start_time = $date->getTimestamp();
if (!$patchContest->force && $new_start_time < $now + 30) {
throw new AccessDeniedHttpException('New start_time not far enough in the future.');
}
$newStartTimeString = date('Y-m-d H:i:s e', $new_start_time);
$contest->setStarttimeEnabled(true);
$contest->setStarttime($new_start_time);
$contest->setStarttimeString($newStartTimeString);
$this->em->flush();
$changed = true;
}
}
if ($request->request->has('scoreboard_thaw_time')) {
if (!$patchContest->force && $contest->getUnfreezetime() !== null) {
throw new AccessDeniedHttpException('Current contest already has an unfreeze time set.');
}
$date = date_create($patchContest->scoreboardThawTime ?? 'not a valid date');
if ($date === false) {
throw new BadRequestHttpException('Invalid "scoreboard_thaw_time" in request.');
}
$new_unfreeze_time = $date->getTimestamp();
if (!$patchContest->force && $new_unfreeze_time < $now - 30) {
throw new AccessDeniedHttpException('New scoreboard_thaw_time too far in the past.');
}
$returnContest = false;
if ($new_unfreeze_time < $now) {
$new_unfreeze_time = $now;
$returnContest = true;
}
$newUnfreezeTimeString = date('Y-m-d H:i:s e', $new_unfreeze_time);
$contest->setUnfreezetime($new_unfreeze_time);
$contest->setUnfreezetimeString($newUnfreezeTimeString);
$this->em->flush();
$changed = true;
if ($returnContest) {
$response = $this->renderData($request, $contest);
}
}
if ($changed) {
$this->eventLogService->log('contests', $contest->getCid(), EventLogService::ACTION_UPDATE,
$contest->getCid());
}
return $response;
}
/**
* Get the contest in YAML format.
* @throws NonUniqueResultException
*/
#[Rest\Get('/{cid}/contest-yaml')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
#[OA\Response(
response: 200,
description: 'The contest in YAML format',
content: new OA\MediaType(mediaType: 'application/x-yaml')
)]
public function getContestYamlAction(Request $request, string $cid): StreamedResponse
{
$contest = $this->getContestWithId($request, $cid);
$response = new StreamedResponse();
$response->setCallback(function () use ($contest) {
echo Yaml::dump($this->importExportService->getContestYamlData($contest, false), 3);
});
$response->headers->set('Content-Type', 'application/x-yaml');
$response->headers->set('Content-Disposition', 'attachment; filename="contest.yaml"');
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Connection', 'Keep-Alive');
$response->headers->set('Accept-Ranges', 'bytes');
return $response;
}
/**
* Get the current contest state
* @throws NonUniqueResultException
*/
#[Rest\Get('/{cid}/state')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
#[OA\Response(
response: 200,
description: 'The contest state',
content: new OA\JsonContent(ref: new Model(type: ContestState::class))
)]
public function getContestStateAction(Request $request, string $cid): ContestState
{
$contest = $this->getContestWithId($request, $cid);
$inactiveAllowed = $this->isGranted('ROLE_API_READER');
if (($inactiveAllowed && $contest->getEnabled()) || (!$inactiveAllowed && $contest->isActive())) {
return $contest->getState();
} else {
throw new AccessDeniedHttpException();
}
}
/**
* Get the event feed for the given contest.
* @throws NonUniqueResultException
*/
#[IsGranted(new Expression("is_granted('ROLE_JURY') or is_granted('ROLE_API_READER')"))]
#[Rest\Get('/{cid}/event-feed')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
#[OA\Parameter(
name: 'since_id',
description: 'Only get events after this event',
in: 'query',
schema: new OA\Schema(type: 'string')
)]
#[OA\Parameter(
name: 'types',
description: 'Types to filter the event feed on',
in: 'query',
schema: new OA\Schema(
type: 'array',
items: new OA\Items(description: 'A single type', type: 'string')
)
)]
#[OA\Parameter(
name: 'stream',
description: 'Whether to stream the output or stop immediately',
in: 'query',
schema: new OA\Schema(type: 'boolean', default: true)
)]
#[OA\Response(
response: 200,
description: 'The events',
content: new OA\MediaType(
mediaType: 'application/x-ndjson',
schema: new OA\Schema(
type: 'array',
items: new OA\Items(
properties: [
new OA\Property(
property: 'id',
type: 'string'
),
new OA\Property(
property: 'type',
type: 'string'
),
new OA\Property(
property: 'op',
type: 'string'
),
new OA\Property(
property: 'data',
type: 'object'
),
new OA\Property(
property: 'time',
type: 'string',
format: 'date-time'
),
],
type: 'object'
)
)
)
)]
public function getEventFeedAction(
Request $request,
string $cid,
#[Autowire(service: 'jms_serializer.metadata_factory')]
MetadataFactoryInterface $metadataFactory,
KernelInterface $kernel,
#[MapQueryParameter(name: 'since_token')]
?string $sinceToken = null,
#[MapQueryParameter(name: 'since_id')]
?string $sinceId = null,
#[MapQueryParameter]
?string $types = null,
#[MapQueryParameter]
bool $strict = false,
#[MapQueryParameter]
bool $stream = true,
): Response {
$contest = $this->getContestWithId($request, $cid);
// Make sure this script doesn't hit the PHP maximum execution timeout.
set_time_limit(0);
if ($sinceToken !== null | $sinceId !== null) {
// This parameter is a string in the spec, but we want an integer
$since_id = (int)($sinceToken ?? $sinceId);
$event = $this->em->getRepository(Event::class)->findOneBy([
'eventid' => $since_id,
'contest' => $contest,
]);
if ($event === null) {
throw new BadRequestHttpException(
sprintf(
'Invalid parameter "%s" requested.',
$request->query->has('since_token') ? 'since_token' : 'since_id'
)
);
}
} else {
$since_id = -1;
}
$format = $this->config->get('event_feed_format');
$response = new StreamedResponse();
$response->headers->set('X-Accel-Buffering', 'no');
$response->headers->set('Content-Type', 'application/x-ndjson');
$response->setCallback(function () use ($format, $cid, $contest, $request, $since_id, $types, $strict, $stream, $metadataFactory, $kernel) {
$lastUpdate = 0;
$lastIdSent = max(0, $since_id); // Don't try to look for event_id=0
$typeFilter = false;
if ($types) {
$typeFilter = explode(',', $types);
}
$canViewAll = $this->isGranted('ROLE_API_READER');
// Keep track of the last send state event; we may have the same
// event more than once in our table and we want to make sure we
// only send it out once.
$lastState = null;
$skippedProperties = [];
// Determine which properties we should not send out for strict clients.
// We do this here instead of every loop to speed up sending events at
// the cost of sending out the first byte a bit slower.
if ($strict) {
$toCheck = [];
$dir = realpath($kernel->getProjectDir() . '/src/Entity');
$files = glob($dir . '/*.php');
foreach ($files as $file) {
$parts = explode('/', $file);
$shortClass = str_replace('.php', '', $parts[count($parts) - 1]);
$class = sprintf('App\\Entity\\%s', $shortClass);
if (class_exists($class)) {
$inflector = InflectorFactory::create()->build();
$plural = strtolower($inflector->pluralize($shortClass));
$toCheck[$plural] = $class;
}
}
// Change some specific endpoints that do not map to our own objects.
$toCheck['problems'] = ContestProblem::class;
$toCheck['judgements'] = $toCheck['judgings'];
$toCheck['groups'] = $toCheck['teamcategories'];
$toCheck['organizations'] = $toCheck['teamaffiliations'];
unset($toCheck['teamcategories']);
unset($toCheck['teamaffiliations']);
unset($toCheck['contestproblems']);
foreach ($toCheck as $plural => $class) {
$serializerMetadata = $metadataFactory->getMetadataForClass($class);
/** @var PropertyMetadata $propertyMetadata */
foreach ($serializerMetadata->propertyMetadata as $propertyMetadata) {
if (is_array($propertyMetadata->groups) &&
!in_array('Default', $propertyMetadata->groups)) {
$skippedProperties[$plural][] = $propertyMetadata->serializedName;
}
}
}
// Special case: do not send external ID for problems in strict mode
// This needs to be here since externalid is a property of the Problem
// entity, not the ContestProblem entity, so the above loop will not
// detect it.
$skippedProperties['problems'][] = 'externalid';
}
// Initialize all static events.
$this->eventLogService->initStaticEvents($contest);
// Reload the contest as the above method will clear the entity manager.
$contest = $this->getContestWithId($request, $cid);
$missingEventRetries = 0;
while (true) {
// Add missing state events that should have happened already.
$this->eventLogService->addMissingStateEvents($contest);
// We fetch *all* events after the last seen to check that
// we don't skip events that are committed out of order.
$q = $this->em->createQueryBuilder()
->from(Event::class, 'e')
->select('e')
->andWhere('e.eventid > :lastIdSent')
->setParameter('lastIdSent', $lastIdSent)
->orderBy('e.eventid', 'ASC')
->getQuery();
/** @var Event[] $events */
$events = $q->getResult();
// Look for any missing sequential events and wait for them to
// be committed if so.
$missingEvents = false;
$expectedId = $lastIdSent + 1;
$lastFoundId = null;
foreach ($events as $event) {
if ($event->getEventid() !== $expectedId) {
$missingEvents = true;
$lastFoundId = $event->getEventid();
break;
}
$expectedId++;
}
if ($missingEvents) {
if ($missingEventRetries == 0) {
$this->logger->info(
'Detected missing events %d ... %d, waiting for these to appear',
[$expectedId, $lastFoundId-1]
);
}
if (++$missingEventRetries < 10) {
usleep(100 * 1000);
continue;
}
// We've decided to permanently ignore these non-existing
// events for this connection. The wait for any
// non-committed events was long enough.
//
// There might be multiple non-existing events. Log the
// first consecutive gap of non-existing events. A consecutive
// gap is guaranteed since the events are ordered.
$this->logger->warning(
'Waited too long for missing events %d ... %d, skipping',
[$expectedId, $lastFoundId-1]
);
}
$missingEventRetries = 0;
$numEventsSent = 0;
foreach ($events as $event) {
// Filter out unwanted events
if ($event->getContest()->getCid() !== $contest->getCid()) {
continue;
}
if ($typeFilter !== false &&
!in_array($event->getEndpointtype(), $typeFilter)) {
continue;
}
if (!$canViewAll) {
$restricted_types = ['judgements', 'runs', 'clarifications'];
if ($contest->getStarttime() === null || Utils::now() < $contest->getStarttime()) {
$restricted_types[] = 'problems';
}
if (in_array($event->getEndpointtype(), $restricted_types)) {
continue;
}
}
$data = $event->getContent();
// Filter fields with specific access restrictions.
if (!$canViewAll) {
if ($event->getEndpointtype() == 'submissions') {
unset($data['entry_point']);
unset($data['language_id']);
}
if ($event->getEndpointtype() == 'problems') {
unset($data['test_data_count']);
}
}
// Do not send out the same state event twice
if ($event->getEndpointtype() === 'state') {
if ($data === $lastState) {
continue;
}
$lastState = $data;
}
if ($strict) {
$toSkip = $skippedProperties[$event->getEndpointtype()] ?? [];
foreach ($toSkip as $property) {
unset($data[$property]);
}
}
switch ($format) {
case EventFeedFormat::Format_2020_03:
$result = [
'id' => (string)$event->getEventid(),
'type' => (string)$event->getEndpointtype(),
'op' => (string)$event->getAction(),
'data' => $data,
];
break;
case EventFeedFormat::Format_2022_07:
if ($event->getAction() === EventLogService::ACTION_DELETE) {
$data = null;
}
$id = (string)$event->getEndpointid() ?: null;
$type = (string)$event->getEndpointtype();
if ($type === 'contests') {
// Special case: the type for a contest is singular and the ID must not be set
$id = null;
$type = 'contest';
}
$result = [
'token' => (string)$event->getEventid(),
'id' => $id,
'type' => $type,
'data' => $data,
];
break;
default:
throw new BadMethodCallException(sprintf('Invalid event feed format %s', $format));
}
if (!$strict) {
$result['time'] = Utils::absTime($event->getEventtime());
}
echo $this->dj->jsonEncode($result) . "\n";
ob_flush();
flush();
$lastUpdate = Utils::now();
$lastIdSent = $event->getEventid();
$numEventsSent++;
if ($missingEvents && $event->getEventid() >= $lastFoundId) {
// The first event after the first gap has been emitted. Stop
// emitting events and restart the gap detection logic to find
// any potential gaps after this last emitted event.
break;
}
}
if ($numEventsSent == 0) {
if (!$stream) {
break;
}
// No new events, check if it's time for a keep alive.
$now = Utils::now();
if ($lastUpdate + 10 < $now) {
# Send keep alive every 10s. Guarantee according to spec is 120s.
# However, nginx drops the connection if we don't update for 60s.
echo "\n";
ob_flush();
flush();
$lastUpdate = $now;
}
# Sleep for little while before checking for new events.
usleep(500 * 1000);
}
}
});
return $response;
}
/**
* Get general status information.
* @throws NoResultException
* @throws NonUniqueResultException
*/
#[IsGranted('ROLE_API_READER')]
#[Rest\Get('/{cid}/status')]
#[OA\Parameter(ref: '#/components/parameters/cid')]
#[OA\Response(
response: 200,
description: 'General status information for the given contest',
content: new OA\JsonContent(ref: new Model(type: ContestStatus::class))
)]
public function getStatusAction(Request $request, string $cid): ContestStatus
{
return $this->dj->getContestStats($this->getContestWithId($request, $cid));
}
#[Rest\Get('/{cid}/samples.zip', name: 'samples_data_zip')]
#[OA\Response(
response: 200,
description: 'The problem samples, statement & attachments as a ZIP archive',
content: new OA\MediaType(mediaType: 'application/zip')
)]
public function samplesDataZipAction(Request $request): Response
{
// getContestQueryBuilder add filters to only get the contests that the user
// has access to.
/** @var Contest|null $contest */
$contest = $this->getContestQueryBuilder()
->andWhere('c.cid = :cid')
->setParameter('cid', $this->getContestId($request))
->getQuery()
->getOneOrNullResult();
if ($contest === null) {
throw new NotFoundHttpException(sprintf('Object with ID \'%s\' not found', $request->attributes->get('cid')));
}
return $this->dj->getSamplesZipForContest($contest);
}
protected function getQueryBuilder(Request $request): QueryBuilder
{
try {
return $this->getContestQueryBuilder($request->query->getBoolean('onlyActive', false));
} catch (TypeError) {
throw new BadRequestHttpException('\'onlyActive\' must be a boolean.');
}
}
protected function getIdField(): string
{
return 'c.externalid';
}
/**
* Get the contest with the given ID.
* @throws NonUniqueResultException
*/
protected function getContestWithId(Request $request, string $id): Contest
{
$queryBuilder = $this->getQueryBuilder($request)
->andWhere(sprintf('%s = :id', $this->getIdField()))
->setParameter('id', $id);
$contest = $queryBuilder->getQuery()->getOneOrNullResult();
if ($contest === null) {
throw new NotFoundHttpException(sprintf('Contest with ID \'%s\' not found', $id));
}
return $contest;
}
/** To be used when contest data is modified. */
private function getContestAndCheckIfLocked(Request $request, string $cid): Contest
{
/** @var Contest|null $contest */
$contest = $this->getQueryBuilder($request)
->andWhere(sprintf('%s = :id', $this->getIdField()))
->setParameter('id', $cid)
->getQuery()
->getOneOrNullResult();
if ($contest === null) {
throw new NotFoundHttpException(sprintf('Contest with ID \'%s\' not found', $cid));
}
if ($contest->isLocked()) {
$contestUrl = $this->generateUrl('jury_contest', ['contestId' => $cid], UrlGeneratorInterface::ABSOLUTE_URL);
throw new AccessDeniedHttpException('Contest is locked, go to ' . $contestUrl . ' to unlock it.');
}
return $contest;
}
}