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\MemoryLimit; |
16
|
|
|
|
17
|
|
|
use Craft; |
|
|
|
|
18
|
|
|
use craft\base\Component; |
|
|
|
|
19
|
|
|
use craft\db\Query; |
|
|
|
|
20
|
|
|
|
21
|
|
|
use yii\helpers\Markdown; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
25
|
|
|
* @package Webperf |
|
|
|
|
26
|
|
|
* @since 1.0.0 |
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
28
|
|
|
class Recommendations extends Component |
29
|
|
|
{ |
30
|
|
|
// Constants |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
const RECOMMENDATIONS_LIST = [ |
34
|
|
|
MemoryLimit::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
// Public Methods |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Return a list of recommendations |
42
|
|
|
* |
43
|
|
|
* @param RecommendationDataSample $sample |
|
|
|
|
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function list(RecommendationDataSample $sample): array |
48
|
|
|
{ |
49
|
|
|
$data = []; |
50
|
|
|
foreach (self::RECOMMENDATIONS_LIST as $recClass) { |
51
|
|
|
/** @var Recommendation $rec */ |
|
|
|
|
52
|
|
|
$rec = new $recClass(['sample' => $sample]); |
53
|
|
|
if ($rec->recommendation()) { |
54
|
|
|
$data[] = [ |
55
|
|
|
'summary' => Markdown::processParagraph($rec->summary()), |
56
|
|
|
'detail' => Markdown::processParagraph($rec->detail()), |
57
|
|
|
'learnMoreLink' => $rec->learnMoreLink(), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return $data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return a list of recommendations |
66
|
|
|
* |
67
|
|
|
* @param string $pageUrl |
|
|
|
|
68
|
|
|
* @param string $start |
|
|
|
|
69
|
|
|
* @param string $end |
|
|
|
|
70
|
|
|
* @param int $siteId |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function data( |
75
|
|
|
$pageUrl = '', |
76
|
|
|
string $start = '', |
77
|
|
|
string $end = '', |
78
|
|
|
$siteId = 0 |
79
|
|
|
): array { |
80
|
|
|
$data = []; |
81
|
|
|
// Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
82
|
|
|
$end = date('Y-m-d', strtotime($end.'+1 day')); |
83
|
|
|
$pageUrl = urldecode($pageUrl); |
84
|
|
|
// Query the db table |
85
|
|
|
$query = (new Query()) |
86
|
|
|
->select([ |
|
|
|
|
87
|
|
|
'COUNT(url) AS cnt', |
88
|
|
|
|
89
|
|
|
'AVG(pageLoad) AS pageLoad', |
90
|
|
|
'AVG(domInteractive) AS domInteractive', |
91
|
|
|
'AVG(firstContentfulPaint) AS firstContentfulPaint', |
92
|
|
|
'AVG(firstPaint) AS firstPaint', |
93
|
|
|
'AVG(firstByte) AS firstByte', |
94
|
|
|
'AVG(connect) AS connect', |
95
|
|
|
'AVG(dns) AS dns', |
96
|
|
|
|
97
|
|
|
'AVG(craftTotalMs) AS craftTotalMs', |
98
|
|
|
'AVG(craftDbMs) AS craftDbMs', |
99
|
|
|
'AVG(craftDbCnt) AS craftDbCnt', |
100
|
|
|
'AVG(craftTwigMs) AS craftTwigMs', |
101
|
|
|
'AVG(craftTwigCnt) AS craftTwigCnt', |
102
|
|
|
'AVG(craftOtherMs) AS craftOtherMs', |
103
|
|
|
'AVG(craftOtherCnt) AS craftOtherCnt', |
104
|
|
|
'AVG(craftTotalMemory) AS craftTotalMemory', |
105
|
|
|
]) |
|
|
|
|
106
|
|
|
->from(['{{%webperf_data_samples}}']) |
107
|
|
|
->where(['between', 'dateCreated', $start, $end]) |
|
|
|
|
108
|
|
|
; |
109
|
|
|
if (!empty($pageUrl)) { |
110
|
|
|
$query->andWhere(['url' => $pageUrl]); |
111
|
|
|
} |
112
|
|
|
if ((int)$siteId !== 0) { |
113
|
|
|
$query->andWhere(['siteId' => $siteId]); |
114
|
|
|
} |
115
|
|
|
$stats = $query->all(); |
116
|
|
|
if ($stats) { |
117
|
|
|
$data = $stats[0]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $data; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|