1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AOE\Crawler\Domain\Model; |
||
6 | |||
7 | /*************************************************************** |
||
8 | * Copyright notice |
||
9 | * |
||
10 | * (c) 2020 AOE GmbH <[email protected]> |
||
11 | * |
||
12 | * All rights reserved |
||
13 | * |
||
14 | * This script is part of the TYPO3 project. The TYPO3 project is |
||
15 | * free software; you can redistribute it and/or modify |
||
16 | * it under the terms of the GNU General Public License as published by |
||
17 | * the Free Software Foundation; either version 3 of the License, or |
||
18 | * (at your option) any later version. |
||
19 | * |
||
20 | * The GNU General Public License can be found at |
||
21 | * http://www.gnu.org/copyleft/gpl.html. |
||
22 | * |
||
23 | * This script is distributed in the hope that it will be useful, |
||
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
26 | * GNU General Public License for more details. |
||
27 | * |
||
28 | * This copyright notice MUST APPEAR in all copies of the script! |
||
29 | ***************************************************************/ |
||
30 | |||
31 | use AOE\Crawler\Domain\Repository\QueueRepository; |
||
32 | use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait; |
||
33 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
34 | use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; |
||
35 | use TYPO3\CMS\Extbase\Object\ObjectManager; |
||
36 | |||
37 | /** |
||
38 | * Class Process |
||
39 | * |
||
40 | * @ignoreAnnotation("noRector") |
||
41 | * @internal since v9.2.5 |
||
42 | */ |
||
43 | class Process extends AbstractEntity |
||
44 | { |
||
45 | use PublicMethodDeprecationTrait; |
||
46 | |||
47 | public const STATE_RUNNING = 'running'; |
||
48 | |||
49 | public const STATE_CANCELLED = 'cancelled'; |
||
50 | |||
51 | public const STATE_COMPLETED = 'completed'; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $processId = ''; |
||
57 | |||
58 | /** |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $active = false; |
||
62 | |||
63 | /** |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $ttl = 0; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $assignedItemsCount = 0; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $deleted = false; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $systemProcessId = ''; |
||
82 | |||
83 | /** |
||
84 | * @var QueueRepository |
||
85 | */ |
||
86 | protected $queueRepository; |
||
87 | |||
88 | /** |
||
89 | * @var string[] |
||
90 | * @noRector \Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector |
||
91 | * @noRector \Rector\DeadCode\Rector\Property\RemoveSetterOnlyPropertyAndMethodCallRector |
||
92 | */ |
||
93 | private $deprecatedPublicMethods = [ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
94 | 'getTimeForFirstItem' => 'Using Process::getTimeForFirstItem() is deprecated since 9.0.1 and will be removed in v11.x', |
||
95 | 'getTimeForLastItem' => 'Using Process::getTimeForLastItem() is deprecated since 9.0.1 and will be removed in v11.x', |
||
96 | ]; |
||
97 | |||
98 | 17 | public function __construct() |
|
99 | { |
||
100 | 17 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
|
101 | 17 | $this->queueRepository = $objectManager->get(QueueRepository::class); |
|
102 | 17 | } |
|
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | 9 | public function getProcessId() |
|
108 | { |
||
109 | 9 | return $this->processId; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param string $processId |
||
114 | */ |
||
115 | 43 | public function setProcessId($processId): void |
|
116 | { |
||
117 | 43 | $this->processId = $processId; |
|
118 | 43 | } |
|
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | 1 | public function isActive() |
|
124 | { |
||
125 | 1 | return $this->active; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param bool $active |
||
130 | */ |
||
131 | 35 | public function setActive($active): void |
|
132 | { |
||
133 | 35 | $this->active = $active; |
|
134 | 35 | } |
|
135 | |||
136 | /** |
||
137 | * @return int |
||
138 | */ |
||
139 | 1 | public function getTtl() |
|
140 | { |
||
141 | 1 | return $this->ttl; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param int $ttl |
||
146 | */ |
||
147 | 35 | public function setTtl($ttl): void |
|
148 | { |
||
149 | 35 | $this->ttl = $ttl; |
|
150 | 35 | } |
|
151 | |||
152 | /** |
||
153 | * @return int |
||
154 | */ |
||
155 | 1 | public function getAssignedItemsCount() |
|
156 | { |
||
157 | 1 | return $this->assignedItemsCount; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param int $assignedItemsCount |
||
162 | */ |
||
163 | 35 | public function setAssignedItemsCount($assignedItemsCount): void |
|
164 | { |
||
165 | 35 | $this->assignedItemsCount = $assignedItemsCount; |
|
166 | 35 | } |
|
167 | |||
168 | /** |
||
169 | * @return bool |
||
170 | */ |
||
171 | 1 | public function isDeleted() |
|
172 | { |
||
173 | 1 | return $this->deleted; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param bool $deleted |
||
178 | */ |
||
179 | 11 | public function setDeleted($deleted): void |
|
180 | { |
||
181 | 11 | $this->deleted = $deleted; |
|
182 | 11 | } |
|
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | 1 | public function getSystemProcessId() |
|
188 | { |
||
189 | 1 | return $this->systemProcessId; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param string $systemProcessId |
||
194 | */ |
||
195 | 11 | public function setSystemProcessId($systemProcessId): void |
|
196 | { |
||
197 | 11 | $this->systemProcessId = $systemProcessId; |
|
198 | 11 | } |
|
199 | |||
200 | /** |
||
201 | * Returns the difference between first and last processed item |
||
202 | * |
||
203 | * @return int |
||
204 | */ |
||
205 | 5 | public function getRuntime() |
|
206 | { |
||
207 | 5 | $lastItem = $this->getQueueRepository()->findOldestEntryForProcess($this); |
|
208 | 5 | $firstItem = $this->getQueueRepository()->findYoungestEntryForProcess($this); |
|
209 | 5 | return $lastItem['exec_time'] - $firstItem['exec_time']; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * @deprecated |
||
214 | */ |
||
215 | 2 | public function getTimeForLastItem(): int |
|
216 | { |
||
217 | 2 | $entry = $this->getQueueRepository()->findOldestEntryForProcess($this); |
|
218 | 2 | return $entry['exec_time'] ?? 0; |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * @deprecated |
||
223 | */ |
||
224 | 2 | public function getTimeForFirstItem(): int |
|
225 | { |
||
226 | 2 | $entry = $this->getQueueRepository()->findYoungestEntryForProcess($this); |
|
227 | 2 | return $entry['exec_time'] ?? 0; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Counts the number of items which need to be processed |
||
232 | * |
||
233 | * @return int |
||
234 | * @codeCoverageIgnore |
||
235 | */ |
||
236 | public function getAmountOfItemsProcessed() |
||
237 | { |
||
238 | return $this->getQueueRepository()->countExecutedItemsByProcess($this); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Counts the number of items which still need to be processed |
||
243 | * |
||
244 | * @return int |
||
245 | * @codeCoverageIgnore |
||
246 | */ |
||
247 | public function getItemsToProcess() |
||
248 | { |
||
249 | return $this->getQueueRepository()->countNonExecutedItemsByProcess($this); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @codeCoverageIgnore as it's a simple addition function |
||
254 | */ |
||
255 | public function getFinallyAssigned(): int |
||
256 | { |
||
257 | return $this->getItemsToProcess() + $this->getAmountOfItemsProcessed(); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Returns the Progress of a crawling process as a percentage value |
||
262 | */ |
||
263 | 11 | public function getProgress(): float |
|
264 | { |
||
265 | 11 | $all = $this->getAssignedItemsCount(); |
|
266 | 11 | if ($all <= 0) { |
|
267 | 2 | return 0.0; |
|
268 | } |
||
269 | |||
270 | 9 | $res = round((100 / $all) * $this->getAmountOfItemsProcessed()); |
|
271 | |||
272 | 9 | if ($res > 100.0) { |
|
273 | 2 | return 100.0; |
|
274 | } |
||
275 | 7 | return $res; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * Return the processes current state |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 4 | public function getState() |
|
284 | { |
||
285 | 4 | if ($this->isActive() && $this->getProgress() < 100) { |
|
286 | 1 | $stage = self::STATE_RUNNING; |
|
287 | 3 | } elseif (! $this->isActive() && $this->getProgress() < 100) { |
|
288 | 1 | $stage = self::STATE_CANCELLED; |
|
289 | } else { |
||
290 | 2 | $stage = self::STATE_COMPLETED; |
|
291 | } |
||
292 | 4 | return $stage; |
|
293 | } |
||
294 | |||
295 | 9 | private function getQueueRepository(): QueueRepository |
|
296 | { |
||
297 | 9 | $this->queueRepository = $this->queueRepository ?? GeneralUtility::makeInstance(QueueRepository::class); |
|
298 | 9 | return $this->queueRepository; |
|
299 | } |
||
300 | } |
||
301 |