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\services; |
12
|
|
|
|
13
|
|
|
use nystudio107\webperf\base\Recommendation; |
14
|
|
|
use nystudio107\webperf\models\RecommendationDataSample; |
15
|
|
|
use nystudio107\webperf\recommendations\CraftQueryCount; |
16
|
|
|
use nystudio107\webperf\recommendations\CraftQueryTime; |
17
|
|
|
use nystudio107\webperf\recommendations\CraftTotalTime; |
18
|
|
|
use nystudio107\webperf\recommendations\CraftTwigTime; |
19
|
|
|
use nystudio107\webperf\recommendations\DomInteractive; |
20
|
|
|
use nystudio107\webperf\recommendations\FirstByte; |
21
|
|
|
use nystudio107\webperf\recommendations\FirstContentfulPaint; |
22
|
|
|
use nystudio107\webperf\recommendations\MemoryLimit; |
23
|
|
|
|
24
|
|
|
use Craft; |
|
|
|
|
25
|
|
|
use craft\base\Component; |
|
|
|
|
26
|
|
|
use craft\db\Query; |
|
|
|
|
27
|
|
|
|
28
|
|
|
use yii\helpers\Markdown; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @author nystudio107 |
|
|
|
|
32
|
|
|
* @package Webperf |
|
|
|
|
33
|
|
|
* @since 1.0.0 |
|
|
|
|
34
|
|
|
*/ |
|
|
|
|
35
|
|
|
class Recommendations extends Component |
36
|
|
|
{ |
37
|
|
|
// Constants |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
const RECOMMENDATIONS_LIST = [ |
41
|
|
|
CraftQueryCount::class, |
42
|
|
|
CraftQueryTime::class, |
43
|
|
|
CraftTwigTime::class, |
44
|
|
|
CraftTotalTime::class, |
45
|
|
|
FirstByte::class, |
46
|
|
|
FirstContentfulPaint::class, |
47
|
|
|
DomInteractive::class, |
48
|
|
|
MemoryLimit::class, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
// Public Methods |
52
|
|
|
// ========================================================================= |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return a list of recommendations |
56
|
|
|
* |
57
|
|
|
* @param RecommendationDataSample $sample |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function list(RecommendationDataSample $sample): array |
62
|
|
|
{ |
63
|
|
|
$data = []; |
64
|
|
|
foreach (self::RECOMMENDATIONS_LIST as $recClass) { |
65
|
|
|
/** @var Recommendation $rec */ |
|
|
|
|
66
|
|
|
$rec = new $recClass(['sample' => $sample]); |
67
|
|
|
if ($rec->hasRecommendation) { |
68
|
|
|
$data[] = [ |
69
|
|
|
'summary' => Markdown::processParagraph($rec->summary), |
70
|
|
|
'detail' => Markdown::processParagraph($rec->detail), |
71
|
|
|
'learnMoreUrl' => $rec->learnMoreUrl, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return a list of recommendations |
80
|
|
|
* |
81
|
|
|
* @param string $pageUrl |
|
|
|
|
82
|
|
|
* @param string $start |
|
|
|
|
83
|
|
|
* @param string $end |
|
|
|
|
84
|
|
|
* @param int $siteId |
|
|
|
|
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function data( |
89
|
|
|
$pageUrl = '', |
90
|
|
|
string $start = '', |
91
|
|
|
string $end = '', |
92
|
|
|
$siteId = 0 |
93
|
|
|
): array { |
94
|
|
|
$data = []; |
95
|
|
|
$db = Craft::$app->getDb(); |
96
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
97
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
98
|
|
|
$pageUrl = urldecode($pageUrl); |
99
|
|
|
if ($db->getIsMysql()) { |
100
|
|
|
// Query the db table |
101
|
|
|
$query = (new Query()) |
102
|
|
|
->select([ |
|
|
|
|
103
|
|
|
'COUNT(url) AS cnt', |
104
|
|
|
|
105
|
|
|
'AVG(pageLoad) AS pageLoad', |
106
|
|
|
'AVG(domInteractive) AS domInteractive', |
107
|
|
|
'AVG(firstContentfulPaint) AS firstContentfulPaint', |
108
|
|
|
'AVG(firstPaint) AS firstPaint', |
109
|
|
|
'AVG(firstByte) AS firstByte', |
110
|
|
|
'AVG(connect) AS connect', |
111
|
|
|
'AVG(dns) AS dns', |
112
|
|
|
|
113
|
|
|
'AVG(craftTotalMs) AS craftTotalMs', |
114
|
|
|
'AVG(craftDbMs) AS craftDbMs', |
115
|
|
|
'AVG(craftDbCnt) AS craftDbCnt', |
116
|
|
|
'AVG(craftTwigMs) AS craftTwigMs', |
117
|
|
|
'AVG(craftTwigCnt) AS craftTwigCnt', |
118
|
|
|
'AVG(craftOtherMs) AS craftOtherMs', |
119
|
|
|
'AVG(craftOtherCnt) AS craftOtherCnt', |
120
|
|
|
'AVG(craftTotalMemory) AS craftTotalMemory', |
121
|
|
|
]) |
|
|
|
|
122
|
|
|
->from(['{{%webperf_data_samples}}']) |
123
|
|
|
->where(['between', 'dateCreated', $start, $end]); |
124
|
|
|
if (!empty($pageUrl)) { |
125
|
|
|
$query->andWhere(['url' => $pageUrl]); |
126
|
|
|
} |
127
|
|
|
if ((int)$siteId !== 0) { |
128
|
|
|
$query->andWhere(['siteId' => $siteId]); |
129
|
|
|
} |
130
|
|
|
$stats = $query->all(); |
131
|
|
|
} |
132
|
|
|
if ($db->getIsPgsql()) { |
133
|
|
|
// Query the db table |
134
|
|
|
$query = (new Query()) |
135
|
|
|
->select([ |
|
|
|
|
136
|
|
|
'COUNT("url") AS cnt', |
137
|
|
|
|
138
|
|
|
'AVG("pageLoad") AS pageLoad', |
139
|
|
|
'AVG("domInteractive") AS domInteractive', |
140
|
|
|
'AVG("firstContentfulPaint") AS firstContentfulPaint', |
141
|
|
|
'AVG("firstPaint") AS firstPaint', |
142
|
|
|
'AVG("firstByte") AS firstByte', |
143
|
|
|
'AVG("connect") AS connect', |
144
|
|
|
'AVG("dns") AS dns', |
145
|
|
|
|
146
|
|
|
'AVG("craftTotalMs") AS craftTotalMs', |
147
|
|
|
'AVG("craftDbMs") AS craftDbMs', |
148
|
|
|
'AVG("craftDbCnt") AS craftDbCnt', |
149
|
|
|
'AVG("craftTwigMs") AS craftTwigMs', |
150
|
|
|
'AVG("craftTwigCnt") AS craftTwigCnt', |
151
|
|
|
'AVG("craftOtherMs") AS craftOtherMs', |
152
|
|
|
'AVG("craftOtherCnt") AS craftOtherCnt', |
153
|
|
|
'AVG("craftTotalMemory") AS craftTotalMemory', |
154
|
|
|
]) |
|
|
|
|
155
|
|
|
->from(['{{%webperf_data_samples}}']) |
156
|
|
|
->where(['between', 'dateCreated', $start, $end]); |
157
|
|
|
if (!empty($pageUrl)) { |
158
|
|
|
$query->andWhere(['url' => $pageUrl]); |
159
|
|
|
} |
160
|
|
|
if ((int)$siteId !== 0) { |
161
|
|
|
$query->andWhere(['siteId' => $siteId]); |
162
|
|
|
} |
163
|
|
|
$stats = $query->all(); |
164
|
|
|
} |
165
|
|
|
if ($stats) { |
|
|
|
|
166
|
|
|
$data = $stats[0]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $data; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|