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\UrlHelper; |
|
|
|
|
18
|
|
|
use craft\web\Controller; |
|
|
|
|
19
|
|
|
|
20
|
|
|
use yii\web\ForbiddenHttpException; |
|
|
|
|
21
|
|
|
use yii\web\Response; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
25
|
|
|
* @package Webperf |
|
|
|
|
26
|
|
|
* @since 1.0.0 |
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
28
|
|
|
class RecommendationsController extends Controller |
29
|
|
|
{ |
30
|
|
|
// Constants |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
// Protected Properties |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @var bool|array |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
protected $allowAnonymous = []; |
40
|
|
|
|
41
|
|
|
// Public Methods |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Handle requests for the dashboard statistics table |
46
|
|
|
* |
47
|
|
|
* @param string $sort |
|
|
|
|
48
|
|
|
* @param int $page |
|
|
|
|
49
|
|
|
* @param int $per_page |
|
|
|
|
50
|
|
|
* @param string $filter |
|
|
|
|
51
|
|
|
* @param string $start |
|
|
|
|
52
|
|
|
* @param string $end |
|
|
|
|
53
|
|
|
* @param int $siteId |
|
|
|
|
54
|
|
|
* |
55
|
|
|
* @return Response |
56
|
|
|
* @throws ForbiddenHttpException |
57
|
|
|
*/ |
58
|
|
|
public function actionRecommendations( |
59
|
|
|
string $start = '', |
60
|
|
|
string $end = '', |
61
|
|
|
string $sort = 'pageLoad|DESC', |
62
|
|
|
int $page = 1, |
63
|
|
|
int $per_page = 20, |
64
|
|
|
$filter = '', |
65
|
|
|
$siteId = 0 |
66
|
|
|
): Response { |
67
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:pages'); |
68
|
|
|
$data = []; |
69
|
|
|
$sortField = 'pageLoad'; |
70
|
|
|
$sortType = 'DESC'; |
71
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
72
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
73
|
|
|
// Figure out the sorting type |
74
|
|
|
if ($sort !== '') { |
75
|
|
|
if (strpos($sort, '|') === false) { |
76
|
|
|
$sortField = $sort; |
77
|
|
|
} else { |
78
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
// Query the db table |
82
|
|
|
$offset = ($page - 1) * $per_page; |
83
|
|
|
$query = (new Query()) |
84
|
|
|
->select([ |
|
|
|
|
85
|
|
|
'url', |
86
|
|
|
'MIN(title) AS title', |
87
|
|
|
'COUNT(url) AS cnt', |
88
|
|
|
'AVG(pageLoad) AS pageLoad', |
89
|
|
|
'AVG(domInteractive) AS domInteractive', |
90
|
|
|
'AVG(firstContentfulPaint) AS firstContentfulPaint', |
91
|
|
|
'AVG(firstPaint) AS firstPaint', |
92
|
|
|
'AVG(firstByte) AS firstByte', |
93
|
|
|
'AVG(connect) AS connect', |
94
|
|
|
'AVG(dns) AS dns', |
95
|
|
|
'AVG(craftTotalMs) AS craftTotalMs', |
96
|
|
|
'AVG(craftDbCnt) AS craftDbCnt', |
97
|
|
|
'AVG(craftDbMs) AS craftDbMs', |
98
|
|
|
'AVG(craftTwigCnt) AS craftTwigCnt', |
99
|
|
|
'AVG(craftTwigMs) AS craftTwigMs', |
100
|
|
|
'AVG(craftTotalMemory) AS craftTotalMemory', |
101
|
|
|
]) |
|
|
|
|
102
|
|
|
->from(['{{%webperf_data_samples}}']) |
103
|
|
|
->offset($offset) |
104
|
|
|
->where(['between', 'dateUpdated', $start, $end]) |
|
|
|
|
105
|
|
|
; |
106
|
|
|
if ((int)$siteId !== 0) { |
107
|
|
|
$query->andWhere(['siteId' => $siteId]); |
108
|
|
|
} |
109
|
|
|
if ($filter !== '') { |
110
|
|
|
$query |
111
|
|
|
->andWhere(['like', 'url', $filter]) |
112
|
|
|
->orWhere(['like', 'title', $filter]) |
|
|
|
|
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
$query |
116
|
|
|
->orderBy("{$sortField} {$sortType}") |
117
|
|
|
->groupBy('url') |
118
|
|
|
->limit($per_page) |
|
|
|
|
119
|
|
|
; |
120
|
|
|
|
121
|
|
|
$stats = $query->all(); |
122
|
|
|
if ($stats) { |
123
|
|
|
$user = Craft::$app->getUser()->getIdentity(); |
124
|
|
|
// Compute the largest page load time |
125
|
|
|
$maxTotalPageLoad = 0; |
126
|
|
|
foreach ($stats as &$stat) { |
127
|
|
|
// Determine the stat type |
|
|
|
|
128
|
|
|
if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { |
129
|
|
|
$stat['type'] = 'both'; |
130
|
|
|
} |
131
|
|
|
if (empty($stat['firstByte'])) { |
132
|
|
|
$stat['type'] = 'craft'; |
133
|
|
|
} |
134
|
|
|
if (empty($stat['craftTotalMs'])) { |
135
|
|
|
$stat['type'] = 'frontend'; |
136
|
|
|
} |
137
|
|
|
if ($stat['pageLoad'] > $maxTotalPageLoad) { |
138
|
|
|
$maxTotalPageLoad = $stat['pageLoad']; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
// Massage the stats |
142
|
|
|
$index = 1; |
143
|
|
|
foreach ($stats as &$stat) { |
144
|
|
|
$stat['id'] = $index++; |
145
|
|
|
$stat['cnt'] = (int)$stat['cnt']; |
146
|
|
|
$stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; |
147
|
|
|
// Decode any emojis in the title |
148
|
|
|
if (!empty($stat['title'])) { |
149
|
|
|
$stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
150
|
|
|
} |
151
|
|
|
// Set up the appropriate helper links |
152
|
|
|
$stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-samples-by-url', [ |
|
|
|
|
153
|
|
|
'pageUrl' => $stat['url'], |
154
|
|
|
'siteId' => $siteId |
155
|
|
|
]); |
|
|
|
|
156
|
|
|
$stat['detailPageUrl'] = UrlHelper::cpUrl('webperf/page-detail', [ |
|
|
|
|
157
|
|
|
'pageUrl' => $stat['url'], |
158
|
|
|
'siteId' => $siteId, |
159
|
|
|
]); |
|
|
|
|
160
|
|
|
// Override based on permissions |
161
|
|
|
if (!$user->can('webperf:delete-data-samples')) { |
162
|
|
|
$stat['deleteLink'] = ''; |
163
|
|
|
} |
164
|
|
|
if (!$user->can('webperf:page-detail')) { |
165
|
|
|
$stat['detailPageUrl'] = ''; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
// Format the data for the API |
169
|
|
|
$data['data'] = $stats; |
170
|
|
|
$query = (new Query()) |
171
|
|
|
->from(['{{%webperf_data_samples}}']) |
172
|
|
|
->groupBy('url') |
173
|
|
|
->where(['between', 'dateUpdated', $start, $end]) |
|
|
|
|
174
|
|
|
; |
175
|
|
|
if ($filter !== '') { |
176
|
|
|
$query->andWhere(['like', 'url', $filter]); |
177
|
|
|
$query->orWhere(['like', 'title', $filter]); |
178
|
|
|
} |
179
|
|
|
$count = $query->count(); |
180
|
|
|
$data['links']['pagination'] = [ |
181
|
|
|
'total' => $count, |
182
|
|
|
'per_page' => $per_page, |
183
|
|
|
'current_page' => $page, |
184
|
|
|
'last_page' => ceil($count / $per_page), |
185
|
|
|
'next_page_url' => null, |
186
|
|
|
'prev_page_url' => null, |
187
|
|
|
'from' => $offset + 1, |
188
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->asJson($data); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Protected Methods |
196
|
|
|
// ========================================================================= |
197
|
|
|
} |
198
|
|
|
|