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

Commit

Permalink
closed #147 now caching feed statistic results
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaishiyoku committed Oct 7, 2020
1 parent 133918f commit f9ac50c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
26 changes: 26 additions & 0 deletions app/Console/Commands/MigrateReportFeeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace App\Console\Commands;

use App\Enums\VoteStatus;
use App\Http\Controllers\StatisticController;
use App\Models\Category;
use App\Models\Feed;
use App\Models\FeedItem;
use App\Models\ReportFeed;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class MigrateReportFeeds extends Command
{
Expand Down Expand Up @@ -93,6 +96,29 @@ public function handle()

$user->reportFeeds()->save($existingReportFeed);
});

Cache::rememberForever(StatisticController::getCategoriesCacheKeyFor($user), function () use ($user) {
return $user->categories()
->with('feeds', 'feeds.reportFeeds')
->get()
->map(function (Category $category) use ($user) {
$category->total_feed_items_count = $category->getTotalFeedItemsCount();
$category->total_upvote_count = $category->getTotalUpVoteCount();
$category->total_downvote_count = $category->getTotaldownVoteCount();
$category->style = $category->getStyle();

$category->feeds->map(function (Feed $feed) use ($user) {
$feed->total_feed_items_count = $feed->reportFeeds->sum('feed_items_count');
$feed->total_upvote_count = $feed->reportFeeds->sum('upvotes');
$feed->total_downvote_count = $feed->reportFeeds->sum('downvotes');
$feed->style = $feed->getStyle();

return $feed;
});

return $category;
});
});
});

return 0;
Expand Down
15 changes: 14 additions & 1 deletion app/Http/Controllers/StatisticController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use App\Models\ReportFeedItem;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Kaishiyoku\LaravelRecharts\LaravelRecharts;
use Khill\Duration\Duration;

class StatisticController extends Controller
{
public const CATEGORIES_CACHE_KEY = 'statistic_categories';

public function index($startingYear = null, $startingMonth = null)
{
if (($startingYear && !$startingMonth) || ($startingMonth && ($startingYear < 2000 || $startingMonth < 1 || $startingMonth > 12))) {
Expand Down Expand Up @@ -48,7 +51,7 @@ public function index($startingYear = null, $startingMonth = null)

$averageDurationBetweenRetrievalAndRead = new Duration($averageTimeInSecondsBetweenRetrievalAndRead);

$categories = auth()->user()->categories()->with('feeds', 'feeds.reportFeeds')->get();
$categories = self::getCategoriesWithFeedData(auth()->user());

return view('statistic.index', compact(
'feedItemsCount',
Expand Down Expand Up @@ -92,4 +95,14 @@ private function getDailyArticlesChart($startingDate, $endingDate)

return $dailyArticlesChart;
}

public static function getCategoriesWithFeedData($user)
{
return Cache::get(self::getCategoriesCacheKeyFor($user));
}

public static function getCategoriesCacheKeyFor($user)
{
return self::CATEGORIES_CACHE_KEY . '.' . $user->id;
}
}
16 changes: 8 additions & 8 deletions resources/views/statistic/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,36 @@ class="btn btn-outline-primary"
@foreach ($categories as $category)
<div class="mb-5">
<div class="flex font-bold p-2 hover:bg-gray-200 transition-all duration-200">
<div class="w-full text-lg" {!! $category->getStyle() !!}>{{ $category->title }}</div>
<div class="w-full text-lg" {!! $category->style !!}>{{ $category->title }}</div>
<div class="w-24 text-right">
<span class="text-success-900">
<i class="fas fa-chevron-up"></i>
{{ $category->getTotalUpVoteCount() }}
{{ $category->total_upvote_count }}
</span>

<span class="text-danger-900">
<i class="fas fa-chevron-down"></i>
{{ $category->getTotalDownVoteCount() }}
{{ $category->total_downvote_count }}
</span>
</div>
<div class="w-32 text-lg text-right">{{ $category->getTotalFeedItemsCount() }}</div>
<div class="w-32 text-lg text-right">{{ $category->total_feed_items_count }}</div>
</div>

@foreach ($category->feeds as $feed)
<div class="flex px-2 pb-1 hover:bg-gray-200 transition-all duration-200">
<div class="w-full" {!! $feed->getStyle() !!}>{{ $feed->title }}</div>
<div class="w-full" {!! $feed->style !!}>{{ $feed->title }}</div>
<div class="w-24 text-right">
<span class="text-success-900">
<i class="fas fa-chevron-up"></i>
{{ $feed->reportFeeds->sum('upvotes') }}
{{ $feed->total_upvote_count }}
</span>

<span class="text-danger-900">
<i class="fas fa-chevron-down"></i>
{{ $feed->reportFeeds->sum('downvotes') }}
{{ $feed->total_downvote_count }}
</span>
</div>
<div class="w-32 text-right">{{ $feed->reportFeeds->sum('feed_items_count') }}</div>
<div class="w-32 text-right">{{ $feed->total_feed_items_count }}</div>
</div>
@endforeach
</div>
Expand Down

0 comments on commit f9ac50c

Please sign in to comment.