Skip to content

Commit

Permalink
Improved PageSpeed (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Jan 31, 2021
1 parent 9d1751a commit 8dce7ba
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 75 deletions.
35 changes: 18 additions & 17 deletions app/Http/Controllers/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
Expand Down Expand Up @@ -101,28 +104,26 @@ 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);
}

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);
}

Expand Down
111 changes: 56 additions & 55 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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');
}
}
6 changes: 3 additions & 3 deletions app/Models/TrainCheckin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 8dce7ba

Please sign in to comment.