Files
UNIT3D-Community-Edition/app/Http/Livewire/InviteLogSearch.php
Roardom b274347313 refactor: use whereRelation when possible
All changes were tested for performance regressions. None of the included changes were affected. However, the code that uses `whereIn('category_id', Category::select('id')->where('movie_meta', '=', 1)` saw a ~10% performance loss so should not be changed.
2024-03-03 15:56:02 +00:00

148 lines
5.3 KiB
PHP

<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Livewire;
use App\Models\Invite;
use App\Traits\LivewireSort;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Livewire\WithPagination;
class InviteLogSearch extends Component
{
use LivewireSort;
use WithPagination;
public string $sender = '';
public string $email = '';
public string $code = '';
public string $receiver = '';
public string $custom = '';
public string $groupBy = 'none';
public int $threshold = 25;
public string $sortField = 'created_at';
public string $sortDirection = 'desc';
public int $perPage = 25;
/**
* @var array<mixed>
*/
protected $queryString = [
'sender' => ['except' => ''],
'email' => ['except' => ''],
'code' => ['except' => ''],
'receiver' => ['except' => ''],
'custom' => ['except' => ''],
'groupBy' => ['except' => 'none'],
'threshold' => ['except' => 25],
'page' => ['except' => 1],
'sortField' => ['except' => 'created_at'],
'sortDirection' => ['except' => 'desc'],
'perPage' => ['except' => ''],
];
final public function mount(): void
{
$this->sortField = match ($this->groupBy) {
'user_id' => 'created_at_max',
default => 'created_at',
};
}
final public function updatedPage(): void
{
$this->emit('paginationChanged');
}
final public function updatingGroupBy($value): void
{
$this->sortField = match ($value) {
'user_id' => 'created_at_max',
default => 'created_at',
};
}
/**
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator<Invite>
*/
final public function getInvitesProperty(): \Illuminate\Contracts\Pagination\LengthAwarePaginator
{
return Invite::withTrashed()
->with(['sender.group', 'receiver.group'])
->when($this->sender, fn ($query) => $query->whereRelation('sender', 'username', '=', $this->sender))
->when($this->email, fn ($query) => $query->where('email', 'LIKE', '%'.$this->email.'%'))
->when($this->code, fn ($query) => $query->where('code', 'LIKE', '%'.$this->code.'%'))
->when($this->receiver, fn ($query) => $query->whereRelation('sender', 'username', '=', $this->receiver))
->when($this->custom, fn ($query) => $query->where('custom', 'LIKE', '%'.$this->custom.'%'))
->when(
$this->groupBy === 'user_id',
fn ($query) => $query->groupBy('user_id')
->from('invites as i1')
->select([
'user_id',
DB::raw('MIN(created_at) as created_at_min'),
DB::raw('FROM_UNIXTIME(AVG(UNIX_TIMESTAMP(created_at))) as created_at_avg'),
DB::raw('MAX(created_at) as created_at_max'),
DB::raw('COUNT(*) as sent_count'),
DB::raw('SUM(IF(accepted_by IS NULL, 0, 1)) as accepted_by_count'),
DB::raw("
(select
count(*)
from
users
where
id in (select accepted_by from invites i2 where i2.user_id = i1.user_id)
and group_id in (select id from `groups` where slug in ('banned', 'pruned', 'disabled'))
) as inactive_count
"),
DB::raw("
100.0 *
(select
count(*)
from
users
where
id in (select accepted_by from invites i2 where i2.user_id = i1.user_id)
and group_id in (select id from `groups` where slug in ('banned', 'pruned', 'disabled'))
)
/ COUNT(*) as inactive_ratio
"),
])
->withCasts([
'created_at_min' => 'datetime',
'created_at_avg' => 'datetime',
'created_at_max' => 'datetime',
])
)
->orderBy($this->sortField, $this->sortDirection)
->paginate($this->perPage);
}
final public function render(): \Illuminate\Contracts\View\View|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\Foundation\Application
{
return view('livewire.invite-log-search', [
'invites' => $this->invites,
]);
}
}