|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VojtaSvoboda\UserAccessLog\ReportWidgets; |
|
4
|
|
|
|
|
5
|
|
|
use App; |
|
6
|
|
|
use ApplicationException; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Backend\Classes\ReportWidgetBase; |
|
10
|
|
|
use VojtaSvoboda\UserAccessLog\Models\AccessLog; |
|
11
|
|
|
use RainLab\User\Models\User; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Demands count overview widget. |
|
15
|
|
|
* |
|
16
|
|
|
* @package namespace VojtaSvoboda\UserAccessLog\ReportWidgets |
|
17
|
|
|
*/ |
|
18
|
|
|
class AccessLogChartLineAggregated extends ReportWidgetBase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Renders the widget. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function render() |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
|
|
$this->vars['all'] = $this->loadData(); |
|
27
|
|
|
} |
|
28
|
|
|
catch (Exception $ex) { |
|
29
|
|
|
$this->vars['error'] = $ex->getMessage(); |
|
30
|
|
|
$this->vars['all'] = ''; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $this->makePartial('widget'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Define widget properties |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function defineProperties() |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
|
|
'title' => [ |
|
45
|
|
|
'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.title', |
|
46
|
|
|
'default' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.title_default', |
|
47
|
|
|
'type' => 'string', |
|
48
|
|
|
'validationPattern' => '^.+$', |
|
49
|
|
|
'validationMessage' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.title_validation', |
|
50
|
|
|
], |
|
51
|
|
|
'days' => [ |
|
52
|
|
|
'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.days_title', |
|
53
|
|
|
'default' => '30', |
|
54
|
|
|
'type' => 'string', |
|
55
|
|
|
'validationPattern' => '^[0-9]+$', |
|
56
|
|
|
] |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function loadData() |
|
61
|
|
|
{ |
|
62
|
|
|
$days = $this->property('days'); |
|
63
|
|
|
if (!$days) { |
|
64
|
|
|
throw new ApplicationException('Invalid days value: ' . $days); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// all accesses for last month |
|
68
|
|
|
$items = AccessLog::where('created_at', '>=', Carbon::now()->subDays($days)->format('Y-m-d'))->get(); |
|
69
|
|
|
|
|
70
|
|
|
// parse data |
|
71
|
|
|
$all = []; |
|
72
|
|
|
foreach ($items as $item) |
|
73
|
|
|
{ |
|
74
|
|
|
// date |
|
75
|
|
|
$timestamp = strtotime($item->created_at) * 1000; |
|
76
|
|
|
$day = Carbon::createFromFormat('Y-m-d H:i:s', $item->created_at)->format('Y-m-d'); |
|
77
|
|
|
|
|
78
|
|
|
if (isset($all[$day])) { |
|
79
|
|
|
$all[$day][1]++; |
|
80
|
|
|
} else { |
|
81
|
|
|
$all[$day] = [$timestamp, 1]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// count accessess for each day |
|
86
|
|
|
$all_render = []; |
|
87
|
|
|
foreach ($all as $a) { |
|
88
|
|
|
$all_render[] = [$a[0], $a[1]]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return str_replace('"', '', substr(substr(json_encode($all_render), 1), 0, -1)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|