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\controllers; |
12
|
|
|
|
13
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
14
|
|
|
|
15
|
|
|
use Craft; |
|
|
|
|
16
|
|
|
use craft\db\Query; |
|
|
|
|
17
|
|
|
use craft\helpers\ArrayHelper; |
|
|
|
|
18
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
19
|
|
|
use craft\web\Controller; |
|
|
|
|
20
|
|
|
|
21
|
|
|
use yii\web\ForbiddenHttpException; |
|
|
|
|
22
|
|
|
use yii\web\Response; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package Webperf |
|
|
|
|
27
|
|
|
* @since 1.0.0 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class ChartsController extends Controller |
30
|
|
|
{ |
31
|
|
|
// Constants |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
// Protected Properties |
35
|
|
|
// ========================================================================= |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var bool|array |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
protected $allowAnonymous = []; |
41
|
|
|
|
42
|
|
|
// Public Methods |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The Dashboard stats average chart |
47
|
|
|
* |
48
|
|
|
* @param string $start |
|
|
|
|
49
|
|
|
* @param string $end |
|
|
|
|
50
|
|
|
* @param string $column |
|
|
|
|
51
|
|
|
* @param string $pageUrl |
|
|
|
|
52
|
|
|
* @param int $siteId |
|
|
|
|
53
|
|
|
* |
54
|
|
|
* @return Response |
55
|
|
|
* @throws ForbiddenHttpException |
56
|
|
|
*/ |
57
|
|
|
public function actionDashboardStatsAverage( |
58
|
|
|
string $start = '', |
59
|
|
|
string $end = '', |
60
|
|
|
string $column = 'pageLoad', |
61
|
|
|
$pageUrl = '', |
62
|
|
|
int $siteId = 0 |
63
|
|
|
): Response { |
64
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
65
|
|
|
$data = []; |
66
|
|
|
if (empty($end) || empty($start)) { |
67
|
|
|
return $this->asJson($data); |
68
|
|
|
} |
69
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
70
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
71
|
|
|
$pageUrl = urldecode($pageUrl); |
72
|
|
|
// Different dbs do it different ways |
73
|
|
|
$stats = null; |
|
|
|
|
74
|
|
|
$db = Craft::$app->getDb(); |
|
|
|
|
75
|
|
|
// Query the db |
76
|
|
|
$query = (new Query()) |
77
|
|
|
->from('{{%webperf_data_samples}}') |
78
|
|
|
->select([ |
|
|
|
|
79
|
|
|
'COUNT([[url]]) AS cnt', |
80
|
|
|
'AVG([['.$column.']]) AS avg', |
81
|
|
|
]) |
|
|
|
|
82
|
|
|
->where(['between', 'dateCreated', $start, $end]) |
83
|
|
|
->andWhere(['not', [$column => null]]); |
84
|
|
|
if ((int)$siteId !== 0) { |
85
|
|
|
$query->andWhere(['siteId' => $siteId]); |
86
|
|
|
} |
87
|
|
|
if ($pageUrl !== '') { |
88
|
|
|
$query->andWhere(['url' => $pageUrl]); |
89
|
|
|
} |
90
|
|
|
$stats = $query->all(); |
91
|
|
|
// Massage the data |
92
|
|
|
if ($stats) { |
93
|
|
|
foreach ($stats as &$stat) { |
94
|
|
|
$stat['cnt'] = (int)$stat['cnt']; |
95
|
|
|
} |
96
|
|
|
$data = $stats[0]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->asJson($data); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The Dashboard stats slowest pages list |
104
|
|
|
* |
105
|
|
|
* @param string $start |
|
|
|
|
106
|
|
|
* @param string $end |
|
|
|
|
107
|
|
|
* @param string $column |
|
|
|
|
108
|
|
|
* @param int $limit |
|
|
|
|
109
|
|
|
* @param int $siteId |
|
|
|
|
110
|
|
|
* |
111
|
|
|
* @return Response |
112
|
|
|
* @throws ForbiddenHttpException |
113
|
|
|
*/ |
114
|
|
|
public function actionDashboardSlowestPages( |
115
|
|
|
string $start, |
116
|
|
|
string $end, |
117
|
|
|
string $column = 'pageLoad', |
118
|
|
|
int $limit = 3, |
119
|
|
|
int $siteId = 0 |
120
|
|
|
): Response { |
121
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
122
|
|
|
$data = []; |
123
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
124
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
125
|
|
|
// Different dbs do it different ways |
126
|
|
|
$stats = null; |
|
|
|
|
127
|
|
|
$db = Craft::$app->getDb(); |
|
|
|
|
128
|
|
|
// Query the db |
129
|
|
|
$query = (new Query()) |
130
|
|
|
->from('{{%webperf_data_samples}}') |
131
|
|
|
->select([ |
|
|
|
|
132
|
|
|
'[[url]]', |
133
|
|
|
'MIN([[title]]) AS title', |
134
|
|
|
'COUNT([[url]]) AS cnt', |
135
|
|
|
'AVG([['.$column.']]) AS avg', |
136
|
|
|
]) |
|
|
|
|
137
|
|
|
->where(['between', 'dateCreated', $start, $end]) |
138
|
|
|
->andWhere(['not', [$column => null]]); |
139
|
|
|
if ((int)$siteId !== 0) { |
140
|
|
|
$query->andWhere(['siteId' => $siteId]); |
141
|
|
|
} |
142
|
|
|
$query |
143
|
|
|
->orderBy('avg DESC') |
144
|
|
|
->groupBy('url') |
145
|
|
|
->limit($limit); |
146
|
|
|
$stats = $query->all(); |
147
|
|
|
// Massage the data |
148
|
|
|
if ($stats) { |
149
|
|
|
foreach ($stats as &$stat) { |
150
|
|
|
$stat['cnt'] = (int)$stat['cnt']; |
151
|
|
|
$stat['detailPageUrl'] = UrlHelper::cpUrl('webperf/performance/page-detail', [ |
|
|
|
|
152
|
|
|
'pageUrl' => $stat['url'], |
153
|
|
|
'siteId' => $siteId, |
154
|
|
|
]); |
|
|
|
|
155
|
|
|
// Decode any emojis in the title |
156
|
|
|
if (!empty($stat['title'])) { |
157
|
|
|
$stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
$data = $stats; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this->asJson($data); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* The Performance stats average chart |
168
|
|
|
* |
169
|
|
|
* @param string $start |
|
|
|
|
170
|
|
|
* @param string $end |
|
|
|
|
171
|
|
|
* @param string $pageUrl |
|
|
|
|
172
|
|
|
* @param int $siteId |
|
|
|
|
173
|
|
|
* |
174
|
|
|
* @return Response |
175
|
|
|
* @throws ForbiddenHttpException |
176
|
|
|
*/ |
177
|
|
|
public function actionPagesAreaChart( |
178
|
|
|
string $start, |
179
|
|
|
string $end, |
180
|
|
|
$pageUrl = '', |
181
|
|
|
int $siteId = 0 |
182
|
|
|
): Response { |
183
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
184
|
|
|
$data = []; |
185
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
186
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
187
|
|
|
$pageUrl = urldecode($pageUrl); |
188
|
|
|
$dateStart = new \DateTime($start); |
189
|
|
|
$dateEnd = new \DateTime($end); |
190
|
|
|
$interval = date_diff($dateStart, $dateEnd); |
191
|
|
|
$dateFormat = "'%Y-%m-%d %H'"; |
192
|
|
|
if ($interval->days > 30) { |
193
|
|
|
$dateFormat = "'%Y-%m-%d'"; |
194
|
|
|
} |
195
|
|
|
// Different dbs do it different ways |
196
|
|
|
$stats = null; |
|
|
|
|
197
|
|
|
$db = Craft::$app->getDb(); |
198
|
|
|
if ($db->getIsPgsql()) { |
199
|
|
|
$dateFormat = "'yyyy-mm-dd HH:MM'"; |
200
|
|
|
if ($interval->days > 30) { |
201
|
|
|
$dateFormat = "'yyyy-mm-dd'"; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
// Query the db |
205
|
|
|
$query = (new Query()) |
206
|
|
|
->from('{{%webperf_data_samples}}') |
207
|
|
|
->select([ |
|
|
|
|
208
|
|
|
'AVG([[pageLoad]]) AS [[pageLoad]]', |
209
|
|
|
'AVG([[domInteractive]]) AS [[domInteractive]]', |
210
|
|
|
'AVG([[firstContentfulPaint]]) AS [[firstContentfulPaint]]', |
211
|
|
|
'AVG([[firstPaint]]) AS [[firstPaint]]', |
212
|
|
|
'AVG([[firstByte]]) AS [[firstByte]]', |
213
|
|
|
'AVG([[connect]]) AS [[connect]]', |
214
|
|
|
'AVG([[dns]]) AS [[dns]]', |
215
|
|
|
'AVG([[craftTotalMs]]) AS [[craftTotalMs]]', |
216
|
|
|
'AVG([[craftTwigMs]]) AS [[craftTwigMs]]', |
217
|
|
|
'AVG([[craftDbMs]]) AS [[craftDbMs]]', |
218
|
|
|
]) |
|
|
|
|
219
|
|
|
->where(['between', 'dateCreated', $start, $end]) |
|
|
|
|
220
|
|
|
; |
221
|
|
|
if ((int)$siteId !== 0) { |
222
|
|
|
$query->andWhere(['siteId' => $siteId]); |
223
|
|
|
} |
224
|
|
|
if ($pageUrl !== '') { |
225
|
|
|
$query->andWhere(['url' => $pageUrl]); |
226
|
|
|
} |
227
|
|
|
if ($db->getIsMysql()) { |
228
|
|
|
$query |
229
|
|
|
->addSelect([ |
|
|
|
|
230
|
|
|
'DATE_FORMAT([[dateCreated]], '.$dateFormat.') AS [[sampleDate]]', |
231
|
|
|
]) |
|
|
|
|
232
|
|
|
->groupBy('sampleDate') |
|
|
|
|
233
|
|
|
; |
234
|
|
|
} |
235
|
|
|
if ($db->getIsPgsql()) { |
236
|
|
|
$query |
237
|
|
|
->addSelect([ |
|
|
|
|
238
|
|
|
'to_char([[dateCreated]], '.$dateFormat.') AS [[sampleDate]]', |
239
|
|
|
]) |
|
|
|
|
240
|
|
|
->groupBy(['to_char([[dateCreated]], '.$dateFormat.')']) |
|
|
|
|
241
|
|
|
; |
242
|
|
|
} |
243
|
|
|
$stats = $query |
244
|
|
|
->all(); |
245
|
|
|
// Massage the data |
246
|
|
|
if ($stats) { |
247
|
|
|
$data[] = [ |
248
|
|
|
'name' => 'Database Queries', |
249
|
|
|
'data' => ArrayHelper::getColumn($stats, 'craftDbMs'), |
250
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
251
|
|
|
]; |
252
|
|
|
$data[] = [ |
253
|
|
|
'name' => 'Twig Rendering', |
254
|
|
|
'data' => ArrayHelper::getColumn($stats, 'craftTwigMs'), |
255
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
256
|
|
|
]; |
257
|
|
|
$data[] = [ |
258
|
|
|
'name' => 'Craft Rendering', |
259
|
|
|
'data' => ArrayHelper::getColumn($stats, 'craftTotalMs'), |
260
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
261
|
|
|
]; |
262
|
|
|
$data[] = [ |
263
|
|
|
'name' => 'DNS Lookup', |
264
|
|
|
'data' => ArrayHelper::getColumn($stats, 'dns'), |
265
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
266
|
|
|
]; |
267
|
|
|
$data[] = [ |
268
|
|
|
'name' => 'Connect', |
269
|
|
|
'data' => ArrayHelper::getColumn($stats, 'connect'), |
270
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
271
|
|
|
]; |
272
|
|
|
$data[] = [ |
273
|
|
|
'name' => 'First Byte', |
274
|
|
|
'data' => ArrayHelper::getColumn($stats, 'firstByte'), |
275
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
276
|
|
|
]; |
277
|
|
|
$data[] = [ |
278
|
|
|
'name' => 'First Paint', |
279
|
|
|
'data' => ArrayHelper::getColumn($stats, 'firstPaint'), |
280
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
281
|
|
|
]; |
282
|
|
|
$data[] = [ |
283
|
|
|
'name' => 'First Contentful Paint', |
284
|
|
|
'data' => ArrayHelper::getColumn($stats, 'firstContentfulPaint'), |
285
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
286
|
|
|
]; |
287
|
|
|
$data[] = [ |
288
|
|
|
'name' => 'DOM Interactive', |
289
|
|
|
'data' => ArrayHelper::getColumn($stats, 'domInteractive'), |
290
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
291
|
|
|
]; |
292
|
|
|
$data[] = [ |
293
|
|
|
'name' => 'Page Load', |
294
|
|
|
'data' => ArrayHelper::getColumn($stats, 'pageLoad'), |
295
|
|
|
'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $this->asJson($data); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* The Dashboard stats average chart |
305
|
|
|
* |
306
|
|
|
* @param string $start |
|
|
|
|
307
|
|
|
* @param string $end |
|
|
|
|
308
|
|
|
* @param string $pageUrl |
|
|
|
|
309
|
|
|
* @param int $siteId |
|
|
|
|
310
|
|
|
* |
311
|
|
|
* @return Response |
312
|
|
|
* @throws ForbiddenHttpException |
313
|
|
|
*/ |
314
|
|
|
public function actionErrorsAreaChart( |
315
|
|
|
string $start, |
316
|
|
|
string $end, |
317
|
|
|
$pageUrl = '', |
318
|
|
|
int $siteId = 0 |
319
|
|
|
): Response { |
320
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:errors'); |
321
|
|
|
$data = []; |
322
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
323
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
324
|
|
|
$pageUrl = urldecode($pageUrl); |
325
|
|
|
$dateStart = new \DateTime($start); |
326
|
|
|
$dateEnd = new \DateTime($end); |
327
|
|
|
$interval = date_diff($dateStart, $dateEnd); |
328
|
|
|
$dateFormat = "'%Y-%m-%d %H'"; |
329
|
|
|
if ($interval->days > 30) { |
330
|
|
|
$dateFormat = "'%Y-%m-%d'"; |
331
|
|
|
} |
332
|
|
|
// Different dbs do it different ways |
333
|
|
|
$stats = null; |
|
|
|
|
334
|
|
|
$db = Craft::$app->getDb(); |
335
|
|
|
if ($db->getIsPgsql()) { |
336
|
|
|
$dateFormat = "'yyyy-mm-dd HH:MM'"; |
337
|
|
|
if ($interval->days > 30) { |
338
|
|
|
$dateFormat = "'yyyy-mm-dd'"; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
// Query the db |
342
|
|
|
$query = (new Query()) |
343
|
|
|
->from('{{%webperf_error_samples}}') |
344
|
|
|
->select([ |
|
|
|
|
345
|
|
|
'[[type]]', |
346
|
|
|
'COUNT([[type]]) AS [[cnt]]', |
347
|
|
|
]) |
|
|
|
|
348
|
|
|
->where(['between', 'dateCreated', $start, $end]) |
|
|
|
|
349
|
|
|
; |
350
|
|
|
if ((int)$siteId !== 0) { |
351
|
|
|
$query->andWhere(['siteId' => $siteId]); |
352
|
|
|
} |
353
|
|
|
if ($pageUrl !== '') { |
354
|
|
|
$query->andWhere(['url' => $pageUrl]); |
355
|
|
|
} |
356
|
|
|
if ($db->getIsMysql()) { |
357
|
|
|
$query |
358
|
|
|
->addSelect([ |
|
|
|
|
359
|
|
|
'DATE_FORMAT([[dateCreated]], '.$dateFormat.') AS [[sampleDate]]', |
360
|
|
|
]) |
|
|
|
|
361
|
|
|
->groupBy('[[sampleDate]], [[type]]') |
|
|
|
|
362
|
|
|
; |
363
|
|
|
} |
364
|
|
|
if ($db->getIsPgsql()) { |
365
|
|
|
$query |
366
|
|
|
->addSelect([ |
|
|
|
|
367
|
|
|
'to_char([[dateCreated]], '.$dateFormat.') AS [[sampleDate]]', |
368
|
|
|
]) |
|
|
|
|
369
|
|
|
->groupBy(['to_char([[dateCreated]], '.$dateFormat.'), [[type]]']) |
|
|
|
|
370
|
|
|
; |
371
|
|
|
} |
372
|
|
|
$stats = $query |
373
|
|
|
->all(); |
374
|
|
|
// Massage the data |
375
|
|
|
if ($stats) { |
376
|
|
|
$craftErrors = [ |
377
|
|
|
'name' => 'Craft Errors', |
378
|
|
|
'data' => [], |
379
|
|
|
'labels' => [], |
380
|
|
|
]; |
381
|
|
|
$boomerangErrors = [ |
382
|
|
|
'name' => 'JavaScript Errors', |
383
|
|
|
'data' => [], |
384
|
|
|
'labels' => [], |
385
|
|
|
]; |
386
|
|
|
foreach ($stats as $stat) { |
387
|
|
|
switch ($stat['type']) { |
388
|
|
|
case 'boomerang': |
|
|
|
|
389
|
|
|
$boomerangErrors['data'][] = (int)$stat['cnt']; |
390
|
|
|
$boomerangErrors['labels'][] = $stat['sampleDate']; |
391
|
|
|
break; |
392
|
|
|
case 'craft': |
|
|
|
|
393
|
|
|
$craftErrors['data'][] = (int)$stat['cnt']; |
394
|
|
|
$craftErrors['labels'][] = $stat['sampleDate']; |
395
|
|
|
break; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
$data[] = $boomerangErrors; |
399
|
|
|
$data[] = $craftErrors; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return $this->asJson($data); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* The Dashboard chart |
407
|
|
|
* |
408
|
|
|
* @param int $days |
|
|
|
|
409
|
|
|
* |
410
|
|
|
* @return Response |
411
|
|
|
*/ |
412
|
|
|
public function actionWidget($days = 1): Response |
413
|
|
|
{ |
414
|
|
|
$data = []; |
415
|
|
|
return $this->asJson($data); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
// Protected Methods |
419
|
|
|
// ========================================================================= |
420
|
|
|
} |
421
|
|
|
|