1
|
|
|
<?php |
2
|
|
|
namespace AOE\Crawler\Domain\Model; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 AOE GmbH <[email protected]> |
8
|
|
|
* |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use AOE\Crawler\Domain\Repository\QueueRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class Process |
32
|
|
|
* |
33
|
|
|
* @package AOE\Crawler\Domain\Model |
34
|
|
|
*/ |
35
|
|
|
class Process |
36
|
|
|
{ |
37
|
|
|
const STATE_RUNNING = 'running'; |
38
|
|
|
const STATE_CANCELLED = 'cancelled'; |
39
|
|
|
const STATE_COMPLETED = 'completed'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $row; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $row |
48
|
|
|
*/ |
49
|
|
|
public function __construct($row = []) |
50
|
|
|
{ |
51
|
|
|
$this->row = $row; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the activity state for this process |
56
|
|
|
* |
57
|
|
|
* @param void |
58
|
|
|
* @return boolean |
59
|
|
|
*/ |
60
|
|
|
public function getActive() |
61
|
|
|
{ |
62
|
|
|
return $this->row['active']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the identifier for the process |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getProcess_id() |
71
|
|
|
{ |
72
|
|
|
return $this->row['process_id']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the timestamp of the exectime for the first relevant queue item. |
77
|
|
|
* This can be used to determine the runtime |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function getTimeForFirstItem() |
82
|
|
|
{ |
83
|
|
|
$queueRepository = new QueueRepository(); |
84
|
|
|
$entry = $queueRepository->findYoungestEntryForProcess($this); |
85
|
|
|
|
86
|
|
|
return $entry->getExecutionTime(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the timestamp of the exectime for the last relevant queue item. |
91
|
|
|
* This can be used to determine the runtime |
92
|
|
|
* |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
public function getTimeForLastItem() |
96
|
|
|
{ |
97
|
|
|
$queueRepository = new QueueRepository(); |
98
|
|
|
$entry = $queueRepository->findOldestEntryForProcess($this); |
99
|
|
|
|
100
|
|
|
return $entry->getExecutionTime(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the difference between first and last processed item |
105
|
|
|
* |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getRuntime() |
109
|
|
|
{ |
110
|
|
|
return $this->getTimeForLastItem() - $this->getTimeForFirstItem(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the ttl of the process |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public function getTTL() |
119
|
|
|
{ |
120
|
|
|
return $this->row['ttl']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Counts the number of items which need to be processed |
125
|
|
|
* |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
|
|
public function countItemsProcessed() |
129
|
|
|
{ |
130
|
|
|
$queueRepository = new QueueRepository(); |
131
|
|
|
return $queueRepository->countExecutedItemsByProcess($this); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Counts the number of items which still need to be processed |
136
|
|
|
* |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
|
|
public function countItemsToProcess() |
140
|
|
|
{ |
141
|
|
|
$queueRepository = new QueueRepository(); |
142
|
|
|
return $queueRepository->countNonExecutedItemsByProcess($this); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the Progress of a crawling process as a percentage value |
147
|
|
|
* |
148
|
|
|
* @return float |
149
|
|
|
*/ |
150
|
|
|
public function getProgress() |
151
|
|
|
{ |
152
|
|
|
$all = $this->countItemsAssigned(); |
153
|
|
|
if ($all <= 0) { |
154
|
|
|
return 0; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$res = round((100 / $all) * $this->countItemsProcessed()); |
158
|
|
|
|
159
|
|
|
if ($res > 100.0) { |
160
|
|
|
return 100.0; |
161
|
|
|
} |
162
|
|
|
return $res; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns the number of assigned entries |
167
|
|
|
* |
168
|
|
|
* @return int |
169
|
|
|
*/ |
170
|
|
|
public function countItemsAssigned() |
171
|
|
|
{ |
172
|
|
|
return $this->row['assigned_items_count']; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return the processes current state |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getState() |
181
|
|
|
{ |
182
|
|
|
if ($this->getActive() && $this->getProgress() < 100) { |
183
|
|
|
$stage = self::STATE_RUNNING; |
184
|
|
|
} elseif (!$this->getActive() && $this->getProgress() < 100) { |
185
|
|
|
$stage = self::STATE_CANCELLED; |
186
|
|
|
} else { |
187
|
|
|
$stage = self::STATE_COMPLETED; |
188
|
|
|
} |
189
|
|
|
return $stage; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns the properties of the object as array |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
public function getRow() |
198
|
|
|
{ |
199
|
|
|
return $this->row; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|