-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAPI.php
372 lines (343 loc) · 12.1 KB
/
API.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
namespace Piwik\Plugins\PerformanceAudit;
require PIWIK_INCLUDE_PATH . '/plugins/PerformanceAudit/vendor/autoload.php';
use Exception;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\DataTable\Map;
use Piwik\DataTable\Row;
use Piwik\Metrics\Formatter;
use Piwik\Piwik;
use Piwik\Plugin\API as BaseAPI;
use Piwik\Plugins\PerformanceAudit\Columns\Filters\AuditScoreClassifier;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\MaxPercent;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\MaxSeconds;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\MedianPercent;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\MedianSeconds;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\MinPercent;
use Piwik\Plugins\PerformanceAudit\Columns\Metrics\MinSeconds;
use Piwik\Request;
use Piwik\Site;
use Piwik\UrlHelper;
class API extends BaseAPI
{
/**
* Get score for mobile devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceScoreMobile(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_Score_Mobile', $idSite, $period, $date, $segment);
}
/**
* Get score for desktop devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceScoreDesktop(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_Score_Desktop', $idSite, $period, $date, $segment);
}
/**
* Get FirstContentfulPaint for mobile devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceFirstContentfulPaintMobile(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_FirstContentfulPaint_Mobile', $idSite, $period, $date, $segment);
}
/**
* Get FirstContentfulPaint for desktop devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceFirstContentfulPaintDesktop(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_FirstContentfulPaint_Desktop', $idSite, $period, $date, $segment);
}
/**
* Get SpeedIndex for mobile devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceSpeedIndexMobile(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_SpeedIndex_Mobile', $idSite, $period, $date, $segment);
}
/**
* Get SpeedIndex for desktop devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceSpeedIndexDesktop(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_SpeedIndex_Desktop', $idSite, $period, $date, $segment);
}
/**
* Get LargestContentfulPaint for mobile devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceLargestContentfulPaintMobile(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_LargestContentfulPaint_Mobile', $idSite, $period, $date, $segment);
}
/**
* Get LargestContentfulPaint for desktop devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceLargestContentfulPaintDesktop(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_LargestContentfulPaint_Desktop', $idSite, $period, $date, $segment);
}
/**
* Get Interactive for mobile devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceInteractiveMobile(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_Interactive_Mobile', $idSite, $period, $date, $segment);
}
/**
* Get Interactive for desktop devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceInteractiveDesktop(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_Interactive_Desktop', $idSite, $period, $date, $segment);
}
/**
* Get TotalBlockingTime for mobile devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceTotalBlockingTimeMobile(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_TotalBlockingTime_Mobile', $idSite, $period, $date, $segment);
}
/**
* Get TotalBlockingTime for desktop devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceTotalBlockingTimeDesktop(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_TotalBlockingTime_Desktop', $idSite, $period, $date, $segment);
}
/**
* Get CumulativeLayoutShift for mobile devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceCumulativeLayoutShiftMobile(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_CumulativeLayoutShift_Mobile', $idSite, $period, $date, $segment);
}
/**
* Get CumulativeLayoutShift for desktop devices.
*
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
public function getPerformanceCumulativeLayoutShiftDesktop(int $idSite, string $period, string $date, $segment = false)
{
return $this->getPerformanceDataTable('PerformanceAudit_Report_CumulativeLayoutShift_Desktop', $idSite, $period, $date, $segment);
}
/**
* General method to retrieve DataTable for given table.
*
* @param string $dataTableName
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @return DataTable|Map
* @throws Exception
*/
private function getPerformanceDataTable(string $dataTableName, int $idSite, string $period, string $date, $segment)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::createDataTableFromArchive($dataTableName, $idSite, $period, $date, $segment);
$this->filterUrlLabel($dataTable, $idSite);
$hasSecondBasedMetric = !strstr($dataTableName, '_Score_');
$this->formatMetricValues($dataTable, $hasSecondBasedMetric);
$this->setMetricTooltips($dataTable, $dataTableName);
$this->disableTotalsRow($dataTable);
return $dataTable;
}
/**
* Get label for given URL.
*
* @param string $url
* @param string $domain
* @return string
*/
private static function getUrlLabelValue(string $url, string $domain)
{
if (!$url) {
return Piwik::translate('General_NotDefined', Piwik::translate('Actions_ColumnPageURL'));
}
$domain = UrlHelper::getHostFromUrl($domain);
$host = UrlHelper::getHostFromUrl($url);
$url = UrlHelper::getPathAndQueryFromUrl($url);
if (mb_substr($url, 0, 1) !== '/') {
$url = '/' . $url;
}
if ($domain !== $host) {
$url = $host . $url;
}
if ($url === '/') {
$url = '/index';
}
return $url;
}
/**
* Set new URL label for each row.
*
* @param DataTable|Map $dataTable
* @param int $idSite
*/
private function filterUrlLabel($dataTable, int $idSite)
{
$siteDomain = Site::getMainUrlFor($idSite);
$dataTable->filter(function (DataTable $dataTable) use ($siteDomain) {
foreach ($dataTable->getRows() as $row) {
$newLabel = $this->getUrlLabelValue($row->getColumn('label'), $siteDomain);
$row->setColumn('label', $newLabel);
}
});
}
/**
* Set new formatted value for each row if metric is based on seconds.
*
* @param DataTable|Map $dataTable
* @param bool $isSecondBasedMetric
*/
private function formatMetricValues($dataTable, bool $isSecondBasedMetric)
{
$formatter = new Formatter();
$dataTable->filter(function (DataTable $dataTable) use ($formatter, $isSecondBasedMetric) {
foreach ($dataTable->getRows() as $row) {
$metrics = ($isSecondBasedMetric) ? [
new MinSeconds(),
new MedianSeconds(),
new MaxSeconds()
] : [
new MinPercent(),
new MedianPercent(),
new MaxPercent()
];
foreach ($metrics as $metric) {
$columnName = $metric->getName();
$newValue = $metric->format($row->getColumn($columnName), $formatter);
$row->setColumn($columnName, $newValue);
}
}
});
}
/**
* Set tooltip for all metrics.
*
* @param DataTable|Map $dataTable
* @param string $dataTableName
* @return void
*/
private function setMetricTooltips($dataTable, string $dataTableName)
{
$dataTable->filter(function ($dataTable) use ($dataTableName) {
$columns = array_diff_key($dataTable->getColumns(), ['label']);
foreach ($columns as $column) {
$dataTable->filter('ColumnCallbackAddMetadata', [$column, $column . '_tooltip', [AuditScoreClassifier::class, 'getTooltip'], [$dataTableName]]);
}
});
}
/**
* Disables totals row as it doesn't provide any reasonable results in this case.
*
* @param DataTable|Map $dataTable
* @return void
* @throws Exception
*/
private function disableTotalsRow($dataTable)
{
if ($dataTable instanceof DataTable && Request::fromRequest()->getBoolParameter('keep_totals_row', false)) {
$dataTable->setTotalsRow(new Row([
'label' => DataTable::LABEL_TOTALS_ROW
]));
}
}
}