Passed
Push — v1 ( 7dfb05...0f87a2 )
by Andrew
07:09 queued 04:02
created

DataSamples::trimOutlierSamples()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 20
rs 9.7333
c 0
b 0
f 0
cc 4
nc 5
nop 0
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
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2019 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\webperf\services;
12
13
use nystudio107\webperf\Webperf;
14
use nystudio107\webperf\base\CraftDataSample;
15
use nystudio107\webperf\base\DbDataSampleInterface;
16
17
use Craft;
0 ignored issues
show
Bug introduced by
The type Craft was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use craft\base\Component;
0 ignored issues
show
Bug introduced by
The type craft\base\Component was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use craft\db\Query;
0 ignored issues
show
Bug introduced by
The type craft\db\Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
26
class DataSamples extends Component
27
{
28
    // Constants
29
    // =========================================================================
30
31
    const OUTLIER_PAGELOAD_MULTIPLIER = 10;
32
33
    // Public Methods
34
    // =========================================================================
35
36
    /**
37
     * Get the total number of data samples optionally limited by siteId
38
     *
39
     * @param int    $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
40
     * @param string $column
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
41
     *
42
     * @return int|string
43
     */
44
    public function totalSamples(int $siteId, string $column)
45
    {
46
        // Get the total number of data samples
47
        $query = (new Query())
48
            ->from(['{{%webperf_data_samples}}'])
49
            ->where(['not', [$column => null]])
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
50
            ;
51
        if ((int)$siteId !== 0) {
52
            $query->andWhere(['siteId' => $siteId]);
53
        }
54
55
        return $query->count();
56
    }
57
58
    /**
59
     * Get the page title from data samples by URL and optionally siteId
60
     *
61
     * @param string   $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
62
     * @param int $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
63
     *
64
     * @return string
65
     */
66
    public function pageTitle(string $url, int $siteId = 0): string
67
    {
68
        // Get the page title from a URL
69
        $query = (new Query())
70
            ->select(['title'])
71
            ->from(['{{%webperf_data_samples}}'])
72
            ->where([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
73
                'and', ['url' => $url],
74
                ['not', ['title' => '']],
75
            ])
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
76
        ;
77
        if ((int)$siteId !== 0) {
78
            $query->andWhere(['siteId' => $siteId]);
79
        }
80
        $result = $query->one();
81
        // Decode any emojis in the title
82
        if (!empty($result['title'])) {
83
            $result['title'] = html_entity_decode($result['title'], ENT_NOQUOTES, 'UTF-8');
84
        }
85
86
        return $result['title'] ?? '';
87
    }
88
89
    /**
90
     * Add a data sample to the webperf_data_samples table
91
     *
92
     * @param DbDataSampleInterface $dataSample
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
94
    public function addDataSample(DbDataSampleInterface $dataSample)
95
    {
96
        // Validate the model before saving it to the db
97
        if ($dataSample->validate() === false) {
98
            Craft::error(
99
                Craft::t(
100
                    'webperf',
101
                    'Error validating data sample: {errors}',
102
                    ['errors' => print_r($dataSample->getErrors(), true)]
103
                ),
104
                __METHOD__
105
            );
106
107
            return;
108
        }
109
        $isNew = true;
110
        if (!empty($dataSample->requestId)) {
0 ignored issues
show
Bug introduced by
Accessing requestId on the interface nystudio107\webperf\base\DbDataSampleInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
111
            // See if a data sample exists with the same requestId already
112
            $testSample = (new Query())
113
                ->from(['{{%webperf_data_samples}}'])
114
                ->where(['requestId' => $dataSample->requestId])
115
                ->one();
116
            // If it exists, update it rather than having duplicates
117
            if (!empty($testSample)) {
118
                $isNew = false;
119
            }
120
        }
121
        // Get the validated model attributes and save them to the db
122
        $dataSampleConfig = $dataSample->getAttributes();
123
        $db = Craft::$app->getDb();
124
        if ($isNew) {
125
            Craft::debug('Creating new data sample', __METHOD__);
126
            // Create a new record
127
            try {
128
                $result = $db->createCommand()->insert(
129
                    '{{%webperf_data_samples}}',
130
                    $dataSampleConfig
131
                )->execute();
132
                Craft::debug($result, __METHOD__);
133
            } catch (\Exception $e) {
134
                Craft::error($e->getMessage(), __METHOD__);
135
            }
136
        } else {
137
            Craft::debug('Updating existing data sample', __METHOD__);
138
            // Update the existing record
139
            try {
140
                $result = $db->createCommand()->update(
141
                    '{{%webperf_data_samples}}',
142
                    $dataSampleConfig,
143
                    [
144
                        'requestId' => $dataSample->requestId,
145
                    ]
146
                )->execute();
147
                Craft::debug($result, __METHOD__);
148
            } catch (\Exception $e) {
149
                Craft::error($e->getMessage(), __METHOD__);
150
            }
151
        }
152
        // Trim orphaned samples
153
        $this->trimOrphanedSamples($dataSample->requestId);
154
        // After adding the DataSample, trim the webperf_data_samples db table
155
        if (Webperf::$settings->automaticallyTrimDataSamples) {
156
            $this->trimDataSamples();
157
        }
158
    }
159
160
    /**
161
     * Delete a data sample by id
162
     *
163
     * @param int $id
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
164
     *
165
     * @return int The result
166
     */
167
    public function deleteSampleById(int $id): int
168
    {
169
        $db = Craft::$app->getDb();
170
        // Delete a row from the db table
171
        try {
172
            $result = $db->createCommand()->delete(
173
                '{{%webperf_data_samples}}',
174
                [
175
                    'id' => $id,
176
                ]
177
            )->execute();
178
        } catch (\Exception $e) {
179
            Craft::error($e->getMessage(), __METHOD__);
180
            $result = 0;
181
        }
182
183
        return $result;
184
    }
185
186
    /**
187
     * Delete data samples by URL and optionally siteId
188
     *
189
     * @param string   $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
190
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
191
     *
192
     * @return int
193
     */
194
    public function deleteDataSamplesByUrl(string $url, int $siteId = null): int
195
    {
196
        $db = Craft::$app->getDb();
197
        // Delete a row from the db table
198
        try {
199
            $conditions = ['url' => $url];
200
            if ($siteId !== null) {
201
                $conditions['siteId'] = $siteId;
202
            }
203
            $result = $db->createCommand()->delete(
204
                '{{%webperf_data_samples}}',
205
                $conditions
206
            )->execute();
207
        } catch (\Exception $e) {
208
            Craft::error($e->getMessage(), __METHOD__);
209
            $result = 0;
210
        }
211
212
        return $result;
213
    }
214
215
    /**
216
     * Delete data all samples optionally siteId
217
     *
218
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
219
     *
220
     * @return int
221
     */
222
    public function deleteAllDataSamples(int $siteId = null): int
223
    {
224
        $db = Craft::$app->getDb();
225
        // Delete a row from the db table
226
        try {
227
            $conditions = [];
228
            if ($siteId !== null) {
229
                $conditions['siteId'] = $siteId;
230
            }
231
            $result = $db->createCommand()->delete(
232
                '{{%webperf_data_samples}}',
233
                $conditions
234
            )->execute();
235
        } catch (\Exception $e) {
236
            Craft::error($e->getMessage(), __METHOD__);
237
            $result = 0;
238
        }
239
240
        return $result;
241
    }
242
243
    /**
244
     * Trim any samples that our outliers
245
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
246
    public function trimOutlierSamples()
247
    {
248
        if (Webperf::$settings->trimOutlierDataSamples) {
249
            $db = Craft::$app->getDb();
250
            Craft::debug('Trimming outlier samples', __METHOD__);
251
            // Get the average pageload time
252
            $stats = (new Query())
253
                ->from('{{%webperf_data_samples}}')
254
                ->average('[[pageLoad]]');
255
            if (!empty($stats['avg'])) {
256
                $threshold = $stats['avg'] * self::OUTLIER_PAGELOAD_MULTIPLIER;
257
                // Delete any samples that are far above average
258
                try {
259
                    $result = $db->createCommand()->delete(
260
                        '{{%webperf_data_samples}}',
261
                        ['>', '[[pageLoad]]', $threshold]
262
                    )->execute();
263
                    Craft::debug($result, __METHOD__);
264
                } catch (\Exception $e) {
265
                    Craft::error($e->getMessage(), __METHOD__);
266
                }
267
            }
268
        }
269
    }
270
271
    /**
272
     * Trim samples that have the placeholder in the URL, aka they never
273
     * received the Boomerang beacon
274
     *
275
     * @param int $requestId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
276
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
277
    public function trimOrphanedSamples($requestId)
278
    {
279
        $db = Craft::$app->getDb();
280
        Craft::debug('Trimming orphaned samples', __METHOD__);
281
        // Update the existing record
282
        try {
283
            $result = $db->createCommand()->delete(
284
                '{{%webperf_data_samples}}',
285
                [
286
                    'and', ['url' => CraftDataSample::PLACEHOLDER_URL],
287
                    ['not', ['requestId' => $requestId]],
288
                ]
289
            )->execute();
290
            Craft::debug($result, __METHOD__);
291
        } catch (\Exception $e) {
292
            Craft::error($e->getMessage(), __METHOD__);
293
        }
294
    }
295
296
    /**
297
     * Trim the webperf_data_samples db table based on the dataSamplesStoredLimit
298
     * config.php setting
299
     *
300
     * @param int|null $limit
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
301
     *
302
     * @return int
303
     */
304
    public function trimDataSamples(int $limit = null): int
305
    {
306
        $this->trimOutlierSamples();
307
        $affectedRows = 0;
308
        $db = Craft::$app->getDb();
309
        $quotedTable = $db->quoteTableName('{{%webperf_data_samples}}');
310
        $limit = $limit ?? Webperf::$settings->dataSamplesStoredLimit;
311
312
        if ($limit !== null) {
0 ignored issues
show
introduced by
The condition $limit !== null is always true.
Loading history...
313
            //  https://stackoverflow.com/questions/578867/sql-query-delete-all-records-from-the-table-except-latest-n
314
            try {
315
                if ($db->getIsMysql()) {
316
                    // Handle MySQL
317
                    $affectedRows = $db->createCommand(/** @lang mysql */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
318
                        "
319
                        DELETE FROM {$quotedTable}
320
                        WHERE id NOT IN (
321
                          SELECT id
322
                          FROM (
323
                            SELECT id
324
                            FROM {$quotedTable}
325
                            ORDER BY dateCreated DESC
326
                            LIMIT {$limit}
327
                          ) foo
328
                        )
329
                        "
330
                    )->execute();
331
                }
332
                if ($db->getIsPgsql()) {
333
                    // Handle Postgres
334
                    $affectedRows = $db->createCommand(/** @lang mysql */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
335
                        "
336
                        DELETE FROM {$quotedTable}
337
                        WHERE id NOT IN (
338
                          SELECT id
339
                          FROM (
340
                            SELECT id
341
                            FROM {$quotedTable}
342
                            ORDER BY \"dateCreated\" DESC
343
                            LIMIT {$limit}
344
                          ) foo
345
                        )
346
                        "
347
                    )->execute();
348
                }
349
            } catch (\Exception $e) {
350
                Craft::error($e->getMessage(), __METHOD__);
351
            }
352
            Craft::info(
353
                Craft::t(
354
                    'webperf',
355
                    'Trimmed {rows} from webperf_data_samples table',
356
                    ['rows' => $affectedRows]
357
                ),
358
                __METHOD__
359
            );
360
        }
361
362
        return $affectedRows;
363
    }
364
}
365