From 8dce7ba5a304c34410a83f94b3a812e0a1aa3a3b Mon Sep 17 00:00:00 2001 From: Kris Date: Sun, 31 Jan 2021 21:48:49 +0100 Subject: [PATCH] Improved PageSpeed (#199) --- app/Http/Controllers/StatusController.php | 35 +++---- app/Http/Controllers/UserController.php | 111 +++++++++++----------- app/Models/TrainCheckin.php | 6 +- 3 files changed, 77 insertions(+), 75 deletions(-) diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php index 05d6660da..cb51a3082 100644 --- a/app/Http/Controllers/StatusController.php +++ b/app/Http/Controllers/StatusController.php @@ -37,13 +37,15 @@ public static function getStatus($statusId): ?Status { */ public static function getActiveStatuses($userId = null, bool $array = true) { if ($userId === null) { - $statuses = Status::with('user', - 'trainCheckin', - 'trainCheckin.Origin', - 'trainCheckin.Destination', - 'trainCheckin.HafasTrip', - 'event') - ->withCount('likes') + $statuses = Status::with([ + 'likes', + 'user', + 'trainCheckin.Origin', + 'trainCheckin.Destination', + 'trainCheckin.HafasTrip.getPolyLine', + 'trainCheckin.HafasTrip.stopoversNEW.trainStation', + 'event' + ]) ->whereHas('trainCheckin', function($query) { $query->where('departure', '<', date('Y-m-d H:i:s')) ->where('arrival', '>', date('Y-m-d H:i:s')); @@ -53,12 +55,13 @@ public static function getActiveStatuses($userId = null, bool $array = true) { return $status->trainCheckin->departure; })->values(); } else { - $statuses = Status::with('user', - 'trainCheckin', - 'trainCheckin.Origin', - 'trainCheckin.Destination', - 'trainCheckin.HafasTrip', - 'event') + $statuses = Status::with([ + 'user', + 'trainCheckin.Origin', + 'trainCheckin.Destination', + 'trainCheckin.HafasTrip.getPolyLine', + 'event' + ]) ->whereHas('trainCheckin', function($query) { $query->where('departure', '<', date('Y-m-d H:i:s')) ->where('arrival', '>', date('Y-m-d H:i:s')); @@ -101,14 +104,13 @@ public static function getDashboard($user): Paginator { return Status::with([ 'event', 'likes', 'user', 'trainCheckin', 'trainCheckin.Origin', 'trainCheckin.Destination', - 'trainCheckin.HafasTrip' + 'trainCheckin.HafasTrip.stopoversNEW' ]) ->join('train_checkins', 'train_checkins.status_id', '=', 'statuses.id') ->select('statuses.*') ->orderBy('train_checkins.departure', 'desc') ->whereIn('user_id', $userIds) ->withCount('likes') - ->latest() ->simplePaginate(15); } @@ -116,13 +118,12 @@ public static function getGlobalDashboard(): Paginator { return Status::with([ 'event', 'likes', 'user', 'trainCheckin', 'trainCheckin.Origin', 'trainCheckin.Destination', - 'trainCheckin.HafasTrip' + 'trainCheckin.HafasTrip.stopoversNEW' ]) ->join('train_checkins', 'train_checkins.status_id', '=', 'statuses.id') ->select('statuses.*') ->orderBy('train_checkins.departure', 'desc') ->withCount('likes') - ->latest() ->simplePaginate(15); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 479bda256..9f15c1e4e 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -346,41 +346,41 @@ private static function isFollowing(User $user, User $userFollow): bool { 'kilometers' => "Illuminate\\Support\\Collection" ])] public static function getLeaderboard(): array { - $trainCheckIns = TrainCheckin::with('status') - ->where('departure', '>=', Carbon::now()->subDays(7)->toIso8601String()) - ->get() - ->groupBy('status.user_id') - ->map(function($trainCheckIns) { - return [ - 'user' => $trainCheckIns->first()->status->user, - 'points' => $trainCheckIns->sum('points'), - 'distance' => $trainCheckIns->sum('distance'), - 'duration' => $trainCheckIns->sum('duration'), - 'speed' => $trainCheckIns->avg('speed') - ]; - }); + $checkIns = TrainCheckin::with('status.user') + ->where('departure', '>=', Carbon::now()->subDays(7)->toIso8601String()) + ->get(); + + $trainCheckIns = (clone $checkIns) + ->groupBy('status.user_id') + ->map(function($trainCheckIns) { + return [ + 'user' => $trainCheckIns->first()->status->user, + 'points' => $trainCheckIns->sum('points'), + 'distance' => $trainCheckIns->sum('distance'), + 'duration' => $trainCheckIns->sum('duration'), + 'speed' => $trainCheckIns->avg('speed') + ]; + }); $friendsTrainCheckIns = null; if (Auth::check()) { - $friendsTrainCheckIns = TrainCheckin::with('status') - ->where('departure', '>=', Carbon::now()->subDays(7)->toIso8601String()) - ->get() - ->filter(function($trainCheckIn) { - return Auth::user()->follows - ->pluck('id') - ->contains($trainCheckIn->status->user_id) - || $trainCheckIn->status->user_id == Auth::user()->id; - }) - ->groupBy('status.user_id') - ->map(function($trainCheckIns) { - return [ - 'user' => $trainCheckIns->first()->status->user, - 'points' => $trainCheckIns->sum('points'), - 'distance' => $trainCheckIns->sum('distance'), - 'duration' => $trainCheckIns->sum('duration'), - 'speed' => $trainCheckIns->avg('speed') - ]; - }); + $friendsTrainCheckIns = (clone $checkIns) + ->filter(function($trainCheckIn) { + return Auth::user()->follows + ->pluck('id') + ->contains($trainCheckIn->status->user_id) + || $trainCheckIn->status->user_id == Auth::user()->id; + }) + ->groupBy('status.user_id') + ->map(function($trainCheckIns) { + return [ + 'user' => $trainCheckIns->first()->status->user, + 'points' => $trainCheckIns->sum('points'), + 'distance' => $trainCheckIns->sum('distance'), + 'duration' => $trainCheckIns->sum('duration'), + 'speed' => $trainCheckIns->avg('speed') + ]; + }); } return [ @@ -423,28 +423,29 @@ public static function searchUser(?string $searchQuery) { } public static function getMonthlyLeaderboard(Carbon $date): Collection { - return Status::join('train_checkins', 'train_checkins.status_id', '=', 'statuses.id') - ->where( - 'train_checkins.departure', - '>=', - $date->clone()->firstOfMonth()->toDateString() - ) - ->where( - 'train_checkins.departure', - '<=', - $date->clone()->lastOfMonth()->toDateString() . ' 23:59:59' - ) - ->get() - ->groupBy('user_id') - ->map(function($statuses) { - return [ - 'user' => $statuses->first()->user, - 'points' => $statuses->sum('trainCheckin.points'), - 'distance' => $statuses->sum('distance'), - 'duration' => $statuses->sum('trainCheckin.duration'), - 'statusCount' => $statuses->count() - ]; - }) - ->sortByDesc('points'); + return Status::with(['trainCheckin', 'user']) + ->join('train_checkins', 'train_checkins.status_id', '=', 'statuses.id') + ->where( + 'train_checkins.departure', + '>=', + $date->clone()->firstOfMonth()->toDateString() + ) + ->where( + 'train_checkins.departure', + '<=', + $date->clone()->lastOfMonth()->toDateString() . ' 23:59:59' + ) + ->get() + ->groupBy('user_id') + ->map(function($statuses) { + return [ + 'user' => $statuses->first()->user, + 'points' => $statuses->sum('trainCheckin.points'), + 'distance' => $statuses->sum('distance'), + 'duration' => $statuses->sum('trainCheckin.duration'), + 'statusCount' => $statuses->count() + ]; + }) + ->sortByDesc('points'); } } diff --git a/app/Models/TrainCheckin.php b/app/Models/TrainCheckin.php index 8e783cdb5..fa13d883c 100644 --- a/app/Models/TrainCheckin.php +++ b/app/Models/TrainCheckin.php @@ -42,11 +42,11 @@ public function getDestinationStopoverAttribute(): ?TrainStopover { } public function getMapLines() { - $hafas = $this->HafasTrip()->first()->getPolyLine()->first(); + $hafas = $this->HafasTrip->getPolyLine; if ($hafas === null) { - $origin = $this->Origin()->first(); + $origin = $this->Origin; - $destination = $this->Destination()->first(); + $destination = $this->Destination; $route = []; $route[0] = [$origin->longitude, $origin->latitude]; $route[1] = [$destination->longitude, $destination->latitude];