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