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\controllers; |
12
|
|
|
|
13
|
|
|
use Craft; |
|
|
|
|
14
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
15
|
|
|
|
16
|
|
|
use craft\db\Query; |
|
|
|
|
17
|
|
|
use craft\helpers\ArrayHelper; |
|
|
|
|
18
|
|
|
use craft\web\Controller; |
|
|
|
|
19
|
|
|
|
20
|
|
|
use yii\web\ForbiddenHttpException; |
|
|
|
|
21
|
|
|
use yii\web\Response; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
25
|
|
|
* @package Webperf |
|
|
|
|
26
|
|
|
* @since 1.0.0 |
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
28
|
|
|
class ChartsController extends Controller |
29
|
|
|
{ |
30
|
|
|
// Constants |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
// Protected Properties |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @var bool|array |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
protected $allowAnonymous = [ |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
// Public Methods |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The Dashboard chart |
47
|
|
|
* |
48
|
|
|
* @param string $range |
|
|
|
|
49
|
|
|
* @param string $column |
|
|
|
|
50
|
|
|
* @param int $siteId |
|
|
|
|
51
|
|
|
* |
52
|
|
|
* @return Response |
53
|
|
|
* @throws ForbiddenHttpException |
54
|
|
|
*/ |
55
|
|
|
public function actionDashboardRadialBar( |
56
|
|
|
string $range = 'day', |
57
|
|
|
string $column = 'pageLoad', |
58
|
|
|
int $siteId = 0 |
59
|
|
|
): Response { |
60
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
61
|
|
|
$data = []; |
62
|
|
|
$days = 1; |
63
|
|
|
switch ($range) { |
64
|
|
|
case 'day': |
|
|
|
|
65
|
|
|
$days = 1; |
66
|
|
|
break; |
67
|
|
|
case 'week': |
|
|
|
|
68
|
|
|
$days = 7; |
69
|
|
|
break; |
70
|
|
|
case 'month': |
|
|
|
|
71
|
|
|
$days = 30; |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
// Different dbs do it different ways |
75
|
|
|
$stats = null; |
76
|
|
|
$db = Craft::$app->getDb(); |
77
|
|
|
if ($db->getIsMysql()) { |
78
|
|
|
// Query the db |
79
|
|
|
$query = (new Query()) |
80
|
|
|
->from('{{%webperf_data_samples}}') |
81
|
|
|
->select([ |
|
|
|
|
82
|
|
|
'AVG('.$column.') AS avg', |
83
|
|
|
]) |
|
|
|
|
84
|
|
|
->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
85
|
|
|
if ((int)$siteId !== 0) { |
86
|
|
|
$query->andWhere(['siteId' => $siteId]); |
87
|
|
|
} |
88
|
|
|
$stats = $query->all(); |
89
|
|
|
} |
90
|
|
|
if ($db->getIsPgsql()) { |
91
|
|
|
// Query the db |
92
|
|
|
$query = (new Query()) |
93
|
|
|
->from('{{%retour_stats}}') |
94
|
|
|
->select([ |
|
|
|
|
95
|
|
|
"to_char(\"hitLastTime\", 'yyyy-mm-dd') AS date_formatted", |
96
|
|
|
"COUNT(\"redirectSrcUrl\") AS cnt", |
97
|
|
|
"COUNT(CASE WHEN \"handledByRetour\" = true THEN 1 END) as handled_cnt", |
98
|
|
|
]) |
|
|
|
|
99
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
100
|
|
|
if ((int)$siteId !== 0) { |
101
|
|
|
$query->andWhere(['siteId' => $siteId]); |
102
|
|
|
} |
103
|
|
|
$query |
104
|
|
|
->orderBy('date_formatted ASC') |
105
|
|
|
->groupBy('date_formatted'); |
106
|
|
|
$stats = $query->all(); |
107
|
|
|
} |
108
|
|
|
if ($stats) { |
109
|
|
|
$data = ArrayHelper::getColumn($stats, 'avg'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->asJson($data); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* The Dashboard chart |
117
|
|
|
* |
118
|
|
|
* @param int $days |
|
|
|
|
119
|
|
|
* |
120
|
|
|
* @return Response |
121
|
|
|
*/ |
122
|
|
|
public function actionWidget($days = 1): Response |
123
|
|
|
{ |
124
|
|
|
$data = []; |
125
|
|
|
// Different dbs do it different ways |
126
|
|
|
$stats = null; |
127
|
|
|
$handledStats = null; |
128
|
|
|
$db = Craft::$app->getDb(); |
129
|
|
|
if ($db->getIsMysql()) { |
130
|
|
|
// Query the db |
131
|
|
|
$stats = (new Query()) |
132
|
|
|
->from('{{%retour_stats}}') |
133
|
|
|
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )") |
134
|
|
|
->count(); |
135
|
|
|
$handledStats = (new Query()) |
136
|
|
|
->from('{{%retour_stats}}') |
137
|
|
|
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )") |
138
|
|
|
->andWhere('handledByRetour is TRUE') |
139
|
|
|
->count(); |
140
|
|
|
} |
141
|
|
|
if ($db->getIsPgsql()) { |
142
|
|
|
// Query the db |
143
|
|
|
$stats = (new Query()) |
144
|
|
|
->from('{{%retour_stats}}') |
145
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )") |
146
|
|
|
->count(); |
147
|
|
|
$handledStats = (new Query()) |
148
|
|
|
->from('{{%retour_stats}}') |
149
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )") |
150
|
|
|
->andWhere('"handledByRetour" = TRUE') |
151
|
|
|
->count(); |
152
|
|
|
} |
153
|
|
|
if ($stats) { |
154
|
|
|
$data = [ |
155
|
|
|
(int)$stats, |
156
|
|
|
(int)$handledStats, |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->asJson($data); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Protected Methods |
164
|
|
|
// ========================================================================= |
165
|
|
|
} |
166
|
|
|
|