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
|
5 |
|
public function getOneByAppAndLabel(string $app, string $label) : \Badoo\LiveProfilerUI\Entity\Snapshot |
118
|
|
|
{ |
119
|
5 |
|
$snapshot = $this->AggregatorStorage->getOne( |
120
|
5 |
|
self::TABLE_NAME, |
121
|
5 |
|
['all'], |
122
|
|
|
[ |
123
|
5 |
|
'filter' => [ |
124
|
5 |
|
['app', $app], |
125
|
5 |
|
['label', $label] |
126
|
|
|
], |
127
|
|
|
'order' => ['date' => 'desc'] |
128
|
|
|
] |
129
|
|
|
); |
130
|
|
|
|
131
|
5 |
|
if (empty($snapshot)) { |
132
|
4 |
|
throw new \InvalidArgumentException('Can\'t get snapshot'); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return new \Badoo\LiveProfilerUI\Entity\Snapshot( |
136
|
1 |
|
$snapshot, |
137
|
1 |
|
$this->FieldList->getAllFieldsWithVariations() |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
5 |
|
public function getOneByAppAndLabelAndDate( |
142
|
|
|
string $app, |
143
|
|
|
string $label, |
144
|
|
|
string $date |
145
|
|
|
) : \Badoo\LiveProfilerUI\Entity\Snapshot { |
146
|
5 |
|
$snapshot = $this->AggregatorStorage->getOne( |
147
|
5 |
|
self::TABLE_NAME, |
148
|
5 |
|
['all'], |
149
|
|
|
[ |
150
|
5 |
|
'filter' => [ |
151
|
5 |
|
['app', $app], |
152
|
5 |
|
['label', $label], |
153
|
5 |
|
['date', $date] |
154
|
|
|
], |
155
|
|
|
'order' => ['date' => 'desc'] |
156
|
|
|
] |
157
|
|
|
); |
158
|
|
|
|
159
|
5 |
|
if (empty($snapshot)) { |
160
|
4 |
|
throw new \InvalidArgumentException('Can\'t get snapshot'); |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
return new \Badoo\LiveProfilerUI\Entity\Snapshot( |
164
|
1 |
|
$snapshot, |
165
|
1 |
|
$this->FieldList->getAllFieldsWithVariations() |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
public function getSnapshotsByDates(array $dates, string $param = '') : array |
170
|
|
|
{ |
171
|
2 |
|
if (empty($dates)) { |
172
|
1 |
|
return []; |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
$fields = ['id', 'app', 'label', 'date', 'type']; |
176
|
1 |
|
if ($param) { |
177
|
1 |
|
$fields[] = $param; |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
$snapshots = $this->AggregatorStorage->getAll( |
181
|
1 |
|
self::TABLE_NAME, |
182
|
1 |
|
$fields, |
183
|
|
|
[ |
184
|
|
|
'filter' => [ |
185
|
1 |
|
['date', $dates], |
186
|
|
|
], |
187
|
|
|
] |
188
|
|
|
); |
189
|
1 |
|
return $snapshots; |
190
|
|
|
} |
191
|
|
|
|
192
|
3 |
|
public function getSnapshotIdsByDates(array $dates, string $app, string $label) : array |
193
|
|
|
{ |
194
|
3 |
|
if (empty($dates) || !$app || !$label) { |
195
|
1 |
|
return []; |
196
|
|
|
} |
197
|
|
|
|
198
|
2 |
|
$snapshots = $this->AggregatorStorage->getAll( |
199
|
2 |
|
self::TABLE_NAME, |
200
|
2 |
|
['id', 'calls_count', 'date'], |
201
|
|
|
[ |
202
|
|
|
'filter' => [ |
203
|
2 |
|
['date', $dates], |
204
|
2 |
|
['app', $app], |
205
|
2 |
|
['label', $label], |
206
|
|
|
], |
207
|
|
|
] |
208
|
|
|
); |
209
|
|
|
|
210
|
2 |
|
$result = []; |
211
|
2 |
|
if (!empty($snapshots)) { |
212
|
2 |
|
foreach ($snapshots as $row) { |
213
|
2 |
|
$result[$row['date']] = [ |
214
|
2 |
|
'id' => (int)$row['id'], |
215
|
2 |
|
'calls_count' => (int)$row['calls_count'], |
216
|
|
|
]; |
217
|
|
|
} |
218
|
|
|
} |
219
|
2 |
|
foreach ($dates as $date) { |
220
|
2 |
|
if (!isset($result[$date])) { |
221
|
2 |
|
$result[$date] = null; |
222
|
|
|
} |
223
|
|
|
} |
224
|
2 |
|
ksort($result); |
225
|
|
|
|
226
|
2 |
|
return $result; |
227
|
|
|
} |
228
|
|
|
|
229
|
1 |
|
public function getOldSnapshots(int $keep_days = 200, int $limit = 2000) : array |
230
|
|
|
{ |
231
|
1 |
|
$snapshots = $this->AggregatorStorage->getAll( |
232
|
1 |
|
self::TABLE_NAME, |
233
|
1 |
|
['id', 'app', 'label', 'date'], |
234
|
|
|
[ |
235
|
1 |
|
'filter' => [ |
236
|
1 |
|
['date', date('Y-m-d', strtotime("-$keep_days day")), '<'], |
237
|
|
|
], |
238
|
|
|
'order' => ['id' => 'asc'], |
239
|
1 |
|
'limit' => $limit |
240
|
|
|
] |
241
|
|
|
); |
242
|
|
|
|
243
|
1 |
|
return $snapshots; |
244
|
|
|
} |
245
|
|
|
|
246
|
1 |
|
public function getDatesByAppAndLabel(string $app, string $label) : array |
247
|
|
|
{ |
248
|
1 |
|
$snapshots = $this->AggregatorStorage->getAll( |
249
|
1 |
|
self::TABLE_NAME, |
250
|
1 |
|
['date'], |
251
|
|
|
[ |
252
|
1 |
|
'filter' => [ |
253
|
1 |
|
['date', date('Y-m-d', strtotime('-1 year')), '>'], |
254
|
1 |
|
['app', $app], |
255
|
1 |
|
['label', $label], |
256
|
|
|
], |
257
|
|
|
'order' => ['date' => 'desc'] |
258
|
|
|
] |
259
|
|
|
); |
260
|
|
|
|
261
|
1 |
|
return $snapshots ? array_column($snapshots, 'date') : []; |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
public function getMaxCallsCntByAppAndLabel(string $app, string $label) : int |
265
|
|
|
{ |
266
|
1 |
|
$result = $this->AggregatorStorage->getOne( |
267
|
1 |
|
self::TABLE_NAME, |
268
|
1 |
|
[['field' => 'calls_count', 'function' => 'max']], |
269
|
|
|
[ |
270
|
1 |
|
'filter' => [ |
271
|
1 |
|
['app', $app], |
272
|
1 |
|
['label', $label], |
273
|
|
|
], |
274
|
|
|
'group' => ['app', 'label'], |
275
|
|
|
] |
276
|
|
|
); |
277
|
|
|
|
278
|
1 |
|
return $result ? (int)current($result) : 0; |
279
|
|
|
} |
280
|
|
|
|
281
|
2 |
|
public function getAppList(string $label = '') : array |
282
|
|
|
{ |
283
|
2 |
|
$filter = []; |
284
|
2 |
|
if ($label) { |
285
|
1 |
|
$filter[] = ['label', $label]; |
286
|
|
|
} |
287
|
2 |
|
$apps = $this->AggregatorStorage->getAll( |
288
|
2 |
|
self::TABLE_NAME, |
289
|
2 |
|
['app'], |
290
|
|
|
[ |
291
|
2 |
|
'filter' => $filter, |
292
|
|
|
'group' => ['app'], |
293
|
|
|
'order' => ['app' => 'asc'], |
294
|
|
|
] |
295
|
|
|
); |
296
|
|
|
|
297
|
2 |
|
return $apps ? array_column($apps, 'app') : []; |
298
|
|
|
} |
299
|
|
|
|
300
|
2 |
|
public function updateSnapshot(int $snapshot_id, array $snapshot_data) : bool |
301
|
|
|
{ |
302
|
2 |
|
if (empty($snapshot_data)) { |
303
|
1 |
|
return false; |
304
|
|
|
} |
305
|
|
|
|
306
|
1 |
|
return $this->AggregatorStorage->update( |
307
|
1 |
|
self::TABLE_NAME, |
308
|
1 |
|
$snapshot_data, |
309
|
1 |
|
['id' => $snapshot_id] |
310
|
|
|
); |
311
|
|
|
} |
312
|
|
|
|
313
|
2 |
|
public function createSnapshot(array $snapshot_data) : int |
314
|
|
|
{ |
315
|
2 |
|
if (empty($snapshot_data)) { |
316
|
1 |
|
return 0; |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
return $this->AggregatorStorage->insert(self::TABLE_NAME, $snapshot_data); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Delete snapshot by id, aggregator_tree and aggregator_method_data will be deleted by cascade |
324
|
|
|
* @param int $snapshot_id |
325
|
|
|
* @return bool |
326
|
|
|
*/ |
327
|
2 |
|
public function deleteById(int $snapshot_id) : bool |
328
|
|
|
{ |
329
|
2 |
|
if (!$snapshot_id) { |
330
|
1 |
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
1 |
|
return $this->AggregatorStorage->delete( |
334
|
1 |
|
self::TABLE_NAME, |
335
|
1 |
|
['id' => $snapshot_id] |
336
|
|
|
); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|