Passed
Push — v1 ( 65d641...7780c4 )
by Andrew
02:59
created

ChartsController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 96
dl 0
loc 189
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B actionDashboardSlowestPages() 0 60 8
B actionDashboardStatsAverage() 0 41 6
A actionWidget() 0 39 4
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) 2018 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\controllers;
12
13
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...
14
use nystudio107\webperf\helpers\Permission as PermissionHelper;
15
16
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...
17
use craft\helpers\ArrayHelper;
0 ignored issues
show
Bug introduced by
The type craft\helpers\ArrayHelper 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\web\Controller;
0 ignored issues
show
Bug introduced by
The type craft\web\Controller 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
20
use yii\web\ForbiddenHttpException;
0 ignored issues
show
Bug introduced by
The type yii\web\ForbiddenHttpException 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...
21
use yii\web\Response;
0 ignored issues
show
Bug introduced by
The type yii\web\Response 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...
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @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...
25
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @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...
27
 */
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...
28
class ChartsController extends Controller
29
{
30
    // Constants
31
    // =========================================================================
32
33
    // Protected Properties
34
    // =========================================================================
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @var    bool|array
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 4
Loading history...
38
     */
39
    protected $allowAnonymous = [
40
    ];
41
42
    // Public Methods
43
    // =========================================================================
44
45
    /**
46
     * The Dashboard stats average chart
47
     *
48
     * @param int    $days
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     * @param string $column
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     * @param int    $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
     *
52
     * @return Response
53
     * @throws ForbiddenHttpException
54
     */
55
    public function actionDashboardStatsAverage(
56
        int $days = 30,
57
        string $column = 'pageLoad',
58
        int $siteId = 0
59
    ): Response {
60
        PermissionHelper::controllerPermissionCheck('webperf:dashboard');
61
        $data = [];
62
        // Different dbs do it different ways
63
        $stats = null;
64
        $db = Craft::$app->getDb();
65
        if ($db->getIsMysql()) {
66
            // Query the db
67
            $query = (new Query())
68
                ->from('{{%webperf_data_samples}}')
69
                ->select([
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...
70
                    'AVG('.$column.') AS avg',
71
                ])
0 ignored issues
show
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...
72
                ->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )");
73
            if ((int)$siteId !== 0) {
74
                $query->andWhere(['siteId' => $siteId]);
75
            }
76
            $stats = $query->all();
77
        }
78
        if ($db->getIsPgsql()) {
79
            // Query the db
80
            $query = (new Query())
81
                ->from('{{%webperf_data_samples}}')
82
                ->select([
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...
83
                    'AVG("'.$column.'") AS avg',
84
                ])
0 ignored issues
show
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...
85
                ->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )");
86
            if ((int)$siteId !== 0) {
87
                $query->andWhere(['siteId' => $siteId]);
88
            }
89
            $stats = $query->all();
90
        }
91
        if ($stats) {
92
            $data = ArrayHelper::getColumn($stats, 'avg');
93
        }
94
95
        return $this->asJson($data);
96
    }
97
98
    /**
99
     * The Dashboard stats slowest pages list
100
     *
101
     * @param int    $days
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
102
     * @param string $column
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
103
     * @param int    $limit
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
104
     * @param int    $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     *
106
     * @return Response
107
     * @throws ForbiddenHttpException
108
     */
109
    public function actionDashboardSlowestPages(
110
        int $days = 30,
111
        string $column = 'pageLoad',
112
        int $limit = 3,
113
        int $siteId = 0
114
    ): Response {
115
        PermissionHelper::controllerPermissionCheck('webperf:dashboard');
116
        $data = [];
117
        // Different dbs do it different ways
118
        $stats = null;
119
        $db = Craft::$app->getDb();
120
        if ($db->getIsMysql()) {
121
            // Query the db
122
            $query = (new Query())
123
                ->from('{{%webperf_data_samples}}')
124
                ->select([
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...
125
                    'url',
126
                    'title',
127
                    'AVG('.$column.') AS avg',
128
                ])
0 ignored issues
show
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...
129
                ->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )");
130
            if ((int)$siteId !== 0) {
131
                $query->andWhere(['siteId' => $siteId]);
132
            }
133
            $query
134
                ->orderBy('avg DESC')
135
                ->groupBy('url')
136
                ->limit($limit);
137
            $stats = $query->all();
138
        }
139
        if ($db->getIsPgsql()) {
140
            // Query the db
141
            $query = (new Query())
142
                ->from('{{%webperf_data_samples}}')
143
                ->select([
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...
144
                    'url',
145
                    'title',
146
                    'AVG("'.$column.'") AS avg',
147
                ])
0 ignored issues
show
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...
148
                ->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )");
149
            if ((int)$siteId !== 0) {
150
                $query->andWhere(['siteId' => $siteId]);
151
                $query
152
                    ->orderBy('avg DESC')
153
                    ->groupBy('url')
154
                    ->limit($limit);
155
            }
156
            $stats = $query->all();
157
        }
158
        if ($stats) {
159
            // Decode any emojis in the title
160
            foreach ($stats as &$stat) {
161
                if (!empty($stat['title'])) {
162
                    $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8');
163
                }
164
            }
165
            $data = $stats;
166
        }
167
168
        return $this->asJson($data);
169
    }
170
171
    /**
172
     * The Dashboard chart
173
     *
174
     * @param int $days
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
175
     *
176
     * @return Response
177
     */
178
    public function actionWidget($days = 1): Response
179
    {
180
        $data = [];
181
        // Different dbs do it different ways
182
        $stats = null;
183
        $handledStats = null;
184
        $db = Craft::$app->getDb();
185
        if ($db->getIsMysql()) {
186
            // Query the db
187
            $stats = (new Query())
188
                ->from('{{%retour_stats}}')
189
                ->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )")
190
                ->count();
191
            $handledStats = (new Query())
192
                ->from('{{%retour_stats}}')
193
                ->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )")
194
                ->andWhere('handledByRetour is TRUE')
195
                ->count();
196
        }
197
        if ($db->getIsPgsql()) {
198
            // Query the db
199
            $stats = (new Query())
200
                ->from('{{%retour_stats}}')
201
                ->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )")
202
                ->count();
203
            $handledStats = (new Query())
204
                ->from('{{%retour_stats}}')
205
                ->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )")
206
                ->andWhere('"handledByRetour" = TRUE')
207
                ->count();
208
        }
209
        if ($stats) {
210
            $data = [
211
                (int)$stats,
212
                (int)$handledStats,
213
            ];
214
        }
215
216
        return $this->asJson($data);
217
    }
218
219
    // Protected Methods
220
    // =========================================================================
221
}
222