Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 41 additions & 49 deletions app/Http/Controllers/Backend/LeaderboardController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Backend;

use App\Helpers\CacheKey;
Expand All @@ -19,7 +21,7 @@ class LeaderboardController extends Controller
{
private const string CACHE_RETENTION_CONFIG_KEY = 'trwl.cache.leaderboard-retention-seconds';

private $ttl;
private int $ttl;

public function __construct()
{
Expand All @@ -32,9 +34,7 @@ public function getCachedGlobalLeaderboard(): Collection
CacheKey::LEADERBOARD_GLOBAL_POINTS,
$this->ttl,
fn () => $this->getLeaderboard()
)->filter(function (stdClass $row) {
return Gate::allows('view', $row->user);
});
)->filter(fn (stdClass $row) => Gate::allows('view', $row->user));
}

public function getCachedFriendsLeaderboard(): ?Collection
Expand All @@ -53,9 +53,7 @@ public function getCachedDistanceLeaderboard(): Collection
CacheKey::LEADERBOARD_GLOBAL_DISTANCE,
$this->ttl,
fn () => $this->getLeaderboard(orderBy: 'distance')
)->filter(function (stdClass $row) {
return Gate::allows('view', $row->user);
});
)->filter(fn (stdClass $row) => Gate::allows('view', $row->user));
}

private function getLeaderboard(
Expand Down Expand Up @@ -85,22 +83,16 @@ private function getLeaderboard(
}

$sumDistance = 'SUM(train_checkins.distance)';

$query = DB::table('statuses')
->join('train_checkins', 'train_checkins.status_id', '=', 'statuses.id')
->join('users', 'statuses.user_id', '=', 'users.id')
->where('train_checkins.departure', '>=', $since->toIso8601String())
->where('train_checkins.departure', '<=', $until->toIso8601String())
->where(function (Builder $query) {
$query->where('users.private_profile', 0);
if (auth()->check()) {
$query->orWhereIn('users.id', auth()->user()->follows->pluck('id'))
->orWhere('users.id', auth()->user()->id);
}
})
->groupBy('statuses.user_id')
$followIds = auth()->check() ? auth()->user()->follows->pluck('id') : collect();

$query = DB::table('train_checkins')
->join('users', 'train_checkins.user_id', '=', 'users.id')
->where('train_checkins.departure', '>=', $since->utc()->format('Y-m-d H:i:s'))
->where('train_checkins.departure', '<=', $until->utc()->format('Y-m-d H:i:s'))
->where(fn (Builder $q) => $this->applyPrivacyFilter($q, $followIds))
->groupBy('train_checkins.user_id')
->select([
'statuses.user_id',
'train_checkins.user_id',
DB::raw('SUM(train_checkins.points) AS points'),
DB::raw($sumDistance . ' AS distance'),
DB::raw(self::getDurationSelector() . ' AS duration'),
Expand All @@ -110,16 +102,18 @@ private function getLeaderboard(
->limit($limit);

if ($onlyFollowings && auth()->check()) {
$query->where(function ($query) {
$query->whereIn('statuses.user_id', auth()->user()->follows->pluck('id'))
->orWhere('statuses.user_id', auth()->user()->id);
$query->where(function (Builder $q) use ($followIds): void {
$q->whereIn('train_checkins.user_id', $followIds)
->orWhere('train_checkins.user_id', auth()->id());
});
}

$data = $query->get();

// Fetch user models in ONE query and map it to the collection
$userCache = User::with(['blockedByUsers', 'blockedUsers'])->whereIn('id', $data->pluck('user_id'))->get();
$userCache = User::with(['blockedByUsers', 'blockedUsers'])
->whereIn('id', $data->pluck('user_id'))
->get();

return $data->map(function ($row) use ($userCache) {
$row->user = $userCache->where('id', $row->user_id)->first();
Expand All @@ -134,40 +128,29 @@ public static function getMonthlyLeaderboard(Carbon $date): Collection
return collect();
}

$data = DB::table('statuses')
->join('train_checkins', 'train_checkins.status_id', '=', 'statuses.id')
->join('users', 'statuses.user_id', '=', 'users.id')
->where(
'train_checkins.departure',
'>=',
$date->clone()->firstOfMonth()->toIso8601String()
)
->where(
'train_checkins.departure',
'<=',
$date->clone()->lastOfMonth()->endOfDay()->toIso8601String()
)
->where(function (Builder $query) {
$query->where('users.private_profile', 0);
if (auth()->check()) {
$query->orWhereIn('users.id', auth()->user()->follows->pluck('id'))
->orWhere('users.id', auth()->user()->id);
}
})
$followIds = auth()->check() ? auth()->user()->follows->pluck('id') : collect();

$data = DB::table('train_checkins')
->join('users', 'train_checkins.user_id', '=', 'users.id')
->where('train_checkins.departure', '>=', $date->clone()->firstOfMonth()->utc()->format('Y-m-d H:i:s'))
->where('train_checkins.departure', '<=', $date->clone()->lastOfMonth()->endOfDay()->utc()->format('Y-m-d H:i:s'))
->where(fn (Builder $q) => self::applyPrivacyFilter($q, $followIds))
->select([
'statuses.user_id',
'train_checkins.user_id',
DB::raw('SUM(train_checkins.points) AS points'),
DB::raw('SUM(train_checkins.distance) AS distance'),
DB::raw(self::getDurationSelector() . ' AS duration'),
DB::raw('SUM(train_checkins.distance) / (' . self::getDurationSelector() . ' / 60) AS speed'),
])
->groupBy('user_id')
->groupBy('train_checkins.user_id')
->orderByDesc('points')
->limit(100)
->get();

// Fetch user models in ONE query and map it to the collection
$userCache = User::whereIn('id', $data->pluck('user_id'))->get();
$userCache = User::with(['blockedByUsers', 'blockedUsers'])
->whereIn('id', $data->pluck('user_id'))
->get();

return $data->map(function ($row) use ($userCache) {
$row->user = $userCache->where('id', $row->user_id)->first();
Expand All @@ -176,6 +159,15 @@ public static function getMonthlyLeaderboard(Carbon $date): Collection
});
}

private static function applyPrivacyFilter(Builder $query, Collection $followIds): void
{
$query->where('users.private_profile', 0);
if (auth()->check()) {
$query->orWhereIn('users.id', $followIds)
->orWhere('users.id', auth()->id());
}
}

private static function getDurationSelector(): string
{
$driver = config('database.default');
Expand Down
Loading