Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
optimized query
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaishiyoku committed Nov 27, 2020
1 parent 79c517a commit c4749d6
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions app/Console/Commands/MigrateReportFeedItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function handle()
$users->each(function (User $user) use ($latestOnly, $yesterday, $latestDays) {
$this->line('----- ' . $user->name . ' -----');

$feedItems = $user->feedItems();
$feedItems = $user->feedItems()->select(['user_id', 'posted_at', 'read_at']);

if ($latestOnly) {
$feedItems = $feedItems->whereDate('posted_at', '>=', now()->subDays($latestDays));
Expand All @@ -66,17 +66,24 @@ public function handle()
return $feedItem->posted_at->format(self::DATE) !== now()->format(self::DATE);
});

$totalCounts = $feedItems->groupBy(function (FeedItem $feedItem) {
return $feedItem->posted_at->format(self::DATE);
})->map(function (Collection $feedItems) {
return $feedItems->count();
});
$totalCounts = $feedItems
->groupBy(function (FeedItem $feedItem) {
return $feedItem->posted_at->format(self::DATE);
})
->map(function (Collection $feedItems) {
return $feedItems->count();
});

$readCounts = $feedItems->filter(function (FeedItem $feedItem) { return $feedItem->read_at !== null; })->groupBy(function (FeedItem $feedItem) {
return $feedItem->read_at->format(self::DATE);
})->map(function (Collection $feedItems) {
return $feedItems->count();
});
$readCounts = $feedItems
->filter(function (FeedItem $feedItem) {
return $feedItem->read_at !== null;
})
->groupBy(function (FeedItem $feedItem) {
return $feedItem->read_at->format(self::DATE);
})
->map(function (Collection $feedItems) {
return $feedItems->count();
});

$totalCounts->each(function ($totalCount, $date) use ($readCounts, $user) {
$readCount = $readCounts->get($date) ?: 0;
Expand Down

0 comments on commit c4749d6

Please sign in to comment.