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 int $siteId |
|
|
|
|
50
|
|
|
* |
51
|
|
|
* @return Response |
52
|
|
|
* @throws ForbiddenHttpException |
53
|
|
|
*/ |
54
|
|
|
public function actionDashboard(string $range = 'day', int $siteId = 0): Response |
55
|
|
|
{ |
56
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
57
|
|
|
$data = []; |
58
|
|
|
$days = 1; |
59
|
|
|
switch ($range) { |
60
|
|
|
case 'day': |
|
|
|
|
61
|
|
|
$days = 1; |
62
|
|
|
break; |
63
|
|
|
case 'week': |
|
|
|
|
64
|
|
|
$days = 7; |
65
|
|
|
break; |
66
|
|
|
case 'month': |
|
|
|
|
67
|
|
|
$days = 30; |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
// Different dbs do it different ways |
71
|
|
|
$stats = null; |
72
|
|
|
$db = Craft::$app->getDb(); |
73
|
|
|
if ($db->getIsMysql()) { |
74
|
|
|
// Query the db |
75
|
|
|
$query = (new Query()) |
76
|
|
|
->from('{{%retour_stats}}') |
77
|
|
|
->select([ |
|
|
|
|
78
|
|
|
"DATE_FORMAT(hitLastTime, '%Y-%m-%d') AS date_formatted", |
79
|
|
|
'COUNT(redirectSrcUrl) AS cnt', |
80
|
|
|
'COUNT(handledByRetour = 1 or null) as handled_cnt', |
81
|
|
|
]) |
|
|
|
|
82
|
|
|
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
83
|
|
|
if ((int)$siteId !== 0) { |
84
|
|
|
$query->andWhere(['siteId' => $siteId]); |
85
|
|
|
} |
86
|
|
|
$query |
87
|
|
|
->orderBy('date_formatted ASC') |
88
|
|
|
->groupBy('date_formatted'); |
89
|
|
|
$stats = $query->all(); |
90
|
|
|
} |
91
|
|
|
if ($db->getIsPgsql()) { |
92
|
|
|
// Query the db |
93
|
|
|
$query = (new Query()) |
94
|
|
|
->from('{{%retour_stats}}') |
95
|
|
|
->select([ |
|
|
|
|
96
|
|
|
"to_char(\"hitLastTime\", 'yyyy-mm-dd') AS date_formatted", |
97
|
|
|
"COUNT(\"redirectSrcUrl\") AS cnt", |
98
|
|
|
"COUNT(CASE WHEN \"handledByRetour\" = true THEN 1 END) as handled_cnt", |
99
|
|
|
]) |
|
|
|
|
100
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
101
|
|
|
if ((int)$siteId !== 0) { |
102
|
|
|
$query->andWhere(['siteId' => $siteId]); |
103
|
|
|
} |
104
|
|
|
$query |
105
|
|
|
->orderBy('date_formatted ASC') |
106
|
|
|
->groupBy('date_formatted'); |
107
|
|
|
$stats = $query->all(); |
108
|
|
|
} |
109
|
|
|
if ($stats) { |
110
|
|
|
$data[] = [ |
111
|
|
|
'name' => '404 hits', |
112
|
|
|
'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'cnt')), |
113
|
|
|
'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')), |
114
|
|
|
]; |
115
|
|
|
$data[] = [ |
116
|
|
|
'name' => 'Handled 404 hits', |
117
|
|
|
'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'handled_cnt')), |
118
|
|
|
'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')), |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->asJson($data); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* The Dashboard chart |
127
|
|
|
* |
128
|
|
|
* @param int $days |
|
|
|
|
129
|
|
|
* |
130
|
|
|
* @return Response |
131
|
|
|
*/ |
132
|
|
|
public function actionWidget($days = 1): Response |
133
|
|
|
{ |
134
|
|
|
$data = []; |
135
|
|
|
// Different dbs do it different ways |
136
|
|
|
$stats = null; |
137
|
|
|
$handledStats = null; |
138
|
|
|
$db = Craft::$app->getDb(); |
139
|
|
|
if ($db->getIsMysql()) { |
140
|
|
|
// Query the db |
141
|
|
|
$stats = (new Query()) |
142
|
|
|
->from('{{%retour_stats}}') |
143
|
|
|
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )") |
144
|
|
|
->count(); |
145
|
|
|
$handledStats = (new Query()) |
146
|
|
|
->from('{{%retour_stats}}') |
147
|
|
|
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )") |
148
|
|
|
->andWhere('handledByRetour is TRUE') |
149
|
|
|
->count(); |
150
|
|
|
} |
151
|
|
|
if ($db->getIsPgsql()) { |
152
|
|
|
// Query the db |
153
|
|
|
$stats = (new Query()) |
154
|
|
|
->from('{{%retour_stats}}') |
155
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )") |
156
|
|
|
->count(); |
157
|
|
|
$handledStats = (new Query()) |
158
|
|
|
->from('{{%retour_stats}}') |
159
|
|
|
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )") |
160
|
|
|
->andWhere('"handledByRetour" = TRUE') |
161
|
|
|
->count(); |
162
|
|
|
} |
163
|
|
|
if ($stats) { |
164
|
|
|
$data = [ |
165
|
|
|
(int)$stats, |
166
|
|
|
(int)$handledStats, |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->asJson($data); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Protected Methods |
174
|
|
|
// ========================================================================= |
175
|
|
|
} |
176
|
|
|
|