1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A page with inheritance of methods of the snapshot |
5
|
|
|
* @maintainer Timur Shagiakhmetov <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Badoo\LiveProfilerUI\Pages; |
9
|
|
|
|
10
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodInterface; |
11
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodDataInterface; |
12
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodTreeInterface; |
13
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SnapshotInterface; |
14
|
|
|
use Badoo\LiveProfilerUI\Entity\Snapshot; |
15
|
|
|
use Badoo\LiveProfilerUI\FieldList; |
16
|
|
|
use Badoo\LiveProfilerUI\Interfaces\ViewInterface; |
17
|
|
|
|
18
|
|
|
class ProfileMethodTreePage extends BasePage |
19
|
|
|
{ |
20
|
|
|
const STAT_INTERVAL_WEEK = 7; |
21
|
|
|
const STAT_INTERVAL_MONTH = 31; |
22
|
|
|
const STAT_INTERVAL_HALF_YEAR = 182; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected static $template_path = 'profile_method_tree'; |
26
|
|
|
/** @var SnapshotInterface */ |
27
|
|
|
protected $Snapshot; |
28
|
|
|
/** @var MethodInterface */ |
29
|
|
|
protected $Method; |
30
|
|
|
/** @var MethodTreeInterface */ |
31
|
|
|
protected $MethodTree; |
32
|
|
|
/** @var MethodDataInterface */ |
33
|
|
|
protected $MethodData; |
34
|
|
|
/** @var FieldList */ |
35
|
|
|
protected $FieldList; |
36
|
|
|
/** @var string */ |
37
|
|
|
protected $calls_count_field = ''; |
38
|
|
|
/** @var array */ |
39
|
|
|
protected static $graph_intervals = [ |
40
|
|
|
'7 days' => self::STAT_INTERVAL_WEEK, |
41
|
|
|
'1 month' => self::STAT_INTERVAL_MONTH, |
42
|
|
|
'6 months' => self::STAT_INTERVAL_HALF_YEAR, |
43
|
|
|
]; |
44
|
|
|
|
45
|
1 |
|
public function __construct( |
46
|
|
|
ViewInterface $View, |
47
|
|
|
SnapshotInterface $Snapshot, |
48
|
|
|
MethodInterface $Method, |
49
|
|
|
MethodTreeInterface $MethodTree, |
50
|
|
|
MethodDataInterface $MethodData, |
51
|
|
|
FieldList $FieldList, |
52
|
|
|
string $calls_count_field |
53
|
|
|
) { |
54
|
1 |
|
$this->View = $View; |
55
|
1 |
|
$this->Snapshot = $Snapshot; |
56
|
1 |
|
$this->Method = $Method; |
57
|
1 |
|
$this->MethodTree = $MethodTree; |
58
|
1 |
|
$this->MethodData = $MethodData; |
59
|
1 |
|
$this->FieldList = $FieldList; |
60
|
1 |
|
$this->calls_count_field = $calls_count_field; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
2 |
|
protected function cleanData() : bool |
64
|
|
|
{ |
65
|
2 |
|
$this->data['app'] = isset($this->data['app']) ? trim($this->data['app']) : ''; |
66
|
2 |
|
$this->data['label'] = isset($this->data['label']) ? trim($this->data['label']) : ''; |
67
|
2 |
|
$this->data['snapshot_id'] = isset($this->data['snapshot_id']) ? (int)$this->data['snapshot_id'] : 0; |
68
|
|
|
|
69
|
2 |
|
if (!$this->data['snapshot_id'] && (!$this->data['app'] || !$this->data['label'])) { |
70
|
1 |
|
throw new \InvalidArgumentException('Empty snapshot_id, app and label'); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$this->initDates(); |
74
|
1 |
|
$this->initMethodId(); |
75
|
|
|
|
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
protected function initMethodId() |
80
|
|
|
{ |
81
|
1 |
|
$this->data['method_id'] = isset($this->data['method_id']) ? (int)$this->data['method_id'] : 0; |
82
|
|
|
|
83
|
1 |
|
if (!empty($this->data['method_name']) && !$this->data['method_id']) { |
84
|
|
|
$this->data['method_name'] = ltrim($this->data['method_name'], '\\'); |
85
|
|
|
$methods = $this->Method->findByName($this->data['method_name'], true); |
86
|
|
|
if (!empty($methods)) { |
87
|
|
|
$this->data['method_id'] = array_keys($methods)[0]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
1 |
|
} |
91
|
|
|
|
92
|
1 |
|
public function initDates() |
93
|
|
|
{ |
94
|
1 |
|
$this->data['date1'] = isset($this->data['date1']) ? trim($this->data['date1']) : ''; |
95
|
1 |
|
$this->data['date2'] = isset($this->data['date2']) ? trim($this->data['date2']) : ''; |
96
|
|
|
|
97
|
1 |
|
if ($this->data['date1'] && $this->data['date2']) { |
98
|
|
|
$this->data['stat_interval'] = 0; |
99
|
|
|
} else { |
100
|
1 |
|
$this->data['stat_interval'] = isset($this->data['stat_interval']) ? (int)$this->data['stat_interval'] : 0; |
101
|
1 |
|
if (!\in_array($this->data['stat_interval'], self::$graph_intervals, true)) { |
102
|
1 |
|
$this->data['stat_interval'] = self::STAT_INTERVAL_MONTH; |
103
|
|
|
} |
104
|
|
|
} |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
* @throws \InvalidArgumentException |
110
|
|
|
*/ |
111
|
4 |
|
public function getTemplateData() : array |
112
|
|
|
{ |
113
|
4 |
|
$Snapshot = $this->getSnapshot(); |
114
|
|
|
|
115
|
4 |
|
if (!$this->data['method_id']) { |
116
|
4 |
|
$this->data['method_id'] = $this->getMainMethodId(); |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
$methods = $this->Method->getListByIds([$this->data['method_id']]); |
120
|
4 |
|
$method_name = '?'; |
121
|
4 |
|
if (!empty($methods)) { |
122
|
4 |
|
$method_name = $methods[$this->data['method_id']]; |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
if ($this->data['date1'] && $this->data['date2']) { |
126
|
|
|
$dates = \Badoo\LiveProfilerUI\DateGenerator::getDatesByRange( |
127
|
|
|
$this->data['date1'], |
128
|
|
|
$this->data['date2'] |
129
|
|
|
); |
130
|
|
|
} else { |
131
|
4 |
|
$dates = \Badoo\LiveProfilerUI\DateGenerator::getDatesArray( |
132
|
4 |
|
$Snapshot->getDate(), |
133
|
4 |
|
$this->data['stat_interval'], |
134
|
4 |
|
$this->data['stat_interval'] |
135
|
|
|
); |
136
|
4 |
|
$this->data['date1'] = current($dates); |
137
|
4 |
|
$this->data['date2'] = end($dates); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$link_base = '/profiler/tree-view.phtml?' |
141
|
4 |
|
. 'app=' . urlencode($Snapshot->getApp()) |
142
|
4 |
|
. '&label=' . urlencode($Snapshot->getLabel()); |
143
|
|
|
|
144
|
|
|
$view_data = [ |
145
|
4 |
|
'snapshot_id' => $Snapshot->getId(), |
146
|
4 |
|
'snapshot_app' => $Snapshot->getApp(), |
147
|
4 |
|
'snapshot_label' => $Snapshot->getLabel(), |
148
|
4 |
|
'snapshot_date' => $Snapshot->getDate(), |
149
|
4 |
|
'method_id' => $this->data['method_id'], |
150
|
4 |
|
'method_name' => $method_name, |
151
|
4 |
|
'method_dates' => $dates, |
152
|
4 |
|
'stat_intervals' => $this->getIntervalsFormData($link_base), |
153
|
4 |
|
'date1' => $this->data['date1'], |
154
|
4 |
|
'date2' => $this->data['date2'], |
155
|
4 |
|
'available_graphs' => $this->getGraphsData(), |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$common_block_data = [ |
159
|
4 |
|
'link_base' => $link_base, |
160
|
4 |
|
'fields' => $this->FieldList->getAllFieldsWithVariations(), |
161
|
4 |
|
'field_descriptions' => $this->FieldList->getFieldDescriptions(), |
162
|
4 |
|
'stat_interval' => $this->data['stat_interval'], |
163
|
4 |
|
'date1' => $this->data['date1'], |
164
|
4 |
|
'date2' => $this->data['date2'], |
165
|
|
|
]; |
166
|
|
|
|
167
|
4 |
|
$date_to_snapshot_map = $this->Snapshot->getSnapshotIdsByDates( |
168
|
4 |
|
$dates, |
169
|
4 |
|
$Snapshot->getApp(), |
170
|
4 |
|
$Snapshot->getLabel() |
171
|
|
|
); |
172
|
|
|
|
173
|
4 |
|
$method_data = $this->getMethodDataWithHistory($date_to_snapshot_map, $this->data['method_id']); |
174
|
4 |
|
if (!empty($method_data)) { |
175
|
1 |
|
$view_data['method_data'] = $this->View->fetchFile( |
176
|
1 |
|
'profiler_result_view_part', |
177
|
1 |
|
$common_block_data + ['data' => $method_data, 'hide_lines_column' => true], |
178
|
1 |
|
false |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
4 |
|
$parents = $this->getMethodParentsWithHistory($date_to_snapshot_map, $this->data['method_id']); |
183
|
4 |
|
if (!empty($parents)) { |
184
|
1 |
|
$this->sortList($parents); |
185
|
1 |
|
$view_data['parents'] = $this->View->fetchFile( |
186
|
1 |
|
'profiler_result_view_part', |
187
|
1 |
|
$common_block_data + ['data' => $parents], |
188
|
1 |
|
false |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
4 |
|
$children = $this->getMethodChildrenWithHistory($date_to_snapshot_map, $this->data['method_id']); |
193
|
4 |
|
if (!empty($children)) { |
194
|
1 |
|
$this->sortList($children); |
195
|
1 |
|
$view_data['children'] = $this->View->fetchFile( |
196
|
1 |
|
'profiler_result_view_part', |
197
|
1 |
|
$common_block_data + ['data' => $children, 'hide_lines_column' => true], |
198
|
1 |
|
false |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
4 |
|
$view_data['js_graph_data_all'] = array_merge($method_data, $children); |
203
|
4 |
|
$view_data['all_apps'] = $this->Snapshot->getAppList($Snapshot->getLabel()); |
204
|
|
|
|
205
|
4 |
|
return $view_data; |
206
|
|
|
} |
207
|
|
|
|
208
|
4 |
|
protected function getSnapshot() : Snapshot |
209
|
|
|
{ |
210
|
|
|
try { |
211
|
4 |
|
if ($this->data['snapshot_id']) { |
212
|
1 |
|
$Snapshot = $this->Snapshot->getOneById($this->data['snapshot_id']); |
213
|
3 |
|
} elseif ($this->data['app'] && $this->data['label']) { |
214
|
2 |
|
$Snapshot = $this->Snapshot->getOneByAppAndLabel($this->data['app'], $this->data['label']); |
215
|
|
|
} else { |
216
|
2 |
|
throw new \InvalidArgumentException('Can\'t get snapshot'); |
217
|
|
|
} |
218
|
3 |
|
} catch (\InvalidArgumentException $Ex) { |
219
|
3 |
|
$Snapshot = new Snapshot( |
220
|
|
|
[ |
221
|
3 |
|
'app' => $this->data['app'], |
222
|
3 |
|
'label' => $this->data['label'], |
223
|
3 |
|
'id' => 0, |
224
|
|
|
], |
225
|
3 |
|
[] |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
4 |
|
return $Snapshot; |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
protected function sortList(array &$records) |
233
|
|
|
{ |
234
|
1 |
|
$sort_field = (string)current($this->FieldList->getFields()); |
235
|
1 |
|
usort($records, function ($Element1, $Element2) use ($sort_field) : int { |
236
|
|
|
/** @var \Badoo\LiveProfilerUI\Entity\MethodData $Element1 */ |
237
|
|
|
/** @var \Badoo\LiveProfilerUI\Entity\MethodData $Element2 */ |
238
|
|
|
return $Element2->getValue($sort_field) > $Element1->getValue($sort_field) ? 1 : -1; |
239
|
1 |
|
}); |
240
|
1 |
|
} |
241
|
|
|
|
242
|
4 |
|
protected function getMainMethodId() : int |
243
|
|
|
{ |
244
|
4 |
|
$methods = $this->Method->findByName('main()', true); |
245
|
4 |
|
if (!empty($methods)) { |
246
|
4 |
|
return array_keys($methods)[0]; |
247
|
|
|
} |
248
|
|
|
return 0; |
249
|
|
|
} |
250
|
|
|
|
251
|
4 |
|
protected function getGraphsData() : array |
252
|
|
|
{ |
253
|
4 |
|
$fields = $this->FieldList->getAllFieldsWithVariations(); |
254
|
4 |
|
$fields['calls_count'] = 'calls_count'; |
255
|
|
|
|
256
|
4 |
|
$data = []; |
257
|
4 |
|
foreach ($fields as $field) { |
258
|
4 |
|
if ($field === 'calls_count') { |
259
|
4 |
|
$data[$field] = [ |
260
|
|
|
'type' => 'times', |
261
|
|
|
'label' => 'profiles count', |
262
|
|
|
'graph_label' => 'calls count' |
263
|
|
|
]; |
264
|
4 |
|
continue; |
265
|
|
|
} |
266
|
|
|
|
267
|
4 |
|
if (strpos($field, 'mem') !== false) { |
268
|
|
|
$type = 'memory'; |
269
|
4 |
|
} elseif (strpos($field, $this->calls_count_field) !== false) { |
270
|
4 |
|
$type = 'times'; |
271
|
|
|
} else { |
272
|
4 |
|
$type = 'time'; |
273
|
|
|
} |
274
|
4 |
|
$data[$field] = [ |
275
|
4 |
|
'type' => $type, |
276
|
4 |
|
'label' => $field, |
277
|
4 |
|
'graph_label' => $field . ' self + children calls graph' |
278
|
|
|
]; |
279
|
|
|
} |
280
|
|
|
|
281
|
4 |
|
return $data; |
282
|
|
|
} |
283
|
|
|
|
284
|
4 |
|
protected function getIntervalsFormData(string $link_base) : array |
285
|
|
|
{ |
286
|
4 |
|
$data = []; |
287
|
4 |
|
foreach (self::$graph_intervals as $name => $value) { |
288
|
4 |
|
$data[] = [ |
289
|
4 |
|
'name' => $name, |
290
|
4 |
|
'link' => $link_base . "&method_id={$this->data['method_id']}&stat_interval=$value", |
291
|
4 |
|
'selected' => $value === $this->data['stat_interval'], |
292
|
|
|
]; |
293
|
|
|
} |
294
|
4 |
|
return $data; |
295
|
|
|
} |
296
|
|
|
|
297
|
6 |
|
protected function getMethodDataWithHistory(array $dates_to_snapshots, int $method_id) : array |
298
|
|
|
{ |
299
|
6 |
|
$snapshot_ids = array_column(array_filter(array_values($dates_to_snapshots)), 'id'); |
300
|
6 |
|
if (empty($snapshot_ids)) { |
301
|
4 |
|
return []; |
302
|
|
|
} |
303
|
|
|
|
304
|
2 |
|
$method_data = $this->MethodData->getDataByMethodIdsAndSnapshotIds($snapshot_ids, [$method_id]); |
305
|
2 |
|
$method_data = $this->Method->injectMethodNames($method_data); |
306
|
|
|
|
307
|
2 |
|
return $this->getProfilerRecordsWithHistory($method_data, $dates_to_snapshots); |
308
|
|
|
} |
309
|
|
|
|
310
|
6 |
|
protected function getMethodParentsWithHistory(array $dates_to_snapshots, int $method_id) : array |
311
|
|
|
{ |
312
|
6 |
|
$snapshot_ids = array_column(array_filter(array_values($dates_to_snapshots)), 'id'); |
313
|
6 |
|
if (empty($snapshot_ids)) { |
314
|
4 |
|
return []; |
315
|
|
|
} |
316
|
|
|
|
317
|
2 |
|
$MethodTree = $this->MethodTree->getDataByMethodIdsAndSnapshotIds($snapshot_ids, [$method_id]); |
318
|
|
|
|
319
|
2 |
|
foreach ($MethodTree as &$Item) { |
320
|
1 |
|
$Item->setMethodId($Item->getParentId()); |
321
|
|
|
} |
322
|
2 |
|
unset($Item); |
323
|
|
|
|
324
|
2 |
|
$MethodTree = $this->Method->injectMethodNames($MethodTree); |
325
|
|
|
|
326
|
2 |
|
return $this->getProfilerRecordsWithHistory($MethodTree, $dates_to_snapshots); |
327
|
|
|
} |
328
|
|
|
|
329
|
6 |
|
protected function getMethodChildrenWithHistory(array $dates_to_snapshots, int $method_id) : array |
330
|
|
|
{ |
331
|
6 |
|
$snapshot_ids = array_column(array_filter(array_values($dates_to_snapshots)), 'id'); |
332
|
6 |
|
if (empty($snapshot_ids)) { |
333
|
4 |
|
return []; |
334
|
|
|
} |
335
|
|
|
|
336
|
2 |
|
$MethodTree = $this->MethodTree->getDataByParentIdsAndSnapshotIds($snapshot_ids, [$method_id]); |
337
|
2 |
|
$MethodTree = $this->Method->injectMethodNames($MethodTree); |
338
|
|
|
|
339
|
2 |
|
return $this->getProfilerRecordsWithHistory($MethodTree, $dates_to_snapshots); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param \Badoo\LiveProfilerUI\Entity\MethodData[] $result |
344
|
|
|
* @param array $dates_to_snapshots |
345
|
|
|
* @return array |
346
|
|
|
*/ |
347
|
1 |
|
protected function getProfilerRecordsWithHistory(array $result, array $dates_to_snapshots) : array |
348
|
|
|
{ |
349
|
1 |
|
$all_fields = $this->FieldList->getAllFieldsWithVariations(); |
350
|
|
|
|
351
|
1 |
|
$snapshot_ids = array_column(array_filter($dates_to_snapshots), 'id'); |
352
|
1 |
|
$last_snapshot_id = end($snapshot_ids); |
353
|
|
|
|
354
|
1 |
|
$history = []; |
355
|
1 |
|
foreach ($result as $Row) { |
356
|
1 |
|
$history[$Row->getMethodId()][$Row->getSnapshotId()] = $Row; |
357
|
|
|
} |
358
|
|
|
|
359
|
1 |
|
$result = []; |
360
|
1 |
|
foreach ($history as $method_rows) { |
361
|
|
|
// the method was not called in the last snapshot, so it will not displayed |
362
|
1 |
|
if (!isset($method_rows[$last_snapshot_id])) { |
363
|
|
|
continue; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** @var \Badoo\LiveProfilerUI\Entity\MethodData $Row */ |
367
|
1 |
|
$Row = $method_rows[$last_snapshot_id]; |
368
|
|
|
|
369
|
1 |
|
$data = []; |
370
|
1 |
|
foreach ($all_fields as $field) { |
371
|
1 |
|
$data[$field] = []; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
// extract data from previous snapshots |
375
|
1 |
|
foreach ($dates_to_snapshots as $snapshot) { |
376
|
1 |
|
$values = []; |
377
|
1 |
|
$snapshot_id = $snapshot['id']; |
378
|
1 |
|
if ($snapshot_id && isset($method_rows[$snapshot_id])) { |
379
|
|
|
/** @var \Badoo\LiveProfilerUI\Entity\MethodData $PreviousRow */ |
380
|
1 |
|
$PreviousRow = $method_rows[$snapshot_id]; |
381
|
1 |
|
$values = $PreviousRow->getValues(); |
382
|
|
|
} |
383
|
|
|
|
384
|
1 |
|
foreach ($all_fields as $field) { |
385
|
1 |
|
$data[$field][] = ['val' => $values[$field] ?? 0]; |
386
|
|
|
} |
387
|
|
|
|
388
|
1 |
|
if ($data) { |
|
|
|
|
389
|
1 |
|
$data['calls_count'][] = ['val' => $snapshot['calls_count']]; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
1 |
|
$Row->setHistoryData($data); |
394
|
|
|
|
395
|
1 |
|
$result[] = $Row; |
396
|
|
|
} |
397
|
|
|
|
398
|
1 |
|
return $result; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.