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) 2018 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\controllers; |
12
|
|
|
|
13
|
|
|
use Craft; |
|
|
|
|
14
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
15
|
|
|
|
16
|
|
|
use craft\db\Query; |
|
|
|
|
17
|
|
|
use craft\helpers\ArrayHelper; |
|
|
|
|
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 ChartsController extends Controller |
29
|
|
|
{ |
30
|
|
|
// Constants |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
// Protected Properties |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @var bool|array |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
protected $allowAnonymous = [ |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
// Public Methods |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The Dashboard stats average chart |
47
|
|
|
* |
48
|
|
|
* @param int $days |
|
|
|
|
49
|
|
|
* @param string $column |
|
|
|
|
50
|
|
|
* @param int $siteId |
|
|
|
|
51
|
|
|
* |
52
|
|
|
* @return Response |
53
|
|
|
* @throws ForbiddenHttpException |
54
|
|
|
*/ |
55
|
|
|
public function actionDashboardStatsAverage( |
56
|
|
|
int $days = 30, |
57
|
|
|
string $column = 'pageLoad', |
58
|
|
|
int $siteId = 0 |
59
|
|
|
): Response { |
60
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
61
|
|
|
$data = []; |
62
|
|
|
// Different dbs do it different ways |
63
|
|
|
$stats = null; |
64
|
|
|
$db = Craft::$app->getDb(); |
65
|
|
|
if ($db->getIsMysql()) { |
66
|
|
|
// Query the db |
67
|
|
|
$query = (new Query()) |
68
|
|
|
->from('{{%webperf_data_samples}}') |
69
|
|
|
->select([ |
|
|
|
|
70
|
|
|
'AVG('.$column.') AS avg', |
71
|
|
|
]) |
|
|
|
|
72
|
|
|
->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
73
|
|
|
if ((int)$siteId !== 0) { |
74
|
|
|
$query->andWhere(['siteId' => $siteId]); |
75
|
|
|
} |
76
|
|
|
$stats = $query->all(); |
77
|
|
|
} |
78
|
|
|
if ($db->getIsPgsql()) { |
79
|
|
|
// Query the db |
80
|
|
|
$query = (new Query()) |
81
|
|
|
->from('{{%webperf_data_samples}}') |
82
|
|
|
->select([ |
|
|
|
|
83
|
|
|
'AVG("'.$column.'") AS avg', |
84
|
|
|
]) |
|
|
|
|
85
|
|
|
->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
86
|
|
|
if ((int)$siteId !== 0) { |
87
|
|
|
$query->andWhere(['siteId' => $siteId]); |
88
|
|
|
} |
89
|
|
|
$stats = $query->all(); |
90
|
|
|
} |
91
|
|
|
if ($stats) { |
92
|
|
|
$data = ArrayHelper::getColumn($stats, 'avg'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->asJson($data); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* The Dashboard stats slowest pages list |
100
|
|
|
* |
101
|
|
|
* @param int $days |
|
|
|
|
102
|
|
|
* @param string $column |
|
|
|
|
103
|
|
|
* @param int $limit |
|
|
|
|
104
|
|
|
* @param int $siteId |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @return Response |
107
|
|
|
* @throws ForbiddenHttpException |
108
|
|
|
*/ |
109
|
|
|
public function actionDashboardSlowestPages( |
110
|
|
|
int $days = 30, |
111
|
|
|
string $column = 'pageLoad', |
112
|
|
|
int $limit = 3, |
113
|
|
|
int $siteId = 0 |
114
|
|
|
): Response { |
115
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
116
|
|
|
$data = []; |
117
|
|
|
// Different dbs do it different ways |
118
|
|
|
$stats = null; |
119
|
|
|
$db = Craft::$app->getDb(); |
120
|
|
|
if ($db->getIsMysql()) { |
121
|
|
|
// Query the db |
122
|
|
|
$query = (new Query()) |
123
|
|
|
->from('{{%webperf_data_samples}}') |
124
|
|
|
->select([ |
|
|
|
|
125
|
|
|
'url', |
126
|
|
|
'title', |
127
|
|
|
'AVG('.$column.') AS avg', |
128
|
|
|
]) |
|
|
|
|
129
|
|
|
->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
130
|
|
|
if ((int)$siteId !== 0) { |
131
|
|
|
$query->andWhere(['siteId' => $siteId]); |
132
|
|
|
} |
133
|
|
|
$query |
134
|
|
|
->orderBy('avg DESC') |
135
|
|
|
->groupBy('url') |
136
|
|
|
->limit($limit); |
137
|
|
|
$stats = $query->all(); |
138
|
|
|
} |
139
|
|
|
if ($db->getIsPgsql()) { |
140
|
|
|
// Query the db |
141
|
|
|
$query = (new Query()) |
142
|
|
|
->from('{{%webperf_data_samples}}') |
143
|
|
|
->select([ |
|
|
|
|
144
|
|
|
'url', |
145
|
|
|
'title', |
146
|
|
|
'AVG("'.$column.'") AS avg', |
147
|
|
|
]) |
|
|
|
|
148
|
|
|
->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
149
|
|
|
if ((int)$siteId !== 0) { |
150
|
|
|
$query->andWhere(['siteId' => $siteId]); |
151
|
|
|
$query |
152
|
|
|
->orderBy('avg DESC') |
153
|
|
|
->groupBy('url') |
154
|
|
|
->limit($limit); |
155
|
|
|
} |
156
|
|
|
$stats = $query->all(); |
157
|
|
|
} |
158
|
|
|
if ($stats) { |
159
|
|
|
// Decode any emojis in the title |
160
|
|
|
foreach ($stats as &$stat) { |
161
|
|
|
if (!empty($stat['title'])) { |
162
|
|
|
$stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
$data = $stats; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->asJson($data); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* The Dashboard chart |
173
|
|
|
* |
174
|
|
|
* @param int $days |
|
|
|
|
175
|
|
|
* |
176
|
|
|
* @return Response |
177
|
|
|
*/ |
178
|
|
|
public function actionWidget($days = 1): Response |
179
|
|
|
{ |
180
|
|
|
$data = []; |
181
|
|
|
// Different dbs do it different ways |
182
|
|
|
$stats = null; |
183
|
|
|
$handledStats = null; |
184
|
|
|
$db = Craft::$app->getDb(); |
185
|
|
|
if ($db->getIsMysql()) { |
186
|
|
|
// Query the db |
187
|
|
|
$stats = (new Query()) |
188
|
|
|
->from('{{%retour_stats}}') |
189
|
|
|
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )") |
190
|
|
|
->count(); |
191
|
|
|
$handledStats = (new Query()) |
192
|
|
|
->from('{{%retour_stats}}') |
193
|
|
|
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )") |
194
|
|
|
->andWhere('handledByRetour is TRUE') |
195
|
|
|
->count(); |
196
|
|
|
} |
197
|
|
|
if ($db->getIsPgsql()) { |
198
|
|
|
// Query the db |
199
|
|
|
$stats = (new Query()) |
200
|
|
|
->from('{{%retour_stats}}') |
201
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )") |
202
|
|
|
->count(); |
203
|
|
|
$handledStats = (new Query()) |
204
|
|
|
->from('{{%retour_stats}}') |
205
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )") |
206
|
|
|
->andWhere('"handledByRetour" = TRUE') |
207
|
|
|
->count(); |
208
|
|
|
} |
209
|
|
|
if ($stats) { |
210
|
|
|
$data = [ |
211
|
|
|
(int)$stats, |
212
|
|
|
(int)$handledStats, |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->asJson($data); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// Protected Methods |
220
|
|
|
// ========================================================================= |
221
|
|
|
} |
222
|
|
|
|