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\DateTimeHelper; |
|
|
|
|
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 TablesController 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
|
|
|
* Handle requests for the dashboard statistics table |
47
|
|
|
* |
48
|
|
|
* @param string $sort |
|
|
|
|
49
|
|
|
* @param int $page |
|
|
|
|
50
|
|
|
* @param int $per_page |
|
|
|
|
51
|
|
|
* @param string $filter |
|
|
|
|
52
|
|
|
* @param string $start |
|
|
|
|
53
|
|
|
* @param string $end |
|
|
|
|
54
|
|
|
* @param int $siteId |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @return Response |
57
|
|
|
* @throws ForbiddenHttpException |
58
|
|
|
*/ |
59
|
|
|
public function actionPagesIndex( |
60
|
|
|
string $start = '', |
61
|
|
|
string $end = '', |
62
|
|
|
string $sort = 'pageLoad|DESC', |
63
|
|
|
int $page = 1, |
64
|
|
|
int $per_page = 20, |
65
|
|
|
$filter = '', |
66
|
|
|
$siteId = 0 |
67
|
|
|
): Response { |
68
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:pages'); |
69
|
|
|
$data = []; |
70
|
|
|
$sortField = 'pageLoad'; |
71
|
|
|
$sortType = 'DESC'; |
72
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
73
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
74
|
|
|
// Figure out the sorting type |
75
|
|
|
if ($sort !== '') { |
76
|
|
|
if (strpos($sort, '|') === false) { |
77
|
|
|
$sortField = $sort; |
78
|
|
|
} else { |
79
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
// Query the db table |
83
|
|
|
$offset = ($page - 1) * $per_page; |
84
|
|
|
$query = (new Query()) |
85
|
|
|
->select([ |
|
|
|
|
86
|
|
|
'url', |
87
|
|
|
'MIN(title) AS title', |
88
|
|
|
'COUNT(url) AS cnt', |
89
|
|
|
'AVG(pageLoad) AS pageLoad', |
90
|
|
|
'AVG(domInteractive) AS domInteractive', |
91
|
|
|
'AVG(firstContentfulPaint) AS firstContentfulPaint', |
92
|
|
|
'AVG(firstPaint) AS firstPaint', |
93
|
|
|
'AVG(firstByte) AS firstByte', |
94
|
|
|
'AVG(connect) AS connect', |
95
|
|
|
'AVG(dns) AS dns', |
96
|
|
|
'AVG(craftTotalMs) AS craftTotalMs', |
97
|
|
|
'AVG(craftDbCnt) AS craftDbCnt', |
98
|
|
|
'AVG(craftDbMs) AS craftDbMs', |
99
|
|
|
'AVG(craftTwigCnt) AS craftTwigCnt', |
100
|
|
|
'AVG(craftTwigMs) AS craftTwigMs', |
101
|
|
|
'AVG(craftTotalMemory) AS craftTotalMemory', |
102
|
|
|
]) |
|
|
|
|
103
|
|
|
->from(['{{%webperf_data_samples}}']) |
104
|
|
|
->offset($offset) |
105
|
|
|
->where(['between', 'dateCreated', $start, $end]) |
|
|
|
|
106
|
|
|
; |
107
|
|
|
if ((int)$siteId !== 0) { |
108
|
|
|
$query->andWhere(['siteId' => $siteId]); |
109
|
|
|
} |
110
|
|
|
if ($filter !== '') { |
111
|
|
|
$query |
112
|
|
|
->andWhere(['like', 'url', $filter]) |
113
|
|
|
->orWhere(['like', 'title', $filter]) |
|
|
|
|
114
|
|
|
; |
115
|
|
|
} |
116
|
|
|
$query |
117
|
|
|
->orderBy("{$sortField} {$sortType}") |
118
|
|
|
->groupBy('url') |
119
|
|
|
->limit($per_page) |
|
|
|
|
120
|
|
|
; |
121
|
|
|
|
122
|
|
|
$stats = $query->all(); |
123
|
|
|
if ($stats) { |
124
|
|
|
$user = Craft::$app->getUser()->getIdentity(); |
125
|
|
|
// Compute the largest page load time |
126
|
|
|
$maxTotalPageLoad = 0; |
127
|
|
|
foreach ($stats as &$stat) { |
128
|
|
|
// Determine the stat type |
|
|
|
|
129
|
|
|
if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { |
130
|
|
|
$stat['type'] = 'both'; |
131
|
|
|
} |
132
|
|
|
if (empty($stat['firstByte'])) { |
133
|
|
|
$stat['type'] = 'craft'; |
134
|
|
|
} |
135
|
|
|
if (empty($stat['craftTotalMs'])) { |
136
|
|
|
$stat['type'] = 'frontend'; |
137
|
|
|
} |
138
|
|
|
if ($stat['pageLoad'] > $maxTotalPageLoad) { |
139
|
|
|
$maxTotalPageLoad = $stat['pageLoad']; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
// Massage the stats |
143
|
|
|
$index = 1; |
144
|
|
|
foreach ($stats as &$stat) { |
145
|
|
|
$stat['id'] = $index++; |
146
|
|
|
$stat['cnt'] = (int)$stat['cnt']; |
147
|
|
|
$stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; |
148
|
|
|
// Decode any emojis in the title |
149
|
|
|
if (!empty($stat['title'])) { |
150
|
|
|
$stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
151
|
|
|
} |
152
|
|
|
// Set up the appropriate helper links |
153
|
|
|
$stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-samples-by-url', [ |
|
|
|
|
154
|
|
|
'pageUrl' => $stat['url'], |
155
|
|
|
'siteId' => $siteId |
156
|
|
|
]); |
|
|
|
|
157
|
|
|
$stat['detailPageUrl'] = UrlHelper::cpUrl('webperf/page-detail', [ |
|
|
|
|
158
|
|
|
'pageUrl' => $stat['url'], |
159
|
|
|
'siteId' => $siteId, |
160
|
|
|
]); |
|
|
|
|
161
|
|
|
// Override based on permissions |
162
|
|
|
if (!$user->can('webperf:delete-data-samples')) { |
163
|
|
|
$stat['deleteLink'] = ''; |
164
|
|
|
} |
165
|
|
|
if (!$user->can('webperf:page-detail')) { |
166
|
|
|
$stat['detailPageUrl'] = ''; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
// Format the data for the API |
170
|
|
|
$data['data'] = $stats; |
171
|
|
|
$query = (new Query()) |
172
|
|
|
->from(['{{%webperf_data_samples}}']) |
173
|
|
|
->groupBy('url') |
174
|
|
|
->where(['between', 'dateCreated', $start, $end]) |
|
|
|
|
175
|
|
|
; |
176
|
|
|
if ($filter !== '') { |
177
|
|
|
$query->andWhere(['like', 'url', $filter]); |
178
|
|
|
$query->orWhere(['like', 'title', $filter]); |
179
|
|
|
} |
180
|
|
|
$count = $query->count(); |
181
|
|
|
$data['links']['pagination'] = [ |
182
|
|
|
'total' => $count, |
183
|
|
|
'per_page' => $per_page, |
184
|
|
|
'current_page' => $page, |
185
|
|
|
'last_page' => ceil($count / $per_page), |
186
|
|
|
'next_page_url' => null, |
187
|
|
|
'prev_page_url' => null, |
188
|
|
|
'from' => $offset + 1, |
189
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this->asJson($data); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Handle requests for the dashboard statistics table |
198
|
|
|
* |
199
|
|
|
* @param string $sort |
|
|
|
|
200
|
|
|
* @param int $page |
|
|
|
|
201
|
|
|
* @param int $per_page |
|
|
|
|
202
|
|
|
* @param string $filter |
|
|
|
|
203
|
|
|
* @param string $pageUrl |
|
|
|
|
204
|
|
|
* @param string $start |
|
|
|
|
205
|
|
|
* @param string $end |
|
|
|
|
206
|
|
|
* @param int $siteId |
|
|
|
|
207
|
|
|
* |
208
|
|
|
* @return Response |
209
|
|
|
* @throws ForbiddenHttpException |
210
|
|
|
*/ |
211
|
|
|
public function actionPageDetail( |
212
|
|
|
string $start = '', |
213
|
|
|
string $end = '', |
214
|
|
|
string $sort = 'pageLoad|DESC', |
215
|
|
|
int $page = 1, |
216
|
|
|
int $per_page = 20, |
217
|
|
|
$filter = '', |
218
|
|
|
$pageUrl = '', |
219
|
|
|
$siteId = 0 |
220
|
|
|
): Response { |
221
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:pages'); |
222
|
|
|
$data = []; |
223
|
|
|
$sortField = 'pageLoad'; |
224
|
|
|
$sortType = 'DESC'; |
225
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
226
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
227
|
|
|
$pageUrl = urldecode($pageUrl); |
228
|
|
|
// Figure out the sorting type |
229
|
|
|
if ($sort !== '') { |
230
|
|
|
if (strpos($sort, '|') === false) { |
231
|
|
|
$sortField = $sort; |
232
|
|
|
} else { |
233
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
// Query the db table |
237
|
|
|
$offset = ($page - 1) * $per_page; |
238
|
|
|
$query = (new Query()) |
239
|
|
|
->from(['{{%webperf_data_samples}}']) |
240
|
|
|
->offset($offset) |
241
|
|
|
->where(['url' => $pageUrl]) |
242
|
|
|
->andWhere(['between', 'dateCreated', $start, $end]) |
|
|
|
|
243
|
|
|
; |
244
|
|
|
if ((int)$siteId !== 0) { |
245
|
|
|
$query->andWhere(['siteId' => $siteId]); |
246
|
|
|
} |
247
|
|
|
if ($filter !== '') { |
248
|
|
|
$query |
249
|
|
|
->andWhere(['like', 'device', $filter]) |
250
|
|
|
->orWhere(['like', 'os', $filter]) |
251
|
|
|
->orWhere(['like', 'browser', $filter]) |
252
|
|
|
->orWhere(['like', 'countryCode', $filter]) |
|
|
|
|
253
|
|
|
; |
254
|
|
|
} |
255
|
|
|
$query |
256
|
|
|
->orderBy("{$sortField} {$sortType}") |
257
|
|
|
->limit($per_page) |
|
|
|
|
258
|
|
|
; |
259
|
|
|
$stats = $query->all(); |
260
|
|
|
if ($stats) { |
261
|
|
|
$user = Craft::$app->getUser()->getIdentity(); |
262
|
|
|
// Compute the largest page load time |
263
|
|
|
$maxTotalPageLoad = 0; |
264
|
|
|
foreach ($stats as &$stat) { |
265
|
|
|
// Determine the stat type |
266
|
|
|
if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { |
267
|
|
|
$stat['type'] = 'both'; |
268
|
|
|
} |
269
|
|
|
if (empty($stat['firstByte'])) { |
270
|
|
|
$stat['type'] = 'craft'; |
271
|
|
|
} |
272
|
|
|
if (empty($stat['craftTotalMs'])) { |
273
|
|
|
$stat['type'] = 'frontend'; |
274
|
|
|
} |
275
|
|
|
if ($stat['pageLoad'] > $maxTotalPageLoad) { |
276
|
|
|
$maxTotalPageLoad = (int)$stat['pageLoad']; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
// Massage the stats |
280
|
|
|
foreach ($stats as &$stat) { |
281
|
|
|
if (!empty($stats['dateCreated'])) { |
282
|
|
|
$date = DateTimeHelper::toDateTime($stats['dateCreated']); |
283
|
|
|
$stats['dateCreated'] = $date->format('Y-m-d H:i:s'); |
284
|
|
|
} |
285
|
|
|
$stat['mobile'] = (bool)$stat['mobile']; |
286
|
|
|
$stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; |
287
|
|
|
// Decode any emojis in the title |
288
|
|
|
if (!empty($stat['title'])) { |
289
|
|
|
$stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
290
|
|
|
} |
291
|
|
|
$stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-sample-by-id', [ |
|
|
|
|
292
|
|
|
'id' => $stat['id'] |
293
|
|
|
]); |
|
|
|
|
294
|
|
|
// Override based on permissions |
295
|
|
|
if (!$user->can('webperf:delete-data-samples')) { |
296
|
|
|
$stat['deleteLink'] = ''; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
// Format the data for the API |
300
|
|
|
$data['data'] = $stats; |
301
|
|
|
$query = (new Query()) |
302
|
|
|
->from(['{{%webperf_data_samples}}']) |
303
|
|
|
->where(['url' => $pageUrl]) |
304
|
|
|
->andWhere(['between', 'dateCreated', $start, $end]) |
|
|
|
|
305
|
|
|
; |
306
|
|
|
if ($filter !== '') { |
307
|
|
|
$query |
308
|
|
|
->andWhere(['like', 'device', $filter]) |
309
|
|
|
->orWhere(['like', 'os', $filter]) |
310
|
|
|
->orWhere(['like', 'browser', $filter]) |
311
|
|
|
->orWhere(['like', 'countryCode', $filter]) |
|
|
|
|
312
|
|
|
; |
313
|
|
|
} |
314
|
|
|
$count = $query->count(); |
315
|
|
|
$data['links']['pagination'] = [ |
316
|
|
|
'total' => $count, |
317
|
|
|
'per_page' => $per_page, |
318
|
|
|
'current_page' => $page, |
319
|
|
|
'last_page' => ceil($count / $per_page), |
320
|
|
|
'next_page_url' => null, |
321
|
|
|
'prev_page_url' => null, |
322
|
|
|
'from' => $offset + 1, |
323
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
324
|
|
|
]; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $this->asJson($data); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// Protected Methods |
331
|
|
|
// ========================================================================= |
332
|
|
|
} |
333
|
|
|
|