|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @maintainer Timur Shagiakhmetov <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Badoo\LiveProfilerUI\DataProviders; |
|
8
|
|
|
|
|
9
|
|
|
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SnapshotInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Snapshot extends Base implements SnapshotInterface |
|
12
|
|
|
{ |
|
13
|
|
|
const TABLE_NAME = 'aggregator_snapshots'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $app |
|
17
|
|
|
* @return \Badoo\LiveProfilerUI\Entity\Snapshot[] |
|
18
|
|
|
*/ |
|
19
|
3 |
|
public function getList(string $app = '') : array |
|
20
|
|
|
{ |
|
21
|
3 |
|
$last_snapshots = $this->getLastSnapshots($app); |
|
22
|
3 |
|
if (empty($last_snapshots)) { |
|
23
|
1 |
|
return []; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
$last_snapshots_data = []; |
|
27
|
2 |
|
foreach ($last_snapshots as $last_snapshot) { |
|
28
|
2 |
|
$snapshot_filter = []; |
|
29
|
2 |
|
unset($last_snapshot['id']); |
|
30
|
2 |
|
foreach ($last_snapshot as $key => $value) { |
|
31
|
2 |
|
$snapshot_filter[] = [$key, $value]; |
|
32
|
|
|
} |
|
33
|
2 |
|
$last_snapshots_data[] = $snapshot_filter; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$filter = [ |
|
37
|
2 |
|
['union', $last_snapshots_data], |
|
38
|
|
|
['label', '', '!='], |
|
39
|
|
|
]; |
|
40
|
2 |
|
if ($app) { |
|
41
|
2 |
|
$filter[] = ['app', $app]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
$snapshots = $this->AggregatorStorage->getAll( |
|
45
|
2 |
|
self::TABLE_NAME, |
|
46
|
2 |
|
['all'], |
|
47
|
|
|
[ |
|
48
|
2 |
|
'filter' => $filter, |
|
49
|
|
|
'order' => [ |
|
50
|
|
|
'date' => 'desc', |
|
51
|
|
|
'label' => 'asc', |
|
52
|
|
|
'app' => 'asc' |
|
53
|
|
|
] |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
2 |
|
$return = []; |
|
58
|
2 |
|
if (!empty($snapshots)) { |
|
59
|
1 |
|
foreach ($snapshots as $snapshot) { |
|
60
|
1 |
|
$return[] = new \Badoo\LiveProfilerUI\Entity\Snapshot( |
|
61
|
1 |
|
$snapshot, |
|
62
|
1 |
|
$this->FieldList->getAllFieldsWithVariations() |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function getLastSnapshots(string $app = '', string $from_date = '') : array |
|
71
|
|
|
{ |
|
72
|
|
|
$filter = [ |
|
73
|
1 |
|
['label', '', '!='], |
|
74
|
|
|
]; |
|
75
|
1 |
|
if ($app) { |
|
76
|
1 |
|
$filter[] = ['app', $app]; |
|
77
|
|
|
} |
|
78
|
1 |
|
if ($from_date) { |
|
79
|
1 |
|
$filter[] = ['date', $from_date, '>=']; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$last_snapshots = $this->AggregatorStorage->getAll( |
|
83
|
1 |
|
self::TABLE_NAME, |
|
84
|
1 |
|
[['field' => 'id', 'function' => 'max'], 'app', 'label', ['field' => 'date', 'function' => 'max']], |
|
85
|
|
|
[ |
|
86
|
1 |
|
'filter' => $filter, |
|
87
|
|
|
'group' => [ |
|
88
|
|
|
'app', |
|
89
|
|
|
'label' |
|
90
|
|
|
] |
|
91
|
|
|
] |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
1 |
|
return $last_snapshots; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
5 |
|
public function getOneById(int $snapshot_id) : \Badoo\LiveProfilerUI\Entity\Snapshot |
|
98
|
|
|
{ |
|
99
|
5 |
|
$snapshot = $this->AggregatorStorage->getOne( |
|
100
|
5 |
|
self::TABLE_NAME, |
|
101
|
5 |
|
['all'], |
|
102
|
|
|
[ |
|
103
|
5 |
|
'filter' => [['id', $snapshot_id]] |
|
104
|
|
|
] |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
5 |
|
if (empty($snapshot)) { |
|
108
|
4 |
|
throw new \InvalidArgumentException('Can\'t get snapshot'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
return new \Badoo\LiveProfilerUI\Entity\Snapshot( |
|
112
|
1 |
|
$snapshot, |
|
113
|
1 |
|
$this->FieldList->getAllFieldsWithVariations() |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
public function getListByIds(array $snapshot_ids) : array |
|
118
|
|
|
{ |
|
119
|
2 |
|
if (empty($snapshot_ids)) { |
|
120
|
1 |
|
return []; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
$snapshots = $this->AggregatorStorage->getAll( |
|
124
|
1 |
|
self::TABLE_NAME, |
|
125
|
1 |
|
['all'], |
|
126
|
|
|
[ |
|
127
|
1 |
|
'filter' => [['id', $snapshot_ids]] |
|
128
|
|
|
] |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
1 |
|
$result = []; |
|
132
|
1 |
|
if (!empty($snapshots)) { |
|
133
|
1 |
|
foreach ($snapshots as $row) { |
|
134
|
1 |
|
$result[$row['id']] = $row; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
return $result; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
5 |
|
public function getOneByAppAndLabel(string $app, string $label) : \Badoo\LiveProfilerUI\Entity\Snapshot |
|
142
|
|
|
{ |
|
143
|
5 |
|
$snapshot = $this->AggregatorStorage->getOne( |
|
144
|
5 |
|
self::TABLE_NAME, |
|
145
|
5 |
|
['all'], |
|
146
|
|
|
[ |
|
147
|
5 |
|
'filter' => [ |
|
148
|
5 |
|
['app', $app], |
|
149
|
5 |
|
['label', $label] |
|
150
|
|
|
], |
|
151
|
|
|
'order' => ['date' => 'desc'] |
|
152
|
|
|
] |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
5 |
|
if (empty($snapshot)) { |
|
156
|
4 |
|
throw new \InvalidArgumentException('Can\'t get snapshot'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
1 |
|
return new \Badoo\LiveProfilerUI\Entity\Snapshot( |
|
160
|
1 |
|
$snapshot, |
|
161
|
1 |
|
$this->FieldList->getAllFieldsWithVariations() |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
5 |
|
public function getOneByAppAndLabelAndDate( |
|
166
|
|
|
string $app, |
|
167
|
|
|
string $label, |
|
168
|
|
|
string $date |
|
169
|
|
|
) : \Badoo\LiveProfilerUI\Entity\Snapshot { |
|
170
|
5 |
|
$snapshot = $this->AggregatorStorage->getOne( |
|
171
|
5 |
|
self::TABLE_NAME, |
|
172
|
5 |
|
['all'], |
|
173
|
|
|
[ |
|
174
|
5 |
|
'filter' => [ |
|
175
|
5 |
|
['app', $app], |
|
176
|
5 |
|
['label', $label], |
|
177
|
5 |
|
['date', $date] |
|
178
|
|
|
], |
|
179
|
|
|
'order' => ['date' => 'desc'] |
|
180
|
|
|
] |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
5 |
|
if (empty($snapshot)) { |
|
184
|
4 |
|
throw new \InvalidArgumentException('Can\'t get snapshot'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
return new \Badoo\LiveProfilerUI\Entity\Snapshot( |
|
188
|
1 |
|
$snapshot, |
|
189
|
1 |
|
$this->FieldList->getAllFieldsWithVariations() |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
2 |
|
public function getSnapshotsByDates(array $dates, string $param = '') : array |
|
194
|
|
|
{ |
|
195
|
2 |
|
if (empty($dates)) { |
|
196
|
1 |
|
return []; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
1 |
|
$fields = ['id', 'app', 'label', 'date', 'type']; |
|
200
|
1 |
|
if ($param) { |
|
201
|
1 |
|
$fields[] = $param; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
1 |
|
$snapshots = $this->AggregatorStorage->getAll( |
|
205
|
1 |
|
self::TABLE_NAME, |
|
206
|
1 |
|
$fields, |
|
207
|
|
|
[ |
|
208
|
|
|
'filter' => [ |
|
209
|
1 |
|
['date', $dates], |
|
210
|
|
|
], |
|
211
|
|
|
] |
|
212
|
|
|
); |
|
213
|
1 |
|
return $snapshots; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
3 |
|
public function getSnapshotIdsByDates(array $dates, string $app, string $label) : array |
|
217
|
|
|
{ |
|
218
|
3 |
|
if (empty($dates) || !$app || !$label) { |
|
219
|
1 |
|
return []; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
2 |
|
$snapshots = $this->AggregatorStorage->getAll( |
|
223
|
2 |
|
self::TABLE_NAME, |
|
224
|
2 |
|
['id', 'calls_count', 'date'], |
|
225
|
|
|
[ |
|
226
|
|
|
'filter' => [ |
|
227
|
2 |
|
['date', $dates], |
|
228
|
2 |
|
['app', $app], |
|
229
|
2 |
|
['label', $label], |
|
230
|
|
|
], |
|
231
|
|
|
] |
|
232
|
|
|
); |
|
233
|
|
|
|
|
234
|
2 |
|
$result = []; |
|
235
|
2 |
|
if (!empty($snapshots)) { |
|
236
|
2 |
|
foreach ($snapshots as $row) { |
|
237
|
2 |
|
$result[$row['date']] = [ |
|
238
|
2 |
|
'id' => (int)$row['id'], |
|
239
|
2 |
|
'calls_count' => (int)$row['calls_count'], |
|
240
|
|
|
]; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
2 |
|
foreach ($dates as $date) { |
|
244
|
2 |
|
if (!isset($result[$date])) { |
|
245
|
2 |
|
$result[$date] = null; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
2 |
|
ksort($result); |
|
249
|
|
|
|
|
250
|
2 |
|
return $result; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
1 |
|
public function getOldSnapshots(int $keep_days = 200, int $limit = 2000) : array |
|
254
|
|
|
{ |
|
255
|
1 |
|
$snapshots = $this->AggregatorStorage->getAll( |
|
256
|
1 |
|
self::TABLE_NAME, |
|
257
|
1 |
|
['id', 'app', 'label', 'date'], |
|
258
|
|
|
[ |
|
259
|
1 |
|
'filter' => [ |
|
260
|
1 |
|
['date', date('Y-m-d', strtotime("-$keep_days day")), '<'], |
|
261
|
|
|
], |
|
262
|
|
|
'order' => ['id' => 'asc'], |
|
263
|
1 |
|
'limit' => $limit |
|
264
|
|
|
] |
|
265
|
|
|
); |
|
266
|
|
|
|
|
267
|
1 |
|
return $snapshots; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
1 |
|
public function getDatesByAppAndLabel(string $app, string $label) : array |
|
271
|
|
|
{ |
|
272
|
1 |
|
$snapshots = $this->AggregatorStorage->getAll( |
|
273
|
1 |
|
self::TABLE_NAME, |
|
274
|
1 |
|
['date'], |
|
275
|
|
|
[ |
|
276
|
1 |
|
'filter' => [ |
|
277
|
1 |
|
['date', date('Y-m-d', strtotime('-1 year')), '>'], |
|
278
|
1 |
|
['app', $app], |
|
279
|
1 |
|
['label', $label], |
|
280
|
|
|
], |
|
281
|
|
|
'order' => ['date' => 'desc'] |
|
282
|
|
|
] |
|
283
|
|
|
); |
|
284
|
|
|
|
|
285
|
1 |
|
return $snapshots ? array_column($snapshots, 'date') : []; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
1 |
|
public function getMaxCallsCntByAppAndLabel(string $app, string $label) : int |
|
289
|
|
|
{ |
|
290
|
1 |
|
$result = $this->AggregatorStorage->getOne( |
|
291
|
1 |
|
self::TABLE_NAME, |
|
292
|
1 |
|
[['field' => 'calls_count', 'function' => 'max']], |
|
293
|
|
|
[ |
|
294
|
1 |
|
'filter' => [ |
|
295
|
1 |
|
['app', $app], |
|
296
|
1 |
|
['label', $label], |
|
297
|
|
|
], |
|
298
|
|
|
'group' => ['app', 'label'], |
|
299
|
|
|
] |
|
300
|
|
|
); |
|
301
|
|
|
|
|
302
|
1 |
|
return $result ? (int)current($result) : 0; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
2 |
|
public function getAppList(string $label = '') : array |
|
306
|
|
|
{ |
|
307
|
2 |
|
$filter = []; |
|
308
|
2 |
|
if ($label) { |
|
309
|
1 |
|
$filter[] = ['label', $label]; |
|
310
|
|
|
} |
|
311
|
2 |
|
$apps = $this->AggregatorStorage->getAll( |
|
312
|
2 |
|
self::TABLE_NAME, |
|
313
|
2 |
|
['app'], |
|
314
|
|
|
[ |
|
315
|
2 |
|
'filter' => $filter, |
|
316
|
|
|
'group' => ['app'], |
|
317
|
|
|
'order' => ['app' => 'asc'], |
|
318
|
|
|
] |
|
319
|
|
|
); |
|
320
|
|
|
|
|
321
|
2 |
|
return $apps ? array_column($apps, 'app') : []; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
2 |
|
public function updateSnapshot(int $snapshot_id, array $snapshot_data) : bool |
|
325
|
|
|
{ |
|
326
|
2 |
|
if (empty($snapshot_data)) { |
|
327
|
1 |
|
return false; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
1 |
|
return $this->AggregatorStorage->update( |
|
331
|
1 |
|
self::TABLE_NAME, |
|
332
|
1 |
|
$snapshot_data, |
|
333
|
1 |
|
['id' => $snapshot_id] |
|
334
|
|
|
); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
2 |
|
public function createSnapshot(array $snapshot_data) : int |
|
338
|
|
|
{ |
|
339
|
2 |
|
if (empty($snapshot_data)) { |
|
340
|
1 |
|
return 0; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
1 |
|
return $this->AggregatorStorage->insert(self::TABLE_NAME, $snapshot_data); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Delete snapshot by id, aggregator_tree and aggregator_method_data will be deleted by cascade |
|
348
|
|
|
* @param int $snapshot_id |
|
349
|
|
|
* @return bool |
|
350
|
|
|
*/ |
|
351
|
2 |
|
public function deleteById(int $snapshot_id) : bool |
|
352
|
|
|
{ |
|
353
|
2 |
|
if (!$snapshot_id) { |
|
354
|
1 |
|
return false; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
1 |
|
return $this->AggregatorStorage->delete( |
|
358
|
1 |
|
self::TABLE_NAME, |
|
359
|
1 |
|
['id' => $snapshot_id] |
|
360
|
|
|
); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|