@@ 16-124 (lines=109) @@ | ||
13 | * |
|
14 | * @package namespace VojtaSvoboda\UserAccessLog\ReportWidgets |
|
15 | */ |
|
16 | class AccessLogChartLineAggregated extends ReportWidgetBase |
|
17 | { |
|
18 | /** |
|
19 | * Renders the widget. |
|
20 | */ |
|
21 | public function render() |
|
22 | { |
|
23 | try { |
|
24 | $this->vars['all'] = $this->loadData(); |
|
25 | } |
|
26 | catch (Exception $ex) { |
|
27 | $this->vars['error'] = $ex->getMessage(); |
|
28 | $this->vars['all'] = ''; |
|
29 | } |
|
30 | ||
31 | return $this->makePartial('widget'); |
|
32 | } |
|
33 | ||
34 | /** |
|
35 | * Define widget properties |
|
36 | * |
|
37 | * @return array |
|
38 | */ |
|
39 | public function defineProperties() |
|
40 | { |
|
41 | return [ |
|
42 | 'title' => [ |
|
43 | 'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.title', |
|
44 | 'default' => 'Access statistics in time', |
|
45 | 'type' => 'string', |
|
46 | 'validationPattern' => '^.+$', |
|
47 | 'validationMessage' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.title_validation', |
|
48 | ], |
|
49 | 'days' => [ |
|
50 | 'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.chartlineaggregated.days_title', |
|
51 | 'default' => '30', |
|
52 | 'type' => 'string', |
|
53 | 'validationPattern' => '^[0-9]+$', |
|
54 | ] |
|
55 | ]; |
|
56 | } |
|
57 | ||
58 | protected function loadData() |
|
59 | { |
|
60 | $days = $this->property('days'); |
|
61 | if (!$days) { |
|
62 | throw new ApplicationException('Invalid days value: ' . $days); |
|
63 | } |
|
64 | ||
65 | // all accesses for last month |
|
66 | $items = AccessLog::where('created_at', '>=', Carbon::now()->subDays($days)->format('Y-m-d'))->get(); |
|
67 | ||
68 | // parse data |
|
69 | $all = $this->sortItemsToDays($items); |
|
70 | ||
71 | // we need at least two days, to display chart |
|
72 | if (sizeof($all) == 1) { |
|
73 | $day = reset($all); |
|
74 | $date = Carbon::createFromFormat('Y-m-d', $day['date'])->subDays(1); |
|
75 | $dateFormated = $date->format('Y-m-d'); |
|
76 | $all[$dateFormated] = [ |
|
77 | 'timestamp' => $date->timestamp * 1000, |
|
78 | 'date' => $dateFormated, |
|
79 | 'count' => 0, |
|
80 | ]; |
|
81 | } |
|
82 | ||
83 | // count accessess for each day |
|
84 | $all_render = []; |
|
85 | foreach ($all as $a) { |
|
86 | $all_render[] = [$a['timestamp'], $a['count']]; |
|
87 | } |
|
88 | ||
89 | return $all_render; |
|
90 | } |
|
91 | ||
92 | /** |
|
93 | * Sort items by day. |
|
94 | * |
|
95 | * @param $items |
|
96 | * |
|
97 | * @return array |
|
98 | */ |
|
99 | private function sortItemsToDays($items) |
|
100 | { |
|
101 | $all = []; |
|
102 | ||
103 | foreach ($items as $item) |
|
104 | { |
|
105 | // date |
|
106 | $timestamp = strtotime($item->created_at) * 1000; |
|
107 | $day = Carbon::createFromFormat('Y-m-d H:i:s', $item->created_at)->format('Y-m-d'); |
|
108 | ||
109 | // init empty day |
|
110 | if (!isset($all[$day])) { |
|
111 | $all[$day] = [ |
|
112 | 'timestamp' => $timestamp, |
|
113 | 'date' => $day, |
|
114 | 'count' => 0, |
|
115 | ]; |
|
116 | } |
|
117 | ||
118 | // increase count |
|
119 | $all[$day]['count']++; |
|
120 | } |
|
121 | ||
122 | return $all; |
|
123 | } |
|
124 | } |
|
125 |
@@ 15-118 (lines=104) @@ | ||
12 | * |
|
13 | * @package namespace VojtaSvoboda\UserAccessLog\ReportWidgets |
|
14 | */ |
|
15 | class Registrations extends ReportWidgetBase |
|
16 | { |
|
17 | /** |
|
18 | * Renders the widget. |
|
19 | */ |
|
20 | public function render() |
|
21 | { |
|
22 | try { |
|
23 | $this->vars['registrations'] = $this->loadData(); |
|
24 | ||
25 | } catch (Exception $ex) { |
|
26 | $this->vars['error'] = $ex->getMessage(); |
|
27 | $this->vars['registrations'] = []; |
|
28 | } |
|
29 | ||
30 | return $this->makePartial('widget'); |
|
31 | } |
|
32 | ||
33 | public function defineProperties() |
|
34 | { |
|
35 | return [ |
|
36 | 'title' => [ |
|
37 | 'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.title', |
|
38 | 'default' => 'New registrations', |
|
39 | 'type' => 'string', |
|
40 | 'validationPattern' => '^.+$', |
|
41 | 'validationMessage' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.title_validation', |
|
42 | ], |
|
43 | 'days' => [ |
|
44 | 'title' => 'vojtasvoboda.useraccesslog::lang.reportwidgets.registrations.days_title', |
|
45 | 'default' => '30', |
|
46 | 'type' => 'string', |
|
47 | 'validationPattern' => '^[0-9]+$', |
|
48 | ] |
|
49 | ]; |
|
50 | } |
|
51 | ||
52 | protected function loadData() |
|
53 | { |
|
54 | $days = $this->property('days'); |
|
55 | if (!$days) { |
|
56 | throw new ApplicationException('Invalid days value: ' . $days); |
|
57 | } |
|
58 | ||
59 | // all accesses for last month |
|
60 | $items = User::where('created_at', '>=', Carbon::now()->subDays($days)->format('Y-m-d'))->get(); |
|
61 | ||
62 | // parse data |
|
63 | $all = $this->sortItemsToDays($items); |
|
64 | ||
65 | // we need at least two days, to display chart |
|
66 | if (sizeof($all) == 1) { |
|
67 | $day = reset($all); |
|
68 | $date = Carbon::createFromFormat('Y-m-d', $day['date'])->subDays(1); |
|
69 | $dateFormated = $date->format('Y-m-d'); |
|
70 | $all[$dateFormated] = [ |
|
71 | 'timestamp' => $date->timestamp * 1000, |
|
72 | 'date' => $dateFormated, |
|
73 | 'count' => 0, |
|
74 | ]; |
|
75 | } |
|
76 | ||
77 | // count all |
|
78 | $all_render = []; |
|
79 | foreach ($all as $a) { |
|
80 | $all_render[] = [$a['timestamp'], $a['count']]; |
|
81 | } |
|
82 | ||
83 | return $all_render; |
|
84 | } |
|
85 | ||
86 | /** |
|
87 | * Sort items by days. |
|
88 | * |
|
89 | * @param $items |
|
90 | * |
|
91 | * @return array |
|
92 | */ |
|
93 | private function sortItemsToDays($items) |
|
94 | { |
|
95 | $all = []; |
|
96 | ||
97 | foreach ($items as $item) |
|
98 | { |
|
99 | // date |
|
100 | $timestamp = strtotime($item->created_at) * 1000; |
|
101 | $day = Carbon::createFromFormat('Y-m-d H:i:s', $item->created_at)->format('Y-m-d'); |
|
102 | ||
103 | // init empty day |
|
104 | if (!isset($all[$day])) { |
|
105 | $all[$day] = [ |
|
106 | 'timestamp' => $timestamp, |
|
107 | 'date' => $day, |
|
108 | 'count' => 0, |
|
109 | ]; |
|
110 | } |
|
111 | ||
112 | // increase count |
|
113 | $all[$day]['count']++; |
|
114 | } |
|
115 | ||
116 | return $all; |
|
117 | } |
|
118 | } |
|
119 |