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 Illuminate\Contracts\View\View; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class BrowsersRatioComposer |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Tracker\ViewComposers\Dashboard |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class BrowsersRatioComposer extends AbstractViewComposer |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Constants |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
const VIEW = 'tracker::foundation._composers.dashboard.browsers-ratio-chart'; |
21
|
|
|
|
22
|
|
|
/* ------------------------------------------------------------------------------------------------ |
23
|
|
|
| Main Functions |
24
|
|
|
| ------------------------------------------------------------------------------------------------ |
25
|
|
|
*/ |
26
|
|
|
/** |
27
|
|
|
* Compose the view. |
28
|
|
|
* |
29
|
|
|
* @param \Illuminate\Contracts\View\View $view |
30
|
|
|
*/ |
31
|
|
|
public function compose(View $view) |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var \Carbon\Carbon $start |
35
|
|
|
* @var \Carbon\Carbon $end |
36
|
|
|
* @var \Illuminate\Support\Collection $range |
37
|
|
|
*/ |
38
|
|
|
extract(DateRange::getCurrentMonthDaysRange()); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$view->with('browsersRatio', $this->getBrowsersCountFromSessions($start, $end)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/* ------------------------------------------------------------------------------------------------ |
44
|
|
|
| Other Functions |
45
|
|
|
| ------------------------------------------------------------------------------------------------ |
46
|
|
|
*/ |
47
|
|
|
/** |
48
|
|
|
* Get the browsers count from sessions. |
49
|
|
|
* |
50
|
|
|
* @param \Carbon\Carbon $start |
51
|
|
|
* @param \Carbon\Carbon $end |
52
|
|
|
* |
53
|
|
|
* @return \Illuminate\Support\Collection $range |
54
|
|
|
*/ |
55
|
|
|
private function getBrowsersCountFromSessions($start, $end) |
56
|
|
|
{ |
57
|
|
|
return $this->getCachedVisitors() |
58
|
|
|
->filter(function (Session $visitor) use ($start, $end) { |
59
|
|
|
return $visitor->updated_at->between($start, $end) && ! is_null($visitor->agent); |
60
|
|
|
}) |
61
|
|
|
->transform(function (Session $visitor) { |
62
|
|
|
return $visitor->agent; |
63
|
|
|
}) |
64
|
|
|
->groupBy('browser') |
65
|
|
|
->transform(function ($items, $key) { |
66
|
|
|
return [ |
67
|
|
|
'name' => $key, |
68
|
|
|
'count' => $items->count(), |
69
|
|
|
]; |
70
|
|
|
}) |
71
|
|
|
->sortByDesc('count'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|