1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace awssat\Visits\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
trait Lists |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Fetch all time trending subjects. |
11
|
|
|
* |
12
|
|
|
* @param int $limit |
13
|
|
|
* @param bool $isLow |
14
|
|
|
* @return \Illuminate\Support\Collection|array |
15
|
|
|
*/ |
16
|
|
|
public function top($limit = 5, $isLow = false) |
17
|
|
|
{ |
18
|
|
|
$cacheKey = $this->keys->cache($limit, $isLow); |
19
|
|
|
$cachedList = $this->cachedList($limit, $cacheKey); |
20
|
|
|
$visitsIds = $this->getVisitsIds($limit, $this->keys->visits, $isLow); |
21
|
|
|
|
22
|
|
|
if($visitsIds === $cachedList->pluck($this->keys->primary)->toArray() && ! $this->fresh) { |
23
|
|
|
return $cachedList; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $this->freshList($cacheKey, $visitsIds); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Top/low countries |
32
|
|
|
* |
33
|
|
|
* @param int $limit |
34
|
|
|
* @param bool $isLow |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function countries($limit = -1, $isLow = false) |
38
|
|
|
{ |
39
|
|
|
$range = $isLow ? 'zrange' : 'zrevrange'; |
40
|
|
|
|
41
|
|
|
return $this->redis->$range($this->keys->visits . "_countries:{$this->keys->id}", 0, $limit, 'WITHSCORES'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* top/lows refs |
46
|
|
|
* |
47
|
|
|
* @param int $limit |
48
|
|
|
* @param bool $isLow |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function refs($limit = -1, $isLow = false) |
52
|
|
|
{ |
53
|
|
|
$range = $isLow ? 'zrange' : 'zrevrange'; |
54
|
|
|
|
55
|
|
|
return $this->redis->$range($this->keys->visits . "_referers:{$this->keys->id}", 0, $limit, 'WITHSCORES'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* top/lows operating systems |
60
|
|
|
* |
61
|
|
|
* @param int $limit |
62
|
|
|
* @param bool $isLow |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function operatingSystems($limit = -1, $isLow = false) |
66
|
|
|
{ |
67
|
|
|
$range = $isLow ? 'zrange' : 'zrevrange'; |
68
|
|
|
|
69
|
|
|
return $this->redis->$range($this->keys->visits . "_OSes:{$this->keys->id}", 0, $limit, 'WITHSCORES'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* top/lows languages |
74
|
|
|
* |
75
|
|
|
* @param int $limit |
76
|
|
|
* @param bool $isLow |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function languages($limit = -1, $isLow = false) |
80
|
|
|
{ |
81
|
|
|
$range = $isLow ? 'zrange' : 'zrevrange'; |
82
|
|
|
|
83
|
|
|
return $this->redis->$range($this->keys->visits . "_languages:{$this->keys->id}", 0, $limit, 'WITHSCORES'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Fetch lowest subjects. |
88
|
|
|
* |
89
|
|
|
* @param int $limit |
90
|
|
|
* @return \Illuminate\Support\Collection|array |
91
|
|
|
*/ |
92
|
|
|
public function low($limit = 5) |
93
|
|
|
{ |
94
|
|
|
return $this->top($limit, true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $limit |
100
|
|
|
* @param $visitsKey |
101
|
|
|
* @param bool $isLow |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
protected function getVisitsIds($limit, $visitsKey, $isLow = false) |
105
|
|
|
{ |
106
|
|
|
$range = $isLow ? 'zrange' : 'zrevrange'; |
107
|
|
|
|
108
|
|
|
return array_map('intval', $this->redis->$range($visitsKey, 0, $limit - 1)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $cacheKey |
113
|
|
|
* @param $visitsIds |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
protected function freshList($cacheKey, $visitsIds) |
117
|
|
|
{ |
118
|
|
|
if (count($visitsIds)) { |
119
|
|
|
$this->redis->del($cacheKey); |
120
|
|
|
|
121
|
|
|
return ($this->subject)::whereIn($this->keys->primary, $visitsIds) |
122
|
|
|
->get() |
123
|
|
|
->sortBy(function ($subject) use ($visitsIds) { |
124
|
|
|
return array_search($subject->{$this->keys->primary}, $visitsIds); |
125
|
|
|
})->each(function ($subject) use ($cacheKey) { |
126
|
|
|
$this->redis->rpush($cacheKey, serialize($subject)); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return []; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $limit |
135
|
|
|
* @param $cacheKey |
136
|
|
|
* @return \Illuminate\Support\Collection|array |
137
|
|
|
*/ |
138
|
|
|
protected function cachedList($limit, $cacheKey) |
139
|
|
|
{ |
140
|
|
|
return Collection::make( |
141
|
|
|
array_map('unserialize', $this->redis->lrange($cacheKey, 0, $limit - 1)) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|