Second task#2
Conversation
|
Breakdown of how I handled Task 2 :
Subtask 2 : Channels
Frontend:
Subtask 3 : Invitations
Frontend:
|
|
|
||
|
|
||
|
|
||
|
|
There was a problem hiding this comment.
add pagination
add filters general use filters
|
|
||
| class CommentController extends Controller | ||
| { | ||
| public function index(Channel $channel, SharedDay $sharedDay) |
There was a problem hiding this comment.
not an error but the channel can be fetched from the shared_day
|
|
||
| return InvitationResource::collection($invitations); | ||
| } | ||
|
|
There was a problem hiding this comment.
add pagination + well done avoiding N+1 query problem
| { | ||
| public function index(ReportRequest $request, ReportService $service) | ||
| { | ||
| return $service->generate(auth()->id(), |
There was a problem hiding this comment.
read $from + $to from the validated form request :)
| public function index(Channel $channel) | ||
| { | ||
| $this->authorize('viewAny', $channel); | ||
|
|
There was a problem hiding this comment.
pagination. in case where we have millions of records it will fails with 500 not handled error
| ->paginate(15); | ||
|
|
||
| return response()->json($dates); | ||
| return response()->json([ |
There was a problem hiding this comment.
ok, but the meta attributes of pagination are returned by default.
| ]; | ||
| } | ||
|
|
||
| public function withValidator($validator): void |
There was a problem hiding this comment.
bad validation. enhance it. 2 queries while it can be done in one
| use HasFactory; | ||
|
|
||
| protected $fillable = ['user_id', 'name', 'description']; | ||
| protected $appends = ['members_count']; |
There was a problem hiding this comment.
good but im a little concerned about adding the 'members_count' in the $append attribute. cuz it will execute a DB query on each fetched object which may cause N+1 problem
in general, try to avoid $append attributes that may make DB queries
| ->inRange($from, $to) | ||
| ->selectRaw('count(*) as total_entries, | ||
| SUM(duration_minutes) as total_minutes, | ||
| COUNT(DISTINCT DATE(created_at)) as total_days |
There was a problem hiding this comment.
COUNT(DISTINCT DATE(created_at)) as total_days bad performance
also read a little more about databases and query planners.
here the Date function break using the index and enforce full table scan. also the DISTINCT alone is heavy process.
| ->orderByDesc('total_minutes') | ||
| ->get(); | ||
|
|
||
| return $rows->map(fn($row) => [ |
There was a problem hiding this comment.
this require the data be processed inside php after fetched from database. which is not the best. try to return the data ready from the database directly as possible
if it costs a lot. then maybe writing a sql function and calling it is easier
| ])->toArray(); | ||
| } | ||
|
|
||
| private function avgLabelsPerDay(int $userId, ?string $from, ?string $to): float |
There was a problem hiding this comment.
some performance issues here
1- using Date() function in select
2- using Date() function in grouping
3- processing the data in php
always try to imagine your query will run by hundreds of users on millions of rows
| ->orderByDesc('avg_minutes') | ||
| ->get(); | ||
|
|
||
| return $rows->map(fn($row) => [ |
There was a problem hiding this comment.
adding the duartion_minutes column to simplify DB queries very good.
processing the data in php not ideal practice
No description provided.