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
|
|
|
$methods = $this->Method->findByName($this->data['method_name'], true); |
85
|
|
|
if (!empty($methods)) { |
86
|
|
|
$this->data['method_id'] = array_keys($methods)[0]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
1 |
|
} |
90
|
|
|
|
91
|
1 |
|
public function initDates() |
92
|
|
|
{ |
93
|
1 |
|
$this->data['date1'] = isset($this->data['date1']) ? trim($this->data['date1']) : ''; |
94
|
1 |
|
$this->data['date2'] = isset($this->data['date2']) ? trim($this->data['date2']) : ''; |
95
|
|
|
|
96
|
1 |
|
if ($this->data['date1'] && $this->data['date2']) { |
97
|
|
|
$this->data['stat_interval'] = 0; |
98
|
|
|
} else { |
99
|
1 |
|
$this->data['stat_interval'] = isset($this->data['stat_interval']) ? (int)$this->data['stat_interval'] : 0; |
100
|
1 |
|
if (!\in_array($this->data['stat_interval'], self::$graph_intervals, true)) { |
101
|
1 |
|
$this->data['stat_interval'] = self::STAT_INTERVAL_MONTH; |
102
|
|
|
} |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
* @throws \InvalidArgumentException |
109
|
|
|
*/ |
110
|
4 |
|
public function getTemplateData() : array |
111
|
|
|
{ |
112
|
4 |
|
$link_base = '/profiler/tree-view.phtml?'; |
113
|
|
|
|
114
|
4 |
|
$Snapshot = $this->getSnapshot(); |
115
|
4 |
|
$link_base .= 'app=' . urlencode($this->data['app']) . '&label=' . urlencode($this->data['label']); |
116
|
|
|
|
117
|
4 |
|
if (!$this->data['method_id']) { |
118
|
4 |
|
$this->data['method_id'] = $this->getMainMethodId(); |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
$methods = $this->Method->getListByIds([$this->data['method_id']]); |
122
|
4 |
|
$method_name = '?'; |
123
|
4 |
|
if (!empty($methods)) { |
124
|
4 |
|
$method_name = $methods[$this->data['method_id']]; |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
if ($this->data['date1'] && $this->data['date2']) { |
128
|
|
|
$dates = \Badoo\LiveProfilerUI\DateGenerator::getDatesByRange( |
129
|
|
|
$this->data['date1'], |
130
|
|
|
$this->data['date2'] |
131
|
|
|
); |
132
|
|
|
} else { |
133
|
4 |
|
$dates = \Badoo\LiveProfilerUI\DateGenerator::getDatesArray( |
134
|
4 |
|
$Snapshot->getDate(), |
135
|
4 |
|
$this->data['stat_interval'], |
136
|
4 |
|
$this->data['stat_interval'] |
137
|
|
|
); |
138
|
4 |
|
$this->data['date1'] = current($dates); |
139
|
4 |
|
$this->data['date2'] = end($dates); |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
$date_to_snapshot_map = $this->Snapshot->getSnapshotIdsByDates( |
143
|
4 |
|
$dates, |
144
|
4 |
|
$Snapshot->getApp(), |
145
|
4 |
|
$Snapshot->getLabel() |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$view_data = [ |
149
|
4 |
|
'snapshot_id' => $Snapshot->getId(), |
150
|
4 |
|
'snapshot_app' => $Snapshot->getApp(), |
151
|
4 |
|
'snapshot_label' => $Snapshot->getLabel(), |
152
|
4 |
|
'snapshot_date' => $Snapshot->getDate(), |
153
|
4 |
|
'method_id' => $this->data['method_id'], |
154
|
4 |
|
'method_name' => $method_name, |
155
|
4 |
|
'method_dates' => $dates, |
156
|
4 |
|
'stat_intervals' => $this->getIntervalsFormData($link_base), |
157
|
4 |
|
'date1' => $this->data['date1'], |
158
|
4 |
|
'date2' => $this->data['date2'], |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$common_block_data = [ |
162
|
4 |
|
'link_base' => $link_base, |
163
|
4 |
|
'fields' => $this->FieldList->getAllFieldsWithVariations(), |
164
|
4 |
|
'field_descriptions' => $this->FieldList->getFieldDescriptions(), |
165
|
4 |
|
'stat_interval' => $this->data['stat_interval'], |
166
|
4 |
|
'date1' => $this->data['date1'], |
167
|
4 |
|
'date2' => $this->data['date2'], |
168
|
|
|
]; |
169
|
|
|
|
170
|
4 |
|
$method_data = $this->getMethodDataWithHistory($date_to_snapshot_map, $this->data['method_id']); |
171
|
4 |
|
if (!empty($method_data)) { |
172
|
|
|
/** @var \Badoo\LiveProfilerUI\Entity\MethodData $MainMethod */ |
173
|
1 |
|
$MainMethod = current($method_data); |
174
|
1 |
|
$view_data['available_graphs'] = $this->getGraphsData($MainMethod); |
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
|
1 |
|
protected function getGraphsData(\Badoo\LiveProfilerUI\Entity\MethodData $MainMethod) : array |
252
|
|
|
{ |
253
|
1 |
|
$data = []; |
254
|
1 |
|
foreach (array_keys($MainMethod->getHistoryData()) as $field) { |
255
|
1 |
|
if (strpos($field, 'mem') !== false) { |
256
|
1 |
|
$type = 'memory'; |
257
|
1 |
|
} elseif (strpos($field, $this->calls_count_field) !== false) { |
258
|
1 |
|
$type = 'times'; |
259
|
|
|
} else { |
260
|
1 |
|
$type = 'time'; |
261
|
|
|
} |
262
|
1 |
|
$data[$field] = [ |
263
|
1 |
|
'type' => $type, |
264
|
1 |
|
'label' => $field, |
265
|
1 |
|
'graph_label' => $field . ' self + children calls graph' |
266
|
|
|
]; |
267
|
|
|
} |
268
|
|
|
|
269
|
1 |
|
return $data; |
270
|
|
|
} |
271
|
|
|
|
272
|
4 |
|
protected function getIntervalsFormData(string $link_base) : array |
273
|
|
|
{ |
274
|
4 |
|
$data = []; |
275
|
4 |
|
foreach (self::$graph_intervals as $name => $value) { |
276
|
4 |
|
$data[] = [ |
277
|
4 |
|
'name' => $name, |
278
|
4 |
|
'link' => $link_base . "&method_id={$this->data['method_id']}&stat_interval=$value", |
279
|
4 |
|
'selected' => $value === $this->data['stat_interval'], |
280
|
|
|
]; |
281
|
|
|
} |
282
|
4 |
|
return $data; |
283
|
|
|
} |
284
|
|
|
|
285
|
6 |
|
protected function getMethodDataWithHistory(array $dates_to_snapshots, int $method_id) : array |
286
|
|
|
{ |
287
|
6 |
|
$snapshot_ids = array_filter(array_values($dates_to_snapshots)); |
288
|
6 |
|
if (empty($snapshot_ids)) { |
289
|
4 |
|
return []; |
290
|
|
|
} |
291
|
|
|
|
292
|
2 |
|
$MethodData = $this->MethodData->getDataByMethodIdsAndSnapshotIds($snapshot_ids, [$method_id]); |
293
|
2 |
|
$MethodData = $this->Method->injectMethodNames($MethodData); |
294
|
|
|
|
295
|
2 |
|
return $this->getProfilerRecordsWithHistory($MethodData, $dates_to_snapshots); |
296
|
|
|
} |
297
|
|
|
|
298
|
6 |
|
protected function getMethodParentsWithHistory(array $dates_to_snapshots, int $method_id) : array |
299
|
|
|
{ |
300
|
6 |
|
$snapshot_ids = array_filter(array_values($dates_to_snapshots)); |
301
|
6 |
|
if (empty($snapshot_ids)) { |
302
|
4 |
|
return []; |
303
|
|
|
} |
304
|
|
|
|
305
|
2 |
|
$MethodTree = $this->MethodTree->getDataByMethodIdsAndSnapshotIds($snapshot_ids, [$method_id]); |
306
|
|
|
|
307
|
2 |
|
foreach ($MethodTree as &$Item) { |
308
|
1 |
|
$Item->setMethodId($Item->getParentId()); |
309
|
|
|
} |
310
|
2 |
|
unset($Item); |
311
|
|
|
|
312
|
2 |
|
$MethodTree = $this->Method->injectMethodNames($MethodTree); |
313
|
|
|
|
314
|
2 |
|
return $this->getProfilerRecordsWithHistory($MethodTree, $dates_to_snapshots); |
315
|
|
|
} |
316
|
|
|
|
317
|
6 |
|
protected function getMethodChildrenWithHistory(array $dates_to_snapshots, int $method_id) : array |
318
|
|
|
{ |
319
|
6 |
|
$snapshot_ids = array_filter(array_values($dates_to_snapshots)); |
320
|
6 |
|
if (empty($snapshot_ids)) { |
321
|
4 |
|
return []; |
322
|
|
|
} |
323
|
|
|
|
324
|
2 |
|
$MethodTree = $this->MethodTree->getDataByParentIdsAndSnapshotIds($snapshot_ids, [$method_id]); |
325
|
2 |
|
$MethodTree = $this->Method->injectMethodNames($MethodTree); |
326
|
|
|
|
327
|
2 |
|
return $this->getProfilerRecordsWithHistory($MethodTree, $dates_to_snapshots); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param \Badoo\LiveProfilerUI\Entity\MethodData[] $result |
332
|
|
|
* @param array $dates_to_snapshots |
333
|
|
|
* @return array |
334
|
|
|
*/ |
335
|
1 |
|
protected function getProfilerRecordsWithHistory(array $result, array $dates_to_snapshots) : array |
336
|
|
|
{ |
337
|
1 |
|
$last_snapshot_id = end($dates_to_snapshots); |
338
|
|
|
|
339
|
1 |
|
$history = []; |
340
|
1 |
|
foreach ($result as $Row) { |
341
|
1 |
|
$history[$Row->getMethodId()][$Row->getSnapshotId()] = $Row; |
342
|
|
|
} |
343
|
|
|
|
344
|
1 |
|
$all_fields = $this->FieldList->getAllFieldsWithVariations(); |
345
|
|
|
|
346
|
1 |
|
$result = []; |
347
|
1 |
|
foreach ($history as $method_rows) { |
348
|
|
|
// the method was not called in the last snapshot, so it will not displayed |
349
|
1 |
|
if (!isset($method_rows[$last_snapshot_id])) { |
350
|
|
|
continue; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** @var \Badoo\LiveProfilerUI\Entity\MethodData $Row */ |
354
|
1 |
|
$Row = $method_rows[$last_snapshot_id]; |
355
|
|
|
|
356
|
1 |
|
$data = []; |
357
|
1 |
|
foreach ($all_fields as $field) { |
358
|
1 |
|
$data[$field] = []; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
// extract data from previous snapshots |
362
|
1 |
|
foreach ($dates_to_snapshots as $snapshot_id) { |
363
|
1 |
|
$values = []; |
364
|
1 |
|
if ($snapshot_id && isset($method_rows[$snapshot_id])) { |
365
|
|
|
/** @var \Badoo\LiveProfilerUI\Entity\MethodData $PreviousRow */ |
366
|
1 |
|
$PreviousRow = $method_rows[$snapshot_id]; |
367
|
1 |
|
$values = $PreviousRow->getValues(); |
368
|
|
|
} |
369
|
|
|
|
370
|
1 |
|
foreach ($all_fields as $field) { |
371
|
1 |
|
$data[$field][] = ['val' => $values[$field] ?? 0]; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
1 |
|
$Row->setHistoryData($data); |
376
|
|
|
|
377
|
1 |
|
$result[] = $Row; |
378
|
|
|
} |
379
|
|
|
|
380
|
1 |
|
return $result; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|