1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-02-01 12:41:37 +01:00

Improved some query efficiencies on user list

This commit is contained in:
Dan Brown 2021-01-10 22:43:22 +00:00
parent 28c706fee3
commit d0a7a8b890
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 23 additions and 18 deletions

View File

@ -1,19 +1,20 @@
<?php namespace BookStack\Auth; <?php namespace BookStack\Auth;
use BookStack\Actions\Activity;
use BookStack\Api\ApiToken; use BookStack\Api\ApiToken;
use BookStack\Interfaces\Loggable; use BookStack\Interfaces\Loggable;
use BookStack\Model; use BookStack\Model;
use BookStack\Notifications\ResetPassword; use BookStack\Notifications\ResetPassword;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
use Carbon\Carbon; use Carbon\Carbon;
use Exception;
use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -46,6 +47,8 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
*/ */
protected $fillable = ['name', 'email']; protected $fillable = ['name', 'email'];
protected $casts = ['last_activity_at' => 'datetime'];
/** /**
* The attributes excluded from the model's JSON form. * The attributes excluded from the model's JSON form.
* @var array * @var array
@ -181,7 +184,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
/** /**
* Get the social account associated with this user. * Get the social account associated with this user.
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return HasMany
*/ */
public function socialAccounts() public function socialAccounts()
{ {
@ -218,7 +221,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
try { try {
$avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default; $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
} catch (\Exception $err) { } catch (Exception $err) {
$avatar = $default; $avatar = $default;
} }
return $avatar; return $avatar;
@ -226,7 +229,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
/** /**
* Get the avatar for the user. * Get the avatar for the user.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @return BelongsTo
*/ */
public function avatar() public function avatar()
{ {
@ -242,11 +245,16 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
} }
/** /**
* Get the latest activity instance for this user. * Get the last activity time for this user.
*/ */
public function latestActivity(): HasOne public function scopeWithLastActivityAt(Builder $query)
{ {
return $this->hasOne(Activity::class)->latest(); $query->addSelect(['activities.created_at as last_activity_at'])
->leftJoinSub(function (\Illuminate\Database\Query\Builder $query) {
$query->from('activities')->select('user_id')
->selectRaw('max(created_at) as created_at')
->groupBy('user_id');
}, 'activities', 'users.id', '=', 'activities.user_id');
} }
/** /**

View File

@ -59,14 +59,10 @@ class UserRepo
public function getAllUsersPaginatedAndSorted(int $count, array $sortData): LengthAwarePaginator public function getAllUsersPaginatedAndSorted(int $count, array $sortData): LengthAwarePaginator
{ {
$sort = $sortData['sort']; $sort = $sortData['sort'];
if ($sort === 'latest_activity') {
$sort = \BookStack\Actions\Activity::query()->select('created_at')
->whereColumn('activities.user_id', 'users.id')
->latest()
->take(1);
}
$query = User::query()->with(['roles', 'avatar', 'latestActivity']) $query = User::query()->select(['*'])
->withLastActivityAt()
->with(['roles', 'avatar'])
->orderBy($sort, $sortData['order']); ->orderBy($sort, $sortData['order']);
if ($sortData['search']) { if ($sortData['search']) {

View File

@ -41,6 +41,7 @@ class UserController extends Controller
'sort' => $request->get('sort', 'name'), 'sort' => $request->get('sort', 'name'),
]; ];
$users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails); $users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails);
$this->setPageTitle(trans('settings.users')); $this->setPageTitle(trans('settings.users'));
$users->appends($listDetails); $users->appends($listDetails);
return view('users.index', ['users' => $users, 'listDetails' => $listDetails]); return view('users.index', ['users' => $users, 'listDetails' => $listDetails]);

View File

@ -37,7 +37,7 @@
</th> </th>
<th>{{ trans('settings.role_user_roles') }}</th> <th>{{ trans('settings.role_user_roles') }}</th>
<th class="text-right"> <th class="text-right">
<a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'latest_activity']) }}">{{ trans('settings.users_latest_activity') }}</a> <a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'last_activity_at']) }}">{{ trans('settings.users_latest_activity') }}</a>
</th> </th>
</tr> </tr>
@foreach($users as $user) @foreach($users as $user)
@ -58,8 +58,8 @@
@endforeach @endforeach
</td> </td>
<td class="text-right text-muted"> <td class="text-right text-muted">
@if($user->latestActivity) @if($user->last_activity_at)
<small title="{{ $user->latestActivity->created_at->format('Y-m-d H:i:s') }}">{{ $user->latestActivity->created_at->diffForHumans() }}</small> <small title="{{ $user->last_activity_at->format('Y-m-d H:i:s') }}">{{ $user->last_activity_at->diffForHumans() }}</small>
@endif @endif
</td> </td>
</tr> </tr>