forked from bilfeldt/laravel-route-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaravelRouteStatisticsCommand.php
130 lines (111 loc) · 4.23 KB
/
LaravelRouteStatisticsCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Bilfeldt\LaravelRouteStatistics\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class LaravelRouteStatisticsCommand extends Command
{
public $signature = 'route:stats
{--user=*}
{--team=*}
{--method=*}
{--route=}
{--status=}
{--ip=}
{--after=}
{--before=}
{--date=}
{--sort=-counter}
{--group=*}
{--limit=20}
';
public $description = 'Show route usage statistics';
public function handle()
{
$query = $this->getQuery();
$fields = $this->getFields();
if ($this->option('group')) {
$query->select($this->option('group'))
->addSelect(DB::raw('MAX(date) as last_used'))
->addSelect(DB::raw('SUM(counter) as counter'));
} else {
$query->select($fields);
}
$this->applyFilters($query);
$this->applyGrouping($query);
$this->applySorting($query);
$results = $query->limit($this->option('limit'))->get();
$this->table(
$fields,
$results->toArray()
);
return Command::SUCCESS;
}
protected function getModelClass(): string
{
return config('route-statistics.model');
}
protected function getQuery(): Builder
{
/** @var Builder */
return ($this->getModelClass())::query();
}
protected function applyFilters(Builder $query): Builder
{
return $query->when(! empty($this->option('user')), function (Builder $query) {
$query->whereIn('user_id', $this->option('user'));
})->when(! empty($this->option('team')), function (Builder $query) {
$query->whereIn('team_id', $this->option('team'));
})->when(! empty($this->option('method')), function (Builder $query) {
$query->whereIn('method', $this->option('method'));
})->when(! empty($this->option('route')), function (Builder $query) {
$query->where('route', 'LIKE', $this->option('route'));
})->when(! empty($this->option('status')), function (Builder $query) {
$query->where('status', 'LIKE', $this->option('status'));
})->when(! empty($this->option('ip')), function (Builder $query) {
$query->where('ip', 'LIKE', $this->option('ip'));
})->when(! empty($this->option('after')), function (Builder $query) {
$query->where('date', '>=', $this->option('after'));
})->when(! empty($this->option('before')), function (Builder $query) {
$query->where('date', '<=', $this->option('before'));
})->when(! empty($this->option('date')), function (Builder $query) {
$query->whereDate('date', $this->option('date'));
});
}
protected function applyGrouping(Builder $query): Builder
{
return $query->when($this->option('group'), function (Builder $query) {
$query->groupBy($this->option('group'));
});
}
protected function applySorting(Builder $query): Builder
{
$sort = $this->option('sort');
if (Str::startsWith($sort, '-')) {
$sort = substr($sort, 1);
$order = 'desc';
} else {
$order = 'asc';
}
return $query->orderBy($sort, $order);
}
protected function getFields(): array
{
if ($this->option('group')) {
return array_merge($this->option('group'), ['last_used', 'counter']);
}
return array_filter([
'id',
'user_id',
'team_id',
'method',
'route',
'status',
config('route-statistics.store_parameters') === true ? 'parameters' : null,
'ip',
'date',
'counter',
]);
}
}