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\controllers; |
12
|
|
|
|
13
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
14
|
|
|
|
15
|
|
|
use Craft; |
|
|
|
|
16
|
|
|
use craft\db\Query; |
|
|
|
|
17
|
|
|
use craft\web\Controller; |
|
|
|
|
18
|
|
|
|
19
|
|
|
use League\Csv\CannotInsertRecord; |
|
|
|
|
20
|
|
|
use League\Csv\Writer; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Webperf |
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class FileController extends Controller |
28
|
|
|
{ |
29
|
|
|
// Constants |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
const EXPORT_DATA_SAMPLES_COLUMNS = [ |
33
|
|
|
'siteId', |
34
|
|
|
'title', |
35
|
|
|
'url', |
36
|
|
|
'queryString', |
37
|
|
|
'dns', |
38
|
|
|
'connect', |
39
|
|
|
'firstByte', |
40
|
|
|
'firstPaint', |
41
|
|
|
'firstContentfulPaint', |
42
|
|
|
'domInteractive', |
43
|
|
|
'pageLoad', |
44
|
|
|
'countryCode', |
45
|
|
|
'device', |
46
|
|
|
'browser', |
47
|
|
|
'os', |
48
|
|
|
'mobile', |
49
|
|
|
'craftTotalMs', |
50
|
|
|
'craftDbMs', |
51
|
|
|
'craftDbCnt', |
52
|
|
|
'craftTwigMs', |
53
|
|
|
'craftTwigCnt', |
54
|
|
|
'craftOtherMs', |
55
|
|
|
'craftOtherCnt', |
56
|
|
|
'craftTotalMemory', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
const EXPORT_ERROR_SAMPLES_COLUMNS = [ |
60
|
|
|
'siteId', |
61
|
|
|
'title', |
62
|
|
|
'url', |
63
|
|
|
'queryString', |
64
|
|
|
'type', |
65
|
|
|
'pageErrors', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
// Protected Properties |
69
|
|
|
// ========================================================================= |
70
|
|
|
|
71
|
|
|
protected $allowAnonymous = []; |
72
|
|
|
|
73
|
|
|
// Public Methods |
74
|
|
|
// ========================================================================= |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Export the data samples table as a CSV file |
78
|
|
|
* |
79
|
|
|
* @param string $pageUrl |
|
|
|
|
80
|
|
|
* @param int|null $siteId |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @throws \yii\web\ForbiddenHttpException |
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function actionExportDataSamples(string $pageUrl = '', int $siteId = null) |
85
|
|
|
{ |
86
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:performance'); |
87
|
|
|
try { |
88
|
|
|
$this->exportCsvFile( |
89
|
|
|
'webperf-data-samples', |
90
|
|
|
'{{%webperf_data_samples}}', |
91
|
|
|
self::EXPORT_DATA_SAMPLES_COLUMNS, |
92
|
|
|
$pageUrl, |
93
|
|
|
$siteId |
94
|
|
|
); |
95
|
|
|
} catch (CannotInsertRecord $e) { |
96
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Export the error samples table as a CSV file |
102
|
|
|
* |
103
|
|
|
* @param string $pageUrl |
|
|
|
|
104
|
|
|
* @param int|null $siteId |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @throws \yii\web\ForbiddenHttpException |
107
|
|
|
*/ |
|
|
|
|
108
|
|
|
public function actionExportErrorSamples(string $pageUrl = '', int $siteId = null) |
109
|
|
|
{ |
110
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:errors'); |
111
|
|
|
try { |
112
|
|
|
$this->exportCsvFile( |
113
|
|
|
'webperf-error-samples', |
114
|
|
|
'{{%webperf_error_samples}}', |
115
|
|
|
self::EXPORT_ERROR_SAMPLES_COLUMNS, |
116
|
|
|
$pageUrl, |
117
|
|
|
$siteId |
118
|
|
|
); |
119
|
|
|
} catch (CannotInsertRecord $e) { |
120
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Public Methods |
125
|
|
|
// ========================================================================= |
126
|
|
|
|
127
|
|
|
/** |
|
|
|
|
128
|
|
|
* @param string $filename |
|
|
|
|
129
|
|
|
* @param string $table |
|
|
|
|
130
|
|
|
* @param array $columns |
|
|
|
|
131
|
|
|
* @param string $pageUrl |
|
|
|
|
132
|
|
|
* @param int|null $siteId |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @throws \League\Csv\CannotInsertRecord |
135
|
|
|
*/ |
|
|
|
|
136
|
|
|
protected function exportCsvFile( |
137
|
|
|
string $filename, |
138
|
|
|
string $table, |
139
|
|
|
array $columns, |
140
|
|
|
string $pageUrl = '', |
141
|
|
|
int $siteId = null |
142
|
|
|
) { |
143
|
|
|
// If your CSV document was created or is read on a Macintosh computer, |
144
|
|
|
// add the following lines before using the library to help PHP detect line ending in Mac OS X |
145
|
|
|
if (!ini_get('auto_detect_line_endings')) { |
146
|
|
|
ini_set('auto_detect_line_endings', '1'); |
147
|
|
|
} |
148
|
|
|
// Query the db table |
149
|
|
|
$query = (new Query()) |
150
|
|
|
->from([$table]) |
151
|
|
|
->select($columns) |
|
|
|
|
152
|
|
|
; |
153
|
|
|
if ($siteId !== null) { |
154
|
|
|
$query |
155
|
|
|
->where(['siteId' => $siteId]) |
|
|
|
|
156
|
|
|
; |
157
|
|
|
} |
158
|
|
|
if (!empty($pageUrl)) { |
159
|
|
|
$query |
160
|
|
|
->where(['pageUrl' => $pageUrl]) |
|
|
|
|
161
|
|
|
; |
162
|
|
|
} |
163
|
|
|
$data = $query |
164
|
|
|
->all(); |
165
|
|
|
// Create our CSV file writer |
166
|
|
|
$csv = Writer::createFromFileObject(new \SplTempFileObject()); |
167
|
|
|
$csv->insertOne($columns); |
168
|
|
|
$csv->insertAll($data); |
169
|
|
|
$csv->output($filename.'.csv'); |
170
|
|
|
exit(0); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|