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\models; |
12
|
|
|
|
13
|
|
|
use Craft; |
|
|
|
|
14
|
|
|
use craft\base\Model; |
|
|
|
|
15
|
|
|
use craft\validators\ArrayValidator; |
|
|
|
|
16
|
|
|
use craft\validators\ColorValidator; |
|
|
|
|
17
|
|
|
|
18
|
|
|
use yii\behaviors\AttributeTypecastBehavior; |
|
|
|
|
19
|
|
|
|
20
|
|
|
use putyourlightson\blitz\Blitz; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Webperf |
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class Settings extends Model |
28
|
|
|
{ |
29
|
|
|
// Constants |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
const BLITZ_PLUGIN_HANDLE = 'blitz'; |
33
|
|
|
|
34
|
|
|
// Public Properties |
35
|
|
|
// ========================================================================= |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var string The public-facing name of the plugin |
39
|
|
|
*/ |
40
|
|
|
public $pluginName = 'Webperf'; |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var bool Whether or not to include the beacon on the page |
44
|
|
|
*/ |
45
|
|
|
public $includeBeacon = true; |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @var bool Whether or not to include the Craft profiling of pages |
49
|
|
|
*/ |
50
|
|
|
public $includeCraftProfiling = true; |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @var bool If the site is static cached, turn this option on to prevent Webperf from generating a unique beacon token |
54
|
|
|
*/ |
55
|
|
|
public $staticCachedSite = false; |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @var int The number of data samples to store |
59
|
|
|
*/ |
60
|
|
|
public $dataSamplesStoredLimit = 100000; |
61
|
|
|
|
62
|
|
|
/** |
|
|
|
|
63
|
|
|
* @var bool Whether the DataSamples should be trimmed after each new DataSample is added |
64
|
|
|
*/ |
65
|
|
|
public $automaticallyTrimDataSamples = true; |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @var bool Whether outlier data samples that are 10x the mean should be deleted |
69
|
|
|
*/ |
70
|
|
|
public $trimOutlierDataSamples = true; |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @var int The number of milliseconds required between recording of frontend beacon data samples |
74
|
|
|
*/ |
75
|
|
|
public $rateLimitMs = 500; |
76
|
|
|
|
77
|
|
|
/** |
|
|
|
|
78
|
|
|
* @var string API Key for WebPageTest.org |
79
|
|
|
*/ |
80
|
|
|
public $webpageTestApiKey = ''; |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @var array [Regular expressions](https://regexr.com/) to match URLs to exclude from tracking |
84
|
|
|
*/ |
85
|
|
|
public $excludePatterns = [ |
86
|
|
|
0 => [ |
87
|
|
|
'pattern' => '/webperf/.*', |
88
|
|
|
], |
89
|
|
|
1 => [ |
90
|
|
|
'pattern' => '/cpresources/.*', |
91
|
|
|
] |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @var bool Whether Craft `warning` messages should be recorded in addition to `error` messages |
96
|
|
|
*/ |
97
|
|
|
public $includeCraftWarnings = false; |
98
|
|
|
|
99
|
|
|
/** |
|
|
|
|
100
|
|
|
* @var int The number of error samples to store |
101
|
|
|
*/ |
102
|
|
|
public $errorSamplesStoredLimit = 1000; |
103
|
|
|
|
104
|
|
|
/** |
|
|
|
|
105
|
|
|
* @var bool Whether the ErrorSamples should be trimmed after each new ErrorSample is added |
106
|
|
|
*/ |
107
|
|
|
public $automaticallyTrimErrorSamples = true; |
108
|
|
|
|
109
|
|
|
/** |
|
|
|
|
110
|
|
|
* @var bool Whether to filter bot user agents from generating profile hits or not |
111
|
|
|
* NOT visible in the GUI currently |
112
|
|
|
*/ |
113
|
|
|
public $filterBotUserAgents = true; |
114
|
|
|
|
115
|
|
|
/** |
|
|
|
|
116
|
|
|
* @var bool Whether the performance summary sidebar should be shown on entry, category, and product pages |
117
|
|
|
*/ |
118
|
|
|
public $displaySidebar = true; |
119
|
|
|
|
120
|
|
|
/** |
|
|
|
|
121
|
|
|
* @var string The dashboard 'fast' color for charts |
122
|
|
|
*/ |
123
|
|
|
public $dashboardFastColor = '#00C800'; |
124
|
|
|
|
125
|
|
|
/** |
|
|
|
|
126
|
|
|
* @var string The dashboard 'average' color for charts |
127
|
|
|
*/ |
128
|
|
|
public $dashboardAverageColor = '#FFFF00'; |
129
|
|
|
|
130
|
|
|
/** |
|
|
|
|
131
|
|
|
* @var string The dashboard 'slow' color for charts |
132
|
|
|
*/ |
133
|
|
|
public $dashboardSlowColor = '#C80000'; |
134
|
|
|
|
135
|
|
|
// Threshold levels |
136
|
|
|
// ========================================================================= |
137
|
|
|
|
138
|
|
|
/** |
|
|
|
|
139
|
|
|
* @var int Threshold in seconds for the dns metric, beyond which it will be considered slow |
140
|
|
|
*/ |
141
|
|
|
public $dnsThreshold = 0.5; |
142
|
|
|
|
143
|
|
|
/** |
|
|
|
|
144
|
|
|
* @var int Threshold in seconds for the connect metric, beyond which it will be considered slow |
145
|
|
|
*/ |
146
|
|
|
public $connectThreshold = 0.5; |
147
|
|
|
|
148
|
|
|
/** |
|
|
|
|
149
|
|
|
* @var int Threshold in seconds for the first byte metric, beyond which it will be considered slow |
150
|
|
|
*/ |
151
|
|
|
public $firstByteThreshold = 2.0; |
152
|
|
|
|
153
|
|
|
/** |
|
|
|
|
154
|
|
|
* @var int Threshold in seconds for the first paint metric, beyond which it will be considered slow |
155
|
|
|
*/ |
156
|
|
|
public $firstPaintThreshold = 5.0; |
157
|
|
|
|
158
|
|
|
/** |
|
|
|
|
159
|
|
|
* @var int Threshold in seconds for the first contentful paint metric, beyond which it will be considered slow |
160
|
|
|
*/ |
161
|
|
|
public $firstContentfulPaintThreshold = 5.0; |
162
|
|
|
|
163
|
|
|
/** |
|
|
|
|
164
|
|
|
* @var int Threshold in seconds for the DOM interactive metric, beyond which it will be considered slow |
165
|
|
|
*/ |
166
|
|
|
public $domInteractiveThreshold = 5.0; |
167
|
|
|
|
168
|
|
|
/** |
|
|
|
|
169
|
|
|
* @var int Threshold in seconds for the page load metric, beyond which it will be considered slow |
170
|
|
|
*/ |
171
|
|
|
public $pageLoadThreshold = 10.0; |
172
|
|
|
|
173
|
|
|
/** |
|
|
|
|
174
|
|
|
* @var int Threshold in seconds for the Craft execution metric, beyond which it will be considered slow |
175
|
|
|
*/ |
176
|
|
|
public $craftTotalMsThreshold = 2.0; |
177
|
|
|
|
178
|
|
|
/** |
|
|
|
|
179
|
|
|
* @var int Threshold in seconds for the database queries metric, beyond which it will be considered slow |
180
|
|
|
*/ |
181
|
|
|
public $craftDbMsThreshold = 2.0; |
182
|
|
|
|
183
|
|
|
/** |
|
|
|
|
184
|
|
|
* @var int Threshold in seconds for the Twig rendering metric, beyond which it will be considered slow |
185
|
|
|
*/ |
186
|
|
|
public $craftTwigMsThreshold = 2.0; |
187
|
|
|
|
188
|
|
|
/** |
|
|
|
|
189
|
|
|
* @var int Threshold in seconds for the Craft other metric, beyond which it will be considered slow |
190
|
|
|
*/ |
191
|
|
|
public $craftOtherMsThreshold = 2.0; |
192
|
|
|
|
193
|
|
|
// Public Methods |
194
|
|
|
// ========================================================================= |
195
|
|
|
|
196
|
|
|
/** |
|
|
|
|
197
|
|
|
* @inheritdoc |
198
|
|
|
*/ |
|
|
|
|
199
|
|
|
public function init() |
200
|
|
|
{ |
201
|
|
|
parent::init(); |
202
|
|
|
// If Blitz is installed & enabled, flip the $staticCachedSite on |
203
|
|
|
$blitz = Craft::$app->getPlugins()->getPlugin(self::BLITZ_PLUGIN_HANDLE); |
204
|
|
|
if ($blitz && Blitz::$plugin->getSettings()->cachingEnabled) { |
205
|
|
|
$this->staticCachedSite = true; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
|
|
|
|
210
|
|
|
* @inheritdoc |
211
|
|
|
*/ |
|
|
|
|
212
|
|
|
public function rules() |
213
|
|
|
{ |
214
|
|
|
return [ |
215
|
|
|
['pluginName', 'string'], |
216
|
|
|
['pluginName', 'default', 'value' => 'Webperf'], |
217
|
|
|
['includeBeacon', 'boolean'], |
218
|
|
|
['includeCraftProfiling', 'boolean'], |
219
|
|
|
['staticCachedSite', 'boolean'], |
220
|
|
|
['dataSamplesStoredLimit', 'integer'], |
221
|
|
|
['dataSamplesStoredLimit', 'default', 'value' => 100000], |
222
|
|
|
['automaticallyTrimDataSamples', 'boolean'], |
223
|
|
|
['trimOutlierDataSamples', 'boolean'], |
224
|
|
|
['rateLimitMs', 'integer'], |
225
|
|
|
['rateLimitMs', 'default', 'value' => 500], |
226
|
|
|
['webpageTestApiKey', 'string'], |
227
|
|
|
['excludePatterns', ArrayValidator::class], |
228
|
|
|
['includeCraftWarnings', 'boolean'], |
229
|
|
|
['errorSamplesStoredLimit', 'integer'], |
230
|
|
|
['errorSamplesStoredLimit', 'default', 'value' => 1000], |
231
|
|
|
['automaticallyTrimErrorSamples', 'boolean'], |
232
|
|
|
['filterBotUserAgents', 'boolean'], |
233
|
|
|
['displaySidebar', 'boolean'], |
234
|
|
|
['dashboardFastColor', 'default', 'value' => '#00C800'], |
235
|
|
|
['dashboardAverageColor', 'default', 'value' => '#FFA500'], |
236
|
|
|
['dashboardSlowColor', 'default', 'value' => '#C80000'], |
237
|
|
|
[ |
238
|
|
|
[ |
239
|
|
|
'dashboardFastColor', |
240
|
|
|
'dashboardAverageColor', |
241
|
|
|
'dashboardSlowColor', |
242
|
|
|
], |
243
|
|
|
ColorValidator::class |
244
|
|
|
], |
245
|
|
|
[ |
246
|
|
|
[ |
247
|
|
|
'dnsThreshold', |
248
|
|
|
'connectThreshold', |
249
|
|
|
'firstByteThreshold', |
250
|
|
|
'firstPaintThreshold', |
251
|
|
|
'firstContentfulPaintThreshold', |
252
|
|
|
'domInteractiveThreshold', |
253
|
|
|
'pageLoadThreshold', |
254
|
|
|
'craftTotalMsThreshold', |
255
|
|
|
'craftDbMsThreshold', |
256
|
|
|
'craftTwigMsThreshold', |
257
|
|
|
'craftOtherMsThreshold', |
258
|
|
|
], |
259
|
|
|
'number', |
260
|
|
|
'min' => 0.1, |
261
|
|
|
'max' => 100, |
262
|
|
|
] |
263
|
|
|
]; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
|
|
|
|
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function behaviors() |
270
|
|
|
{ |
271
|
|
|
return [ |
272
|
|
|
'typecast' => [ |
273
|
|
|
'class' => AttributeTypecastBehavior::class, |
274
|
|
|
// 'attributeTypes' will be composed automatically according to `rules()` |
275
|
|
|
], |
276
|
|
|
]; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|