|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @maintainer Timur Shagiakhmetov <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Badoo\LiveProfilerUI\Pages; |
|
8
|
|
|
|
|
9
|
|
|
use Badoo\LiveProfilerUI\Aggregator; |
|
10
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SourceInterface; |
|
11
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\JobInterface; |
|
12
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodInterface; |
|
13
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodDataInterface; |
|
14
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SnapshotInterface; |
|
15
|
|
|
use Badoo\LiveProfilerUI\FieldList; |
|
16
|
|
|
|
|
17
|
|
|
class AjaxPages |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var SnapshotInterface */ |
|
20
|
|
|
protected $Snapshot; |
|
21
|
|
|
/** @var MethodInterface */ |
|
22
|
|
|
protected $Method; |
|
23
|
|
|
/** @var MethodDataInterface */ |
|
24
|
|
|
protected $MethodData; |
|
25
|
|
|
/** @var JobInterface */ |
|
26
|
|
|
protected $Job; |
|
27
|
|
|
/** @var Aggregator */ |
|
28
|
|
|
protected $Aggregator; |
|
29
|
|
|
/** @var SourceInterface */ |
|
30
|
|
|
protected $Source; |
|
31
|
|
|
/** @var FieldList */ |
|
32
|
|
|
protected $FieldList; |
|
33
|
|
|
/** @var bool */ |
|
34
|
|
|
protected $use_jobs; |
|
35
|
|
|
|
|
36
|
1 |
|
public function __construct( |
|
37
|
|
|
SnapshotInterface $Snapshot, |
|
38
|
|
|
MethodInterface $Method, |
|
39
|
|
|
MethodDataInterface $MethodData, |
|
40
|
|
|
JobInterface $Job, |
|
41
|
|
|
Aggregator $Aggregator, |
|
42
|
|
|
SourceInterface $Source, |
|
43
|
|
|
FieldList $FieldList, |
|
44
|
|
|
bool $use_jobs = false |
|
45
|
|
|
) { |
|
46
|
1 |
|
$this->Snapshot = $Snapshot; |
|
47
|
1 |
|
$this->Method = $Method; |
|
48
|
1 |
|
$this->MethodData = $MethodData; |
|
49
|
1 |
|
$this->Job = $Job; |
|
50
|
1 |
|
$this->Aggregator = $Aggregator; |
|
51
|
1 |
|
$this->Source = $Source; |
|
52
|
1 |
|
$this->FieldList = $FieldList; |
|
53
|
1 |
|
$this->use_jobs = $use_jobs; |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
6 |
|
public function rebuildSnapshot(string $app, string $label, string $date) : array |
|
57
|
|
|
{ |
|
58
|
6 |
|
$status = false; |
|
59
|
6 |
|
if ($this->use_jobs) { |
|
60
|
|
|
try { |
|
61
|
3 |
|
$this->Job->getJob( |
|
62
|
3 |
|
$app, |
|
63
|
|
|
$label, |
|
64
|
|
|
$date, |
|
65
|
3 |
|
[JobInterface::STATUS_NEW, JobInterface::STATUS_PROCESSING] |
|
66
|
|
|
); |
|
67
|
1 |
|
$message = "Job for snapshot ($app, $label, $date) is already exists"; |
|
68
|
2 |
|
} catch (\InvalidArgumentException $Ex) { |
|
69
|
2 |
|
if ($this->Job->add($app, $label, $date, 'manual')) { |
|
70
|
1 |
|
$message = "Added a job for aggregating a snapshot ($app, $label, $date)"; |
|
71
|
1 |
|
$status = true; |
|
72
|
|
|
} else { |
|
73
|
3 |
|
$message = "Error in the snapshot ($app, $label, $date) aggregating"; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} else { |
|
77
|
|
|
try { |
|
78
|
3 |
|
$result = $this->Aggregator->setApp($app) |
|
79
|
3 |
|
->setLabel($label) |
|
80
|
3 |
|
->setDate($date) |
|
81
|
3 |
|
->setIsManual(true) |
|
82
|
3 |
|
->process(); |
|
83
|
2 |
|
if (!empty($result)) { |
|
84
|
1 |
|
$status = true; |
|
85
|
1 |
|
$message = "Job for the snapshot ($app, $label, $date) is finished"; |
|
86
|
|
|
} else { |
|
87
|
1 |
|
$last_error = $this->Aggregator->getLastError(); |
|
88
|
2 |
|
$message = "Error in the snapshot ($app, $label, $date) aggregating: " . $last_error; |
|
89
|
|
|
} |
|
90
|
1 |
|
} catch (\Throwable $Ex) { |
|
91
|
1 |
|
$message = "Error in the snapshot ($app, $label, $date) aggregating: " . $Ex->getMessage(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return [ |
|
96
|
6 |
|
'status' => $status, |
|
97
|
6 |
|
'message' => $message, |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
7 |
|
public function checkSnapshot(string $app, string $label, string $date) : array |
|
102
|
|
|
{ |
|
103
|
7 |
|
if (!$this->use_jobs) { |
|
104
|
|
|
try { |
|
105
|
2 |
|
$this->Snapshot->getOneByAppAndLabelAndDate($app, $label, $date); |
|
106
|
1 |
|
$is_processing = false; |
|
107
|
1 |
|
$message = "Job for the snapshot ($app, $label, $date) is finished"; |
|
108
|
1 |
|
} catch (\InvalidArgumentException $Ex) { |
|
109
|
1 |
|
$is_processing = true; |
|
110
|
1 |
|
$message = "Job for the snapshot ($app, $label, $date) is processing now"; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return [ |
|
114
|
2 |
|
'is_new' => false, |
|
115
|
2 |
|
'is_processing' => $is_processing, |
|
116
|
|
|
'is_error' => false, |
|
117
|
2 |
|
'is_finished' => !$is_processing, |
|
118
|
2 |
|
'message' => $message |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
5 |
|
$is_new = $is_processing = $is_error = $is_finished = false; |
|
123
|
|
|
|
|
124
|
|
|
try { |
|
125
|
5 |
|
$ExistsJob = $this->Job->getJob( |
|
126
|
5 |
|
$app, |
|
127
|
|
|
$label, |
|
128
|
|
|
$date, |
|
129
|
|
|
[ |
|
130
|
5 |
|
JobInterface::STATUS_NEW, |
|
131
|
|
|
JobInterface::STATUS_PROCESSING, |
|
132
|
|
|
JobInterface::STATUS_FINISHED, |
|
133
|
|
|
JobInterface::STATUS_ERROR |
|
134
|
|
|
] |
|
135
|
|
|
); |
|
136
|
4 |
|
if ($ExistsJob->getStatus() === JobInterface::STATUS_NEW) { |
|
137
|
1 |
|
$is_new = true; |
|
138
|
1 |
|
$message = "Added a job for aggregating snapshot ($app, $label, $date)"; |
|
139
|
3 |
|
} elseif ($ExistsJob->getStatus() === JobInterface::STATUS_PROCESSING) { |
|
140
|
1 |
|
$is_processing = true; |
|
141
|
1 |
|
$message = "Job for the snapshot ($app, $label, $date) is processing now"; |
|
142
|
2 |
|
} elseif ($ExistsJob->getStatus() === JobInterface::STATUS_ERROR) { |
|
143
|
1 |
|
$is_error = true; |
|
144
|
1 |
|
$message = "Job for the snapshot ($app, $label, $date) is finished with error"; |
|
145
|
|
|
} else { |
|
146
|
1 |
|
$is_finished = true; |
|
147
|
4 |
|
$message = "Job for the snapshot ($app, $label, $date) is finished"; |
|
148
|
|
|
} |
|
149
|
1 |
|
} catch (\InvalidArgumentException $Ex) { |
|
150
|
1 |
|
$is_finished = true; |
|
151
|
1 |
|
$message = "Job for the snapshot ($app, $label, $date) is finished"; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return [ |
|
155
|
5 |
|
'is_new' => $is_new, |
|
156
|
5 |
|
'is_processing' => $is_processing, |
|
157
|
5 |
|
'is_error' => $is_error, |
|
158
|
5 |
|
'is_finished' => $is_finished, |
|
159
|
5 |
|
'message' => $message |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
2 |
|
public function searchMethods(string $term) : array |
|
164
|
|
|
{ |
|
165
|
2 |
|
$term = ltrim($term, '\\'); |
|
166
|
|
|
try { |
|
167
|
2 |
|
return $this->Method->findByName($term); |
|
168
|
1 |
|
} catch (\Throwable $Ex) { |
|
169
|
1 |
|
return []; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getMethodUsedApps(string $method_name) : array |
|
174
|
|
|
{ |
|
175
|
|
|
$method_name = ltrim($method_name, '\\'); |
|
176
|
|
|
try { |
|
177
|
|
|
$methods = $this->Method->findByName($method_name, true); |
|
178
|
|
|
if (empty($methods)) { |
|
179
|
|
|
return []; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$method = current($methods); |
|
183
|
|
|
|
|
184
|
|
|
$last_two_days = \Badoo\LiveProfilerUI\DateGenerator::getDatesArray(date('Y-m-d'), 2, 2); |
|
185
|
|
|
$start_snapshot_id = in_array(current($methods)['date'], $last_two_days, true) |
|
186
|
|
|
? $this->Snapshot->getMinSnapshotIdByDates($last_two_days) |
|
187
|
|
|
: 0; |
|
188
|
|
|
|
|
189
|
|
|
$method_data = $this->MethodData->getDataByMethodIdsAndSnapshotIds( |
|
190
|
|
|
[], |
|
191
|
|
|
[$method['id']], |
|
192
|
|
|
100, |
|
193
|
|
|
$start_snapshot_id |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
|
|
$snapshot_ids = []; |
|
197
|
|
|
foreach ($method_data as $Row) { |
|
198
|
|
|
$snapshot_id = $Row->getSnapshotId(); |
|
199
|
|
|
$snapshot_ids[$snapshot_id] = $snapshot_id; |
|
200
|
|
|
} |
|
201
|
|
|
$snapshots = $this->Snapshot->getListByIds($snapshot_ids); |
|
202
|
|
|
|
|
203
|
|
|
$fields = $this->FieldList->getFields(); |
|
204
|
|
|
|
|
205
|
|
|
$results = []; |
|
206
|
|
|
foreach ($method_data as $Row) { |
|
207
|
|
|
$result = []; |
|
208
|
|
|
$result['app'] = $snapshots[$Row->getSnapshotId()]['app']; |
|
209
|
|
|
$result['label'] = $snapshots[$Row->getSnapshotId()]['label']; |
|
210
|
|
|
$result['date'] = $snapshots[$Row->getSnapshotId()]['date']; |
|
211
|
|
|
|
|
212
|
|
|
$uniq_key = $result['app'] . '_' . $result['label']; |
|
213
|
|
|
if (!empty($results[$uniq_key])) { |
|
214
|
|
|
continue; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$values = $Row->getValues(); |
|
218
|
|
|
foreach ($fields as $field) { |
|
219
|
|
|
$result['fields'][$field] = $values[$field]; |
|
220
|
|
|
} |
|
221
|
|
|
$result['fields']['calls_count'] = $snapshots[$Row->getSnapshotId()]['calls_count']; |
|
222
|
|
|
|
|
223
|
|
|
$results[$uniq_key] = $result; |
|
224
|
|
|
} |
|
225
|
|
|
return array_values($results); |
|
226
|
|
|
} catch (\Throwable $Ex) { |
|
227
|
|
|
return []; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function allMethods() : array |
|
232
|
|
|
{ |
|
233
|
|
|
try { |
|
234
|
|
|
$methods = $this->Method->all(); |
|
235
|
|
|
|
|
236
|
|
|
$result = []; |
|
237
|
|
|
foreach ($methods as $method) { |
|
238
|
|
|
$result[$method['name']] = $method['date']; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return $result; |
|
242
|
|
|
} catch (\Throwable $Ex) { |
|
243
|
|
|
return []; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
2 |
|
public function getSourceAppList() : array |
|
248
|
|
|
{ |
|
249
|
|
|
try { |
|
250
|
2 |
|
return $this->Source->getAppList(); |
|
251
|
1 |
|
} catch (\Exception $Ex) { |
|
252
|
1 |
|
return []; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
2 |
|
public function getSourceLabelList() : array |
|
257
|
|
|
{ |
|
258
|
|
|
try { |
|
259
|
2 |
|
return $this->Source->getLabelList(); |
|
260
|
1 |
|
} catch (\Exception $Ex) { |
|
261
|
1 |
|
return []; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|