1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Tack Tracker - A library for tracking long-running task progress |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT |
7
|
|
|
* @link https://github.com/caseyamcl/tasktracker |
8
|
|
|
* @version 2.0 |
9
|
|
|
* @package caseyamcl/tasktracker |
10
|
|
|
* @author Casey McLaughlin <[email protected]> |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* ------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace TaskTracker; |
19
|
|
|
|
20
|
|
|
use TaskTracker\Helper\MagicPropsTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Task Tracker Report |
24
|
|
|
* |
25
|
|
|
* Represents a snapshot of task state from a specific time* |
26
|
|
|
* |
27
|
|
|
* @author Casey McLaughlin <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Report implements ReportInterface |
30
|
|
|
{ |
31
|
|
|
use MagicPropsTrait { |
32
|
|
|
toArray as traitToArray; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Tick |
37
|
|
|
*/ |
38
|
|
|
private $tick; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Tracker |
42
|
|
|
*/ |
43
|
|
|
private $tracker; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
private $memUsage; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
private $memPeakUsage; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var float |
57
|
|
|
*/ |
58
|
|
|
private $itemTime; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var float |
62
|
|
|
*/ |
63
|
|
|
private $maxTickTime; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var float |
67
|
|
|
*/ |
68
|
|
|
private $minTickTime; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Constructor |
72
|
|
|
* |
73
|
|
|
* @param Tick $tick Empty if no tick yet |
74
|
|
|
* @param Tracker $tracker The Task Tracker |
75
|
|
|
*/ |
76
|
|
|
public function __construct(Tick $tick, Tracker $tracker) |
77
|
|
|
{ |
78
|
|
|
$this->tick = $tick; |
79
|
|
|
$this->tracker = $tracker; |
80
|
|
|
|
81
|
|
|
// A snapshot of these values needs to be created upon report generation |
82
|
|
|
$this->memUsage = memory_get_usage(true); |
83
|
|
|
$this->memPeakUsage = memory_get_peak_usage(true); |
84
|
|
|
|
85
|
|
|
// Also, determining item time values needs to happen immediately |
86
|
|
|
$this->itemTime = ($this->tracker->getLastTick()) |
87
|
|
|
? $this->tick->getTimestamp() - $this->tracker->getLastTick()->getTimestamp() |
88
|
|
|
: $this->getTimeElapsed(); |
89
|
|
|
|
90
|
|
|
if ($this->tracker->getLastTick()) { |
91
|
|
|
$this->minTickTime = min($this->getItemTime(), $this->tracker->getLastTick()->getReport()->getMinItemTime()); |
92
|
|
|
$this->maxTickTime = max($this->getItemTime(), $this->tracker->getLastTick()->getReport()->getMaxItemTime()); |
93
|
|
|
} |
94
|
|
|
else { |
95
|
|
|
$this->minTickTime = $this->minTickTime = $this->getItemTime(); |
96
|
|
|
$this->maxTickTime = $this->minTickTime = $this->getItemTime(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the time this task started in microseconds |
102
|
|
|
* |
103
|
|
|
* @return float |
104
|
|
|
*/ |
105
|
|
|
public function getTimeStarted() |
106
|
|
|
{ |
107
|
|
|
return $this->tracker->getStartTime(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the total number of items that are to be processed. |
112
|
|
|
* |
113
|
|
|
* If unknown or not specified, this returns Tracker::UNKNOWN |
114
|
|
|
* |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function getTotalItemCount() |
118
|
|
|
{ |
119
|
|
|
return $this->tracker->getNumTotalItems(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the Tracker Tick object for this report |
124
|
|
|
* |
125
|
|
|
* @return Tick |
126
|
|
|
*/ |
127
|
|
|
public function getTick() |
128
|
|
|
{ |
129
|
|
|
return $this->tick; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the total number of items processed (including skipped and failed) |
134
|
|
|
* |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
|
|
public function getNumItemsProcessed() |
138
|
|
|
{ |
139
|
|
|
return $this->tracker->getNumProcessedItems(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the time elapsed in microseconds |
144
|
|
|
* |
145
|
|
|
* @return float |
146
|
|
|
*/ |
147
|
|
|
public function getTimeElapsed() |
148
|
|
|
{ |
149
|
|
|
return $this->tick->getTimestamp() - $this->tracker->getStartTime(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the number of items thus far that successfully processed |
155
|
|
|
* |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
public function getNumItemsSuccess() |
159
|
|
|
{ |
160
|
|
|
return $this->tracker->getNumProcessedItems(Tick::SUCCESS); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the number of items processed thus far that failed |
165
|
|
|
* |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
|
|
public function getNumItemsFail() |
169
|
|
|
{ |
170
|
|
|
return $this->tracker->getNumProcessedItems(Tick::FAIL); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the number of items processed thus far that were skipped |
175
|
|
|
* |
176
|
|
|
* @return int |
177
|
|
|
*/ |
178
|
|
|
public function getNumItemsSkip() |
179
|
|
|
{ |
180
|
|
|
return $this->tracker->getNumProcessedItems(Tick::SKIP); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns the amount of time the last item took to process |
185
|
|
|
* |
186
|
|
|
* @return float |
187
|
|
|
*/ |
188
|
|
|
public function getItemTime() |
189
|
|
|
{ |
190
|
|
|
return $this->itemTime; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns the maximum amount of time any one item has taken to process thus far |
195
|
|
|
* |
196
|
|
|
* @return float |
197
|
|
|
*/ |
198
|
|
|
public function getMaxItemTime() |
199
|
|
|
{ |
200
|
|
|
return $this->maxTickTime; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the minimum amount of time any one item has taken to process thus far |
205
|
|
|
* |
206
|
|
|
* @return float |
207
|
|
|
*/ |
208
|
|
|
public function getMinItemTime() |
209
|
|
|
{ |
210
|
|
|
return $this->minTickTime; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the current average (mean) amount of time that items have taken to process thus far |
215
|
|
|
* |
216
|
|
|
* @return float |
217
|
|
|
*/ |
218
|
|
|
public function getAvgItemTime() |
219
|
|
|
{ |
220
|
|
|
return ($this->getNumItemsProcessed() > 0) |
221
|
|
|
? ($this->getTimeElapsed() / $this->getNumItemsProcessed()) |
222
|
|
|
: 0; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns the message associated with the last Tick event |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getMessage() |
231
|
|
|
{ |
232
|
|
|
return $this->tick->getMessage(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Returns the timestamp (microtime float) for this Tick event |
237
|
|
|
* |
238
|
|
|
* @return float |
239
|
|
|
*/ |
240
|
|
|
public function getTimestamp() |
241
|
|
|
{ |
242
|
|
|
return $this->tick->getTimestamp(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Returns the status (Tick::SUCCESS, Tick::FAIL, TICK::SKIP) of the last item processed |
247
|
|
|
* |
248
|
|
|
* @return int |
249
|
|
|
*/ |
250
|
|
|
public function getStatus() |
251
|
|
|
{ |
252
|
|
|
return $this->tick->getStatus(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Returns the number of increments associated with the last processed item |
257
|
|
|
* |
258
|
|
|
* @return int |
259
|
|
|
*/ |
260
|
|
|
public function getIncrementBy() |
261
|
|
|
{ |
262
|
|
|
return $this->tick->getIncrementBy(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the memory usage at the time of the last processed item (in bytes) |
267
|
|
|
* |
268
|
|
|
* @return int |
269
|
|
|
*/ |
270
|
|
|
public function getMemUsage() |
271
|
|
|
{ |
272
|
|
|
return $this->memUsage; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Returns the peak memory usage thus far |
277
|
|
|
* |
278
|
|
|
* @return int |
279
|
|
|
*/ |
280
|
|
|
public function getMemPeakUsage() |
281
|
|
|
{ |
282
|
|
|
return $this->memPeakUsage; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Returns this report |
287
|
|
|
* |
288
|
|
|
* @return Report |
289
|
|
|
*/ |
290
|
|
|
public function getReport() |
291
|
|
|
{ |
292
|
|
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Returns any extra information associated with the last tick |
297
|
|
|
* |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
|
|
public function getExtraInfo() |
301
|
|
|
{ |
302
|
|
|
return $this->tick->getExtraInfo(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Converts this report to an array |
307
|
|
|
* |
308
|
|
|
* @return array |
309
|
|
|
*/ |
310
|
|
|
public function toArray() |
311
|
|
|
{ |
312
|
|
|
$arr = $this->traitToArray(); |
313
|
|
|
unset($arr['report'], $arr['tick']); |
314
|
|
|
return $arr; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|