1
|
|
|
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Tracker\Models\Session; |
4
|
|
|
use Arcanesoft\Tracker\Support\DateRange; |
5
|
|
|
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Contracts\View\View; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CountriesListComposer |
12
|
|
|
* |
13
|
|
|
* @package Arcanesoft\Tracker\ViewComposers\Dashboard |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class CountriesListComposer extends AbstractViewComposer |
17
|
|
|
{ |
18
|
|
|
/* ------------------------------------------------------------------------------------------------ |
19
|
|
|
| Constants |
20
|
|
|
| ------------------------------------------------------------------------------------------------ |
21
|
|
|
*/ |
22
|
|
|
const VIEW = 'tracker::foundation._composers.dashboard.countries-ratio-list'; |
23
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
25
|
|
|
| Main Functions |
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
27
|
|
|
*/ |
28
|
|
|
/** |
29
|
|
|
* Compose the view. |
30
|
|
|
* |
31
|
|
|
* @param \Illuminate\Contracts\View\View $view |
32
|
|
|
*/ |
33
|
|
|
public function compose(View $view) |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var \Carbon\Carbon $start |
37
|
|
|
* @var \Carbon\Carbon $end |
38
|
|
|
* @var \Illuminate\Support\Collection $range |
39
|
|
|
*/ |
40
|
|
|
extract(DateRange::getCurrentMonthDaysRange()); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$view->with('countriesRatio', $this->calculateCountriesPercentage( |
43
|
|
|
$this->getCountriesCountFromSessions($start, $end) |
44
|
|
|
)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* ------------------------------------------------------------------------------------------------ |
48
|
|
|
| Other Functions |
49
|
|
|
| ------------------------------------------------------------------------------------------------ |
50
|
|
|
*/ |
51
|
|
|
/** |
52
|
|
|
* Get the countries count from sessions. |
53
|
|
|
* |
54
|
|
|
* @param \Carbon\Carbon $start |
55
|
|
|
* @param \Carbon\Carbon $end |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Support\Collection |
58
|
|
|
*/ |
59
|
|
|
private function getCountriesCountFromSessions(Carbon $start, Carbon $end) |
60
|
|
|
{ |
61
|
|
|
return $this->getCachedVisitors() |
62
|
|
|
->filter(function (Session $visitor) use ($start, $end) { |
63
|
|
|
return $visitor->updated_at->between($start, $end); |
64
|
|
|
}) |
65
|
|
|
->transform(function (Session $visitor) { |
66
|
|
|
return is_null($visitor->geoip) |
67
|
|
|
? [ |
68
|
|
|
'code' => 'undefined', |
69
|
|
|
'geoip' => null, |
70
|
|
|
] |
71
|
|
|
: [ |
72
|
|
|
'code' => $visitor->geoip->iso_code, |
73
|
|
|
'geoip' => $visitor->geoip, |
74
|
|
|
]; |
75
|
|
|
}) |
76
|
|
|
->groupBy('code') |
77
|
|
|
->transform(function (Collection $items, $key) { |
78
|
|
|
return ($key === 'undefined') |
79
|
|
|
? [ |
80
|
|
|
'code' => null, |
81
|
|
|
'name' => trans('tracker::geoip.undefined'), |
82
|
|
|
'count' => $items->count(), |
83
|
|
|
] |
84
|
|
|
: [ |
85
|
|
|
'code' => $key, |
86
|
|
|
'name' => $items->first()['geoip']->country, |
87
|
|
|
'count' => $items->count(), |
88
|
|
|
]; |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Calculate countries percentage. |
94
|
|
|
* |
95
|
|
|
* @param \Illuminate\Support\Collection $countries |
96
|
|
|
* |
97
|
|
|
* @return \Illuminate\Support\Collection |
98
|
|
|
*/ |
99
|
|
|
private function calculateCountriesPercentage(Collection $countries) |
100
|
|
|
{ |
101
|
|
|
$total = $countries->sum('count'); |
102
|
|
|
|
103
|
|
|
return $countries->transform(function ($item) use ($total) { |
104
|
|
|
return $item + [ |
105
|
|
|
'percentage' => round(($item['count'] / $total) * 100, 2) |
106
|
|
|
]; |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|