|
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
|
|
|
// Query the db table |
|
100
|
|
|
$query = (new Query()) |
|
101
|
|
|
->select([ |
|
|
|
|
|
|
102
|
|
|
'COUNT([[url]]) AS cnt', |
|
103
|
|
|
|
|
104
|
|
|
'AVG([[pageLoad]]) AS pageLoad', |
|
105
|
|
|
'AVG([[domInteractive]]) AS domInteractive', |
|
106
|
|
|
'AVG([[firstContentfulPaint]]) AS firstContentfulPaint', |
|
107
|
|
|
'AVG([[firstPaint]]) AS firstPaint', |
|
108
|
|
|
'AVG([[firstByte]]) AS firstByte', |
|
109
|
|
|
'AVG([[connect]]) AS connect', |
|
110
|
|
|
'AVG([[dns]]) AS dns', |
|
111
|
|
|
|
|
112
|
|
|
'AVG([[craftTotalMs]]) AS craftTotalMs', |
|
113
|
|
|
'AVG([[craftDbMs]]) AS craftDbMs', |
|
114
|
|
|
'AVG([[craftDbCnt]]) AS craftDbCnt', |
|
115
|
|
|
'AVG([[craftTwigMs]]) AS craftTwigMs', |
|
116
|
|
|
'AVG([[craftTwigCnt]]) AS craftTwigCnt', |
|
117
|
|
|
'AVG([[craftOtherMs]]) AS craftOtherMs', |
|
118
|
|
|
'AVG([[craftOtherCnt]]) AS craftOtherCnt', |
|
119
|
|
|
'AVG([[craftTotalMemory]]) AS craftTotalMemory', |
|
120
|
|
|
]) |
|
|
|
|
|
|
121
|
|
|
->from(['{{%webperf_data_samples}}']) |
|
122
|
|
|
->where(['between', 'dateCreated', $start, $end]); |
|
123
|
|
|
if (!empty($pageUrl)) { |
|
124
|
|
|
$query->andWhere(['url' => $pageUrl]); |
|
125
|
|
|
} |
|
126
|
|
|
if ((int)$siteId !== 0) { |
|
127
|
|
|
$query->andWhere(['siteId' => $siteId]); |
|
128
|
|
|
} |
|
129
|
|
|
$stats = $query->all(); |
|
130
|
|
|
if ($stats) { |
|
131
|
|
|
$data = $stats[0]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $data; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|