|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tracking\Http\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Tracking\Jobs\CrunchStatistics; |
|
10
|
|
|
use Tracking\Jobs\CleanStatisticsRequests; |
|
11
|
|
|
|
|
12
|
|
|
class TrackStatistics |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Handle an incoming request. |
|
16
|
|
|
* |
|
17
|
|
|
* @param \Illuminate\Http\Request $request |
|
18
|
|
|
* @param \Closure $next |
|
19
|
|
|
* |
|
20
|
|
|
* @return mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
public function handle(Request $request, Closure $next) |
|
23
|
|
|
{ |
|
24
|
|
|
return $next($request); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// /** |
|
28
|
|
|
// * Perform any final actions for the request lifecycle. |
|
29
|
|
|
// * |
|
30
|
|
|
// * @param \Illuminate\Http\Request $request |
|
31
|
|
|
// * @param \Symfony\Component\HttpFoundation\Response $response |
|
32
|
|
|
// * |
|
33
|
|
|
// * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
|
34
|
|
|
// * |
|
35
|
|
|
// * @return void |
|
36
|
|
|
// */ |
|
37
|
|
|
// public function terminate($request, $response): void |
|
38
|
|
|
// { |
|
39
|
|
|
// $currentUser = $request->user(); |
|
40
|
|
|
|
|
41
|
|
|
// app('tracking.statistics.datum')->fill([ |
|
42
|
|
|
// 'session_id' => $request->session()->getId(), |
|
43
|
|
|
// 'user_id' => optional($currentUser)->getKey(), |
|
44
|
|
|
// 'user_type' => optional($currentUser)->getMorphClass(), |
|
45
|
|
|
// 'status_code' => $response->getStatusCode(), |
|
46
|
|
|
// 'uri' => $request->getUri(), |
|
47
|
|
|
// 'method' => $request->getMethod(), |
|
48
|
|
|
// 'server' => $request->server() ?: null, |
|
49
|
|
|
// 'input' => $request->input() ?: null, |
|
50
|
|
|
// 'created_at' => now(), |
|
51
|
|
|
// ])->save(); |
|
52
|
|
|
|
|
53
|
|
|
// // Here we will see if this request hits the statistics crunching lottery by hitting |
|
54
|
|
|
// // the odds needed to perform statistics crunching on any given request. If we do |
|
55
|
|
|
// // hit it, we'll call this handler to let it crunch numbers and the hard work. |
|
56
|
|
|
// if ($this->configHitsLottery()) { |
|
57
|
|
|
// CrunchStatistics::dispatch(); |
|
58
|
|
|
|
|
59
|
|
|
// // Now let's do some garbage collection and clean old statistics requests |
|
60
|
|
|
// CleanStatisticsRequests::dispatch(); |
|
61
|
|
|
// } |
|
62
|
|
|
// } |
|
63
|
|
|
|
|
64
|
|
|
// /** |
|
65
|
|
|
// * Determine if the configuration odds hit the lottery. |
|
66
|
|
|
// * |
|
67
|
|
|
// * @return bool |
|
68
|
|
|
// */ |
|
69
|
|
|
// protected function configHitsLottery(): bool |
|
70
|
|
|
// { |
|
71
|
|
|
// $config = \Illuminate\Support\Facades\Config::get('tracking.statistics.lottery'); |
|
72
|
|
|
|
|
73
|
|
|
// return $config ? random_int(1, $config[1]) <= $config[0] : false; |
|
74
|
|
|
// } |
|
75
|
|
|
} |
|
76
|
|
|
|