-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ChartsController.php
149 lines (136 loc) · 4.39 KB
/
ChartsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Retour plugin for Craft CMS 3.x
*
* Retour allows you to intelligently redirect legacy URLs, so that you don't
* lose SEO value when rebuilding & restructuring a website
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2018 nystudio107
*/
namespace nystudio107\retour\controllers;
use Craft;
use nystudio107\retour\helpers\Permission as PermissionHelper;
use craft\db\Query;
use craft\helpers\ArrayHelper;
use craft\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\Response;
/**
* @author nystudio107
* @package Retour
* @since 3.0.0
*/
class ChartsController extends Controller
{
// Constants
// =========================================================================
// Protected Properties
// =========================================================================
/**
* @var bool|array
*/
protected $allowAnonymous = [
];
// Public Methods
// =========================================================================
/**
* The Dashboard chart
*
* @param string $range
*
* @return Response
* @throws ForbiddenHttpException
*/
public function actionDashboard(string $range = 'day'): Response
{
PermissionHelper::controllerPermissionCheck('retour:dashboard');
$data = [];
$days = 1;
switch ($range) {
case 'day':
$days = 1;
break;
case 'week':
$days = 7;
break;
case 'month':
$days = 30;
break;
}
// Different dbs do it different ways
$stats = null;
$db = Craft::$app->getDb();
if ($db->getIsMysql()) {
// Query the db
$stats = (new Query())
->from('{{%retour_stats}}')
->select([
"DATE_FORMAT(hitLastTime, '%Y-%m-%d') AS date_formatted",
'COUNT(redirectSrcUrl) AS cnt',
'COUNT(handledByRetour = 1 or null) as handled_cnt',
])
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )")
->orderBy('date_formatted ASC')
->groupBy('date_formatted')
->all();
}
if ($db->getIsPgsql()) {
// Query the db
$stats = (new Query())
->from('{{%retour_stats}}')
->select([
"to_char(\"hitLastTime\", 'yyyy-mm-dd') AS date_formatted",
"COUNT(\"redirectSrcUrl\") AS cnt",
"COUNT(CASE WHEN \"handledByRetour\" = true THEN 1 END) as handled_cnt",
])
->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )")
->orderBy('date_formatted ASC')
->groupBy('date_formatted')
->all();
}
if ($stats) {
$data[] = [
'name' => '404 hits',
'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'cnt')),
'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')),
];
$data[] = [
'name' => 'Handled 404 hits',
'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'handled_cnt')),
'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')),
];
}
return $this->asJson($data);
}
/**
* The Dashboard chart
*
* @param int $days
*
* @return Response
*/
public function actionWidget($days = 1): Response
{
$data = [];
// Query the db
$stats = (new Query())
->from('{{%retour_stats}}')
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )")
->count();
$handledStats = (new Query())
->from('{{%retour_stats}}')
->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )")
->andWhere('handledByRetour is TRUE')
->count();
if ($stats) {
$data = [
(int)$stats,
(int)$handledStats,
];
}
return $this->asJson($data);
}
// Protected Methods
// =========================================================================
}