1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2019 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\services; |
12
|
|
|
|
13
|
|
|
use nystudio107\webperf\Webperf; |
14
|
|
|
use nystudio107\webperf\base\CraftDataSample; |
15
|
|
|
use nystudio107\webperf\base\DbDataSampleInterface; |
16
|
|
|
|
17
|
|
|
use Craft; |
|
|
|
|
18
|
|
|
use craft\base\Component; |
|
|
|
|
19
|
|
|
use craft\db\Query; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package Webperf |
|
|
|
|
24
|
|
|
* @since 1.0.0 |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class DataSamples extends Component |
27
|
|
|
{ |
28
|
|
|
// Constants |
29
|
|
|
// ========================================================================= |
30
|
|
|
|
31
|
|
|
const OUTLIER_PAGELOAD_MULTIPLIER = 10; |
32
|
|
|
|
33
|
|
|
// Public Methods |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the total number of data samples optionally limited by siteId |
38
|
|
|
* |
39
|
|
|
* @param int $siteId |
|
|
|
|
40
|
|
|
* @param string $column |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @return int|string |
43
|
|
|
*/ |
44
|
|
|
public function totalSamples(int $siteId, string $column) |
45
|
|
|
{ |
46
|
|
|
// Get the total number of data samples |
47
|
|
|
$query = (new Query()) |
48
|
|
|
->from(['{{%webperf_data_samples}}']) |
49
|
|
|
->where(['not', [$column => null]]) |
|
|
|
|
50
|
|
|
; |
51
|
|
|
if ((int)$siteId !== 0) { |
52
|
|
|
$query->andWhere(['siteId' => $siteId]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $query->count(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the page title from data samples by URL and optionally siteId |
60
|
|
|
* |
61
|
|
|
* @param string $url |
|
|
|
|
62
|
|
|
* @param int $siteId |
|
|
|
|
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function pageTitle(string $url, int $siteId = 0): string |
67
|
|
|
{ |
68
|
|
|
// Get the page title from a URL |
69
|
|
|
$query = (new Query()) |
70
|
|
|
->select(['title']) |
71
|
|
|
->from(['{{%webperf_data_samples}}']) |
72
|
|
|
->where([ |
|
|
|
|
73
|
|
|
'and', ['url' => $url], |
74
|
|
|
['not', ['title' => '']], |
75
|
|
|
]) |
|
|
|
|
76
|
|
|
; |
77
|
|
|
if ((int)$siteId !== 0) { |
78
|
|
|
$query->andWhere(['siteId' => $siteId]); |
79
|
|
|
} |
80
|
|
|
$result = $query->one(); |
81
|
|
|
// Decode any emojis in the title |
82
|
|
|
if (!empty($result['title'])) { |
83
|
|
|
$result['title'] = html_entity_decode($result['title'], ENT_NOQUOTES, 'UTF-8'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $result['title'] ?? ''; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add a data sample to the webperf_data_samples table |
91
|
|
|
* |
92
|
|
|
* @param DbDataSampleInterface $dataSample |
|
|
|
|
93
|
|
|
*/ |
|
|
|
|
94
|
|
|
public function addDataSample(DbDataSampleInterface $dataSample) |
95
|
|
|
{ |
96
|
|
|
// Validate the model before saving it to the db |
97
|
|
|
if ($dataSample->validate() === false) { |
98
|
|
|
Craft::error( |
99
|
|
|
Craft::t( |
100
|
|
|
'webperf', |
101
|
|
|
'Error validating data sample: {errors}', |
102
|
|
|
['errors' => print_r($dataSample->getErrors(), true)] |
103
|
|
|
), |
104
|
|
|
__METHOD__ |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
$isNew = true; |
110
|
|
|
if (!empty($dataSample->requestId)) { |
|
|
|
|
111
|
|
|
// See if a data sample exists with the same requestId already |
112
|
|
|
$testSample = (new Query()) |
113
|
|
|
->from(['{{%webperf_data_samples}}']) |
114
|
|
|
->where(['requestId' => $dataSample->requestId]) |
115
|
|
|
->one(); |
116
|
|
|
// If it exists, update it rather than having duplicates |
117
|
|
|
if (!empty($testSample)) { |
118
|
|
|
$isNew = false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
// Get the validated model attributes and save them to the db |
122
|
|
|
$dataSampleConfig = $dataSample->getAttributes(); |
123
|
|
|
$db = Craft::$app->getDb(); |
124
|
|
|
if ($isNew) { |
125
|
|
|
Craft::debug('Creating new data sample', __METHOD__); |
126
|
|
|
// Create a new record |
127
|
|
|
try { |
128
|
|
|
$result = $db->createCommand()->insert( |
129
|
|
|
'{{%webperf_data_samples}}', |
130
|
|
|
$dataSampleConfig |
131
|
|
|
)->execute(); |
132
|
|
|
Craft::debug($result, __METHOD__); |
133
|
|
|
} catch (\Exception $e) { |
134
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
135
|
|
|
} |
136
|
|
|
} else { |
137
|
|
|
Craft::debug('Updating existing data sample', __METHOD__); |
138
|
|
|
// Update the existing record |
139
|
|
|
try { |
140
|
|
|
$result = $db->createCommand()->update( |
141
|
|
|
'{{%webperf_data_samples}}', |
142
|
|
|
$dataSampleConfig, |
143
|
|
|
[ |
144
|
|
|
'requestId' => $dataSample->requestId, |
145
|
|
|
] |
146
|
|
|
)->execute(); |
147
|
|
|
Craft::debug($result, __METHOD__); |
148
|
|
|
} catch (\Exception $e) { |
149
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
// Trim orphaned samples |
153
|
|
|
$this->trimOrphanedSamples($dataSample->requestId); |
154
|
|
|
// After adding the DataSample, trim the webperf_data_samples db table |
155
|
|
|
if (Webperf::$settings->automaticallyTrimDataSamples) { |
156
|
|
|
$this->trimDataSamples(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Delete a data sample by id |
162
|
|
|
* |
163
|
|
|
* @param int $id |
|
|
|
|
164
|
|
|
* |
165
|
|
|
* @return int The result |
166
|
|
|
*/ |
167
|
|
|
public function deleteSampleById(int $id): int |
168
|
|
|
{ |
169
|
|
|
$db = Craft::$app->getDb(); |
170
|
|
|
// Delete a row from the db table |
171
|
|
|
try { |
172
|
|
|
$result = $db->createCommand()->delete( |
173
|
|
|
'{{%webperf_data_samples}}', |
174
|
|
|
[ |
175
|
|
|
'id' => $id, |
176
|
|
|
] |
177
|
|
|
)->execute(); |
178
|
|
|
} catch (\Exception $e) { |
179
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
180
|
|
|
$result = 0; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Delete data samples by URL and optionally siteId |
188
|
|
|
* |
189
|
|
|
* @param string $url |
|
|
|
|
190
|
|
|
* @param int|null $siteId |
|
|
|
|
191
|
|
|
* |
192
|
|
|
* @return int |
193
|
|
|
*/ |
194
|
|
|
public function deleteDataSamplesByUrl(string $url, int $siteId = null): int |
195
|
|
|
{ |
196
|
|
|
$db = Craft::$app->getDb(); |
197
|
|
|
// Delete a row from the db table |
198
|
|
|
try { |
199
|
|
|
$conditions = ['url' => $url]; |
200
|
|
|
if ($siteId !== null) { |
201
|
|
|
$conditions['siteId'] = $siteId; |
202
|
|
|
} |
203
|
|
|
$result = $db->createCommand()->delete( |
204
|
|
|
'{{%webperf_data_samples}}', |
205
|
|
|
$conditions |
206
|
|
|
)->execute(); |
207
|
|
|
} catch (\Exception $e) { |
208
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
209
|
|
|
$result = 0; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $result; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Delete data all samples optionally siteId |
217
|
|
|
* |
218
|
|
|
* @param int|null $siteId |
|
|
|
|
219
|
|
|
* |
220
|
|
|
* @return int |
221
|
|
|
*/ |
222
|
|
|
public function deleteAllDataSamples(int $siteId = null): int |
223
|
|
|
{ |
224
|
|
|
$db = Craft::$app->getDb(); |
225
|
|
|
// Delete a row from the db table |
226
|
|
|
try { |
227
|
|
|
$conditions = []; |
228
|
|
|
if ($siteId !== null) { |
229
|
|
|
$conditions['siteId'] = $siteId; |
230
|
|
|
} |
231
|
|
|
$result = $db->createCommand()->delete( |
232
|
|
|
'{{%webperf_data_samples}}', |
233
|
|
|
$conditions |
234
|
|
|
)->execute(); |
235
|
|
|
} catch (\Exception $e) { |
236
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
237
|
|
|
$result = 0; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $result; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Trim any samples that our outliers |
245
|
|
|
*/ |
|
|
|
|
246
|
|
|
public function trimOutlierSamples() |
247
|
|
|
{ |
248
|
|
|
if (Webperf::$settings->trimOutlierDataSamples) { |
249
|
|
|
$db = Craft::$app->getDb(); |
250
|
|
|
Craft::debug('Trimming outlier samples', __METHOD__); |
251
|
|
|
// Get the average pageload time |
252
|
|
|
$stats = (new Query()) |
253
|
|
|
->from('{{%webperf_data_samples}}') |
254
|
|
|
->average('[[pageLoad]]'); |
255
|
|
|
if (!empty($stats['avg'])) { |
256
|
|
|
$threshold = $stats['avg'] * self::OUTLIER_PAGELOAD_MULTIPLIER; |
257
|
|
|
// Delete any samples that are far above average |
258
|
|
|
try { |
259
|
|
|
$result = $db->createCommand()->delete( |
260
|
|
|
'{{%webperf_data_samples}}', |
261
|
|
|
['>', '[[pageLoad]]', $threshold] |
262
|
|
|
)->execute(); |
263
|
|
|
Craft::debug($result, __METHOD__); |
264
|
|
|
} catch (\Exception $e) { |
265
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Trim samples that have the placeholder in the URL, aka they never |
273
|
|
|
* received the Boomerang beacon |
274
|
|
|
* |
275
|
|
|
* @param int $requestId |
|
|
|
|
276
|
|
|
*/ |
|
|
|
|
277
|
|
|
public function trimOrphanedSamples($requestId) |
278
|
|
|
{ |
279
|
|
|
$db = Craft::$app->getDb(); |
280
|
|
|
Craft::debug('Trimming orphaned samples', __METHOD__); |
281
|
|
|
// Update the existing record |
282
|
|
|
try { |
283
|
|
|
$result = $db->createCommand()->delete( |
284
|
|
|
'{{%webperf_data_samples}}', |
285
|
|
|
[ |
286
|
|
|
'and', ['url' => CraftDataSample::PLACEHOLDER_URL], |
287
|
|
|
['not', ['requestId' => $requestId]], |
288
|
|
|
] |
289
|
|
|
)->execute(); |
290
|
|
|
Craft::debug($result, __METHOD__); |
291
|
|
|
} catch (\Exception $e) { |
292
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Trim the webperf_data_samples db table based on the dataSamplesStoredLimit |
298
|
|
|
* config.php setting |
299
|
|
|
* |
300
|
|
|
* @param int|null $limit |
|
|
|
|
301
|
|
|
* |
302
|
|
|
* @return int |
303
|
|
|
*/ |
304
|
|
|
public function trimDataSamples(int $limit = null): int |
305
|
|
|
{ |
306
|
|
|
$this->trimOutlierSamples(); |
307
|
|
|
$affectedRows = 0; |
308
|
|
|
$db = Craft::$app->getDb(); |
309
|
|
|
$quotedTable = $db->quoteTableName('{{%webperf_data_samples}}'); |
310
|
|
|
$limit = $limit ?? Webperf::$settings->dataSamplesStoredLimit; |
311
|
|
|
|
312
|
|
|
if ($limit !== null) { |
|
|
|
|
313
|
|
|
// https://stackoverflow.com/questions/578867/sql-query-delete-all-records-from-the-table-except-latest-n |
314
|
|
|
try { |
315
|
|
|
if ($db->getIsMysql()) { |
316
|
|
|
// Handle MySQL |
317
|
|
|
$affectedRows = $db->createCommand(/** @lang mysql */ |
|
|
|
|
318
|
|
|
" |
319
|
|
|
DELETE FROM {$quotedTable} |
320
|
|
|
WHERE id NOT IN ( |
321
|
|
|
SELECT id |
322
|
|
|
FROM ( |
323
|
|
|
SELECT id |
324
|
|
|
FROM {$quotedTable} |
325
|
|
|
ORDER BY dateCreated DESC |
326
|
|
|
LIMIT {$limit} |
327
|
|
|
) foo |
328
|
|
|
) |
329
|
|
|
" |
330
|
|
|
)->execute(); |
331
|
|
|
} |
332
|
|
|
if ($db->getIsPgsql()) { |
333
|
|
|
// Handle Postgres |
334
|
|
|
$affectedRows = $db->createCommand(/** @lang mysql */ |
|
|
|
|
335
|
|
|
" |
336
|
|
|
DELETE FROM {$quotedTable} |
337
|
|
|
WHERE id NOT IN ( |
338
|
|
|
SELECT id |
339
|
|
|
FROM ( |
340
|
|
|
SELECT id |
341
|
|
|
FROM {$quotedTable} |
342
|
|
|
ORDER BY \"dateCreated\" DESC |
343
|
|
|
LIMIT {$limit} |
344
|
|
|
) foo |
345
|
|
|
) |
346
|
|
|
" |
347
|
|
|
)->execute(); |
348
|
|
|
} |
349
|
|
|
} catch (\Exception $e) { |
350
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
351
|
|
|
} |
352
|
|
|
Craft::info( |
353
|
|
|
Craft::t( |
354
|
|
|
'webperf', |
355
|
|
|
'Trimmed {rows} from webperf_data_samples table', |
356
|
|
|
['rows' => $affectedRows] |
357
|
|
|
), |
358
|
|
|
__METHOD__ |
359
|
|
|
); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $affectedRows; |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|