|
| 1 | +<?php declare(strict_types=1); |
| 2 | +namespace App\Entity; |
| 3 | + |
| 4 | +use App\Utils\Utils; |
| 5 | +use Doctrine\Common\Collections\ArrayCollection; |
| 6 | +use Doctrine\Common\Collections\Collection; |
| 7 | +use Doctrine\ORM\Mapping as ORM; |
| 8 | +use JMS\Serializer\Annotation as Serializer; |
| 9 | + |
| 10 | +/** |
| 11 | + * Result of a generic task. |
| 12 | + */ |
| 13 | +#[ORM\Entity] |
| 14 | +#[ORM\Table(options: [ |
| 15 | + 'collation' => 'utf8mb4_unicode_ci', |
| 16 | + 'charset' => 'utf8mb4', |
| 17 | + 'comment' => 'Result of a generic task', |
| 18 | +])] |
| 19 | +class GenericTask extends BaseApiEntity |
| 20 | +{ |
| 21 | + #[ORM\Id] |
| 22 | + #[ORM\GeneratedValue] |
| 23 | + #[ORM\Column(options: ['comment' => 'Task ID', 'unsigned' => true])] |
| 24 | + #[Serializer\SerializedName('id')] |
| 25 | + #[Serializer\Type('string')] |
| 26 | + protected int $taskid; |
| 27 | + |
| 28 | + #[ORM\Column( |
| 29 | + nullable: true, |
| 30 | + options: ['comment' => 'JudgeTask ID', 'unsigned' => true, 'default' => null] |
| 31 | + )] |
| 32 | + #[Serializer\Exclude] |
| 33 | + private ?int $judgetaskid = null; |
| 34 | + |
| 35 | + #[ORM\Column( |
| 36 | + nullable: true, |
| 37 | + options: ['comment' => 'Running time for this task'] |
| 38 | + )] |
| 39 | + #[Serializer\Exclude] |
| 40 | + private string|float|null $runtime = null; |
| 41 | + |
| 42 | + #[ORM\Column( |
| 43 | + type: 'decimal', |
| 44 | + precision: 32, |
| 45 | + scale: 9, |
| 46 | + nullable: true, |
| 47 | + options: ['comment' => 'Time task ended', 'unsigned' => true] |
| 48 | + )] |
| 49 | + #[Serializer\Exclude] |
| 50 | + private string|float|null $endtime = null; |
| 51 | + |
| 52 | + #[ORM\Column( |
| 53 | + type: 'decimal', |
| 54 | + precision: 32, |
| 55 | + scale: 9, |
| 56 | + nullable: true, |
| 57 | + options: ['comment' => 'Time task started', 'unsigned' => true] |
| 58 | + )] |
| 59 | + #[Serializer\Exclude] |
| 60 | + private string|float|null $startTime = null; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var Collection<int, JudgingRunOutput> |
| 64 | + * |
| 65 | + * We use a OneToMany instead of a OneToOne here, because otherwise this |
| 66 | + * relation will always be loaded. See the commit message of commit |
| 67 | + * 9e421f96691ec67ed62767fe465a6d8751edd884 for a more elaborate explanation |
| 68 | + */ |
| 69 | + #[ORM\OneToMany(mappedBy: 'run', targetEntity: GenericTaskOutput::class, cascade: ['persist'], orphanRemoval: true)] |
| 70 | + #[Serializer\Exclude] |
| 71 | + private Collection $output; |
| 72 | + |
| 73 | + #[ORM\ManyToOne(inversedBy: 'judging_runs')] |
| 74 | + #[ORM\JoinColumn(name: 'judgetaskid', referencedColumnName: 'judgetaskid', onDelete: 'CASCADE')] |
| 75 | + #[Serializer\Exclude] |
| 76 | + private ?JudgeTask $judgetask = null; |
| 77 | + |
| 78 | + public function __construct() |
| 79 | + { |
| 80 | + $this->output = new ArrayCollection(); |
| 81 | + } |
| 82 | + |
| 83 | + public function getTaskid(): int |
| 84 | + { |
| 85 | + return $this->taskid; |
| 86 | + } |
| 87 | + |
| 88 | + public function setJudgeTaskId(int $judgetaskid): GenericTask |
| 89 | + { |
| 90 | + $this->judgetaskid = $judgetaskid; |
| 91 | + return $this; |
| 92 | + } |
| 93 | + |
| 94 | + public function getJudgeTaskId(): ?int |
| 95 | + { |
| 96 | + return $this->judgetaskid; |
| 97 | + } |
| 98 | + |
| 99 | + public function getJudgeTask(): ?JudgeTask |
| 100 | + { |
| 101 | + return $this->judgetask; |
| 102 | + } |
| 103 | + |
| 104 | + public function setJudgeTask(JudgeTask $judgeTask): GenericTask |
| 105 | + { |
| 106 | + $this->judgetask = $judgeTask; |
| 107 | + return $this; |
| 108 | + } |
| 109 | + |
| 110 | + public function setRuntime(string|float $runtime): GenericTask |
| 111 | + { |
| 112 | + $this->runtime = $runtime; |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + #[Serializer\VirtualProperty] |
| 117 | + #[Serializer\SerializedName('run_time')] |
| 118 | + #[Serializer\Type('float')] |
| 119 | + public function getRuntime(): string|float|null |
| 120 | + { |
| 121 | + return Utils::roundedFloat($this->runtime); |
| 122 | + } |
| 123 | + |
| 124 | + public function setEndtime(string|float $endtime): GenericTask |
| 125 | + { |
| 126 | + $this->endtime = $endtime; |
| 127 | + return $this; |
| 128 | + } |
| 129 | + |
| 130 | + public function setStarttime(string|float $startTime): GenericTask |
| 131 | + { |
| 132 | + $this->startTime = $startTime; |
| 133 | + return $this; |
| 134 | + } |
| 135 | + |
| 136 | + public function getStarttime(): string|float|null |
| 137 | + { |
| 138 | + return $this->startTime; |
| 139 | + } |
| 140 | + |
| 141 | + public function getEndtime(): string|float|null |
| 142 | + { |
| 143 | + return $this->endtime; |
| 144 | + } |
| 145 | + |
| 146 | + #[Serializer\VirtualProperty] |
| 147 | + #[Serializer\SerializedName('time')] |
| 148 | + #[Serializer\Type('string')] |
| 149 | + public function getAbsoluteEndTime(): string |
| 150 | + { |
| 151 | + return Utils::absTime($this->getEndtime()); |
| 152 | + } |
| 153 | + |
| 154 | + public function setOutput(GenericTaskOutput $output): GenericTask |
| 155 | + { |
| 156 | + $this->output->clear(); |
| 157 | + $this->output->add($output); |
| 158 | + $output->setRun($this); |
| 159 | + |
| 160 | + return $this; |
| 161 | + } |
| 162 | + |
| 163 | + public function getOutput(): ?GenericTaskOutput |
| 164 | + { |
| 165 | + return $this->output->first() ?: null; |
| 166 | + } |
| 167 | +} |
0 commit comments