|
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) 2018 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\services; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\webperf\Webperf; |
|
14
|
|
|
use nystudio107\webperf\models\DataSample; |
|
15
|
|
|
|
|
16
|
|
|
use Craft; |
|
|
|
|
|
|
17
|
|
|
use craft\base\Component; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
|
|
|
|
|
20
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
21
|
|
|
* @package Webperf |
|
|
|
|
|
|
22
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
|
|
24
|
|
|
class DataSamples extends Component |
|
25
|
|
|
{ |
|
26
|
|
|
// Public Methods |
|
27
|
|
|
// ========================================================================= |
|
28
|
|
|
|
|
29
|
|
|
public function addDataSample(DataSample $dataSample) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
// Validate the model before saving it to the db |
|
32
|
|
|
if ($dataSample->validate() === false) { |
|
33
|
|
|
Craft::error( |
|
34
|
|
|
Craft::t( |
|
35
|
|
|
'webperf', |
|
36
|
|
|
'Error validating data sample: {errors}', |
|
37
|
|
|
['errors' => print_r($dataSample->getErrors(), true)] |
|
38
|
|
|
), |
|
39
|
|
|
__METHOD__ |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
// Get the validated model attributes and save them to the db |
|
45
|
|
|
$dataSampleConfig = $dataSample->getAttributes(); |
|
46
|
|
|
$db = Craft::$app->getDb(); |
|
47
|
|
|
// Create a new record |
|
48
|
|
|
try { |
|
49
|
|
|
$db->createCommand()->insert( |
|
50
|
|
|
'{{%webperf_data_samples}}', |
|
51
|
|
|
$dataSampleConfig |
|
52
|
|
|
)->execute(); |
|
53
|
|
|
} catch (\Exception $e) { |
|
54
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
55
|
|
|
} |
|
56
|
|
|
// After adding the DataSample, trim the webperf_data_samples db table |
|
57
|
|
|
if (Webperf::$settings->automaticallyTrimDataSamples) { |
|
58
|
|
|
$this->trimDataSamples(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Trim the webperf_data_samples db table based on the dataSamplesStoredLimit |
|
64
|
|
|
* config.php setting |
|
65
|
|
|
* |
|
66
|
|
|
* @param int|null $limit |
|
|
|
|
|
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public function trimDataSamples(int $limit = null): int |
|
71
|
|
|
{ |
|
72
|
|
|
$affectedRows = 0; |
|
73
|
|
|
$db = Craft::$app->getDb(); |
|
74
|
|
|
$quotedTable = $db->quoteTableName('{{%webperf_data_samples}}'); |
|
75
|
|
|
$limit = $limit ?? Webperf::$settings->dataSamplesStoredLimit; |
|
76
|
|
|
|
|
77
|
|
|
if ($limit !== null) { |
|
|
|
|
|
|
78
|
|
|
// https://stackoverflow.com/questions/578867/sql-query-delete-all-records-from-the-table-except-latest-n |
|
79
|
|
|
try { |
|
80
|
|
|
if ($db->getIsMysql()) { |
|
81
|
|
|
// Handle MySQL |
|
82
|
|
|
$affectedRows = $db->createCommand(/** @lang mysql */ |
|
|
|
|
|
|
83
|
|
|
" |
|
84
|
|
|
DELETE FROM {$quotedTable} |
|
85
|
|
|
WHERE id NOT IN ( |
|
86
|
|
|
SELECT id |
|
87
|
|
|
FROM ( |
|
88
|
|
|
SELECT id |
|
89
|
|
|
FROM {$quotedTable} |
|
90
|
|
|
ORDER BY hitLastTime DESC |
|
91
|
|
|
LIMIT {$limit} |
|
92
|
|
|
) foo |
|
93
|
|
|
) |
|
94
|
|
|
" |
|
95
|
|
|
)->execute(); |
|
96
|
|
|
} |
|
97
|
|
|
if ($db->getIsPgsql()) { |
|
98
|
|
|
// Handle Postgres |
|
99
|
|
|
$affectedRows = $db->createCommand(/** @lang mysql */ |
|
|
|
|
|
|
100
|
|
|
" |
|
101
|
|
|
DELETE FROM {$quotedTable} |
|
102
|
|
|
WHERE id NOT IN ( |
|
103
|
|
|
SELECT id |
|
104
|
|
|
FROM ( |
|
105
|
|
|
SELECT id |
|
106
|
|
|
FROM {$quotedTable} |
|
107
|
|
|
ORDER BY \"hitLastTime\" DESC |
|
108
|
|
|
LIMIT {$limit} |
|
109
|
|
|
) foo |
|
110
|
|
|
) |
|
111
|
|
|
" |
|
112
|
|
|
)->execute(); |
|
113
|
|
|
} |
|
114
|
|
|
} catch (\Exception $e) { |
|
115
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
116
|
|
|
} |
|
117
|
|
|
Craft::info( |
|
118
|
|
|
Craft::t( |
|
119
|
|
|
'webperf', |
|
120
|
|
|
'Trimmed {rows} from webperf_data_samples table', |
|
121
|
|
|
['rows' => $affectedRows] |
|
122
|
|
|
), |
|
123
|
|
|
__METHOD__ |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $affectedRows; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|