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\models; |
12
|
|
|
|
13
|
|
|
use nystudio107\webperf\validators\DbStringValidator; |
14
|
|
|
|
15
|
|
|
use yii\behaviors\AttributeTypecastBehavior; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @author nystudio107 |
|
|
|
|
19
|
|
|
* @package Webperf |
|
|
|
|
20
|
|
|
* @since 1.0.0 |
|
|
|
|
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
class DataSample extends DbModel |
23
|
|
|
{ |
24
|
|
|
// Constants |
25
|
|
|
// ========================================================================= |
26
|
|
|
|
27
|
|
|
const SCENARIO_BOOMERANG_BEACON = 'boomerang-beacon'; |
28
|
|
|
const SCENARIO_CRAFT_BEACON = 'craft-beacon'; |
29
|
|
|
const BOOMERANG_BEACON_FIELDS = [ |
30
|
|
|
'requestId', |
31
|
|
|
'siteId', |
32
|
|
|
'title', |
33
|
|
|
'url', |
34
|
|
|
'dns', |
35
|
|
|
'connect', |
36
|
|
|
'firstByte', |
37
|
|
|
'firstPaint', |
38
|
|
|
'firstContentfulPaint', |
39
|
|
|
'domInteractive', |
40
|
|
|
'pageLoad', |
41
|
|
|
'countryCode', |
42
|
|
|
'device', |
43
|
|
|
'browser', |
44
|
|
|
'os', |
45
|
|
|
'mobile', |
46
|
|
|
]; |
47
|
|
|
const CRAFT_BEACON_FIELDS = [ |
48
|
|
|
'requestId', |
49
|
|
|
'url', |
50
|
|
|
'craftTotalMs', |
51
|
|
|
'craftDbMs', |
52
|
|
|
'craftDbCnt', |
53
|
|
|
'craftTwigMs', |
54
|
|
|
'craftTwigCnt', |
55
|
|
|
'craftOtherMs', |
56
|
|
|
'craftOtherCnt', |
57
|
|
|
'craftTotalMemory', |
58
|
|
|
]; |
59
|
|
|
const PLACEHOLDER_URL = 'webperf-craft-placeholder'; |
60
|
|
|
|
61
|
|
|
// Public Properties |
62
|
|
|
// ========================================================================= |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @var int |
66
|
|
|
*/ |
67
|
|
|
public $requestId; |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
public $siteId; |
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* @var string the title of the document (if any) |
76
|
|
|
*/ |
77
|
|
|
public $title; |
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* @var string u - the URL of the User Timing sample |
81
|
|
|
*/ |
82
|
|
|
public $url; |
83
|
|
|
|
84
|
|
|
/** |
|
|
|
|
85
|
|
|
* @var int (nt_dns_end - nt_dns_st) in ms |
86
|
|
|
*/ |
87
|
|
|
public $dns; |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @var int (nt_con_end - nt_con_st) in ms |
91
|
|
|
*/ |
92
|
|
|
public $connect; |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @var int t_resp in ms |
96
|
|
|
*/ |
97
|
|
|
public $firstByte; |
98
|
|
|
|
99
|
|
|
/** |
|
|
|
|
100
|
|
|
* @var int pt_fp in ms |
101
|
|
|
*/ |
102
|
|
|
public $firstPaint; |
103
|
|
|
|
104
|
|
|
/** |
|
|
|
|
105
|
|
|
* @var int pt_fcp in ms |
106
|
|
|
*/ |
107
|
|
|
public $firstContentfulPaint; |
108
|
|
|
|
109
|
|
|
/** |
|
|
|
|
110
|
|
|
* @var int (nt_domint - nt_nav_st) in ms |
111
|
|
|
*/ |
112
|
|
|
public $domInteractive; |
113
|
|
|
|
114
|
|
|
/** |
|
|
|
|
115
|
|
|
* @var int (nt_domcomp - nt_nav_st) or t_done in ms |
116
|
|
|
*/ |
117
|
|
|
public $pageLoad; |
118
|
|
|
|
119
|
|
|
/** |
|
|
|
|
120
|
|
|
* @var string the country code from the IP address |
121
|
|
|
*/ |
122
|
|
|
public $countryCode; |
123
|
|
|
|
124
|
|
|
/** |
|
|
|
|
125
|
|
|
* @var string the device name |
126
|
|
|
*/ |
127
|
|
|
public $device; |
128
|
|
|
|
129
|
|
|
/** |
|
|
|
|
130
|
|
|
* @var string the browser name |
131
|
|
|
*/ |
132
|
|
|
public $browser; |
133
|
|
|
|
134
|
|
|
/** |
|
|
|
|
135
|
|
|
* @var string the operating system |
136
|
|
|
*/ |
137
|
|
|
public $os; |
138
|
|
|
|
139
|
|
|
/** |
|
|
|
|
140
|
|
|
* @var bool mobile or non-mobile |
141
|
|
|
*/ |
142
|
|
|
public $mobile; |
143
|
|
|
|
144
|
|
|
/** |
|
|
|
|
145
|
|
|
* @var int |
146
|
|
|
*/ |
147
|
|
|
public $craftTotalMs; |
148
|
|
|
|
149
|
|
|
/** |
|
|
|
|
150
|
|
|
* @var int |
151
|
|
|
*/ |
152
|
|
|
public $craftDbMs; |
153
|
|
|
|
154
|
|
|
/** |
|
|
|
|
155
|
|
|
* @var int |
156
|
|
|
*/ |
157
|
|
|
public $craftDbCnt; |
158
|
|
|
|
159
|
|
|
/** |
|
|
|
|
160
|
|
|
* @var int |
161
|
|
|
*/ |
162
|
|
|
public $craftTwigMs; |
163
|
|
|
|
164
|
|
|
/** |
|
|
|
|
165
|
|
|
* @var int |
166
|
|
|
*/ |
167
|
|
|
public $craftTwigCnt; |
168
|
|
|
|
169
|
|
|
/** |
|
|
|
|
170
|
|
|
* @var int |
171
|
|
|
*/ |
172
|
|
|
public $craftOtherMs; |
173
|
|
|
|
174
|
|
|
/** |
|
|
|
|
175
|
|
|
* @var int |
176
|
|
|
*/ |
177
|
|
|
public $craftOtherCnt; |
178
|
|
|
|
179
|
|
|
/** |
|
|
|
|
180
|
|
|
* @var int |
181
|
|
|
*/ |
182
|
|
|
public $craftTotalMemory; |
183
|
|
|
|
184
|
|
|
// Public Methods |
185
|
|
|
// ========================================================================= |
186
|
|
|
|
187
|
|
|
/** |
|
|
|
|
188
|
|
|
* @inheritdoc |
189
|
|
|
*/ |
|
|
|
|
190
|
|
|
public function rules() |
191
|
|
|
{ |
192
|
|
|
return [ |
193
|
|
|
['requestId', 'integer'], |
194
|
|
|
['siteId', 'integer'], |
195
|
|
|
['url', 'required'], |
196
|
|
|
['title', DbStringValidator::class, 'max' => 120], |
197
|
|
|
['url', DbStringValidator::class, 'max' => 255], |
198
|
|
|
['countryCode', DbStringValidator::class, 'max' => 2], |
199
|
|
|
['device', DbStringValidator::class, 'max' => 50], |
200
|
|
|
['browser', DbStringValidator::class, 'max' => 50], |
201
|
|
|
['os', DbStringValidator::class, 'max' => 50], |
202
|
|
|
[ |
203
|
|
|
[ |
204
|
|
|
'title', |
205
|
|
|
'url', |
206
|
|
|
'countryCode', |
207
|
|
|
'device', |
208
|
|
|
'browser', |
209
|
|
|
'os', |
210
|
|
|
], |
211
|
|
|
'string' |
212
|
|
|
], |
213
|
|
|
[ |
214
|
|
|
[ |
215
|
|
|
'dns', |
216
|
|
|
'connect', |
217
|
|
|
'firstByte', |
218
|
|
|
'firstPaint', |
219
|
|
|
'firstContentfulPaint', |
220
|
|
|
'domInteractive', |
221
|
|
|
'pageLoad', |
222
|
|
|
'craftTotalMs', |
223
|
|
|
'craftDbMs', |
224
|
|
|
'craftDbCnt', |
225
|
|
|
'craftTwigMs', |
226
|
|
|
'craftTwigCnt', |
227
|
|
|
'craftOtherMs', |
228
|
|
|
'craftOtherCnt', |
229
|
|
|
'craftTotalMemory', |
230
|
|
|
], |
231
|
|
|
'integer' |
232
|
|
|
], |
233
|
|
|
['mobile', 'boolean'], |
234
|
|
|
]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
|
|
|
|
238
|
|
|
* @inheritdoc |
239
|
|
|
*/ |
|
|
|
|
240
|
|
|
public function fields() |
241
|
|
|
{ |
242
|
|
|
$fields = parent::fields(); |
243
|
|
|
switch ($this->scenario) { |
244
|
|
|
case self::SCENARIO_BOOMERANG_BEACON: |
|
|
|
|
245
|
|
|
$fields = self::BOOMERANG_BEACON_FIELDS; |
246
|
|
|
break; |
247
|
|
|
case self::SCENARIO_CRAFT_BEACON: |
|
|
|
|
248
|
|
|
$fields = self::CRAFT_BEACON_FIELDS; |
249
|
|
|
break; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $fields; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
|
|
|
|
256
|
|
|
* @inheritdoc |
257
|
|
|
*/ |
|
|
|
|
258
|
|
|
public function scenarios() |
259
|
|
|
{ |
260
|
|
|
return [ |
261
|
|
|
self::SCENARIO_BOOMERANG_BEACON => self::BOOMERANG_BEACON_FIELDS, |
262
|
|
|
self::SCENARIO_CRAFT_BEACON => self::CRAFT_BEACON_FIELDS, |
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
|
|
|
|